Permalink
Switch branches/tags
tag-test3 tag-test2 tag-test1 release-3.1.0-sr3 release-3.1.0-sr2 release-3.1.0-sr1.1 release-3.1.0-sr1 release-3.1.0-rc1 release-3.0.0-sr4.1 release-3.0.0-sr4 release-3.0.0-sr3 release-3.0.0-sr2 release-3.0.0-sr1 release-3.0.0-rc2 release-3.0.0-rc1 release-2.5.1 release-2.5.1-sr4 release-2.5.1-sr3 release-2.5.1-sr2 release-2.5.1-sr1 release-2.5.0 release-2.5.0-sr5 release-2.5.0-sr4 release-2.5.0-sr3 release-2.5.0-sr2 release-2.5.0-sr1 release-2.4.99-rc1 release-2.4.0 release-2.4.0-sr6 release-2.4.0-sr5 release-2.4.0-sr4 release-2.4.0-sr3 release-2.4.0-sr2 release-2.4.0-sr1 release-2.4.0-rc2 release-2.3.4 release-2.3.4-sr6 release-2.3.4-sr5 release-2.3.4-sr4 release-2.3.4-sr3 release-2.3.4-sr2 release-2.3.4-sr1 release-2.3.3 release-2.3.3-sr2 release-2.3.3-sr1.1 release-2.3.3-sr1 release-2.3.3-hf1 release-2.3.2 release-2.3.2-hf1 release-2.3.1 release-2.3.0 release-2.3.0-hf1 release-2.2.0 release-2.2.0-hf1 beta-cycle6buildtest beta-9.9.999-rui beta-9.9.99-rui beta-9.9.9-rui beta-9.9.8-rui beta-3.2.0-pre2 beta-3.2.0-pre1.1 beta-3.2.0-pre1 beta-3.1.0-pre4.1 beta-3.1.0-pre4 beta-3.1.0-pre3 beta-3.1.0-pre2.3 beta-3.1.0-pre2.2 beta-3.1.0-pre2 beta-3.1.0-pre1.2 beta-3.1.0-pre1 beta-3.0.0-rui beta-3.0.0-pre4.4 beta-3.0.0-pre4.3 beta-3.0.0-pre4.2 beta-3.0.0-pre4.1 beta-3.0.0-pre4 beta-3.0.0-pre3 beta-3.0.0-pre2.2 beta-3.0.0-pre2 beta-3.0.0-pre1.2 beta-3.0.0-pre1 beta-2.5.1-pre3 beta-2.5.1-pre2 beta-2.5.1-pre1 beta-2.5.0-pre3 beta-2.5.0-pre2 beta-2.5.0-pre1 beta-2.4.1-rui beta-2.4.0-pre3 beta-2.4.0-pre2 beta-2.4.0-pre1 beta-2.3.6-complete-unstable beta-2.3.5-pre6 beta-2.3.5-pre5 beta-2.3.5-pre4 beta-2.3.5-pre3 beta-2.3.5-pre2 beta-2.3.5-pre1 beta-2.3.4-pre6 beta-2.3.4-pre5
Nothing to show
Find file Copy path
356 lines (306 sloc) 9.6 KB
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;
#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;
#endif
#if __MOBILE__
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;
}
}
#endif
static IReadOnlyList<string> s_flags;
public static IReadOnlyList<string> Flags => s_flags ?? (s_flags = new List<string>().AsReadOnly());
public static void SetFlags(params string[] flags)
{
if (IsInitialized)
{
throw new InvalidOperationException($"{nameof(SetFlags)} must be called before {nameof(Init)}");
}
s_flags = flags.ToList().AsReadOnly();
}
public static void Init()
{
if (IsInitialized)
return;
IsInitialized = true;
Color.SetAccent(Color.FromRgba(50, 79, 133, 255));
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
Device.SetIdiom(TargetIdiom.Desktop);
Device.SetFlowDirection(NSApplication.SharedApplication.UserInterfaceLayoutDirection.ToFlowDirection());
#endif
Device.SetFlags(s_flags);
Device.PlatformServices = new IOSPlatformServices();
Device.Info = new IOSDeviceInfo();
Internals.Registrar.RegisterAll(new[]
{ typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute) });
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);
}
}
internal class IOSDeviceInfo : DeviceInfo
{
#if __MOBILE__
readonly NSObject _notification;
#endif
readonly Size _scaledScreenSize;
readonly double _scalingFactor;
public IOSDeviceInfo()
{
#if __MOBILE__
_notification = UIDevice.Notifications.ObserveOrientationDidChange((sender, args) => CurrentOrientation = UIDevice.CurrentDevice.Orientation.ToDeviceOrientation());
_scalingFactor = UIScreen.MainScreen.Scale;
_scaledScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
#else
_scalingFactor = NSScreen.MainScreen.BackingScaleFactor;
_scaledScreenSize = new Size(NSScreen.MainScreen.Frame.Width, NSScreen.MainScreen.Frame.Height);
#endif
PixelScreenSize = new Size(_scaledScreenSize.Width * _scalingFactor, _scaledScreenSize.Height * _scalingFactor);
}
public override Size PixelScreenSize { get; }
public override Size ScaledScreenSize => _scaledScreenSize;
public override double ScalingFactor => _scalingFactor;
protected override void Dispose(bool disposing)
{
#if __MOBILE__
_notification.Dispose();
#endif
base.Dispose(disposing);
}
}
class IOSPlatformServices : IPlatformServices
{
static readonly MD5CryptoServiceProvider s_checksum = new MD5CryptoServiceProvider();
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 GetMD5Hash(string input)
{
var bytes = s_checksum.ComputeHash(Encoding.UTF8.GetBytes(input));
var ret = new char[32];
for (var i = 0; i < 16; i++)
{
ret[i * 2] = (char)Hex(bytes[i] >> 4);
ret[i * 2 + 1] = (char)Hex(bytes[i] & 0xf);
}
return new string(ret);
}
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.
switch (size)
{
case NamedSize.Default:
return typeof(Button).IsAssignableFrom(targetElementType) ? 15 : 17;
case NamedSize.Micro:
return 12;
case NamedSize.Small:
return 14;
case NamedSize.Medium:
return 17;
case NamedSize.Large:
return 22;
default:
throw new ArgumentOutOfRangeException("size");
}
}
public async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
{
using (var client = GetHttpClient())
using (var response = await client.GetAsync(uri, cancellationToken))
{
if (!response.IsSuccessStatusCode)
{
Log.Warning("HTTP Request", $"Could not retrieve {uri}, status code {response.StatusCode}");
return null;
}
return await response.Content.ReadAsStreamAsync();
}
}
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
}
}
}
}