-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacWindow.cs
1248 lines (1099 loc) · 33.4 KB
/
MacWindow.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 System;
using System.ComponentModel;
using System.Linq;
using Eto.Drawing;
using Eto.Forms;
using System.Threading;
namespace Eto.Mac.Forms
{
public class EtoWindow : NSWindow, IMacControl
{
CGRect oldFrame;
bool zoom;
public WeakReference WeakHandler { get; set; }
public IMacWindow Handler { get { return (IMacWindow)WeakHandler.Target; } set { WeakHandler = new WeakReference(value); } }
public EtoWindow(CGRect rect, NSWindowStyle style, NSBackingStore store, bool flag)
: base(rect, style, store, flag)
{
}
public bool? CanFocus { get; set; } = true;
public override bool CanBecomeKeyWindow => CanFocus ?? base.CanBecomeKeyWindow;
public bool DisableCenterParent { get; set; }
public NSWindow OwnerWindow { get; set; }
public override void Center()
{
if (DisableCenterParent)
return;
// implement centering to parent if there is a parent window for this one..
var window = OwnerWindow ?? ParentWindow;
if (window != null)
{
var parentFrame = window.Frame;
var frame = Frame;
var location = new CGPoint((parentFrame.Width - frame.Width) / 2 + parentFrame.X, (parentFrame.Height - frame.Height) / 2 + parentFrame.Y);
SetFrameOrigin(location);
}
else
base.Center();
}
public override void Zoom(NSObject sender)
{
if (zoom)
{
SetFrame(oldFrame, true, true);
zoom = false;
}
else
{
oldFrame = Frame;
base.Zoom(sender ?? this); // null when double clicking the title bar, but xammac/monomac doesn't allow it
zoom = true;
}
Handler.Callback.OnWindowStateChanged(Handler.Widget, EventArgs.Empty);
}
public bool DisableSetOrigin { get; set; }
public override void SetFrameOrigin(CGPoint aPoint)
{
if (!DisableSetOrigin)
base.SetFrameOrigin(aPoint);
}
public override void RecalculateKeyViewLoop()
{
base.RecalculateKeyViewLoop();
NSView last = null;
Handler?.RecalculateKeyViewLoop(ref last);
}
}
public class EtoPanel : NSPanel, IMacControl
{
CGRect oldFrame;
bool zoom;
public WeakReference WeakHandler { get; set; }
public IMacWindow Handler { get { return (IMacWindow)WeakHandler.Target; } set { WeakHandler = new WeakReference(value); } }
public EtoPanel(CGRect rect, NSWindowStyle style, NSBackingStore store, bool flag)
: base(rect, style, store, flag)
{
}
public bool? CanFocus { get; set; }
public override bool CanBecomeKeyWindow => CanFocus ?? base.CanBecomeKeyWindow;
public bool DisableCenterParent { get; set; }
public NSWindow OwnerWindow { get; set; }
public override void Center()
{
if (DisableCenterParent)
return;
// implement centering to parent if there is a parent window for this one..
var window = OwnerWindow ?? ParentWindow;
if (window != null)
{
var parentFrame = window.Frame;
var frame = Frame;
var location = new CGPoint((parentFrame.Width - frame.Width) / 2 + parentFrame.X, (parentFrame.Height - frame.Height) / 2 + parentFrame.Y);
SetFrameOrigin(location);
}
else
base.Center();
}
public override void Zoom(NSObject sender)
{
if (zoom)
{
SetFrame(oldFrame, true, true);
zoom = false;
}
else
{
oldFrame = Frame;
base.Zoom(sender ?? this); // null when double clicking the title bar, but xammac/monomac doesn't allow it
zoom = true;
}
Handler.Callback.OnWindowStateChanged(Handler.Widget, EventArgs.Empty);
}
public bool DisableSetOrigin { get; set; }
public override void SetFrameOrigin(CGPoint aPoint)
{
if (!DisableSetOrigin)
base.SetFrameOrigin(aPoint);
}
public override void RecalculateKeyViewLoop()
{
base.RecalculateKeyViewLoop();
NSView last = null;
Handler?.RecalculateKeyViewLoop(ref last);
}
}
class EtoContentView : MacPanelView
{
public EtoContentView()
{
}
public EtoContentView(IntPtr handle) : base(handle)
{
}
}
public interface IMacWindow : IMacControlHandler
{
Rectangle? RestoreBounds { get; set; }
Window Widget { get; }
NSMenu MenuBar { get; }
NSObject FieldEditorClient { get; set; }
MacFieldEditor FieldEditor { get; }
bool CloseWindow(Action<CancelEventArgs> closing = null);
NSWindow Control { get; }
Window.ICallback Callback { get; }
}
static class MacWindow
{
internal static readonly object InitialLocation_Key = new object();
internal static readonly object PreferredClientSize_Key = new object();
internal static readonly Selector selSetStyleMask = new Selector("setStyleMask:");
internal static IntPtr selMainMenu = Selector.GetHandle("mainMenu");
internal static IntPtr selSetMainMenu = Selector.GetHandle("setMainMenu:");
internal static readonly object SetAsChildWindow_Key = new object();
internal static readonly IntPtr selWindows_Handle = Selector.GetHandle("windows");
internal static readonly IntPtr selCount_Handle = Selector.GetHandle("count");
internal static readonly IntPtr selObjectAtIndex_Handle = Selector.GetHandle("objectAtIndex:");
internal static readonly IntPtr selMakeKeyWindow_Handle = Selector.GetHandle("makeKeyWindow");
internal static readonly IntPtr selIsVisible_Handle = Selector.GetHandle("isVisible");
internal static readonly object AnimateSizeChanges_Key = new object();
internal static readonly object DisableAutoSize_Key = new object();
internal static readonly object Topmost_Key = new object();
}
public abstract class MacWindow<TControl, TWidget, TCallback> : MacPanel<TControl, TWidget, TCallback>, Window.IHandler, IMacWindow
where TControl : NSWindow
where TWidget : Window
where TCallback : Window.ICallback
{
MacFieldEditor fieldEditor;
MenuBar menuBar;
Icon icon;
Eto.Forms.ToolBar toolBar;
Rectangle? restoreBounds;
bool setInitialSize = true;
WindowState? initialState;
bool maximizable = true;
Point? oldLocation;
Point? InitialLocation
{
get => Widget.Properties.Get<Point?>(MacWindow.InitialLocation_Key);
set => Widget.Properties.Set(MacWindow.InitialLocation_Key, value);
}
Window.ICallback IMacWindow.Callback { get { return Callback; } }
public override NSView ContainerControl { get { return Control.ContentView; } }
public override object EventObject { get { return Control; } }
public NSObject FieldEditorClient { get; set; }
public MacFieldEditor FieldEditor => fieldEditor;
/// <summary>
/// Allow moving the window by dragging the background, null to only enable it in certain cases (e.g. when borderless)
/// </summary>
public bool MovableByWindowBackground
{
get => Control.MovableByWindowBackground;
set => Control.MovableByWindowBackground = value;
}
protected override Color DefaultBackgroundColor => NSColor.WindowBackground.ToEtoWithAppearance(false);
protected override SizeF GetNaturalSize(SizeF availableSize)
{
SizeF naturalSize = new SizeF(200, 200);
var preferredClientSize = PreferredClientSize;
if (Content != null && Content.Visible)
{
if (preferredClientSize?.Width > 0)
availableSize.Width = preferredClientSize.Value.Width;
if (preferredClientSize?.Height > 0)
availableSize.Height = preferredClientSize.Value.Height;
naturalSize = Content.GetPreferredSize(availableSize - Padding.Size) + Padding.Size;
}
if (preferredClientSize != null)
{
if (preferredClientSize.Value.Width >= 0)
naturalSize.Width = preferredClientSize.Value.Width;
if (preferredClientSize.Value.Height >= 0)
naturalSize.Height = preferredClientSize.Value.Height;
}
return naturalSize;
}
public NSMenu MenuBar
{
get { return menuBar == null ? null : menuBar.ControlObject as NSMenu; }
}
IntPtr oldMenu;
static void HandleDidBecomeKey(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
handler?.SetMenu();
}
static void HandleDidResignKey(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
handler?.RestoreMenu();
}
static bool HandleShouldZoom(NSWindow window, CGRect newFrame)
{
var handler = GetHandler(window) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return true;
if (!handler.Maximizable)
return false;
if (!window.IsZoomed && window.Screen != null)
{
handler.RestoreBounds = handler.Widget.Bounds;
}
return true;
}
static void HandleWillMiniaturize(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return;
handler.RestoreBounds = handler.Widget.Bounds;
}
static void HandleWillClose(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null || !handler.Widget.Loaded) // could already be closed
return;
if (ApplicationHandler.Instance.ShouldCloseForm(handler.Widget, true))
handler.Callback.OnClosed(handler.Widget, EventArgs.Empty);
}
static bool HandleWindowShouldClose(NSObject sender)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return true;
var args = new CancelEventArgs();
if (ApplicationHandler.Instance.ShouldCloseForm(handler.Widget, false))
handler.Callback.OnClosing(handler.Widget, args);
return !args.Cancel;
}
static void HandleWindowStateChanged(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return;
handler.Callback.OnWindowStateChanged(handler.Widget, EventArgs.Empty);
}
static void HandleGotFocus(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return;
handler.Callback.OnGotFocus(handler.Widget, EventArgs.Empty);
}
static void HandleLostFocus(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return;
handler.Callback.OnLostFocus(handler.Widget, EventArgs.Empty);
}
public override void AttachEvent(string id)
{
// when attaching to a native control, we can't handle any events as it'll override its delegate.
if (!(Control is IMacControl))
return;
switch (id)
{
case Window.ClosedEvent:
Control.WillClose += HandleWillClose;
break;
case Window.ClosingEvent:
Control.WindowShouldClose = HandleWindowShouldClose;
break;
case Window.WindowStateChangedEvent:
Control.DidMiniaturize += HandleWindowStateChanged;
Control.DidDeminiaturize += HandleWindowStateChanged;
break;
case Eto.Forms.Control.ShownEvent:
// handled when shown
break;
case Eto.Forms.Control.GotFocusEvent:
Control.DidBecomeKey += HandleGotFocus;
break;
case Eto.Forms.Control.LostFocusEvent:
Control.DidResignKey += HandleLostFocus;
break;
case Eto.Forms.Control.SizeChangedEvent:
{
Size? oldSize = null;
AddObserver(NSWindow.DidResizeNotification, e =>
{
var handler = (MacWindow<TControl, TWidget, TCallback>)e.Handler;
var newSize = handler.Size;
if (oldSize != newSize)
{
handler.Callback.OnSizeChanged(handler.Widget, EventArgs.Empty);
oldSize = newSize;
}
});
}
break;
case Window.LocationChangedEvent:
{
AddObserver(NSWindow.DidMoveNotification, e =>
{
var handler = e.Handler as MacWindow<TControl, TWidget, TCallback>;
if (handler != null)
{
var old = handler.oldLocation;
handler.oldLocation = null;
var newLocation = handler.Location;
if (old != newLocation)
{
handler.oldLocation = newLocation;
handler.Callback.OnLocationChanged(handler.Widget, EventArgs.Empty);
}
}
});
// WillMove is only called when the user moves the window via the mouse
Control.WillMove += HandleWillMove;
}
break;
case Window.LogicalPixelSizeChangedEvent:
AddObserver(NSWindow.DidChangeBackingPropertiesNotification, e =>
{
var handler = e.Handler as MacWindow<TControl, TWidget, TCallback>;
if (handler != null)
{
var args = new NSWindowBackingPropertiesEventArgs(e.Notification);
if (args.OldScaleFactor != handler.Control.BackingScaleFactor)
handler.Callback.OnLogicalPixelSizeChanged(handler.Widget, EventArgs.Empty);
}
});
break;
default:
base.AttachEvent(id);
break;
}
}
void CreateCursorRect()
{
if (Cursor != null)
{
Control.ContentView.DiscardCursorRects();
Control.ContentView.AddCursorRect(new CGRect(CGPoint.Empty, Control.Frame.Size), Cursor.ControlObject as NSCursor);
}
else
Control.ContentView.DiscardCursorRects();
}
/// <summary>
/// Tracks movement of the window until the mouse up button is found
/// </summary>
static void HandleWillMove(object sender, EventArgs e)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return;
handler.oldLocation = null;
// find offset of mouse cursor to location of window
var moveOffset = Size.Round((SizeF)(Mouse.Position - handler.Location));
ThreadPool.QueueUserWorkItem(a =>
{
bool tracking = true;
while (tracking)
{
NSApplication.SharedApplication.InvokeOnMainThread(() =>
{
var newLocation = Point.Round(Mouse.Position - moveOffset);
if (handler.oldLocation != newLocation)
{
handler.Callback.OnLocationChanged(handler.Widget, EventArgs.Empty);
handler.oldLocation = newLocation;
}
// check for mouse up event
tracking = NSApplication.SharedApplication.NextEvent(NSEventMask.LeftMouseUp, null, NSRunLoopMode.EventTracking, false) == null;
});
}
handler.oldLocation = null;
NSApplication.SharedApplication.InvokeOnMainThread(() => handler.Callback.OnLocationChanged(handler.Widget, EventArgs.Empty));
});
}
EtoContentView contentView;
protected virtual void ConfigureWindow()
{
var myWindow = Control as EtoWindow;
if (myWindow != null)
myWindow.Handler = this;
Control.ContentView = contentView = new EtoContentView { WeakHandler = new WeakReference(this) };
//Control.ContentMinSize = new System.Drawing.SizeF(0, 0);
Control.ContentView.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
if (!MacVersion.IsAtLeast(10, 12))
{
// need at least one constraint to enable auto-layout, which calls NSView.Layout automatically.
Control.ContentView.AddConstraint(NSLayoutConstraint.Create(Control.ContentView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, Control.ContentView, NSLayoutAttribute.Leading, 1, 0));
}
Control.ReleasedWhenClosed = false;
Control.HasShadow = true;
Control.ShowsResizeIndicator = true;
Control.AutorecalculatesKeyViewLoop = true;
Control.WillReturnFieldEditor = HandleWillReturnFieldEditor;
Control.DidBecomeKey += HandleDidBecomeKey;
Control.DidResignKey += HandleDidResignKey;
Control.ShouldZoom = HandleShouldZoom;
Control.WillMiniaturize += HandleWillMiniaturize;
Control.WillResize = HandleWillResize;
#if MONOMAC
// AppKit still calls some delegate methods on the window after closing a form (e.g. WillReturnFieldEditor),
// causing exceptions trying to recreate the delegate if it has been garbage collected.
// This is because MonoMac doesn't use ref counting to determine when objects can be GC'd like Xamarin.Mac.
// We avoid this problem by clearing out the delegate after the window is closed.
// In Eto, we don't expect any events to be called after that point anyway.
Widget.Closed += (sender, e) => Application.Instance.AsyncInvoke(() => Control.Delegate = null);
#endif
}
private static CGSize HandleWillResize(NSWindow sender, CGSize toFrameSize)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return toFrameSize;
var minimumSize = handler.MinimumSize;
if (handler.Widget.Loaded && handler.Control.IsVisible)
{
handler.AutoSize = false;
}
return new CGSize((float)Math.Max(toFrameSize.Width, minimumSize.Width), (float)Math.Max(toFrameSize.Height, minimumSize.Height));
}
static NSObject HandleWillReturnFieldEditor(NSWindow sender, NSObject client)
{
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
if (handler == null)
return null;
handler.FieldEditorClient = client;
return handler.fieldEditor ?? (handler.fieldEditor = new MacFieldEditor());
}
public override NSView ContentControl => Control.ContentView;
public virtual string Title { get => Control.Title; set => Control.Title = value ?? ""; }
void SetButtonStates()
{
var button = Control.StandardWindowButton(NSWindowButton.ZoomButton);
if (button != null)
button.Enabled = Maximizable && Resizable;
}
public bool Resizable
{
get { return Control.StyleMask.HasFlag(NSWindowStyle.Resizable); }
set
{
if (Control.RespondsToSelector(MacWindow.selSetStyleMask))
{
if (value)
Control.StyleMask |= NSWindowStyle.Resizable;
else
Control.StyleMask &= ~NSWindowStyle.Resizable;
SetButtonStates();
}
}
}
public bool Minimizable
{
get { return Control.StyleMask.HasFlag(NSWindowStyle.Miniaturizable); }
set
{
if (Control.RespondsToSelector(MacWindow.selSetStyleMask))
{
if (value)
Control.StyleMask |= NSWindowStyle.Miniaturizable;
else
Control.StyleMask &= ~NSWindowStyle.Miniaturizable;
SetButtonStates();
}
}
}
public bool Maximizable
{
get { return maximizable; }
set
{
if (maximizable != value)
{
maximizable = value;
SetButtonStates();
}
}
}
public bool ShowInTaskbar
{
get;
set;
}
protected virtual NSWindowLevel TopmostWindowLevel => NSWindowLevel.PopUpMenu;
public virtual bool Topmost
{
get => Control.Level >= NSWindowLevel.Floating;
set
{
// need to remember the preferred state as it can be changed on us when setting the owner
if (WantsTopmost != value)
{
WantsTopmost = value;
Control.Level = value ? TopmostWindowLevel : NSWindowLevel.Normal;
}
}
}
internal virtual bool DefaultTopmost => false;
internal bool WantsTopmost
{
get => Widget.Properties.Get(MacWindow.Topmost_Key, DefaultTopmost);
set => Widget.Properties.Set(MacWindow.Topmost_Key, value, DefaultTopmost);
}
public override Size Size
{
get
{
if (!Widget.Loaded)
return UserPreferredSize;
return Control.Frame.Size.ToEtoSize();
}
set
{
UserPreferredSize = value;
if (PreferredClientSize != null)
{
if (value.Width != -1 && value.Height != -1)
PreferredClientSize = new Size(-1, -1);
else if (value.Width != -1)
PreferredClientSize = new Size(-1, PreferredClientSize.Value.Height);
else if (value.Height != -1)
PreferredClientSize = new Size(PreferredClientSize.Value.Width, -1);
}
if (!SetAutoSize())
{
var oldFrame = Control.Frame;
var newFrame = oldFrame;
if (value.Width >= 0)
newFrame.Width = value.Width;
if (value.Height > 0)
{
newFrame.Height = value.Height;
newFrame.Y = (nfloat)Math.Max(0, oldFrame.Y - (value.Height - oldFrame.Height));
}
Control.SetFrame(newFrame, true, AnimateSizeChanges);
}
}
}
public virtual bool AutoSize
{
get { return Widget.Properties.Get<bool>(MacView.AutoSize_Key); }
set
{
if (Widget.Properties.TrySet(MacView.AutoSize_Key, value))
{
if (Widget.Loaded && value)
{
PerformAutoSize();
}
}
}
}
protected bool SetAutoSize()
{
var userPreferredSize = UserPreferredSize;
if (PreferredClientSize != null)
{
var preferredClientSize = PreferredClientSize.Value;
setInitialSize = userPreferredSize.Width == -1 && preferredClientSize.Width == -1;
setInitialSize |= userPreferredSize.Height == -1 && preferredClientSize.Height == -1;
}
else
{
setInitialSize = userPreferredSize.Width == -1 || userPreferredSize.Height == -1;
}
var ret = AutoSize || setInitialSize;
if (Widget.Loaded)
{
PerformAutoSize();
}
return ret;
}
private void PerformAutoSize()
{
if (AutoSize || setInitialSize)
{
setInitialSize = false;
var availableSize = SizeF.PositiveInfinity;
var borderSize = GetBorderSize();
if (UserPreferredSize.Width != -1)
availableSize.Width = UserPreferredSize.Width - borderSize.Width;
if (UserPreferredSize.Height != -1)
availableSize.Height = UserPreferredSize.Height - borderSize.Height;
var size = GetPreferredSize(availableSize);
SetContentSize(size.ToNS());
}
}
public MenuBar Menu
{
get
{
return menuBar;
}
set
{
menuBar = value;
if (Control.IsKeyWindow)
SetMenu();
}
}
void SetMenu()
{
if (MenuBar != null)
{
// if not zero, it's already saved
if (oldMenu == IntPtr.Zero)
{
oldMenu = Messaging.IntPtr_objc_msgSend(NSApplication.SharedApplication.Handle, MacWindow.selMainMenu);
if (oldMenu != IntPtr.Zero)
{
// remember old native menu so we can restore it later
MacExtensions.Retain(oldMenu);
}
}
NSApplication.SharedApplication.MainMenu = MenuBar;
RemoveSuperfluousCloseAll();
}
else
{
// restore the menu since we no longer have a menu specific to this window.
RestoreMenu();
}
}
void RestoreMenu()
{
if (oldMenu != IntPtr.Zero)
{
// restore old native menu
Messaging.void_objc_msgSend_IntPtr(NSApplication.SharedApplication.Handle, MacWindow.selSetMainMenu, oldMenu);
MacExtensions.Release(oldMenu);
oldMenu = IntPtr.Zero;
RemoveSuperfluousCloseAll();
}
}
/// <summary>
/// Removes the Close All menu item for document-based apps
/// </summary>
/// <remarks>
/// macOS automatically re-adds this back for document based apps, but not for SaveAs/Duplicate
/// Appears to be a bug (feature) of macOS.
/// </remarks>
void RemoveSuperfluousCloseAll()
{
var menu = NSApplication.SharedApplication.MainMenu;
if (menu == null)
return;
for (int j = 0; j < menu.Count; j++)
{
var item = menu.ItemAt(j);
if (!item.HasSubmenu)
continue;
var submenu = item.Submenu;
for (int i = 0; i < submenu.Count; i++)
{
var submenuItem = submenu.ItemAt(i);
if (submenuItem.Title == "<<Close All - unlocalized>>" && submenuItem.Action?.Name == "closeAll:")
{
submenu.RemoveItemAt(i);
return;
}
}
}
}
public bool CloseWindow(Action<CancelEventArgs> closing = null)
{
if (!Widget.Loaded)
return true;
var args = new CancelEventArgs();
Callback.OnClosing(Widget, args);
if (!args.Cancel && closing != null)
closing(args);
if (!args.Cancel)
{
Callback.OnClosed(Widget, EventArgs.Empty);
}
return !args.Cancel;
}
public virtual void Close()
{
var args = new CancelEventArgs();
Callback.OnClosing(Widget, args);
if (!args.Cancel)
Control.Close();
}
public Eto.Forms.ToolBar ToolBar
{
get
{
return toolBar;
}
set
{
toolBar = value;
Control.Toolbar = (NSToolbar)toolBar.ControlObject;
}
}
public Icon Icon
{
get { return icon; }
set
{
icon = value;
// don't really do anything here.. no where to put it
}
}
public override void Focus()
{
Control.BecomeFirstResponder();
}
public string Id { get; set; }
public Size? PreferredClientSize
{
get { return Widget.Properties.Get<Size?>(MacWindow.PreferredClientSize_Key); }
set { Widget.Properties[MacWindow.PreferredClientSize_Key] = value; }
}
public override Size ClientSize
{
get { return Control.ContentView.Frame.Size.ToEtoSize(); }
set
{
if (value.Height != -1)
{
var oldFrame = Control.Frame;
var oldSize = Control.ContentView.Frame;
Control.SetFrameOrigin(new CGPoint(oldFrame.X, (nfloat)Math.Max(0, oldFrame.Y - (value.Height - oldSize.Height))));
}
PreferredClientSize = value;
if (value.Width != -1 && value.Height != -1)
UserPreferredSize = new Size(-1, -1);
else if (value.Width != -1)
UserPreferredSize = new Size(-1, UserPreferredSize.Height);
else if (value.Height != -1)
UserPreferredSize = new Size(UserPreferredSize.Width, -1);
if (!SetAutoSize())
{
Control.SetContentSize(value.ToNS());
}
}
}
public override bool HasFocus
{
get { return Control.IsKeyWindow; }
}
public override bool Visible
{
get { return Control.IsVisible; }
set
{
if (Visible != value)
{
Control.IsVisible = value;
if (Widget.Loaded && value)
FireOnShown();
}
}
}
public override Cursor Cursor
{
get { return base.Cursor; }
set
{
base.Cursor = value;
CreateCursorRect();
}
}
public virtual Point Location
{
get
{
if (oldLocation != null)
return oldLocation.Value;
// translate location relative to the top left corner of main screen
var mainFrame = NSScreen.Screens[0].Frame;
var frame = Control.Frame;
return new Point((int)frame.X, (int)(mainFrame.Height - frame.Y - frame.Height));
}
set
{
if (Widget.Loaded)
SetLocation(value);
else
InitialLocation = value;
var etoWindow = Control as EtoWindow;
if (etoWindow != null)
etoWindow.DisableCenterParent = true;
}
}
void SetLocation(Point value)
{
// location is relative to the main screen, translate to bottom left, inversed
var mainFrame = NSScreen.Screens[0].Frame;
var frame = Control.Frame;
var point = new CGPoint((nfloat)value.X, (nfloat)(mainFrame.Height - value.Y));
if (Control.Screen == null)
{
// ensure that the control lands on a screen
point.X = (nfloat)Math.Min(Math.Max(mainFrame.X, point.X), mainFrame.Right - frame.Width);
point.Y = (nfloat)Math.Min(Math.Max(mainFrame.Y, point.Y), mainFrame.Bottom - frame.Height);
}
Control.SetFrameTopLeftPoint(point);
}
public WindowState WindowState
{
get
{
if (initialState != null)
return initialState.Value;
if (Control.IsMiniaturized)
return WindowState.Minimized;
if (Control.IsZoomed)
return WindowState.Maximized;
return WindowState.Normal;
}
set
{
if (!Widget.Loaded)
{
initialState = value;
return;
}
switch (value)
{
case WindowState.Maximized:
if (Control.IsMiniaturized)
Control.Deminiaturize(Control);
if (!Control.IsZoomed)
Control.PerformZoom(Control);
break;
case WindowState.Minimized:
if (!Control.IsMiniaturized)
Control.Miniaturize(Control);
break;
case WindowState.Normal:
if (Control.IsZoomed)
Control.Zoom(Control);
if (Control.IsMiniaturized)
Control.Deminiaturize(Control);
break;
}
}
}
public Rectangle RestoreBounds
{
get { return WindowState == WindowState.Normal ? Widget.Bounds : restoreBounds ?? Widget.Bounds; }
set { restoreBounds = value; }
}
public double Opacity
{
get { return Control.IsOpaque ? 1.0 : Control.AlphaValue; }