Pixiview/Pixiview/UI/AdaptedPage.cs

103 lines
2.4 KiB
C#

using System;
using Pixiview.UI.Theme;
using Pixiview.Utils;
using Xamarin.Forms;
namespace Pixiview.UI
{
public class AdaptedPage : ContentPage
{
public Orientation CurrentOrientation { get; private set; }
public event EventHandler Load;
public event EventHandler Unload;
public event EventHandler<OrientationEventArgs> OrientationChanged;
public AdaptedPage()
{
SetDynamicResource(Screen.StatusBarStyleProperty, ThemeBase.StatusBarStyle);
Shell.SetNavBarHasShadow(this, true);
}
public virtual void OnLoad()
{
Load?.Invoke(this, EventArgs.Empty);
}
public virtual void OnUnload()
{
Unload?.Invoke(this, EventArgs.Empty);
}
public virtual void OnOrientationChanged(Orientation orientation)
{
OrientationChanged?.Invoke(this, new OrientationEventArgs { CurrentOrientation = orientation });
}
public void InitOrientation(Orientation orientation)
{
CurrentOrientation = orientation;
}
protected void Start(Action action)
{
if (Tap.IsBusy)
{
App.DebugPrint("gesture recognizer is now busy...");
return;
}
using (Tap.Start())
{
action();
}
}
private class Tap : IDisposable
{
public static bool IsBusy => _instance?.isBusy == true;
private static readonly object sync = new object();
private static Tap _instance;
private Tap() { }
public static Tap Start()
{
if (_instance == null)
{
_instance = new Tap();
}
lock (sync)
{
_instance.isBusy = true;
}
return _instance;
}
private bool isBusy = false;
public void Dispose()
{
lock (sync)
{
isBusy = false;
}
}
}
}
public class OrientationEventArgs : EventArgs
{
public Orientation CurrentOrientation { get; set; }
}
public enum Orientation
{
Unknown,
Portrait,
PortraitUpsideDown,
LandscapeLeft,
LandscapeRight
}
}