-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWpfWindow.cs
executable file
·1139 lines (1012 loc) · 29 KB
/
WpfWindow.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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Eto.Wpf.CustomControls;
using Eto.Wpf.Forms.Menu;
using Eto.Wpf.Drawing;
namespace Eto.Wpf.Forms
{
public interface IWpfWindow
{
sw.Window Control { get; }
void SetOwnerFor(sw.Window child);
}
public interface IWpfValidateBinding
{
void Validate();
}
static class WpfWindow
{
internal static readonly object MovableByWindowBackground_Key = new object();
internal static EventInfo dpiChangedEvent = typeof(sw.Window).GetEvent("DpiChanged");
internal static readonly object LastPixelSize_Key = new object();
internal static readonly object IsClosing_Key = new object();
internal static readonly object LocationSet_Key = new object();
internal static readonly object Minimizable_Key = new object();
internal static readonly object Maximizable_Key = new object();
internal static readonly object Closeable_Key = new object();
internal static readonly object Resizable_Key = new object();
internal static readonly object Icon_Key = new object();
internal static readonly object ShowSystemMenu_Key = new object();
}
public class EtoWindowContent : swc.DockPanel
{
}
public abstract class WpfWindow<TControl, TWidget, TCallback> : WpfPanel<TControl, TWidget, TCallback>, Window.IHandler, IWpfWindow, IInputBindingHost
where TControl : sw.Window
where TWidget : Window
where TCallback : Window.ICallback
{
MenuBar menu;
Eto.Forms.ToolBar toolBar;
swc.DockPanel main;
swc.ContentControl menuHolder;
swc.ContentControl toolBarHolder;
protected swc.DockPanel content;
Size? initialClientSize;
PerMonitorDpiHelper dpiHelper;
Point? initialLocation;
bool isSourceInitialized;
protected virtual bool IsAttached => false;
sw.Interop.WindowInteropHelper windowInterop;
public override IntPtr NativeHandle
{
get
{
if (windowInterop == null)
{
windowInterop = new sw.Interop.WindowInteropHelper(Control);
windowInterop.EnsureHandle();
}
return windowInterop.Handle;
}
}
public static bool EnablePerMonitorDpiSupport { get; set; } = true;
public swc.DockPanel ContentPanel { get { return content; } }
public swc.DockPanel MainPanel { get { return main; } }
public bool MovableByWindowBackground
{
get => Widget.Properties.Get<bool>(WpfWindow.MovableByWindowBackground_Key);
set
{
if (Widget.Properties.TrySet(WpfWindow.MovableByWindowBackground_Key, value))
{
if (value)
content.MouseLeftButtonDown += Content_MouseLeftButtonDown;
else
content.MouseLeftButtonDown -= Content_MouseLeftButtonDown;
}
}
}
void Content_MouseLeftButtonDown(object sender, swi.MouseButtonEventArgs e)
{
// mouse could be captured by something else, so we release it here to ensure the DragMove works.
// we only get here if no control has handled the mouse down event.
swi.Mouse.Captured?.ReleaseMouseCapture();
Control.DragMove();
e.Handled = true;
}
protected override void Initialize()
{
if (IsAttached)
return;
content = new EtoWindowContent();
UseShellDropManager = true;
base.Initialize();
Control.SizeToContent = sw.SizeToContent.WidthAndHeight;
Control.SnapsToDevicePixels = true;
Control.UseLayoutRounding = true;
main = new swc.DockPanel();
menuHolder = new swc.ContentControl { IsTabStop = false };
toolBarHolder = new swc.ContentControl { IsTabStop = false };
swc.DockPanel.SetDock(menuHolder, swc.Dock.Top);
swc.DockPanel.SetDock(toolBarHolder, swc.Dock.Top);
main.Children.Add(menuHolder);
main.Children.Add(toolBarHolder);
main.Children.Add(content);
Control.Content = main;
Control.SourceInitialized += Control_SourceInitialized;
Control.Loaded += Control_Loaded;
Control.PreviewKeyDown += (sender, e) =>
{
// need to call validate on the input bindings before trying to execute them
foreach (var binding in Control.InputBindings.Cast<swi.InputBinding>().Select(r => r.Command).OfType<IWpfValidateBinding>())
{
binding.Validate();
}
};
Control.SizeChanged += Control_SizeChanged;
// needed to handle Application.Terminating event
HandleEvent(Window.ClosingEvent);
}
private void Control_SourceInitialized(object sender, EventArgs e)
{
isSourceInitialized = true;
if (Resizable && WindowStyle == WindowStyle.None)
{
SetWindowChrome();
}
if (!Minimizable || !Maximizable)
{
SetResizeMode();
}
SetSystemMenu();
if (initialLocation != null)
{
SetLocation(initialLocation.Value);
initialLocation = null;
}
}
private IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x0084 /*WM_NCHITTEST*/ )
{
// This prevents a crash in WindowChromeWorker._HandleNCHitTest with some mouse drivers
// See https://github.com/dotnet/wpf/issues/6777 for more information
// This was fixed in .NET 8 but this is here to avoid the crash in .NET Framework 4.8 and .NET 7.
try
{
lParam.ToInt32();
}
catch (OverflowException)
{
handled = true;
}
}
return IntPtr.Zero;
}
private void Control_Loaded(object sender, sw.RoutedEventArgs e)
{
SetMinimumSize();
if (initialClientSize != null)
{
initialClientSize = null;
SetContentSize();
}
// Set sizing mode - if it is set to manual before here it will be visible before it is loaded
// which we do not want.
SetSizeToContent();
if (Control.ShowActivated)
Control.MoveFocus(new swi.TraversalRequest(swi.FocusNavigationDirection.Next));
((swin.HwndSource)sw.PresentationSource.FromVisual(Control))?.AddHook(HookProc);
if (FireOnLoadComplete)
Callback.OnLoadComplete(Widget, EventArgs.Empty);
}
protected bool FireOnLoadComplete { get; set; }
private void Control_SizeChanged(object sender, sw.SizeChangedEventArgs e)
{
if (Widget.Loaded && Control.SizeToContent == sw.SizeToContent.Manual)
Widget.Properties.Set(AutoSize_Key, false);
}
public bool UseShellDropManager
{
get => Widget.Properties.Get<WpfShellDropBehavior>(typeof(WpfShellDropBehavior)) != null;
set
{
if (value != UseShellDropManager)
{
if (value)
Widget.Properties.TrySet(typeof(WpfShellDropBehavior), new WpfShellDropBehavior(Control));
else
{
var shellDrop = Widget.Properties.Get<WpfShellDropBehavior>(typeof(WpfShellDropBehavior));
shellDrop.Detatch();
Widget.Properties.Remove(typeof(WpfShellDropBehavior));
}
}
}
}
void SetupPerMonitorDpi()
{
if (EnablePerMonitorDpiSupport && dpiHelper == null && Win32.PerMonitorDpiSupported)
{
dpiHelper = new PerMonitorDpiHelper(Control);
if (!PerMonitorDpiHelper.BuiltInPerMonitorSupported)
dpiHelper.ScaleChanged += (sender, e) => SetMinimumSize();
}
}
protected override void SetContentScale(bool xscale, bool yscale)
{
base.SetContentScale(true, true);
}
public override void AttachEvent(string id)
{
switch (id)
{
case Eto.Forms.Control.ShownEvent:
Control.IsVisibleChanged += (sender, e) =>
{
if ((bool)e.NewValue)
{
// this is a trick to achieve similar behaviour as in WinForms
// (IsVisibleChanged triggers too early, we want it after measure-lay-render)
Control.Dispatcher.BeginInvoke(new Action(() =>
Callback.OnShown(Widget, EventArgs.Empty)),
sw.Threading.DispatcherPriority.ContextIdle, null);
}
};
break;
case Window.ClosedEvent:
Control.Closed += delegate
{
Callback.OnClosed(Widget, EventArgs.Empty);
};
break;
case Window.ClosingEvent:
Control.Closing += Control_Closing;
break;
case Window.WindowStateChangedEvent:
Control.StateChanged += (sender, e) => Callback.OnWindowStateChanged(Widget, EventArgs.Empty);
break;
case Eto.Forms.Control.GotFocusEvent:
Control.Activated += (sender, e) => Callback.OnGotFocus(Widget, EventArgs.Empty);
break;
case Eto.Forms.Control.LostFocusEvent:
// Use AsyncInvoke here otherwise calling Close() during LostFocus does not work as expected
// and can lead to the z-order of windows not being correct.
Control.Deactivated += (sender, e) => Application.Instance.AsyncInvoke(() => Callback.OnLostFocus(Widget, EventArgs.Empty));
break;
case Window.LocationChangedEvent:
Control.LocationChanged += (sender, e) => Callback.OnLocationChanged(Widget, EventArgs.Empty);
break;
case Window.LogicalPixelSizeChangedEvent:
if (PerMonitorDpiHelper.BuiltInPerMonitorSupported && WpfWindow.dpiChangedEvent != null) // .NET 4.6.2 support!
{
var method = typeof(WpfWindow<TControl, TWidget, TCallback>).GetMethod(nameof(HandleLogicalPixelSizeChanged), BindingFlags.Instance | BindingFlags.NonPublic);
WpfWindow.dpiChangedEvent.AddEventHandler(Control, Delegate.CreateDelegate(WpfWindow.dpiChangedEvent.EventHandlerType, this, method));
break;
}
SetupPerMonitorDpi();
if (dpiHelper != null)
dpiHelper.ScaleChanged += HandleLogicalPixelSizeChanged;
break;
default:
base.AttachEvent(id);
break;
}
}
bool IsClosing
{
get => Widget.Properties.Get<bool>(WpfWindow.IsClosing_Key);
set => Widget.Properties.Set(WpfWindow.IsClosing_Key, value);
}
private void Control_Closing(object sender, CancelEventArgs e)
{
IsClosing = true;
var args = new CancelEventArgs { Cancel = e.Cancel };
Callback.OnClosing(Widget, args);
var willShutDown =
(
sw.Application.Current.ShutdownMode == sw.ShutdownMode.OnLastWindowClose
&& sw.Application.Current.Windows.Count == 1
)
|| (
sw.Application.Current.ShutdownMode == sw.ShutdownMode.OnMainWindowClose
&& sw.Application.Current.MainWindow == Control
);
if (!args.Cancel && willShutDown)
{
// last window closing, so call OnTerminating to let the app abort terminating
var app = ((ApplicationHandler)Application.Instance.Handler);
app.Callback.OnTerminating(app.Widget, args);
}
e.Cancel = args.Cancel;
IsApplicationClosing = !e.Cancel && willShutDown;
IsClosing = !e.Cancel;
if (!e.Cancel)
{
InternalClosing();
}
}
float LastPixelSize
{
get => Widget.Properties.Get<float>(WpfWindow.LastPixelSize_Key);
set => Widget.Properties.Set(WpfWindow.LastPixelSize_Key, value);
}
void HandleLogicalPixelSizeChanged(object sender, EventArgs e)
{
if (LastPixelSize != LogicalPixelSize)
{
Callback.OnLogicalPixelSizeChanged(Widget, EventArgs.Empty);
LastPixelSize = LogicalPixelSize;
}
}
static bool IsApplicationClosing { get; set; }
public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SetScale(false, false);
SetupPerMonitorDpi();
}
protected virtual void UpdateClientSize(Size size)
{
if (IsAttached)
return;
Control.SizeToContent = sw.SizeToContent.Manual;
var xdiff = Control.ActualWidth - content.ActualWidth;
var ydiff = Control.ActualHeight - content.ActualHeight;
Control.Width = size.Width + xdiff;
Control.Height = size.Height + ydiff;
}
protected override void SetSize()
{
if (IsAttached)
return;
SetSizeToContent();
var size = UserPreferredSize;
// don't set the minimum size of a window, just the preferred size
ContainerControl.Width = size.Width;
ContainerControl.Height = size.Height;
SetMinimumSize();
}
private void SetSizeToContent()
{
sw.SizeToContent sizing;
if (Control.IsLoaded && !AutoSize)
{
sizing = sw.SizeToContent.Manual;
}
else if (Control.WindowState == sw.WindowState.Maximized)
{
sizing = sw.SizeToContent.Manual;
}
else
{
var size = UserPreferredSize;
if (double.IsNaN(size.Width) && double.IsNaN(size.Height))
sizing = sw.SizeToContent.WidthAndHeight;
else if (double.IsNaN(size.Width))
sizing = sw.SizeToContent.Width;
else if (double.IsNaN(size.Height))
sizing = sw.SizeToContent.Height;
else
{
Widget.Properties.Set(AutoSize_Key, false);
sizing = sw.SizeToContent.Manual;
}
}
// If we set it to manual before loaded, it gets shown before Loaded event fires,
// which we do not want.
if (sizing == sw.SizeToContent.Manual && !Control.IsLoaded)
return;
Control.SizeToContent = sizing;
}
static readonly object AutoSize_Key = new object();
public virtual bool AutoSize
{
get => Widget.Properties.Get<bool>(AutoSize_Key);
set
{
if (Widget.Properties.TrySet(AutoSize_Key, value))
{
SetSizeToContent();
}
}
}
void SetMinimumSize()
{
if (IsAttached)
return;
// can't use WpfScale reliably until it is loaded
if (!Control.IsLoaded)
return;
ContainerControl.MinWidth = MinimumSize.Width * WpfScale;
ContainerControl.MinHeight = MinimumSize.Height * WpfScale;
}
public Eto.Forms.ToolBar ToolBar
{
get { return toolBar; }
set
{
if (IsAttached)
throw new NotSupportedException();
toolBar = value;
toolBarHolder.Content = toolBar != null ? toolBar.ControlObject : null;
}
}
protected virtual void InternalClosing()
{
}
public void Close()
{
if (!IsApplicationClosing)
{
// prevent crash if we call this more than once..
if (!IsClosing)
{
Control.Close();
}
}
else
Visible = false;
}
void CopyKeyBindings(swc.ItemCollection items)
{
foreach (var item in items.OfType<swc.MenuItem>())
{
Control.InputBindings.AddRange(item.InputBindings);
if (item.HasItems)
CopyKeyBindings(item.Items);
}
}
public MenuBar Menu
{
get { return menu; }
set
{
if (IsAttached)
throw new NotSupportedException();
menu = value;
if (menu != null)
{
var handler = (MenuBarHandler)menu.Handler;
menuHolder.Content = handler.Control;
Control.InputBindings.AddKeyBindings(handler.Control.Items);
}
else
{
menuHolder.Content = null;
}
}
}
public override SizeF GetPreferredSize(SizeF availableSize)
{
var preferred = UserPreferredSize;
var available = preferred.IfNaN(availableSize.ToWpf());
SizeF desired;
if (!Control.IsLoaded)
{
var oldSizeToContent = Control.SizeToContent;
var oldVisibility = Control.Visibility;
var _ = NativeHandle; // Ensure SourceInitialized is triggered
Control.SizeToContent = sw.SizeToContent.WidthAndHeight;
Control.Visibility = sw.Visibility.Hidden; // Set to hidden (instead of Collapsed)
Control.Measure(available);
desired = Control.DesiredSize.ToEto();
Control.Visibility = oldVisibility;
Control.SizeToContent = oldSizeToContent;
}
else
{
Control.Measure(available);
desired = Control.DesiredSize.ToEto();
}
desired = SizeF.Max(MinimumSize, desired);
if (!double.IsNaN(preferred.Width))
desired.Width = (float)preferred.Width;
if (!double.IsNaN(preferred.Height))
desired.Height = (float)preferred.Height;
return desired;
}
public Icon Icon
{
get => Widget.Properties.Get<Icon>(WpfWindow.Icon_Key, () => {
var icon = Control.Icon.ToEtoIcon();
Widget.Properties.Set(WpfWindow.Icon_Key, icon);
return icon;
});
set
{
if (Widget.Properties.TrySet<Icon>(WpfWindow.Icon_Key, value))
{
var style = SaveWindowStyle();
Control.Icon = (swm.ImageSource)value?.ControlObject;
RestoreWindowStyle(style);
}
}
}
public virtual bool Resizable
{
get => Widget.Properties.Get<bool>(WpfWindow.Resizable_Key, true);
set
{
if (Widget.Properties.TrySet(WpfWindow.Resizable_Key, value, true))
{
SetResizeMode();
SetWindowChrome();
}
}
}
public virtual bool Maximizable
{
get => Widget.Properties.Get<bool>(WpfWindow.Maximizable_Key, true);
set
{
if (Widget.Properties.TrySet(WpfWindow.Maximizable_Key, value, true))
{
SetResizeMode();
SetSystemMenu();
}
}
}
public virtual bool Minimizable
{
get => Widget.Properties.Get<bool>(WpfWindow.Minimizable_Key, true);
set
{
if (Widget.Properties.TrySet(WpfWindow.Minimizable_Key, value, true))
{
SetResizeMode();
SetSystemMenu();
}
}
}
public virtual bool Closeable
{
get => Widget.Properties.Get<bool>(WpfWindow.Closeable_Key, true);
set
{
if (Widget.Properties.TrySet(WpfWindow.Closeable_Key, value, true))
SetSystemMenu();
}
}
public bool? ShowSystemMenu
{
get => Widget.Properties.Get<bool?>(WpfWindow.ShowSystemMenu_Key);
set
{
if (Widget.Properties.TrySet(WpfWindow.ShowSystemMenu_Key, value))
SetSystemMenu();
}
}
void SetSystemMenu()
{
if (!isSourceInitialized)
return;
// hide system menu (and close button) if all commands are disabled
var useSystemMenu = ShowSystemMenu ?? (Closeable || Minimizable || Maximizable);
SetStyle(Win32.WS.SYSMENU, useSystemMenu);
// enable/disable the close button if shown (does not disable Alt+F4)
var sysMenu = Win32.GetSystemMenu(NativeHandle, false);
if (sysMenu != IntPtr.Zero)
{
var closeFlags = Closeable ? Win32.MF.BYCOMMAND : Win32.MF.GRAYED;
Win32.EnableMenuItem(sysMenu, Win32.SC.CLOSE, closeFlags);
}
}
internal void SetStyleEx(Win32.WS_EX style, bool value)
{
SetStyleEx(value ? style : 0, value ? 0 : style);
}
internal void SetStyleEx(Win32.WS_EX styleAdd, Win32.WS_EX styleRemove = 0)
{
var styleInt = Win32.GetWindowLong(NativeHandle, Win32.GWL.EXSTYLE);
styleInt |= (uint)styleAdd;
styleInt &= (uint)~styleRemove;
Win32.SetWindowLong(NativeHandle, Win32.GWL.EXSTYLE, styleInt);
}
internal void SetStyle(Win32.WS style, bool value)
{
SetStyle(value ? style : 0, value ? 0 : style);
}
internal void SetStyle(Win32.WS styleAdd, Win32.WS styleRemove = 0)
{
var styleInt = Win32.GetWindowLong(NativeHandle, Win32.GWL.STYLE);
styleInt |= (uint)styleAdd;
styleInt &= (uint)~styleRemove;
Win32.SetWindowLong(NativeHandle, Win32.GWL.STYLE, styleInt);
}
protected virtual void SetResizeMode()
{
if (Resizable)
Control.ResizeMode = sw.ResizeMode.CanResize;
else if (Minimizable)
Control.ResizeMode = sw.ResizeMode.CanMinimize;
else
Control.ResizeMode = sw.ResizeMode.NoResize;
if (isSourceInitialized)
{
SetStyle(Win32.WS.MAXIMIZEBOX, Maximizable);
SetStyle(Win32.WS.MINIMIZEBOX, Minimizable);
}
}
public virtual bool ShowInTaskbar
{
get { return Control.ShowInTaskbar; }
set { Control.ShowInTaskbar = value; }
}
public virtual bool Topmost
{
get { return Control.Topmost; }
set { Control.Topmost = value; }
}
public void Minimize()
{
Control.WindowState = sw.WindowState.Minimized;
}
public override Size ClientSize
{
get
{
if (Control.IsLoaded)
return new Size((int)content.ActualWidth, (int)content.ActualHeight);
return initialClientSize ?? Size.Empty;
}
set
{
if (IsAttached)
throw new NotSupportedException();
if (Control.IsLoaded)
UpdateClientSize(value);
else
{
initialClientSize = value;
SetContentSize();
}
}
}
void SetContentSize()
{
if (IsAttached)
return;
if (initialClientSize != null)
{
var value = initialClientSize.Value;
content.MinWidth = value.Width >= 0 ? value.Width : 0;
content.MinHeight = value.Height >= 0 ? value.Height : 0;
content.MaxWidth = value.Width >= 0 ? value.Width : double.PositiveInfinity;
content.MaxHeight = value.Height >= 0 ? value.Height : double.PositiveInfinity;
}
else
{
content.MinWidth = content.MinHeight = 0;
content.MaxWidth = content.MaxHeight = double.PositiveInfinity;
}
}
public override Size Size
{
get
{
if (Control.IsLoaded && NativeHandle != IntPtr.Zero)
{
// WPF doesn't always report the correct size when maximized
var rect = Win32.ExecuteInDpiAwarenessContext(() => Win32.GetWindowRect(NativeHandle, out var r) ? r : (Win32.RECT?)null);
if (rect != null)
{
var scale = DpiScale;
return new Size(
(int)Math.Round(rect.Value.width * scale),
(int)Math.Round(rect.Value.height * scale)
);
}
}
return base.Size;
}
set
{
if (IsAttached)
throw new NotSupportedException();
base.Size = value;
if (!Control.IsLoaded)
{
initialClientSize = null;
SetContentSize();
}
}
}
public string Title
{
get { return Control.Title; }
set
{
var style = SaveWindowStyle();
Control.Title = value ?? string.Empty;
RestoreWindowStyle(style);
}
}
protected bool LocationSet
{
get { return Widget.Properties.Get<bool>(WpfWindow.LocationSet_Key); }
set { Widget.Properties.Set(WpfWindow.LocationSet_Key, value); }
}
System.Windows.Forms.Screen SwfScreen => Win32.GetScreenFromWindow(NativeHandle);
double DpiScale
{
get
{
var source = sw.PresentationSource.FromVisual(Control);
double scale;
if (source != null)
{
scale = source.CompositionTarget.TransformFromDevice.M22;
if (Win32.IsSystemDpiAware)
{
scale = scale * Win32.SystemDpi / SwfScreen.GetLogicalPixelSize();
}
}
else scale = 1f / (Widget.Screen ?? Screen.PrimaryScreen).LogicalPixelSize;
return scale;
}
}
public new Point Location
{
get
{
if (initialLocation != null)
return initialLocation.Value;
var handle = NativeHandle;
if (handle != IntPtr.Zero)
{
// Left/Top doesn't always report correct location when maximized, so use Win32 when we can.
var rect = Win32.ExecuteInDpiAwarenessContext(() => Win32.GetWindowRect(handle, out var r) ? r : (Win32.RECT?)null);
if (rect != null)
{
var location = new Point(rect.Value.left, rect.Value.top);
return Point.Round(location.ScreenToLogical(SwfScreen));
}
}
// in WPF, left/top of a window is transformed by the (current) screen dpi, which makes absolutely no sense.
var left = Control.Left;
var top = Control.Top;
if (double.IsNaN(left) || double.IsNaN(top))
return Point.Empty;
// convert to screen co-ordinates
var scale = DpiScale;
var position = new Point((int)(left / scale), (int)(top / scale));
// now, convert to "proper" logical co-ordinates, taking all screens into account
return Point.Round(position.ScreenToLogical(SwfScreen));
}
set
{
if (IsAttached)
throw new NotSupportedException();
if (!isSourceInitialized)
{
// set location when the source is initialized and we have a Win32 handle to move about
// using Left/Top doesn't work (properly) in a per-monitor dpi environment.
initialLocation = value;
LocationSet = true;
}
else
{
LocationSet = true;
SetLocation(value);
}
}
}
void SetLocation(PointF location)
{
var loc = location.LogicalToScreen();
Win32.ExecuteInDpiAwarenessContext(() => Win32.SetWindowPos(NativeHandle, IntPtr.Zero, loc.X, loc.Y, 0, 0, Win32.SWP.NOSIZE | Win32.SWP.NOACTIVATE));
}
public WindowState WindowState
{
get
{
switch (Control.WindowState)
{
case sw.WindowState.Maximized:
return WindowState.Maximized;
case sw.WindowState.Minimized:
return WindowState.Minimized;
case sw.WindowState.Normal:
return WindowState.Normal;
default:
throw new NotSupportedException();
}
}
set
{
switch (value)
{
case WindowState.Maximized:
Control.WindowState = sw.WindowState.Maximized;
break;
case WindowState.Minimized:
Control.WindowState = sw.WindowState.Minimized;
break;
case WindowState.Normal:
Control.WindowState = sw.WindowState.Normal;
break;
default:
throw new NotSupportedException();
}
SetSizeToContent();
}
}
public Rectangle RestoreBounds
{
get
{
if (Control.WindowState == sw.WindowState.Normal || Control.RestoreBounds.IsEmpty)
return Widget.Bounds;
var restoreBounds = Control.RestoreBounds.ToEto();
var scale = DpiScale;
var position = new Point((int)(restoreBounds.X / scale), (int)(restoreBounds.Y / scale));
restoreBounds.Location = Point.Truncate(position.ScreenToLogical(SwfScreen));
return restoreBounds;
}
}
sw.Window IWpfWindow.Control
{
get { return Control; }
}
public double Opacity
{
get { return Control.Opacity; }
set
{
if (Math.Abs(value - 1.0) > 0.01f)
{
if (Control.IsLoaded)
{
GlassHelper.BlurBehindWindow(Control);
//GlassHelper.ExtendGlassFrame (Control);
Control.Opacity = value;
}
else
{
Control.Loaded += delegate
{
GlassHelper.BlurBehindWindow(Control);
//GlassHelper.ExtendGlassFrame (Control);
Control.Opacity = value;
};
}
}
else
{
Control.Opacity = value;
}
}
}
public override bool HasFocus
{
get { return Control.IsActive && ((ApplicationHandler)Application.Instance.Handler).IsActive; }
}
public WindowStyle WindowStyle
{
get { return Control.WindowStyle.ToEto(); }
set
{
if (WindowStyle != value)
{
Control.WindowStyle = value.ToWpf();
SetWindowChrome();
}
}
}
uint? SaveWindowStyle()
{
if (!isSourceInitialized)
return null;
// Annoyingly, WPF gets an AritheticOverflow if the window style has WS_POPUP in it as it treats it as an int
// So, we remove that style then re-apply it after.
uint? oldStyle = null;
var style = Win32.GetWindowLong(NativeHandle, Win32.GWL.STYLE);
if (style > (uint)Int32.MaxValue)
{
// style will overflow, so remove the last bit
oldStyle = style & (uint)(0x80000000);
Win32.SetWindowLong(NativeHandle, Win32.GWL.STYLE, style & 0x7FFFFFFF);
}
return oldStyle;
}
void RestoreWindowStyle(uint? oldStyle)
{
if (oldStyle == null)
return;
// reapply the old style bit
var style = Win32.GetWindowLong(NativeHandle, Win32.GWL.STYLE);
style |= oldStyle.Value;
Win32.SetWindowLong(NativeHandle, Win32.GWL.STYLE, style);
}
void SetWindowChrome()