-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
WindowExtensions.cs
127 lines (108 loc) · 3.8 KB
/
WindowExtensions.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using ElmSharp;
using Microsoft.Maui.ApplicationModel;
using Tizen.UIExtensions.Common;
using Tizen.UIExtensions.ElmSharp;
using ELayout = ElmSharp.Layout;
namespace Microsoft.Maui.Platform
{
public static partial class WindowExtensions
{
static Dictionary<Window, Func<bool>> s_windowBackButtonPressedHandler = new Dictionary<Window, Func<bool>>();
static Dictionary<Window, Action> s_windowCloseRequestHandler = new Dictionary<Window, Action>();
static Dictionary<Window, ELayout> s_windowBaseLayout = new Dictionary<Window, ELayout>();
static Dictionary<Window, ModalStack> s_windowModalStack = new Dictionary<Window, ModalStack>();
public static void Initialize(this Window platformWindow)
{
var baseLayout = (ELayout?)platformWindow.GetType().GetProperty("BaseLayout")?.GetValue(platformWindow);
if (baseLayout == null)
{
var conformant = new Conformant(platformWindow);
conformant.Show();
var layout = new ApplicationLayout(conformant);
layout.Show();
baseLayout = layout;
conformant.SetContent(baseLayout);
}
platformWindow.SetBaseLayout(baseLayout);
var modalStack = new ModalStack(baseLayout)
{
AlignmentX = -1,
AlignmentY = -1,
WeightX = 1,
WeightY = 1,
};
modalStack.Show();
baseLayout.SetContent(modalStack);
platformWindow.SetModalStack(modalStack);
platformWindow.Active();
platformWindow.Show();
platformWindow.AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_90 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270;
platformWindow.RotationChanged += (sender, e) =>
{
// TODO : should update later
};
platformWindow.BackButtonPressed += (s, e) => OnBackButtonPressed(platformWindow);
}
public static void SetOverlay(this Window window, EvasObject content)
{
content?.SetAlignment(-1, -1);
content?.SetWeight(1, 1);
content?.Show();
window.AddResizeObject(content);
}
public static void SetWindowCloseRequestHandler(this Window window, Action handler)
{
s_windowCloseRequestHandler[window] = handler;
}
public static void SetBackButtonPressedHandler(this Window window, Func<bool> handler)
{
s_windowBackButtonPressedHandler[window] = handler;
}
public static ELayout GetBaseLayout(this Window window)
{
return s_windowBaseLayout[window];
}
public static void SetBaseLayout(this Window window, ELayout layout)
{
s_windowBaseLayout[window] = layout;
}
public static ModalStack GetModalStack(this Window window)
{
return s_windowModalStack[window];
}
public static void SetModalStack(this Window window, ModalStack modalStack)
{
s_windowModalStack[window] = modalStack;
}
public static float GetDisplayDensity(this Window window)
{
return (float)DeviceInfo.ScalingFactor;
}
static void OnBackButtonPressed(Window window)
{
if (s_windowBackButtonPressedHandler.ContainsKey(window))
{
if (s_windowBackButtonPressedHandler[window].Invoke())
return;
}
if (s_windowCloseRequestHandler.ContainsKey(window))
s_windowCloseRequestHandler[window].Invoke();
}
internal static Devices.DisplayOrientation GetOrientation(this IWindow window)
{
bool isTV = Elementary.GetProfile() == "tv";
return window.Handler?.MauiContext?.GetPlatformWindow()?.Rotation switch
{
0 => isTV ? Devices.DisplayOrientation.Landscape : Devices.DisplayOrientation.Portrait,
90 => isTV ? Devices.DisplayOrientation.Portrait : Devices.DisplayOrientation.Landscape,
180 => isTV ? Devices.DisplayOrientation.Landscape : Devices.DisplayOrientation.Portrait,
270 => isTV ? Devices.DisplayOrientation.Portrait : Devices.DisplayOrientation.Landscape,
_ => Devices.DisplayOrientation.Unknown
};
}
}
}