-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathGtkWindow.cs
913 lines (818 loc) · 22.1 KB
/
GtkWindow.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
using Eto.GtkSharp.Drawing;
using Eto.GtkSharp.Forms;
using Eto.GtkSharp.Forms.Menu;
namespace Eto.GtkSharp.Forms
{
public interface IGtkWindow
{
bool CloseWindow(Action<CancelEventArgs> closing = null);
Gtk.Window Control { get; }
Size UserPreferredSize { get; }
}
public class GtkShrinkableVBox : Gtk.VBox
{
public bool Resizable { get; set; }
public GtkShrinkableVBox()
{
}
public GtkShrinkableVBox(Gtk.Widget child)
{
if (child != null)
PackStart(child, true, true, 0);
}
#if GTK3
protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidth(out minimum_width, out natural_width);
if (Resizable)
minimum_width = 0;
}
protected override void OnGetPreferredHeight(out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeight(out minimum_height, out natural_height);
if (Resizable)
minimum_height = 0;
}
#if GTKCORE
protected override void OnGetPreferredHeightAndBaselineForWidth(int width, out int minimum_height, out int natural_height, out int minimum_baseline, out int natural_baseline)
{
base.OnGetPreferredHeightAndBaselineForWidth(width, out minimum_height, out natural_height, out minimum_baseline, out natural_baseline);
if (Resizable)
minimum_height = 0;
}
#endif
#endif
}
static class GtkWindow
{
internal static readonly object MovableByWindowBackground_Key = new object();
internal static readonly object DisableAutoSizeUpdate_Key = new object();
internal static readonly object AutoSizePerformed_Key = new object();
internal static readonly object AutoSize_Key = new object();
internal static readonly object Minimizable_Key = new object();
internal static readonly object Maximizable_Key = new object();
}
public abstract class GtkWindow<TControl, TWidget, TCallback> : GtkPanel<TControl, TWidget, TCallback>, Window.IHandler, IGtkWindow
where TControl : Gtk.Window
where TWidget : Window
where TCallback : Window.ICallback
{
Gtk.VBox vbox;
readonly Gtk.VBox actionvbox;
readonly Gtk.Box topToolbarBox;
Gtk.Box menuBox;
GtkShrinkableVBox containerBox;
readonly Gtk.Box bottomToolbarBox;
MenuBar menuBar;
Icon icon;
Eto.Forms.ToolBar toolBar;
Gtk.AccelGroup accelGroup;
Rectangle? restoreBounds;
Point? currentLocation;
Size minimumSize;
WindowState state;
WindowStyle style;
bool topmost;
bool resizable;
Size? clientSize;
protected GtkWindow()
{
vbox = new Gtk.VBox();
actionvbox = new Gtk.VBox();
menuBox = new Gtk.HBox();
topToolbarBox = new Gtk.VBox();
containerBox = new GtkShrinkableVBox();
containerBox.Resizable = true;
containerBox.Visible = true;
bottomToolbarBox = new Gtk.VBox();
actionvbox.PackStart(menuBox, false, false, 0);
actionvbox.PackStart(topToolbarBox, false, false, 0);
vbox.PackStart(containerBox, true, true, 0);
vbox.PackStart(bottomToolbarBox, false, false, 0);
}
protected override Color DefaultBackgroundColor
{
get { return Control.GetBackground(); }
}
protected override bool UseMinimumSizeRequested
{
get { return false; }
}
public override Size MinimumSize
{
get { return minimumSize; }
set
{
minimumSize = value;
SetMinMax(null);
}
}
public override bool HasFocus => Control.HasToplevelFocus;
public Gtk.Widget WindowContentControl
{
get { return vbox; }
}
public Gtk.Widget WindowActionControl
{
get { return actionvbox; }
}
public override Gtk.Widget ContainerContentControl
{
get { return containerBox; }
}
public bool Resizable
{
get { return resizable; }
set
{
containerBox.Resizable = value;
resizable = value;
#if GTK3
Control.Resizable = value;
#endif
SetMinMax(null);
}
}
private void SetMinMax(Size? size)
{
var geom = new Gdk.Geometry();
geom.MinWidth = minimumSize.Width;
geom.MinHeight = minimumSize.Height;
#if GTK2
if (!resizable)
{
if (size != null)
{
geom.MinWidth = geom.MaxWidth = size.Value.Width;
geom.MinHeight = geom.MaxHeight = size.Value.Height;
}
else if (Control.IsRealized)
{
geom.MinWidth = geom.MaxWidth = Control.Allocation.Width;
geom.MinHeight = geom.MaxHeight = Control.Allocation.Height;
}
else
{
geom.MinWidth = geom.MaxWidth = Control.DefaultWidth;
geom.MinHeight = geom.MaxHeight = Control.DefaultHeight;
}
}
#endif
Control.SetGeometryHints(Control, geom, Gdk.WindowHints.MinSize);
}
public bool Minimizable
{
get => Widget.Properties.Get<bool>(GtkWindow.Minimizable_Key, true);
set
{
if (Widget.Properties.TrySet(GtkWindow.Minimizable_Key, value, true))
SetTypeHint();
}
}
public bool Maximizable
{
get => Widget.Properties.Get<bool>(GtkWindow.Maximizable_Key, true);
set
{
if (Widget.Properties.TrySet(GtkWindow.Maximizable_Key, value, true))
SetTypeHint();
}
}
public bool ShowInTaskbar
{
get { return !Control.SkipTaskbarHint; }
set { Control.SkipTaskbarHint = !value; }
}
public bool Closeable
{
get => Control.Deletable;
set => Control.Deletable = value;
}
public bool Topmost
{
get { return topmost; }
set
{
if (topmost != value)
{
topmost = value;
Control.KeepAbove = topmost;
}
}
}
public WindowStyle WindowStyle
{
get { return style; }
set
{
if (style != value)
{
style = value;
switch (style)
{
case WindowStyle.Default:
Control.Decorated = true;
break;
case WindowStyle.None:
Control.Decorated = false;
break;
case WindowStyle.Utility:
Control.Decorated = true;
break;
default:
throw new NotSupportedException();
}
SetTypeHint();
}
}
}
protected virtual Gdk.WindowTypeHint DefaultTypeHint => Gdk.WindowTypeHint.Normal;
void SetTypeHint()
{
if (WindowStyle == WindowStyle.Default && (Minimizable || Maximizable))
{
Control.TypeHint = DefaultTypeHint;
}
else
{
Control.TypeHint = Gdk.WindowTypeHint.Utility;
}
}
public override Size Size
{
get => Widget.Loaded ? Control.GetWindow()?.FrameExtents.Size.ToEto() ?? UserPreferredSize : UserPreferredSize;
set
{
DisableAutoSizeUpdate++;
UserPreferredSize = value;
if (Widget.Loaded)
{
var diff = WindowDecorationSize;
Control.Resize(value.Width - diff.Width, value.Height - diff.Height);
}
else
{
clientSize = null;
// this doesn't take into account window decoration size
// there doesn't seem to be an (easy) way to do that currently..
Control.SetDefaultSize(value.Width, value.Height);
}
SetMinMax(value);
DisableAutoSizeUpdate--;
}
}
public override Size ClientSize
{
get
{
return containerBox.IsRealized ? containerBox.Allocation.Size.ToEto() : containerBox.GetPreferredSize().ToEto();
}
set
{
DisableAutoSizeUpdate++;
if (Control.IsRealized)
{
var diff = vbox.Allocation.Size.ToEto() - containerBox.Allocation.Size.ToEto();
Control.Resize(value.Width + diff.Width, value.Height + diff.Height);
SetMinMax(value + diff);
}
else
{
clientSize = value;
Control.SetDefaultSize(value.Width, value.Height);
SetMinMax(value);
}
DisableAutoSizeUpdate--;
}
}
public bool MovableByWindowBackground
{
get => Widget.Properties.Get<bool>(GtkWindow.MovableByWindowBackground_Key);
set
{
if (Widget.Properties.TrySet(GtkWindow.MovableByWindowBackground_Key, value))
{
if (value)
Control.ButtonPressEvent += Connector.ButtonPressEvent_Movable;
else
Control.ButtonPressEvent -= Connector.ButtonPressEvent_Movable;
}
}
}
private void Control_Realized(object sender, EventArgs e)
{
Control.Realized -= Control_Realized;
Size size;
if (clientSize.HasValue)
{
size = clientSize.Value;
}
else
{
size = UserPreferredSize - WindowDecorationSize;
}
if (size.Width > 0 || size.Height > 0)
{
var allocated = Control.Allocation.Size;
if (size.Width < 0)
size.Width = allocated.Width;
if (size.Height < 0)
size.Height = allocated.Height;
Control.Resize(size.Width, size.Height);
}
}
protected override void Initialize()
{
base.Initialize();
DisableAutoSizeUpdate++;
HandleEvent(Window.WindowStateChangedEvent); // to set restore bounds properly
HandleEvent(Window.ClosingEvent); // to chain application termination events
HandleEvent(Eto.Forms.Control.SizeChangedEvent); // for RestoreBounds
HandleEvent(Window.LocationChangedEvent); // for RestoreBounds
Control.SetSizeRequest(-1, -1);
Control.Realized += Connector.Control_Realized;
ApplicationHandler.Instance.RegisterIsActiveChanged(Control);
}
public override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
DisableAutoSizeUpdate--;
}
public override void AttachEvent(string id)
{
switch (id)
{
case Eto.Forms.Control.KeyDownEvent:
EventControl.AddEvents((int)Gdk.EventMask.KeyPressMask);
EventControl.KeyPressEvent += Connector.HandleWindowKeyPressEvent;
break;
case Window.ClosedEvent:
HandleEvent(Window.ClosingEvent);
break;
case Window.ClosingEvent:
Control.DeleteEvent += Connector.HandleDeleteEvent;
break;
case Eto.Forms.Control.ShownEvent:
Control.Shown += Connector.HandleShownEvent;
break;
case Window.WindowStateChangedEvent:
Connector.OldState = WindowState;
Control.WindowStateEvent += Connector.HandleWindowStateEvent;
break;
case Eto.Forms.Control.SizeChangedEvent:
Control.SizeAllocated += Connector.HandleWindowSizeAllocated;
break;
case Window.LocationChangedEvent:
Control.ConfigureEvent += Connector.HandleConfigureEvent;
break;
case Window.LogicalPixelSizeChangedEvent:
// not supported on GTK, yet.
break;
default:
base.AttachEvent(id);
break;
}
}
protected new GtkWindowConnector Connector { get { return (GtkWindowConnector)base.Connector; } }
protected override WeakConnector CreateConnector()
{
return new GtkWindowConnector();
}
protected class GtkWindowConnector : GtkPanelEventConnector
{
Size? oldSize;
public WindowState OldState { get; set; }
public new GtkWindow<TControl, TWidget, TCallback> Handler { get { return (GtkWindow<TControl, TWidget, TCallback>)base.Handler; } }
public void HandleDeleteEvent(object o, Gtk.DeleteEventArgs args)
{
var handler = Handler;
if (handler == null)
return;
args.RetVal = !handler.CloseWindow();
}
public void HandleShownEvent(object sender, EventArgs e)
{
Handler?.Callback.OnShown(Handler.Widget, EventArgs.Empty);
}
public void HandleWindowStateEvent(object o, Gtk.WindowStateEventArgs args)
{
var handler = Handler;
if (handler == null)
return;
var windowState = handler.WindowState;
if (windowState == WindowState.Normal)
{
if (args.Event.ChangedMask.HasFlag(Gdk.WindowState.Maximized) && !args.Event.NewWindowState.HasFlag(Gdk.WindowState.Maximized))
{
handler.restoreBounds = handler.Widget.Bounds;
}
else if (args.Event.ChangedMask.HasFlag(Gdk.WindowState.Iconified) && !args.Event.NewWindowState.HasFlag(Gdk.WindowState.Iconified))
{
handler.restoreBounds = handler.Widget.Bounds;
}
else if (args.Event.ChangedMask.HasFlag(Gdk.WindowState.Fullscreen) && !args.Event.NewWindowState.HasFlag(Gdk.WindowState.Fullscreen))
{
handler.restoreBounds = handler.Widget.Bounds;
}
}
if (windowState != OldState)
{
OldState = windowState;
Handler.Callback.OnWindowStateChanged(Handler.Widget, EventArgs.Empty);
}
}
// do not connect before, otherwise it is sent before sending to child
public void HandleWindowKeyPressEvent(object o, Gtk.KeyPressEventArgs args)
{
var handler = Handler;
if (handler == null)
return;
var e = args.Event.ToEto();
if (e != null)
{
handler.Callback.OnKeyDown(handler.Widget, e);
args.RetVal = e.Handled;
}
}
public void HandleWindowSizeAllocated(object o, Gtk.SizeAllocatedArgs args)
{
var handler = Handler;
if (handler == null)
return;
var newSize = handler.Control.Allocation.Size.ToEto();
if (handler.Control.IsRealized && oldSize != newSize)
{
handler.Callback.OnSizeChanged(Handler.Widget, EventArgs.Empty);
// annoyingly, there's no way to tell if the user is resizing things
if (handler.AutoSizePerformed == 0 && handler.DisableAutoSizeUpdate == 0)
handler.AutoSize = false;
if (handler.WindowState == WindowState.Normal)
handler.restoreBounds = handler.Widget.Bounds;
oldSize = newSize;
}
if (handler.AutoSizePerformed > 0)
handler.AutoSizePerformed--;
}
Point? oldLocation;
[GLib.ConnectBefore]
public void HandleConfigureEvent(object o, Gtk.ConfigureEventArgs args)
{
var handler = Handler;
if (handler == null)
return;
handler.currentLocation = new Point(args.Event.X, args.Event.Y);
if (handler.Control.IsRealized && handler.Widget.Loaded && oldLocation != handler.currentLocation)
{
handler.Callback.OnLocationChanged(handler.Widget, EventArgs.Empty);
if (handler.WindowState == WindowState.Normal)
handler.restoreBounds = handler.Widget.Bounds;
oldLocation = handler.currentLocation;
}
handler.currentLocation = null;
}
internal void Control_Realized(object sender, EventArgs e) => Handler?.Control_Realized(sender, e);
internal void ButtonPressEvent_Movable(object o, Gtk.ButtonPressEventArgs args)
{
var handler = Handler;
if (handler == null)
return;
var evt = args.Event;
if (handler != null && evt.Type == Gdk.EventType.ButtonPress && evt.Button == 1)
{
handler.Control.BeginMoveDrag((int)evt.Button, (int)evt.XRoot, (int)evt.YRoot, evt.Time);
}
}
}
public MenuBar Menu
{
get { return menuBar; }
set
{
if (menuBar != null)
menuBox.Remove((Gtk.Widget)menuBar.ControlObject);
if (accelGroup != null)
Control.RemoveAccelGroup(accelGroup);
accelGroup = new Gtk.AccelGroup();
Control.AddAccelGroup(accelGroup);
// set accelerators
menuBar = value;
var handler = menuBar != null ? menuBar.Handler as IMenuHandler : null;
if (handler != null)
handler.SetAccelGroup(accelGroup);
if (value != null)
{
menuBox.PackStart((Gtk.Widget)value.ControlObject, true, true, 0);
((Gtk.Widget)value.ControlObject).ShowAll();
}
}
}
protected override void SetContainerContent(Gtk.Widget content)
{
containerBox.PackStart(content, true, true, 0);
}
public string Title
{
get { return Control.Title; }
set { Control.Title = value; }
}
public bool CloseWindow(Action<CancelEventArgs> closing = null)
{
var args = new CancelEventArgs();
Callback.OnClosing(Widget, args);
var shouldQuit = false;
if (!args.Cancel)
{
if (closing != null)
closing(args);
else
{
var windows = Gdk.Screen.Default.ToplevelWindows.Where(r => r.State != Gdk.WindowState.Withdrawn && r.WindowType != Gdk.WindowType.Temp).ToList();
if (windows.Count == 1 && ReferenceEquals(windows[0], Control.GetWindow()))
{
var app = ((ApplicationHandler)Application.Instance.Handler);
app.Callback.OnTerminating(app.Widget, args);
shouldQuit = !args.Cancel;
}
}
}
if (!args.Cancel)
{
Callback.OnClosed(Widget, EventArgs.Empty);
if (shouldQuit)
Gtk.Application.Quit();
}
return !args.Cancel;
}
public virtual void Close()
{
if (Widget.Loaded && CloseWindow())
{
Control.Hide();
Control.Unrealize();
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
#if !GTKCORE
Control.Destroy();
#endif
if (menuBox != null)
{
menuBox.Dispose();
menuBox = null;
}
if (vbox != null)
{
vbox.Dispose();
vbox = null;
}
if (containerBox != null)
{
containerBox.Dispose();
containerBox = null;
}
}
base.Dispose(disposing);
}
public Eto.Forms.ToolBar ToolBar
{
get
{
return toolBar;
}
set
{
if (toolBar != null)
topToolbarBox.Remove((Gtk.Widget)toolBar.ControlObject);
toolBar = value;
if (toolBar != null)
topToolbarBox.Add((Gtk.Widget)toolBar.ControlObject);
topToolbarBox.ShowAll();
}
}
public Icon Icon
{
get { return icon; }
set
{
icon = value;
Control.Icon = ((IconHandler)icon.Handler).Pixbuf;
}
}
public new Point Location
{
get
{
if (currentLocation != null)
return currentLocation.Value;
int x, y;
Control.GetPosition(out x, out y);
return new Point(x, y);
}
set
{
Control.Move(value.X, value.Y);
}
}
public WindowState WindowState
{
get
{
var gdkWindow = Control.GetWindow();
if (gdkWindow == null)
return state;
if (gdkWindow.State.HasFlag(Gdk.WindowState.Fullscreen))
return WindowState.FullScreen;
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
return WindowState.Minimized;
if (gdkWindow.State.HasFlag(Gdk.WindowState.Maximized))
return WindowState.Maximized;
return WindowState.Normal;
}
set
{
if (WindowState != value)
{
state = value;
var gdkWindow = Control.GetWindow();
switch (value)
{
case WindowState.FullScreen:
if (gdkWindow != null)
{
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Deiconify();
}
Control.Fullscreen();
break;
case WindowState.Maximized:
if (gdkWindow != null)
{
if (gdkWindow.State.HasFlag(Gdk.WindowState.Fullscreen))
Control.Unfullscreen();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Deiconify();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Present();
}
Control.Maximize();
break;
case WindowState.Minimized:
Control.Iconify();
break;
case WindowState.Normal:
if (gdkWindow != null)
{
if (gdkWindow.State.HasFlag(Gdk.WindowState.Fullscreen))
Control.Unfullscreen();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Maximized))
Control.Unmaximize();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Deiconify();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Present();
}
break;
}
}
}
}
public Rectangle RestoreBounds
{
get
{
return WindowState == WindowState.Normal ? Widget.Bounds : restoreBounds ?? Widget.Bounds;
}
}
public double Opacity
{
get { return Control.Opacity; }
set { Control.Opacity = value; }
}
Gtk.Window IGtkWindow.Control { get { return Control; } }
public Screen Screen
{
get
{
var screen = Control.Screen;
var gdkWindow = Control.GetWindow();
if (screen != null && gdkWindow != null)
{
#if GTKCORE
var monitor = screen.Display.GetMonitorAtWindow(gdkWindow);
return new Screen(new ScreenHandler(monitor));
#else
var monitor = screen.GetMonitorAtWindow(gdkWindow);
return new Screen(new ScreenHandler(screen, monitor));
#endif
}
return null;
}
}
protected override void GrabFocus() => Control.Present();
public void BringToFront()
{
if (WindowState == WindowState.Minimized)
{
Control.Deiconify();
// if it didn't work, present it
if (WindowState == WindowState.Minimized)
Control.Present();
}
Control.GetWindow()?.Raise();
}
public void SendToBack() => Control.GetWindow()?.Lower();
public virtual void SetOwner(Window owner)
{
Control.TransientFor = owner.ToGtk();
}
public override bool Visible
{
get { return base.Visible; }
set
{
base.Visible = value;
Control.NoShowAll = false; // always show all.
}
}
public float LogicalPixelSize
{
get
{
#if GTKCORE
var screen = Control.Screen;
var gdkWindow = Control.GetWindow();
if (screen != null && gdkWindow != null)
{
var monitor = screen.Display.GetMonitorAtWindow(gdkWindow);
return monitor?.ScaleFactor ?? 1f;
}
#endif
return 1f;
}
}
internal int DisableAutoSizeUpdate
{
get => Widget.Properties.Get<int>(GtkWindow.DisableAutoSizeUpdate_Key);
set => Widget.Properties.Set(GtkWindow.DisableAutoSizeUpdate_Key, value);
}
internal int AutoSizePerformed
{
get => Widget.Properties.Get<int>(GtkWindow.AutoSizePerformed_Key);
set => Widget.Properties.Set(GtkWindow.AutoSizePerformed_Key, value);
}
public bool AutoSize
{
get => Widget.Properties.Get<bool>(GtkWindow.AutoSize_Key) || !Resizable; // gtk always auto sizes when the window is not resizable
set
{
if (Widget.Properties.TrySet(GtkWindow.AutoSize_Key, value))
{
InvalidateMeasure();
}
}
}
public override SizeF GetPreferredSize(SizeF availableSize)
{
var size = base.GetPreferredSize(availableSize);
return size + WindowDecorationSize;
}
Size WindowDecorationSize
{
get
{
var window = Control.GetWindow();
if (window == null)
return Size.Empty;
return window.FrameExtents.Size.ToEto() - Control.Allocation.Size.ToEto();
}
}
bool isInvalidated;
internal void PerformResize()
{
Control.GetSize(out var width, out var height);
var availableSize = Screen?.WorkingArea.Size ?? SizeF.PositiveInfinity;
var preferred = Size.Round(SizeF.Min(base.GetPreferredSize(availableSize), availableSize));
if (preferred.Width != width || preferred.Height != height)
{
// signal that we are auto sizing ourselves so don't turn off AutoSize.
AutoSizePerformed++;
Control.Resize(preferred.Width, preferred.Height);
}
isInvalidated = false;
DisableAutoSizeUpdate--;
}
public override void InvalidateMeasure()
{
base.InvalidateMeasure();
if (!isInvalidated && Resizable && Widget.Loaded && !Widget.IsSuspended && AutoSize && DisableAutoSizeUpdate == 0 && AutoSizePerformed == 0)
{
isInvalidated = true;
DisableAutoSizeUpdate++;
Application.Instance.AsyncInvoke(PerformResize);
}
}
}
}