Ooui-tws-port/Ooui.Forms/Forms.cs

215 lines
7.3 KiB
C#
Raw Normal View History

2017-11-09 07:12:59 +00:00
using System;
using Xamarin.Forms.Internals;
using Ooui.Forms;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
2017-11-09 09:38:19 +00:00
using Ooui;
2019-02-04 22:22:45 +00:00
using System.Net.Http;
2021-05-09 18:27:13 +00:00
using System.Collections.Generic;
using System.Linq;
2017-11-09 07:12:59 +00:00
namespace Xamarin.Forms
{
2017-11-09 21:03:56 +00:00
public static class Forms
{
public static bool IsInitialized { get; private set; }
2017-11-09 07:12:59 +00:00
2017-11-09 21:03:56 +00:00
public static void Init ()
{
if (IsInitialized)
return;
IsInitialized = true;
2017-11-09 07:12:59 +00:00
2018-03-10 05:28:39 +00:00
Log.Listeners.Add (new DelegateLogListener ((c, m) => System.Diagnostics.Debug.WriteLine (m, c)));
2017-11-09 07:12:59 +00:00
2017-11-09 21:03:56 +00:00
Device.SetIdiom (TargetIdiom.Desktop);
Device.PlatformServices = new OouiPlatformServices ();
Device.Info = new OouiDeviceInfo ();
2017-12-11 01:53:36 +00:00
Color.SetAccent (Color.FromHex ("#337ab7")); // Bootstrap Blue
2017-11-09 07:12:59 +00:00
2017-11-09 21:03:56 +00:00
Registrar.RegisterAll (new[] {
typeof(ExportRendererAttribute),
typeof(ExportCellAttribute),
2017-12-11 01:51:05 +00:00
typeof(ExportImageSourceHandlerAttribute),
2017-12-10 06:19:16 +00:00
});
2017-11-09 21:03:56 +00:00
}
public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;
2019-02-04 22:22:45 +00:00
public static HttpClientHandler HttpClientHandler { get; set; }
2017-11-09 21:03:56 +00:00
public static void SendViewInitialized (this VisualElement self, Ooui.Element nativeView)
{
ViewInitialized?.Invoke (self, new ViewInitializedEventArgs { View = self, NativeView = nativeView });
}
class OouiDeviceInfo : DeviceInfo
{
public override Size PixelScreenSize => new Size (640, 480);
public override Size ScaledScreenSize => PixelScreenSize;
public override double ScalingFactor => 1;
}
class OouiPlatformServices : IPlatformServices
{
public bool IsInvokeRequired => false;
public string RuntimePlatform => "Ooui";
2021-05-09 18:27:13 +00:00
public OSAppTheme RequestedTheme => OSAppTheme.Unspecified;
2017-11-09 21:03:56 +00:00
public void BeginInvokeOnMainThread (Action action)
{
Task.Run (action);
}
public Assembly[] GetAssemblies ()
{
return AppDomain.CurrentDomain.GetAssemblies ();
}
public string GetMD5Hash (string input)
{
2019-02-04 22:22:45 +00:00
return Utilities.GetMd5Hash(input);
2017-11-09 21:03:56 +00:00
}
public double GetNamedSize (NamedSize size, Type targetElementType, bool useOldSizes)
{
2017-11-10 01:23:41 +00:00
switch (size) {
default:
case NamedSize.Default:
return 16;
case NamedSize.Micro:
return 9;
case NamedSize.Small:
return 12;
case NamedSize.Medium:
return 22;
case NamedSize.Large:
return 32;
}
2017-11-09 21:03:56 +00:00
}
2019-02-04 22:22:45 +00:00
public async Task<Stream> GetStreamAsync (Uri uri, CancellationToken cancellationToken)
2017-11-09 21:03:56 +00:00
{
2019-02-04 22:22:45 +00:00
//NOTE: Wanted to use the same facility that ImageLoaderSourceHandler uses,
// but couldn't find an optional way to ignore certificate errors with self-signed
// certificates with that approach. Calling:
// ServicePointManager.ServerCertificateValidationCallback += (o, cert, chain, errors) => true;
// in web application seemed to get ignored.
//var imageSource = new UriImageSource() { Uri = uri };
//return imageSource.GetStreamAsync(cancellationToken);
using (var client = HttpClientHandler == null ? new HttpClient() : new HttpClient(HttpClientHandler))
{
HttpResponseMessage streamResponse = await client.GetAsync(uri.AbsoluteUri).ConfigureAwait(false);
if (!streamResponse.IsSuccessStatusCode)
{
Log.Warning("HTTP Request", $"Could not retrieve {uri}, status code {streamResponse.StatusCode}");
return null;
}
return await streamResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
2017-11-09 21:03:56 +00:00
}
public IIsolatedStorageFile GetUserStoreForApplication ()
{
return new LocalIsolatedStorageFile ();
2017-11-09 21:03:56 +00:00
}
public void OpenUriAction (Uri uri)
{
throw new NotImplementedException ();
}
public void StartTimer (TimeSpan interval, Func<bool> callback)
{
2017-11-10 17:57:45 +00:00
Timer timer = null;
timer = new Timer ((_ => {
if (!callback ()) {
timer?.Dispose ();
timer = null;
}
}), null, (int)interval.TotalMilliseconds, (int)interval.TotalMilliseconds);
2017-11-09 21:03:56 +00:00
}
2017-12-10 06:19:16 +00:00
public Ticker CreateTicker ()
{
return new OouiTicker ();
}
class OouiTicker : Ticker
{
Timer timer;
protected override void DisableTimer ()
{
var t = timer;
timer = null;
t?.Dispose ();
}
protected override void EnableTimer ()
{
if (timer != null)
return;
2018-03-09 23:14:51 +00:00
var interval = TimeSpan.FromSeconds (1.0 / Ooui.UI.MaxFps);
2017-12-10 06:19:16 +00:00
timer = new Timer ((_ => {
this.SendSignals ();
}), null, (int)interval.TotalMilliseconds, (int)interval.TotalMilliseconds);
}
}
public void QuitApplication()
{
}
2019-03-21 22:01:16 +00:00
public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
var renderer = Ooui.Forms.Platform.GetRenderer(view);
return renderer.GetDesiredSize(widthConstraint, heightConstraint);
}
public string GetHash(string input)
2021-05-09 18:27:13 +00:00
{
return Utilities.GetHash(input);
}
2021-05-09 18:27:13 +00:00
static readonly Dictionary<string, Color> colorNames =
typeof (Color)
.GetFields (BindingFlags.Static | BindingFlags.Public)
.Where(x => x.FieldType == typeof(Color))
.ToDictionary (x => x.Name, x => (Color)x.GetValue (null));
public Color GetNamedColor(string name)
{
2021-05-09 18:27:13 +00:00
if (colorNames.TryGetValue (name, out var color))
return color;
return Color.Default;
}
2017-11-09 21:03:56 +00:00
}
public class ViewInitializedEventArgs
{
public VisualElement View { get; set; }
public Ooui.Element NativeView { get; set; }
}
public static void LoadApplication (Application application)
{
Application.Current = application;
var mainPage = application.MainPage;
if (mainPage != null) {
2023-03-17 08:08:32 +00:00
PageExtensions.Ui.Publish ("/", application.MainPage.GetOouiElement ());
}
}
2017-11-09 21:03:56 +00:00
}
2017-11-09 07:12:59 +00:00
}