This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Forms.cs
830 lines (736 loc) · 24.1 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
using Foundation;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using Xamarin.Forms;
#if __MOBILE__
using UIKit;
using Xamarin.Forms.Platform.iOS;
using TNativeView = UIKit.UIView;
#else
using AppKit;
using Xamarin.Forms.Platform.MacOS;
using TNativeView = AppKit.NSView;
#endif
namespace Xamarin.Forms
{
public static class Forms
{
public static bool IsInitialized { get; private set; }
#if __MOBILE__
static bool? s_isiOS9OrNewer;
static bool? s_isiOS10OrNewer;
static bool? s_isiOS11OrNewer;
static bool? s_isiOS12OrNewer;
static bool? s_isiOS13OrNewer;
static bool? s_isiOS14OrNewer;
static bool? s_respondsTosetNeedsUpdateOfHomeIndicatorAutoHidden;
internal static bool IsiOS9OrNewer
{
get
{
if (!s_isiOS9OrNewer.HasValue)
s_isiOS9OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(9, 0);
return s_isiOS9OrNewer.Value;
}
}
internal static bool IsiOS10OrNewer
{
get
{
if (!s_isiOS10OrNewer.HasValue)
s_isiOS10OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(10, 0);
return s_isiOS10OrNewer.Value;
}
}
internal static bool IsiOS11OrNewer
{
get
{
if (!s_isiOS11OrNewer.HasValue)
s_isiOS11OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(11, 0);
return s_isiOS11OrNewer.Value;
}
}
internal static bool IsiOS12OrNewer
{
get
{
if (!s_isiOS12OrNewer.HasValue)
s_isiOS12OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(12, 0);
return s_isiOS12OrNewer.Value;
}
}
internal static bool IsiOS13OrNewer
{
get
{
if (!s_isiOS13OrNewer.HasValue)
s_isiOS13OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(13, 0);
return s_isiOS13OrNewer.Value;
}
}
internal static bool IsiOS14OrNewer
{
get
{
if (!s_isiOS14OrNewer.HasValue)
s_isiOS14OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(14, 0);
return s_isiOS14OrNewer.Value;
}
}
internal static bool RespondsToSetNeedsUpdateOfHomeIndicatorAutoHidden
{
get
{
if (!s_respondsTosetNeedsUpdateOfHomeIndicatorAutoHidden.HasValue)
s_respondsTosetNeedsUpdateOfHomeIndicatorAutoHidden = new UIViewController().RespondsToSelector(new ObjCRuntime.Selector("setNeedsUpdateOfHomeIndicatorAutoHidden"));
return s_respondsTosetNeedsUpdateOfHomeIndicatorAutoHidden.Value;
}
}
#else
static bool? s_isSierraOrNewer;
internal static bool IsSierraOrNewer
{
get
{
if (!s_isSierraOrNewer.HasValue)
s_isSierraOrNewer = NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(10, 12, 0));
return s_isSierraOrNewer.Value;
}
}
static bool? s_isHighSierraOrNewer;
internal static bool IsHighSierraOrNewer
{
get
{
if (!s_isHighSierraOrNewer.HasValue)
s_isHighSierraOrNewer = NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(10, 13, 0));
return s_isHighSierraOrNewer.Value;
}
}
static bool? s_isMojaveOrNewer;
internal static bool IsMojaveOrNewer
{
get
{
if (!s_isMojaveOrNewer.HasValue)
s_isMojaveOrNewer = NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(10, 14, 0));
return s_isMojaveOrNewer.Value;
}
}
#endif
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 Init()
{
if (IsInitialized)
return;
IsInitialized = true;
Color.SetAccent(ColorExtensions.AccentColor.ToColor());
Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c)));
#if __MOBILE__
Device.SetIdiom(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad ? TargetIdiom.Tablet : TargetIdiom.Phone);
Device.SetFlowDirection(UIApplication.SharedApplication.UserInterfaceLayoutDirection.ToFlowDirection());
#else
// Subscribe to notifications in OS Theme changes
NSDistributedNotificationCenter.GetDefaultCenter().AddObserver((NSString)"AppleInterfaceThemeChangedNotification", (n) =>
{
var interfaceStyle = NSUserDefaults.StandardUserDefaults.StringForKey("AppleInterfaceStyle");
var aquaAppearance = NSAppearance.GetAppearance(interfaceStyle == "Dark" ? NSAppearance.NameDarkAqua : NSAppearance.NameAqua);
NSApplication.SharedApplication.Appearance = aquaAppearance;
Application.Current?.TriggerThemeChanged(new AppThemeChangedEventArgs(interfaceStyle == "Dark" ? OSAppTheme.Dark : OSAppTheme.Light));
});
Device.SetIdiom(TargetIdiom.Desktop);
Device.SetFlowDirection(NSApplication.SharedApplication.UserInterfaceLayoutDirection.ToFlowDirection());
if (IsMojaveOrNewer)
{
var interfaceStyle = NSUserDefaults.StandardUserDefaults.StringForKey("AppleInterfaceStyle");
var aquaAppearance = NSAppearance.GetAppearance(interfaceStyle == "Dark" ? NSAppearance.NameDarkAqua : NSAppearance.NameAqua);
NSApplication.SharedApplication.Appearance = aquaAppearance;
}
#endif
Device.SetFlags(s_flags);
var platformServices = new IOSPlatformServices();
Device.PlatformServices = platformServices;
#if __MOBILE__
Device.PlatformInvalidator = platformServices;
Device.Info = new IOSDeviceInfo();
#else
Device.Info = new Platform.macOS.MacDeviceInfo();
#endif
Internals.Registrar.RegisterAll(new[]
{ typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute), typeof(ExportFontAttribute) });
ExpressionSearch.Default = new iOSExpressionSearch();
}
public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;
internal static void SendViewInitialized(this VisualElement self, TNativeView nativeView)
{
ViewInitialized?.Invoke(self, new ViewInitializedEventArgs { View = self, NativeView = nativeView });
}
class iOSExpressionSearch : 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);
}
}
class IOSPlatformServices : IPlatformServices
#if __MOBILE__
, IPlatformInvalidate
#endif
{
readonly double _fontScalingFactor = 1;
public IOSPlatformServices()
{
#if __MOBILE__
//The standard accessibility size for a font is 17, we can get a
//close approximation to the new Size by multiplying by this scale factor
_fontScalingFactor = (double)UIFont.PreferredBody.PointSize / 17f;
#endif
}
public void BeginInvokeOnMainThread(Action action)
{
NSRunLoop.Main.BeginInvokeOnMainThread(action.Invoke);
}
public Ticker CreateTicker()
{
return new CADisplayLinkTicker();
}
public Assembly[] GetAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
public string GetHash(string input) => Crc64.GetHash(input);
string IPlatformServices.GetMD5Hash(string input) => GetHash(input);
public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
{
// We make these up anyway, so new sizes didn't really change
// iOS docs say default button font size is 15, default label font size is 17 so we use those as the defaults.
var scalingFactor = _fontScalingFactor;
if (Application.Current?.On<iOS>().GetEnableAccessibilityScalingForNamedFontSizes() == false)
{
scalingFactor = 1;
}
switch (size)
{
//We multiply the fonts by the scale factor, and cast to an int, to make them whole numbers.
case NamedSize.Default:
return (int)((typeof(Button).IsAssignableFrom(targetElementType) ? 15 : 17) * scalingFactor);
case NamedSize.Micro:
return (int)(12 * scalingFactor);
case NamedSize.Small:
return (int)(14 * scalingFactor);
case NamedSize.Medium:
return (int)(17 * scalingFactor);
case NamedSize.Large:
return (int)(22 * scalingFactor);
#if __IOS__
case NamedSize.Body:
return (double)UIFont.PreferredBody.PointSize;
case NamedSize.Caption:
return (double)UIFont.PreferredCaption1.PointSize;
case NamedSize.Header:
return (double)UIFont.PreferredHeadline.PointSize;
case NamedSize.Subtitle:
return (double)UIFont.PreferredTitle2.PointSize;
case NamedSize.Title:
return (double)UIFont.PreferredTitle1.PointSize;
#else
case NamedSize.Body:
return 23;
case NamedSize.Caption:
return 18;
case NamedSize.Header:
return 23;
case NamedSize.Subtitle:
return 28;
case NamedSize.Title:
return 34;
#endif
default:
throw new ArgumentOutOfRangeException("size");
}
}
public Color GetNamedColor(string name)
{
#if __IOS__
UIColor resultColor = null;
// If not iOS 13, but 11+ we can only get the named colors
if (!IsiOS13OrNewer && IsiOS11OrNewer)
return (resultColor = UIColor.FromName(name)) == null ? Color.Default : resultColor.ToColor();
// If iOS 13+ check all dynamic colors too
switch (name)
{
case NamedPlatformColor.Label:
resultColor = UIColor.LabelColor;
break;
case NamedPlatformColor.Link:
resultColor = UIColor.LinkColor;
break;
case NamedPlatformColor.OpaqueSeparator:
resultColor = UIColor.OpaqueSeparatorColor;
break;
case NamedPlatformColor.PlaceholderText:
resultColor = UIColor.PlaceholderTextColor;
break;
case NamedPlatformColor.QuaternaryLabel:
resultColor = UIColor.QuaternaryLabelColor;
break;
case NamedPlatformColor.SecondaryLabel:
resultColor = UIColor.SecondaryLabelColor;
break;
case NamedPlatformColor.Separator:
resultColor = UIColor.SeparatorColor;
break;
case NamedPlatformColor.SystemBlue:
resultColor = UIColor.SystemBlueColor;
break;
case NamedPlatformColor.SystemGray:
resultColor = UIColor.SystemGrayColor;
break;
case NamedPlatformColor.SystemGray2:
resultColor = UIColor.SystemGray2Color;
break;
case NamedPlatformColor.SystemGray3:
resultColor = UIColor.SystemGray3Color;
break;
case NamedPlatformColor.SystemGray4:
resultColor = UIColor.SystemGray4Color;
break;
case NamedPlatformColor.SystemGray5:
resultColor = UIColor.SystemGray5Color;
break;
case NamedPlatformColor.SystemGray6:
resultColor = UIColor.SystemGray6Color;
break;
case NamedPlatformColor.SystemGreen:
resultColor = UIColor.SystemGreenColor;
break;
case NamedPlatformColor.SystemIndigo:
resultColor = UIColor.SystemIndigoColor;
break;
case NamedPlatformColor.SystemOrange:
resultColor = UIColor.SystemOrangeColor;
break;
case NamedPlatformColor.SystemPink:
resultColor = UIColor.SystemPinkColor;
break;
case NamedPlatformColor.SystemPurple:
resultColor = UIColor.SystemPurpleColor;
break;
case NamedPlatformColor.SystemRed:
resultColor = UIColor.SystemRedColor;
break;
case NamedPlatformColor.SystemTeal:
resultColor = UIColor.SystemTealColor;
break;
case NamedPlatformColor.SystemYellow:
resultColor = UIColor.SystemYellowColor;
break;
case NamedPlatformColor.TertiaryLabel:
resultColor = UIColor.TertiaryLabelColor;
break;
default:
resultColor = UIColor.FromName(name);
break;
}
if (resultColor == null)
return Color.Default;
return resultColor.ToColor();
#elif __MACOS__
NSColor resultColor = null;
switch (name)
{
case NamedPlatformColor.AlternateSelectedControlTextColor:
resultColor = NSColor.AlternateSelectedControlText;
break;
case NamedPlatformColor.ControlAccent:
if (IsMojaveOrNewer)
resultColor = NSColor.ControlAccentColor;
break;
case NamedPlatformColor.ControlBackgroundColor:
resultColor = NSColor.ControlBackground;
break;
case NamedPlatformColor.ControlColor:
resultColor = NSColor.Control;
break;
case NamedPlatformColor.ControlTextColor:
resultColor = NSColor.ControlText;
break;
case NamedPlatformColor.DisabledControlTextColor:
resultColor = NSColor.DisabledControlText;
break;
case NamedPlatformColor.FindHighlightColor:
if (IsHighSierraOrNewer)
resultColor = NSColor.FindHighlightColor;
break;
case NamedPlatformColor.GridColor:
resultColor = NSColor.Grid;
break;
case NamedPlatformColor.HeaderTextColor:
resultColor = NSColor.HeaderText;
break;
case NamedPlatformColor.HighlightColor:
resultColor = NSColor.Highlight;
break;
case NamedPlatformColor.KeyboardFocusIndicatorColor:
resultColor = NSColor.KeyboardFocusIndicator;
break;
case NamedPlatformColor.LabelColor:
resultColor = NSColor.LabelColor;
break;
case NamedPlatformColor.LinkColor:
resultColor = NSColor.LinkColor;
break;
case NamedPlatformColor.PlaceholderTextColor:
resultColor = NSColor.PlaceholderTextColor;
break;
case NamedPlatformColor.QuaternaryLabelColor:
resultColor = NSColor.QuaternaryLabelColor;
break;
case NamedPlatformColor.SecondaryLabelColor:
resultColor = NSColor.SecondaryLabelColor;
break;
case NamedPlatformColor.SelectedContentBackgroundColor:
resultColor = NSColor.SelectedContentBackgroundColor;
break;
case NamedPlatformColor.SelectedControlColor:
resultColor = NSColor.SelectedControl;
break;
case NamedPlatformColor.SelectedControlTextColor:
resultColor = NSColor.SelectedControlText;
break;
case NamedPlatformColor.SelectedMenuItemTextColor:
resultColor = NSColor.SelectedMenuItemText;
break;
case NamedPlatformColor.SelectedTextBackgroundColor:
resultColor = NSColor.SelectedTextBackground;
break;
case NamedPlatformColor.SelectedTextColor:
resultColor = NSColor.SelectedText;
break;
case NamedPlatformColor.SeparatorColor:
resultColor = NSColor.SeparatorColor;
break;
case NamedPlatformColor.ShadowColor:
resultColor = NSColor.Shadow;
break;
case NamedPlatformColor.TertiaryLabelColor:
resultColor = NSColor.TertiaryLabelColor;
break;
case NamedPlatformColor.TextBackgroundColor:
resultColor = NSColor.TextBackground;
break;
case NamedPlatformColor.TextColor:
resultColor = NSColor.Text;
break;
case NamedPlatformColor.UnderPageBackgroundColor:
resultColor = NSColor.UnderPageBackgroundColor;
break;
case NamedPlatformColor.UnemphasizedSelectedContentBackgroundColor:
if (IsMojaveOrNewer)
resultColor = NSColor.UnemphasizedSelectedContentBackgroundColor;
break;
case NamedPlatformColor.UnemphasizedSelectedTextBackgroundColor:
if (IsMojaveOrNewer)
resultColor = NSColor.UnemphasizedSelectedTextBackgroundColor;
break;
case NamedPlatformColor.UnemphasizedSelectedTextColor:
if (IsMojaveOrNewer)
resultColor = NSColor.UnemphasizedSelectedTextColor;
break;
case NamedPlatformColor.WindowBackgroundColor:
resultColor = NSColor.WindowBackground;
break;
case NamedPlatformColor.WindowFrameTextColor:
resultColor = NSColor.WindowFrameText;
break;
case NamedPlatformColor.Label:
resultColor = NSColor.LabelColor;
break;
case NamedPlatformColor.Link:
resultColor = NSColor.LinkColor;
break;
case NamedPlatformColor.PlaceholderText:
resultColor = NSColor.PlaceholderTextColor;
break;
case NamedPlatformColor.QuaternaryLabel:
resultColor = NSColor.QuaternaryLabelColor;
break;
case NamedPlatformColor.SecondaryLabel:
resultColor = NSColor.SecondaryLabelColor;
break;
case NamedPlatformColor.Separator:
if (IsMojaveOrNewer)
resultColor = NSColor.SeparatorColor;
break;
case NamedPlatformColor.SystemBlue:
resultColor = NSColor.SystemBlueColor;
break;
case NamedPlatformColor.SystemGray:
resultColor = NSColor.SystemGrayColor;
break;
case NamedPlatformColor.SystemGreen:
resultColor = NSColor.SystemGreenColor;
break;
case NamedPlatformColor.SystemIndigo:
resultColor = NSColor.SystemIndigoColor;
break;
case NamedPlatformColor.SystemOrange:
resultColor = NSColor.SystemOrangeColor;
break;
case NamedPlatformColor.SystemPink:
resultColor = NSColor.SystemPinkColor;
break;
case NamedPlatformColor.SystemPurple:
resultColor = NSColor.SystemPurpleColor;
break;
case NamedPlatformColor.SystemRed:
resultColor = NSColor.SystemRedColor;
break;
case NamedPlatformColor.SystemTeal:
resultColor = NSColor.SystemTealColor;
break;
case NamedPlatformColor.SystemYellow:
resultColor = NSColor.SystemYellowColor;
break;
case NamedPlatformColor.TertiaryLabel:
resultColor = NSColor.TertiaryLabelColor;
break;
default:
resultColor = NSColor.FromName(name);
break;
}
if (resultColor == null)
return Color.Default;
return resultColor.ToColor(NSColorSpace.GenericRGBColorSpace);
#else
return Color.Default;
#endif
}
public async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
{
using (var client = GetHttpClient())
{
// Do not remove this await otherwise the client will dispose before
// the stream even starts
var result = await StreamWrapper.GetStreamAsync(uri, cancellationToken, client).ConfigureAwait(false);
return result;
}
}
public IIsolatedStorageFile GetUserStoreForApplication()
{
return new _IsolatedStorageFile(IsolatedStorageFile.GetUserStoreForApplication());
}
public bool IsInvokeRequired => !NSThread.IsMain;
#if __MOBILE__
public string RuntimePlatform => Device.iOS;
#else
public string RuntimePlatform => Device.macOS;
#endif
public void OpenUriAction(Uri uri)
{
NSUrl url;
if (uri.Scheme == "tel" || uri.Scheme == "mailto")
url = new NSUrl(uri.AbsoluteUri);
else
url = NSUrl.FromString(uri.OriginalString) ?? new NSUrl(uri.Scheme, uri.Host, uri.PathAndQuery);
#if __MOBILE__
UIApplication.SharedApplication.OpenUrl(url);
#else
NSWorkspace.SharedWorkspace.OpenUrl(url);
#endif
}
public void StartTimer(TimeSpan interval, Func<bool> callback)
{
NSTimer timer = NSTimer.CreateRepeatingTimer(interval, t =>
{
if (!callback())
t.Invalidate();
});
NSRunLoop.Main.AddTimer(timer, NSRunLoopMode.Common);
}
HttpClient GetHttpClient()
{
var proxy = CoreFoundation.CFNetwork.GetSystemProxySettings();
var handler = new HttpClientHandler();
if (!string.IsNullOrEmpty(proxy.HTTPProxy))
{
handler.Proxy = CoreFoundation.CFNetwork.GetDefaultProxy();
handler.UseProxy = true;
}
return new HttpClient(handler);
}
static int Hex(int v)
{
if (v < 10)
return '0' + v;
return 'a' + v - 10;
}
public class _IsolatedStorageFile : IIsolatedStorageFile
{
readonly IsolatedStorageFile _isolatedStorageFile;
public _IsolatedStorageFile(IsolatedStorageFile isolatedStorageFile)
{
_isolatedStorageFile = isolatedStorageFile;
}
public Task CreateDirectoryAsync(string path)
{
_isolatedStorageFile.CreateDirectory(path);
return Task.FromResult(true);
}
public Task<bool> GetDirectoryExistsAsync(string path)
{
return Task.FromResult(_isolatedStorageFile.DirectoryExists(path));
}
public Task<bool> GetFileExistsAsync(string path)
{
return Task.FromResult(_isolatedStorageFile.FileExists(path));
}
public Task<DateTimeOffset> GetLastWriteTimeAsync(string path)
{
return Task.FromResult(_isolatedStorageFile.GetLastWriteTime(path));
}
public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access)
{
Stream stream = _isolatedStorageFile.OpenFile(path, mode, access);
return Task.FromResult(stream);
}
public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share)
{
Stream stream = _isolatedStorageFile.OpenFile(path, mode, access, share);
return Task.FromResult(stream);
}
}
public void QuitApplication()
{
#if __MOBILE__
Log.Warning(nameof(IOSPlatformServices), "Platform doesn't implement QuitApp");
#else
NSApplication.SharedApplication.Terminate(new NSObject());
#endif
}
public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
#if __MOBILE__
return Platform.iOS.Platform.GetNativeSize(view, widthConstraint, heightConstraint);
#else
return Platform.MacOS.Platform.GetNativeSize(view, widthConstraint, heightConstraint);
#endif
}
public OSAppTheme RequestedTheme
{
get
{
#if __IOS__ || __TVOS__
if (!IsiOS13OrNewer)
return OSAppTheme.Unspecified;
var uiStyle = GetCurrentUIViewController()?.TraitCollection?.UserInterfaceStyle ??
UITraitCollection.CurrentTraitCollection.UserInterfaceStyle;
switch (uiStyle)
{
case UIUserInterfaceStyle.Light:
return OSAppTheme.Light;
case UIUserInterfaceStyle.Dark:
return OSAppTheme.Dark;
default:
return OSAppTheme.Unspecified;
};
#else
return AppearanceIsDark(NSApplication.SharedApplication.EffectiveAppearance) ? OSAppTheme.Dark : OSAppTheme.Light;
#endif
}
}
#if __MACOS__
bool AppearanceIsDark(NSAppearance appearance)
{
if (IsMojaveOrNewer)
{
var matchedAppearance = appearance.FindBestMatch(new string[] { NSAppearance.NameAqua, NSAppearance.NameDarkAqua });
return matchedAppearance == NSAppearance.NameDarkAqua;
}
else
{
return false;
}
}
#endif
#if __IOS__ || __TVOS__
static UIViewController GetCurrentUIViewController() =>
GetCurrentViewController(false);
static UIViewController GetCurrentViewController(bool throwIfNull = true)
{
UIViewController viewController = null;
var window = UIApplication.SharedApplication.GetKeyWindow();
if (window != null && window.WindowLevel == UIWindowLevel.Normal)
viewController = window.RootViewController;
if (viewController == null)
{
window = UIApplication.SharedApplication
.Windows
.OrderByDescending(w => w.WindowLevel)
.FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);
if (window == null && throwIfNull)
throw new InvalidOperationException("Could not find current view controller.");
else
viewController = window?.RootViewController;
}
while (viewController?.PresentedViewController != null)
viewController = viewController.PresentedViewController;
if (throwIfNull && viewController == null)
throw new InvalidOperationException("Could not find current view controller.");
return viewController;
}
public void Invalidate(VisualElement visualElement)
{
var renderer = Platform.iOS.Platform.GetRenderer(visualElement);
if (renderer == null)
{
return;
}
renderer.NativeView.SetNeedsLayout();
}
#endif
}
}
}