This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
FormsWindow.cs
149 lines (116 loc) · 3.3 KB
/
FormsWindow.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Gtk;
using System;
using System.ComponentModel;
using System.Threading;
namespace Xamarin.Forms.Platform.GTK
{
public class FormsWindow : Window
{
private Application _application;
private Gdk.Size _lastSize;
public FormsWindow()
: base(WindowType.Toplevel)
{
SetDefaultSize(800, 600);
SetSizeRequest(400, 400);
MainThreadID = Thread.CurrentThread.ManagedThreadId;
MainWindow = this;
if (SynchronizationContext.Current == null)
SynchronizationContext.SetSynchronizationContext(new GtkSynchronizationContext());
WindowStateEvent += OnWindowStateEvent;
}
public static int MainThreadID { get; set; }
public static Window MainWindow { get; set; }
public void LoadApplication (Application application)
{
if (application == null)
throw new ArgumentNullException (nameof (application));
Application.SetCurrentApplication (application);
_application = application;
application.PropertyChanged += ApplicationOnPropertyChanged;
UpdateMainPage ();
_application.SendStart ();
}
public void SetApplicationTitle (string title)
{
if (string.IsNullOrEmpty (title))
return;
Title = title;
}
public void SetApplicationIcon (string icon)
{
if (string.IsNullOrEmpty (icon))
return;
var appliccationIconPixbuf = new Gdk.Pixbuf(icon);
Icon = appliccationIconPixbuf;
}
public sealed override void Dispose ()
{
base.Dispose ();
Dispose (true);
}
protected override bool OnDeleteEvent (Gdk.Event evnt)
{
base.OnDeleteEvent (evnt);
Gtk.Application.Quit ();
return true;
}
private void ApplicationOnPropertyChanged (object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == nameof (Application.MainPage)) {
UpdateMainPage ();
}
}
protected override bool OnConfigureEvent (Gdk.EventConfigure evnt)
{
Gdk.Size newSize = new Gdk.Size(evnt.Width, evnt.Height);
if (_lastSize != newSize) {
_lastSize = newSize;
var pageRenderer = Platform.GetRenderer(_application.MainPage);
pageRenderer?.SetElementSize (new Size (newSize.Width, newSize.Height));
}
return base.OnConfigureEvent (evnt);
}
private void UpdateMainPage ()
{
if (_application.MainPage == null)
return;
var platformRenderer = Child as PlatformRenderer;
if (platformRenderer != null) {
RemoveChildIfExists ();
((IDisposable)platformRenderer.Platform).Dispose ();
}
var platform = new Platform();
platform.PlatformRenderer.SetSizeRequest (WidthRequest, HeightRequest);
Add (platform.PlatformRenderer);
platform.SetPage (_application.MainPage);
Child.ShowAll ();
}
private void RemoveChildIfExists ()
{
foreach (var child in Children) {
var widget = child as Widget;
if (widget != null) {
Remove (widget);
}
}
}
private void OnWindowStateEvent (object o, WindowStateEventArgs args)
{
if (args.Event.ChangedMask == Gdk.WindowState.Iconified) {
var windowState = args.Event.NewWindowState;
if (windowState == Gdk.WindowState.Iconified)
_application.SendSleep();
else
_application.SendResume ();
}
}
private void Dispose (bool disposing)
{
if (disposing && _application != null) {
WindowStateEvent -= OnWindowStateEvent;
_application.PropertyChanged -= ApplicationOnPropertyChanged;
}
}
}
}