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

164 lines
5.0 KiB
C#
Raw Normal View History

2017-11-09 07:12:59 +00:00
using System;
using System.Diagnostics;
using Xamarin.Forms;
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;
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;
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";
public void BeginInvokeOnMainThread (Action action)
{
Task.Run (action);
}
public Assembly[] GetAssemblies ()
{
return AppDomain.CurrentDomain.GetAssemblies ();
}
public string GetMD5Hash (string input)
{
throw new NotImplementedException ();
}
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
}
public Task<Stream> GetStreamAsync (Uri uri, CancellationToken cancellationToken)
{
throw new NotImplementedException ();
}
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()
{
}
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) {
UI.Publish ("/", application.MainPage.GetOouiElement ());
}
}
2017-11-09 21:03:56 +00:00
}
2017-11-09 07:12:59 +00:00
}