forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForms.cs
635 lines (549 loc) · 17.3 KB
/
Forms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Maui.Internals;
using System.Maui.Platform.Tizen;
using ElmSharp;
using Tizen.Applications;
using TSystemInfo = Tizen.System.Information;
using ELayout = ElmSharp.Layout;
using DeviceOrientation = System.Maui.Internals.DeviceOrientation;
using ElmSharp.Wearable;
namespace System.Maui
{
public enum StaticRegistrarStrategy
{
None,
StaticRegistrarOnly,
All,
}
public enum PlatformType
{
Defalut,
Lightweight,
}
public class InitializationOptions
{
public CoreApplication Context { get; set; }
public bool UseDeviceIndependentPixel { get; set; }
public HandlerAttribute[] Handlers { get; set; }
public Dictionary<Type, Func<IRegisterable>> CustomHandlers { get; set; } // for static registers
public Assembly[] Assemblies { get; set; }
public EffectScope[] EffectScopes { get; set; }
public InitializationFlags Flags { get; set; }
public StaticRegistrarStrategy StaticRegistarStrategy { get; set; }
public PlatformType PlatformType { get; set; }
public bool UseMessagingCenter { get; set; } = true;
public struct EffectScope
{
public string Name;
public ExportEffectAttribute[] Effects;
}
public InitializationOptions(CoreApplication application)
{
Context = application;
}
public InitializationOptions(CoreApplication application, bool useDeviceIndependentPixel, HandlerAttribute[] handlers)
{
Context = application;
UseDeviceIndependentPixel = useDeviceIndependentPixel;
Handlers = handlers;
}
public InitializationOptions(CoreApplication application, bool useDeviceIndependentPixel, params Assembly[] assemblies)
{
Context = application;
UseDeviceIndependentPixel = useDeviceIndependentPixel;
Assemblies = assemblies;
}
public void UseStaticRegistrar(StaticRegistrarStrategy strategy, Dictionary<Type, Func<IRegisterable>> customHandlers=null, bool disableCss=false)
{
StaticRegistarStrategy = strategy;
CustomHandlers = customHandlers;
if (disableCss) // about 10ms
Flags = InitializationFlags.DisableCss;
}
}
public static class Maui
{
static Lazy<string> s_profile = new Lazy<string>(() =>
{
//TODO : Fix me if elm_config_profile_get() unavailable
return Elementary.GetProfile();
});
static Lazy<int> s_dpi = new Lazy<int>(() =>
{
int dpi = 0;
if (s_profile.Value == "tv")
{
// Use fixed DPI value (72) if TV profile
return 72;
}
TSystemInfo.TryGetValue<int>("http://tizen.org/feature/screen.dpi", out dpi);
return dpi;
});
static Lazy<double> s_elmScale = new Lazy<double>(() =>
{
// 72.0 is from EFL which is using fixed DPI value (72.0) to determine font size internally. Thus, we are restoring the size by deviding the DPI by 72.0 here.
return s_dpi.Value / 72.0 / Elementary.GetScale();
});
class TizenDeviceInfo : DeviceInfo
{
readonly Size pixelScreenSize;
readonly Size scaledScreenSize;
readonly double scalingFactor;
readonly string profile;
public override Size PixelScreenSize
{
get
{
return this.pixelScreenSize;
}
}
public override Size ScaledScreenSize
{
get
{
return this.scaledScreenSize;
}
}
public override double ScalingFactor
{
get
{
return this.scalingFactor;
}
}
public string Profile
{
get
{
return this.profile;
}
}
public TizenDeviceInfo()
{
int width = 0;
int height = 0;
TSystemInfo.TryGetValue("http://tizen.org/feature/screen.width", out width);
TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out height);
scalingFactor = 1.0; // scaling is disabled, we're using pixels as Xamarin's geometry units
if (s_useDeviceIndependentPixel)
{
scalingFactor = s_dpi.Value / 160.0;
}
pixelScreenSize = new Size(width, height);
scaledScreenSize = new Size(width / scalingFactor, height / scalingFactor);
profile = s_profile.Value;
}
}
static bool s_useDeviceIndependentPixel = false;
static StaticRegistrarStrategy s_staticRegistrarStrategy = StaticRegistrarStrategy.None;
static PlatformType s_platformType = PlatformType.Defalut;
static bool s_useMessagingCenter = true;
public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;
public static CoreApplication Context
{
get;
internal set;
}
public static EvasObject NativeParent
{
get; internal set;
}
public static ELayout BaseLayout => NativeParent as ELayout;
public static CircleSurface CircleSurface
{
get; internal set;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public static Element RotaryFocusObject
{
get; internal set;
}
public static bool IsInitialized
{
get;
private set;
}
public static DeviceOrientation NaturalOrientation { get; } = GetDeviceOrientation();
public static StaticRegistrarStrategy StaticRegistrarStrategy => s_staticRegistrarStrategy;
public static PlatformType PlatformType => s_platformType;
public static bool UseMessagingCenter => s_useMessagingCenter;
internal static TizenTitleBarVisibility TitleBarVisibility
{
get;
private set;
}
static DeviceOrientation GetDeviceOrientation()
{
int width = 0;
int height = 0;
TSystemInfo.TryGetValue<int>("http://tizen.org/feature/screen.width", out width);
TSystemInfo.TryGetValue<int>("http://tizen.org/feature/screen.height", out height);
if (height >= width)
{
return DeviceOrientation.Portrait;
}
else
{
return DeviceOrientation.Landscape;
}
}
internal static void SendViewInitialized(this VisualElement self, EvasObject nativeView)
{
EventHandler<ViewInitializedEventArgs> viewInitialized = System.Maui.Maui.ViewInitialized;
if (viewInitialized != null)
{
viewInitialized.Invoke(self, new ViewInitializedEventArgs
{
View = self,
NativeView = nativeView
});
}
}
static IReadOnlyList<string> s_flags;
public static IReadOnlyList<string> Flags => s_flags ?? (s_flags = new string[0]);
public static void SetFlags(params string[] flags)
{
if (IsInitialized)
{
throw new InvalidOperationException($"{nameof(SetFlags)} must be called before {nameof(Init)}");
}
s_flags = (string[])flags.Clone();
if (s_flags.Contains ("Profile"))
Profile.Enable();
}
public static void SetTitleBarVisibility(TizenTitleBarVisibility visibility)
{
TitleBarVisibility = visibility;
}
public static TOut GetHandler<TOut>(Type type, params object[] args) where TOut : class, IRegisterable
{
if (s_staticRegistrarStrategy == StaticRegistrarStrategy.None)
{
// Find hander in internal registrar, that is using reflection (default).
return Registrar.Registered.GetHandler<TOut>(type, args);
}
else
{
// 1. Find hander in static registrar first
TOut ret = StaticRegistrar.Registered.GetHandler<TOut>(type, args);
// 2. If there is no handler, try to find hander in internal registrar, that is using reflection.
if (ret == null && s_staticRegistrarStrategy == StaticRegistrarStrategy.All)
{
ret = Registrar.Registered.GetHandler<TOut>(type, args);
}
return ret;
}
}
public static TOut GetHandlerForObject<TOut>(object obj) where TOut : class, IRegisterable
{
if (s_staticRegistrarStrategy == StaticRegistrarStrategy.None)
{
// Find hander in internal registrar, that is using reflection (default).
return Registrar.Registered.GetHandlerForObject<TOut>(obj);
}
else
{
// 1. Find hander in static registrar first
TOut ret = StaticRegistrar.Registered.GetHandlerForObject<TOut>(obj);
// 2. If there is no handler, try to find hander in internal registrar, that is using reflection.
if (ret == null && s_staticRegistrarStrategy == StaticRegistrarStrategy.All)
{
ret = Registrar.Registered.GetHandlerForObject<TOut>(obj);
}
return ret;
}
}
public static TOut GetHandlerForObject<TOut>(object obj, params object[] args) where TOut : class, IRegisterable
{
if (s_staticRegistrarStrategy == StaticRegistrarStrategy.None)
{
// Find hander in internal registrar, that is using reflection (default).
return Registrar.Registered.GetHandlerForObject<TOut>(obj, args);
}
else
{
// 1. Find hander in static registrar first without fallback handler.
TOut ret = StaticRegistrar.Registered.GetHandlerForObject<TOut>(obj, args);
// 2. If there is no handler, try to find hander in internal registrar, that is using reflection.
if (ret == null && s_staticRegistrarStrategy == StaticRegistrarStrategy.All)
{
ret = StaticRegistrar.Registered.GetHandlerForObject<TOut>(obj, args);
}
return ret;
}
}
public static void Init(CoreApplication application)
{
Init(application, false);
}
public static void Init(CoreApplication application, bool useDeviceIndependentPixel)
{
s_useDeviceIndependentPixel = useDeviceIndependentPixel;
SetupInit(application);
}
public static void Init(InitializationOptions options)
{
SetupInit(options.Context, options);
}
static void SetupInit(CoreApplication application, InitializationOptions options = null)
{
Context = application;
if (!IsInitialized)
{
Internals.Log.Listeners.Add(new XamarinLogListener());
if (System.Threading.SynchronizationContext.Current == null)
{
TizenSynchronizationContext.Initialize();
}
Elementary.Initialize();
Elementary.ThemeOverlay();
Utility.AppendGlobalFontPath(@"/usr/share/fonts");
}
Device.PlatformServices = new TizenPlatformServices();
if (Device.info != null)
{
((TizenDeviceInfo)Device.info).Dispose();
Device.info = null;
}
Device.Info = new System.Maui.Maui.TizenDeviceInfo();
Device.SetFlags(s_flags);
string profile = ((TizenDeviceInfo)Device.Info).Profile;
if (profile == "mobile")
{
Device.SetIdiom(TargetIdiom.Phone);
}
else if (profile == "tv")
{
Device.SetIdiom(TargetIdiom.TV);
}
else if (profile == "desktop")
{
Device.SetIdiom(TargetIdiom.Desktop);
}
else if (profile == "wearable")
{
Device.SetIdiom(TargetIdiom.Watch);
}
else
{
Device.SetIdiom(TargetIdiom.Unsupported);
}
if (!System.Maui.Maui.IsInitialized)
{
if (options != null)
{
s_useDeviceIndependentPixel = options.UseDeviceIndependentPixel;
s_platformType = options.PlatformType;
s_useMessagingCenter = options.UseMessagingCenter;
if (options.Assemblies != null && options.Assemblies.Length > 0)
{
TizenPlatformServices.AppDomain.CurrentDomain.AddAssemblies(options.Assemblies);
}
// renderers
if (options.Handlers != null)
{
Registrar.RegisterRenderers(options.Handlers);
}
else
{
// Add System.Maui.Core assembly by default to apply the styles.
TizenPlatformServices.AppDomain.CurrentDomain.AddAssembly(Assembly.GetAssembly(typeof(System.Maui.View)));
// static registrar
if (options.StaticRegistarStrategy != StaticRegistrarStrategy.None)
{
s_staticRegistrarStrategy = options.StaticRegistarStrategy;
StaticRegistrar.RegisterHandlers(options.CustomHandlers);
if (options.StaticRegistarStrategy == StaticRegistrarStrategy.All)
{
Registrar.RegisterAll(new Type[]
{
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
else
{
Registrar.RegisterAll(new Type[]
{
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
// effects
var effectScopes = options.EffectScopes;
if (effectScopes != null)
{
for (var i = 0; i < effectScopes.Length; i++)
{
var effectScope = effectScopes[0];
Registrar.RegisterEffects(effectScope.Name, effectScope.Effects);
}
}
// css
Registrar.RegisterStylesheets(options.Flags);
}
else
{
// In .NETCore, AppDomain feature is not supported.
// The list of assemblies returned by AppDomain.GetAssemblies() method should be registered manually.
// The assembly of the executing application and referenced assemblies of it are added into the list here.
TizenPlatformServices.AppDomain.CurrentDomain.RegisterAssemblyRecursively(application.GetType().GetTypeInfo().Assembly);
Registrar.RegisterAll(new Type[]
{
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
Color.SetAccent(GetAccentColor(profile));
ExpressionSearch.Default = new TizenExpressionSearch();
if (application is WatchApplication)
s_platformType = PlatformType.Lightweight;
IsInitialized = true;
}
static Color GetAccentColor(string profile)
{
// On Windows Phone, this is the complementary color chosen by the user.
// Good Windows Phone applications use this as part of their styling to provide a native look and feel.
// On iOS and Android this instance is set to a contrasting color that is visible on the default
// background but is not the same as the default text color.
switch (profile)
{
case "mobile":
// [Tizen_3.0]Basic_Interaction_GUI_[REF].xlsx Theme 001 (Default) 1st HSB: 188 70 80
return Color.FromRgba(61, 185, 204, 255);
case "tv":
return Color.FromRgba(15, 15, 15, 230);
case "wearable":
// Theme A (Default) 1st HSB: 207 75 16
return Color.FromRgba(10, 27, 41, 255);
default:
return Color.Black;
}
}
/// <summary>
/// Converts the dp into pixel considering current DPI value.
/// </summary>
/// <remarks>
/// Use this API if you want to get pixel size without scaling factor.
/// </remarks>
/// <param name="dp"></param>
/// <returns></returns>
public static int ConvertToPixel(double dp)
{
return (int)Math.Round(dp * s_dpi.Value / 160.0);
}
/// <summary>
/// Converts the dp into pixel by using scaling factor.
/// </summary>
/// <remarks>
/// Use this API if you want to get pixel size from dp by using scaling factor.
/// If the scaling factor is 1.0 by user setting, the same value is returned. This is mean that user doesn't want to pixel independent metric.
/// </remarks>
/// <param name="dp"></param>
/// <returns></returns>
public static int ConvertToScaledPixel(double dp)
{
return (int)Math.Round(dp * Device.Info.ScalingFactor);
}
/// <summary>
/// Converts the pixel into dp value by using scaling factor.
/// </summary>
/// <remarks>
/// If the scaling factor is 1.0 by user setting, the same value is returned. This is mean that user doesn't want to pixel independent metric.
/// </remarks>
/// <param name="pixel"></param>
/// <returns></returns>
public static double ConvertToScaledDP(int pixel)
{
return pixel / Device.Info.ScalingFactor;
}
/// <summary>
/// Converts the pixel into dp value by using scaling factor.
/// </summary>
/// <remarks>
/// If the scaling factor is 1.0 by user setting, the same value is returned. This is mean that user doesn't want to pixel independent metric.
/// </remarks>
/// <param name="pixel"></param>
/// <returns></returns>
public static double ConvertToScaledDP(double pixel)
{
return pixel / Device.Info.ScalingFactor;
}
/// <summary>
/// Converts the sp into EFL's font size metric (EFL point).
/// </summary>
/// <param name="sp"></param>
/// <returns></returns>
public static int ConvertToEflFontPoint(double sp)
{
return (int)Math.Round(sp * s_elmScale.Value);
}
/// <summary>
/// Convert the EFL's point into sp.
/// </summary>
/// <param name="eflPt"></param>
/// <returns></returns>
public static double ConvertToDPFont(int eflPt)
{
return eflPt / s_elmScale.Value;
}
/// <summary>
/// Get the EFL's profile
/// </summary>
/// <returns></returns>
public static string GetProfile()
{
return s_profile.Value;
}
// for internal use only
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Preload()
{
Elementary.Initialize();
Elementary.ThemeOverlay();
var window = new PreloadedWindow();
TSystemInfo.TryGetValue("http://tizen.org/feature/screen.width", out int width);
TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out int height);
}
}
class TizenExpressionSearch : ExpressionVisitor, IExpressionSearch
{
List<object> _results;
Type _targetType;
public List<T> FindObjects<T>(Expression expression) where T : class
{
_results = new List<object>();
_targetType = typeof(T);
Visit(expression);
return _results.Select(o => o as T).ToList();
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.Expression is ConstantExpression && node.Member is FieldInfo)
{
var container = ((ConstantExpression)node.Expression).Value;
var value = ((FieldInfo)node.Member).GetValue(container);
if (_targetType.IsInstanceOfType(value))
_results.Add(value);
}
return base.VisitMember(node);
}
}
}