/
window.rs
1473 lines (1296 loc) · 51.1 KB
/
window.rs
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
use super::{
drag_context::{DragContext, NSDragOperation},
engine::PlatformEngine,
error::{PlatformError, PlatformResult},
menu::PlatformMenu,
screen_manager::PlatformScreenManager,
utils::*,
};
use crate::{
codec::Value,
shell::{
api_model::{
BoolTransition, DragEffect, DragRequest, PopupMenuRequest, PopupMenuResponse,
WindowCollectionBehavior, WindowFrame, WindowGeometry, WindowGeometryFlags,
WindowGeometryRequest, WindowStateFlags, WindowStyle,
},
Context, PlatformWindowDelegate, Point, Size,
},
util::{LateRefCell, OkLog},
};
use cocoa::{
appkit::{
CGPoint, NSApplication, NSEvent, NSEventType, NSView, NSViewHeightSizable,
NSViewWidthSizable, NSWindow, NSWindowCollectionBehavior, NSWindowStyleMask,
NSWindowTabbingMode, NSWindowTitleVisibility,
},
base::{id, nil, BOOL, NO, YES},
foundation::{
NSArray, NSInteger, NSPoint, NSProcessInfo, NSRect, NSSize, NSString, NSUInteger,
},
};
use core_foundation::base::CFRelease;
use core_graphics::event::CGEventType;
use objc::{
class,
declare::ClassDecl,
msg_send,
rc::{autoreleasepool, StrongPtr, WeakPtr},
runtime::{Class, Object, Sel},
sel, sel_impl,
};
use once_cell::sync::Lazy;
use std::{
cell::{Cell, RefCell},
collections::HashMap,
ffi::c_void,
mem::ManuallyDrop,
rc::{Rc, Weak},
time::Duration,
};
use NSEventType::{
NSLeftMouseDown, NSLeftMouseDragged, NSLeftMouseUp, NSMouseEntered, NSMouseExited,
NSMouseMoved, NSRightMouseDown, NSRightMouseUp,
};
pub type PlatformWindowType = StrongPtr;
pub struct PlatformWindow {
context: Context,
platform_window: PlatformWindowType,
parent_platform_window: Option<WeakPtr>,
platform_delegate: StrongPtr,
weak_self: LateRefCell<Weak<PlatformWindow>>,
delegate: Weak<dyn PlatformWindowDelegate>,
modal_close_callback: RefCell<Option<Box<dyn FnOnce(PlatformResult<Value>)>>>,
ready_to_show: Cell<bool>,
show_when_ready: Cell<bool>,
drag_context: LateRefCell<DragContext>,
last_event: RefCell<HashMap<u64, StrongPtr>>,
ignore_enter_leave_until: Cell<f64>,
window_buttons: StrongPtr,
flutter_view: LateRefCell<StrongPtr>,
mouse_down: Cell<bool>,
mouse_dragged: Cell<bool>,
window_state_flags: RefCell<WindowStateFlags>,
window_dragging: Cell<bool>,
}
#[link(name = "AppKit", kind = "framework")]
extern "C" {
pub static NSPasteboardTypeFileURL: id;
}
extern "C" {
fn im_link_objc_dummy_method();
}
impl PlatformWindow {
pub fn new(
context: Context,
delegate: Weak<dyn PlatformWindowDelegate>,
parent: Option<Rc<PlatformWindow>>,
) -> Self {
autoreleasepool(|| unsafe {
let rect = NSRect::new(NSPoint::new(400.0, 400.0), NSSize::new(400.0, 400.0));
let style = NSWindowStyleMask::NSTitledWindowMask
| NSWindowStyleMask::NSClosableWindowMask
| NSWindowStyleMask::NSResizableWindowMask
| NSWindowStyleMask::NSMiniaturizableWindowMask;
let window: id = msg_send![*WINDOW_CLASS, alloc];
let window = window.initWithContentRect_styleMask_backing_defer_(
rect,
style,
cocoa::appkit::NSBackingStoreType::NSBackingStoreBuffered,
NO,
);
let window = StrongPtr::new(window);
window.setReleasedWhenClosed_(NO);
NSWindow::setAllowsAutomaticWindowTabbing_(*window, NO);
NSWindow::setTabbingMode_(*window, NSWindowTabbingMode::NSWindowTabbingModeDisallowed);
let platform_delegate: id = msg_send![*WINDOW_DELEGATE_CLASS, new];
let platform_delegate = StrongPtr::new(platform_delegate);
window.setDelegate_(*platform_delegate);
let window_buttons: id = msg_send![class!(IMWindowButtons), new];
let window_buttons = StrongPtr::new(window_buttons);
Self {
context,
platform_window: window,
parent_platform_window: parent.map(|w| w.platform_window.weak()),
platform_delegate,
weak_self: LateRefCell::new(),
delegate,
modal_close_callback: RefCell::new(None),
ready_to_show: Cell::new(false),
show_when_ready: Cell::new(false),
last_event: RefCell::new(HashMap::new()),
drag_context: LateRefCell::new(),
ignore_enter_leave_until: Cell::new(0.0),
window_buttons,
flutter_view: LateRefCell::new(),
mouse_down: Cell::new(false),
mouse_dragged: Cell::new(false),
window_state_flags: RefCell::new(WindowStateFlags::default()),
window_dragging: Cell::new(false),
}
})
}
pub fn assign_weak_self(&self, weak: Weak<PlatformWindow>, engine: &PlatformEngine) {
self.weak_self.set(weak.clone());
unsafe {
// dummy method to force rust to link macos_extra.a
im_link_objc_dummy_method();
let state_ptr = weak.clone().into_raw() as *mut c_void;
(**self.platform_delegate).set_ivar("imState", state_ptr);
let state_ptr = weak.clone().into_raw() as *mut c_void;
(**self.platform_window).set_ivar("imState", state_ptr);
let flutter_view: id = msg_send![*engine.view_controller, view];
self.flutter_view.set(StrongPtr::retain(flutter_view));
let view: id = msg_send![class!(IMContentView), alloc];
let view = StrongPtr::new(msg_send![view, init]);
let () = msg_send![*self.platform_window, setContentView: *view];
let () = msg_send![*view, addSubview: flutter_view];
// Add traffic light
let () = msg_send![*view, addSubview: *self.window_buttons];
let () = msg_send![*engine.view_controller, setMouseTrackingMode: 3]; // always track mouse
// Temporarily set non empty window size so that flutter engine doesn't complain
NSWindow::setContentSize_(*self.platform_window, Size::wh(1.0, 1.0).into());
let () = msg_send![flutter_view, setFrame: NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(1.0, 1.0)
)];
NSView::setAutoresizingMask_(flutter_view, NSViewWidthSizable | NSViewHeightSizable);
}
if let Some(context) = self.context.get() {
let drag_context = DragContext::new(&context, weak);
drag_context.register(*self.platform_window);
self.drag_context.set(drag_context);
}
}
pub fn get_platform_window(&self) -> PlatformWindowType {
self.platform_window.clone()
}
pub fn set_geometry(
&self,
geometry: WindowGeometryRequest,
) -> PlatformResult<WindowGeometryFlags> {
autoreleasepool(|| unsafe {
let geometry = geometry.filtered_by_preference();
let mut res = WindowGeometryFlags {
..Default::default()
};
// for modal window position is handled by the system
let modal = self.is_modal();
if !modal {
if let Some(frame_origin) = geometry.frame_origin {
self.set_frame_origin(frame_origin);
res.frame_origin = true;
}
}
if let Some(frame_size) = geometry.frame_size {
self.set_frame_size(frame_size);
res.frame_size = true;
}
if !modal {
if let Some(content_origin) = geometry.content_origin {
self.set_content_position(content_origin);
res.content_origin = true;
}
}
if let Some(content_size) = geometry.content_size {
self.set_content_size(content_size);
res.content_size = true;
}
if let Some(size) = geometry.min_frame_size {
self.set_min_frame_size(size);
res.min_frame_size = true;
}
if let Some(size) = geometry.max_frame_size {
self.set_max_frame_size(size);
res.max_frame_size = true;
}
if let Some(size) = geometry.min_content_size {
self.set_min_content_size(size);
res.min_content_size = true;
}
if let Some(size) = geometry.max_content_size {
self.set_max_content_size(size);
res.max_content_size = true;
}
Ok(res)
})
}
pub fn get_geometry(&self) -> PlatformResult<WindowGeometry> {
autoreleasepool(|| unsafe {
Ok(WindowGeometry {
frame_origin: Some(self.get_frame_origin()),
frame_size: Some(self.get_frame_size()),
content_origin: Some(self.get_content_position()),
content_size: Some(self.get_content_size()),
min_frame_size: Some(self.get_min_frame_size()),
max_frame_size: Some(self.get_max_frame_size()),
min_content_size: Some(self.get_min_content_size()),
max_content_size: Some(self.get_max_content_size()),
})
})
}
pub fn supported_geometry(&self) -> PlatformResult<WindowGeometryFlags> {
let modal = self.is_modal();
// MacOS supports everything, but when modal (sheet) position is handled by system
Ok(WindowGeometryFlags {
frame_origin: !modal,
frame_size: true,
content_origin: !modal,
content_size: true,
min_frame_size: true,
max_frame_size: true,
min_content_size: true,
max_content_size: true,
})
}
pub fn get_screen_id(&self) -> PlatformResult<i64> {
unsafe {
let screen = NSWindow::screen(*self.platform_window);
Ok(PlatformScreenManager::get_screen_id(screen))
}
}
unsafe fn set_frame_origin(&self, position: Point) {
let screen_frame = global_screen_frame();
let position = Point {
x: position.x,
y: screen_frame.y2() - position.y,
};
self.platform_window.setFrameTopLeftPoint_(position.into());
}
unsafe fn get_frame_origin(&self) -> Point {
let screen_frame = global_screen_frame();
let window_frame = NSWindow::frame(*self.platform_window);
Point {
x: window_frame.origin.x,
y: screen_frame.y2() - (window_frame.origin.y + window_frame.size.height),
}
}
unsafe fn set_frame_size(&self, size: Size) {
self.platform_window.setFrameSize(size.into());
}
unsafe fn get_frame_size(&self) -> Size {
NSWindow::frame(*self.platform_window).size.into()
}
unsafe fn set_content_position(&self, position: Point) {
let screen_frame = global_screen_frame();
let content_size = NSView::frame(self.platform_window.contentView()).size;
let content_rect = NSRect::new(
Point {
x: position.x,
y: screen_frame.y2() - (position.y + content_size.height),
}
.into(),
content_size,
);
let window_frame = self.platform_window.frameRectForContentRect_(content_rect);
self.platform_window.setFrame_display_(window_frame, YES);
}
unsafe fn get_content_position(&self) -> Point {
let screen_frame = global_screen_frame();
let window_frame = NSWindow::frame(*self.platform_window);
let content_rect = self.platform_window.contentRectForFrameRect_(window_frame);
Point {
x: content_rect.origin.x,
y: screen_frame.y2() - (content_rect.origin.y + content_rect.size.height),
}
}
unsafe fn set_content_size(&self, size: Size) {
self.platform_window.setContentSize_(size.into());
}
pub(super) unsafe fn get_content_size(&self) -> Size {
NSView::frame(self.platform_window.contentView())
.size
.into()
}
unsafe fn set_min_frame_size(&self, size: Size) {
self.platform_window.setMinSize_(size.into());
}
unsafe fn get_min_frame_size(&self) -> Size {
self.platform_window.minSize().into()
}
unsafe fn set_max_frame_size(&self, size: Size) {
self.platform_window.setMaxSize_(size.into());
}
unsafe fn get_max_frame_size(&self) -> Size {
self.platform_window.maxSize().into()
}
unsafe fn set_min_content_size(&self, size: Size) {
self.platform_window.setContentMinSize_(size.into());
}
unsafe fn get_min_content_size(&self) -> Size {
self.platform_window.contentMinSize().into()
}
unsafe fn set_max_content_size(&self, size: Size) {
self.platform_window.setContentMaxSize_(size.into());
}
unsafe fn get_max_content_size(&self) -> Size {
self.platform_window.contentMaxSize().into()
}
pub fn perform_window_drag(&self) -> PlatformResult<()> {
if self.window_dragging.get() {
return Ok(());
}
unsafe {
let last_event = self
.last_event
.borrow()
.values()
.filter(|e| {
let event_type = e.eventType();
event_type == NSLeftMouseDown
})
.max_by_key(|x| x.eventNumber())
.cloned();
if let Some(last_event) = last_event {
// ensure flutter doesn't get mouse events during dragging
// (to be consistent with other platforms)
self.synthetize_mouse_up_event();
self.window_dragging.set(true);
let () = msg_send![*self.platform_window, performWindowDragWithEvent:*last_event];
Ok(())
} else {
Err(PlatformError::NoEventFound)
}
}
}
pub fn set_style(&self, style: WindowStyle) -> PlatformResult<()> {
unsafe {
let mut mask: NSWindowStyleMask = NSWindowStyleMask::NSBorderlessWindowMask;
if style.frame == WindowFrame::Regular {
NSWindow::setTitlebarAppearsTransparent_(*self.platform_window, NO);
NSWindow::setTitleVisibility_(
*self.platform_window,
NSWindowTitleVisibility::NSWindowTitleVisible,
);
} else {
NSWindow::setTitlebarAppearsTransparent_(*self.platform_window, YES);
NSWindow::setTitleVisibility_(
*self.platform_window,
NSWindowTitleVisibility::NSWindowTitleHidden,
);
}
if style.frame == WindowFrame::NoTitle {
mask |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
let () = msg_send![*self.window_buttons, setEnabled: YES];
if let Some(offset) = &style.traffic_light_offset {
let offset: NSPoint = offset.into();
let () = msg_send![*self.window_buttons, setOrigin: offset];
}
} else {
let () = msg_send![*self.window_buttons, setEnabled: NO];
}
if style.frame != WindowFrame::NoFrame {
mask |= NSWindowStyleMask::NSTitledWindowMask;
if style.can_close {
mask |= NSWindowStyleMask::NSClosableWindowMask;
}
if style.can_resize {
mask |= NSWindowStyleMask::NSResizableWindowMask;
}
if style.can_minimize {
mask |= NSWindowStyleMask::NSMiniaturizableWindowMask;
}
NSWindow::setHasShadow_(*self.platform_window, YES);
} else {
NSWindow::setHasShadow_(*self.platform_window, NO);
}
let mut collection_behavior = NSWindow::collectionBehavior(*self.platform_window);
let no_fullscreen: NSWindowCollectionBehavior =
std::mem::transmute((1 << 9) as NSUInteger);
if !style.can_full_screen {
collection_behavior |= no_fullscreen;
} else {
collection_behavior &= !no_fullscreen;
}
NSWindow::setCollectionBehavior_(*self.platform_window, collection_behavior);
NSWindow::setStyleMask_(*self.platform_window, mask);
NSWindow::setLevel_(
*self.platform_window,
match style.always_on_top {
true => style.always_on_top_level.unwrap_or(3), /* kCGFloatingWindowLevel */
false => 0, /* kCGNormalWindowLevel */
},
);
}
Ok(())
}
pub fn set_title(&self, title: String) -> PlatformResult<()> {
unsafe {
NSWindow::setTitle_(*self.platform_window, *to_nsstring(&title));
}
Ok(())
}
pub fn save_position_to_string(&self) -> PlatformResult<String> {
unsafe {
let string: id = msg_send![*self.platform_window, stringWithSavedFrame];
Ok(from_nsstring(string))
}
}
pub fn restore_position_from_string(&self, position: String) -> PlatformResult<()> {
unsafe {
let position = to_nsstring(&position);
let () = msg_send![*self.platform_window, setFrameFromString:*position];
}
Ok(())
}
pub fn get_window_state_flags(&self) -> PlatformResult<WindowStateFlags> {
Ok(self.window_state_flags.borrow().clone())
}
pub fn is_modal(&self) -> bool {
self.modal_close_callback.borrow().is_some()
}
pub fn set_collection_behavior(
&self,
behavior: WindowCollectionBehavior,
) -> PlatformResult<()> {
let mut b = NSWindowCollectionBehavior::empty();
if behavior.can_join_all_spaces {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces;
}
if behavior.move_to_active_space {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorMoveToActiveSpace;
}
if behavior.managed {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorManaged;
}
if behavior.transient {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorTransient;
}
if behavior.participates_in_cycle {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorParticipatesInCycle;
}
if behavior.ignores_cycle {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorIgnoresCycle;
}
if behavior.full_screen_primary {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenPrimary;
}
if behavior.full_screen_auxiliary {
b |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary;
}
if behavior.full_screen_none {
b |= unsafe { std::mem::transmute((1 << 9) as NSUInteger) };
}
if behavior.allows_tiling {
b |= unsafe { std::mem::transmute((1 << 11) as NSUInteger) };
}
if behavior.disallows_tiling {
b |= unsafe { std::mem::transmute((1 << 12) as NSUInteger) };
}
unsafe {
NSWindow::setCollectionBehavior_(*self.platform_window, b);
}
Ok(())
}
pub fn set_minimized(&self, minimized: bool) -> PlatformResult<()> {
if minimized {
unsafe { NSWindow::miniaturize_(*self.platform_window, nil) };
} else {
unsafe { NSWindow::deminiaturize_(*self.platform_window, nil) };
}
Ok(())
}
pub fn set_maximized(&self, maximized: bool) -> PlatformResult<()> {
let is_zoomed: BOOL = unsafe { msg_send![*self.platform_window, isZoomed] };
let is_zoomed = is_zoomed == YES;
if (maximized && !is_zoomed) || (!maximized && is_zoomed) {
unsafe { NSWindow::zoom_(*self.platform_window, nil) };
}
Ok(())
}
pub fn set_full_screen(&self, full_screen: bool) -> PlatformResult<()> {
let masks = unsafe { NSWindow::styleMask(*self.platform_window) };
let is_full_screen = masks.contains(NSWindowStyleMask::NSFullScreenWindowMask);
if (full_screen && !is_full_screen) || (!full_screen && is_full_screen) {
unsafe { NSWindow::toggleFullScreen_(*self.platform_window, nil) };
}
Ok(())
}
unsafe fn actually_show(&self) {
if self.is_modal() {
let parent = self.parent_platform_window.as_ref().unwrap().clone().load();
let () = msg_send![*parent, beginSheet:*self.platform_window completionHandler:nil];
} else {
self.platform_window.makeKeyAndOrderFront_(nil);
}
}
fn show_when_ready(weak_self: Weak<PlatformWindow>, attempt: i32) {
if let Some(s) = weak_self.upgrade() {
autoreleasepool(|| unsafe {
let view = s.flutter_view.borrow().clone();
let subviews: id = msg_send![*view, subviews];
let view = {
if subviews.count() > 0 {
subviews.objectAtIndex(0)
} else {
*view
}
};
// If our assumptions about the layout below are wrong, don't keep
// waiting indefinitely.
let mut show = attempt == 5;
if !show {
let layer = view.layer();
let sublayers: id = msg_send![layer, sublayers];
let first = sublayers.objectAtIndex(0);
let contents: id = msg_send![first, contents];
if contents != nil {
// This makes assumptions about FlutterView internals :-/
let class: id = msg_send![contents, className];
if !class.isEqualToString("IOSurface") {
panic!("Expected IOSurface content");
}
let scale = NSWindow::backingScaleFactor(*s.platform_window);
let content_size = NSView::frame(*s.flutter_view.borrow().clone());
let expected_width = scale * content_size.size.width;
let expected_height = scale * content_size.size.height;
// IOSurface width/height
let actual_width: NSInteger = msg_send![contents, width];
let actual_height: NSInteger = msg_send![contents, height];
// only show if size matches, otherwise we caught the view during resizing
if actual_width == expected_width as NSInteger
&& actual_height == expected_height as NSInteger
{
show = true;
}
}
}
if show {
s.actually_show();
if let Some(delegate) = s.delegate.upgrade() {
delegate.visibility_changed(true);
};
} else if let Some(context) = s.context.get() {
// wait until we have content generated (with proper size)
context
.run_loop
.borrow()
.schedule(Duration::from_secs_f64(1.0 / 60.0), move || {
Self::show_when_ready(weak_self, attempt + 1)
})
.detach();
}
});
}
}
pub fn ready_to_show(&self) -> PlatformResult<()> {
self.ready_to_show.set(true);
if self.show_when_ready.get() {
Self::show_when_ready(self.weak_self.clone_value(), 0);
}
Ok(())
}
pub fn show(&self) -> PlatformResult<()> {
if self.ready_to_show.get() {
Self::show_when_ready(self.weak_self.clone_value(), 0);
} else {
self.show_when_ready.set(true);
}
Ok(())
}
pub fn show_modal<F>(&self, done_callback: F)
where
F: FnOnce(PlatformResult<Value>) + 'static,
{
self.modal_close_callback
.borrow_mut()
.replace(Box::new(done_callback));
self.show().ok_log();
}
pub fn close_with_result(&self, result: Value) -> PlatformResult<()> {
let callback = self.modal_close_callback.borrow_mut().take();
if let Some(callback) = callback {
callback(Ok(result));
}
self.close()
}
pub fn close(&self) -> PlatformResult<()> {
autoreleasepool(|| unsafe {
let sheet_parent: id = msg_send![*self.platform_window, sheetParent];
if sheet_parent != nil {
let () = msg_send![sheet_parent, endSheet:*self.platform_window];
}
self.platform_window.close();
});
Ok(())
}
pub fn hide(&self) -> PlatformResult<()> {
if self.ready_to_show.get() {
autoreleasepool(|| unsafe {
self.platform_window.orderOut_(nil);
});
if let Some(delegate) = self.delegate.upgrade() {
delegate.visibility_changed(false);
}
} else {
self.show_when_ready.set(false);
}
Ok(())
}
pub fn activate(&self, activate_application: bool) -> PlatformResult<bool> {
unsafe {
let app = NSApplication::sharedApplication(nil);
NSApplication::activateIgnoringOtherApps_(app, YES);
if activate_application {
let app = NSApplication::sharedApplication(nil);
NSApplication::activateIgnoringOtherApps_(app, YES);
}
NSWindow::makeKeyAndOrderFront_(*self.platform_window, nil);
}
Ok(true)
}
pub fn deactivate(&self, deactivate_application: bool) -> PlatformResult<bool> {
unsafe {
let () = msg_send![*self.platform_window, resignFirstResponder];
NSWindow::orderBack_(*self.platform_window, nil);
if deactivate_application {
let app = NSApplication::sharedApplication(nil);
let () = msg_send![app, hide: nil];
let () = msg_send![app, unhideWithoutActivation];
}
}
Ok(true)
}
unsafe fn synthetize_mouse_up_event(&self) {
let last_event = self
.last_event
.borrow()
.values()
.filter(|e| {
let event_type = e.eventType();
event_type == NSLeftMouseDown
|| event_type == NSLeftMouseUp
|| event_type == NSRightMouseDown
|| event_type == NSRightMouseUp
})
.max_by_key(|x| x.eventNumber())
.cloned();
if let Some(event) = last_event {
let opposite = match event.eventType() {
NSLeftMouseDown => CGEventType::LeftMouseUp,
NSRightMouseDown => CGEventType::RightMouseUp,
_ => return,
};
let event = NSEvent::CGEvent(*event) as core_graphics::sys::CGEventRef;
let event = CGEventCreateCopy(event);
CGEventSetType(event, opposite);
let synthetized: id = msg_send![class!(NSEvent), eventWithCGEvent: event];
CFRelease(event as *mut _);
let () = msg_send![*self.platform_window, sendEvent: synthetized];
}
}
pub(super) fn synthetize_mouse_move_if_needed(&self) {
autoreleasepool(|| unsafe {
let last_event = self
.last_event
.borrow()
.values()
.filter(|e| {
let event_type = e.eventType();
event_type as i32 >= NSLeftMouseDown as i32
&& event_type as i32 <= NSMouseExited as i32
})
.max_by_key(|x| x.eventNumber())
.cloned();
if let Some(last_event) = last_event {
let location = NSEvent::mouseLocation(nil);
let window_frame = NSWindow::frame(*self.platform_window);
let content_rect = self.platform_window.contentRectForFrameRect_(window_frame);
let tail = NSPoint {
x: content_rect.origin.x + content_rect.size.width,
y: content_rect.origin.y + content_rect.size.height,
};
if location.x > content_rect.origin.x
&& location.x < tail.x
&& location.y > content_rect.origin.y
&& location.y < tail.y
{
let location: NSPoint =
msg_send![*self.platform_window, convertPointFromScreen: location];
let event: id = msg_send![class!(NSEvent), mouseEventWithType: NSMouseMoved
location:location
modifierFlags:NSEvent::modifierFlags(nil)
timestamp: Self::system_uptime()
windowNumber:0
context:nil
eventNumber:NSEvent::eventNumber(*last_event)
clickCount:1
pressure:0
];
let () = msg_send![*self.platform_window, sendEvent: event];
}
}
});
}
pub fn on_layout(&self) {
if unsafe { NSWindow::inLiveResize(*self.platform_window) } == YES {
if let Some(context) = self.context.get() {
// Neither run loop nor main dispatch queue are running during
// window resizing; So we poll the run loop manually to keep things
// updated.
context.run_loop.borrow().platform_run_loop.poll();
}
}
}
pub fn should_send_event(&self, event: StrongPtr) -> bool {
let event_type = unsafe { NSEvent::eventType(*event) };
if event_type == NSMouseEntered || event_type == NSMouseExited {
let timestamp = unsafe { NSEvent::timestamp(*event) };
// we attempt to ignore the event, unfortunately this doesn't work for
// MouseEntered, as it is delivered to NSTrackingArea by NSApplication
// directlly. We do however counteract it with
// subsequent MouseMove produced by synthetize_mouse_move_if_needed()
if timestamp < self.ignore_enter_leave_until.get() {
self.synthetize_mouse_move_if_needed();
return false;
}
}
self.check_window_dragging(event);
true
}
// Special handling for dragging window with popup menu open.
//
// If user drags mouse on window with popup menu open, the LeftMouseDown event
// swallowed (used to close the popup menu) and the window start getting
// NSLeftMouseDragged events. We try to detect that, and upon getting the second
// NSLeftMouseDragged event without prior NSLeftMouseDown, we synthetize the
// NSLeftMouseDown event ourself (necessary for flutter view to start getting
// mouse drag events). We ignore the first NSLeftMouseDragged event because
// that's sometimes posted without a subsequent NSLeftMouseUp event.
//
// This all is necessary to have custom titlebars draggable while popup menu
// is open.
fn check_window_dragging(&self, event: StrongPtr) {
let event_type = unsafe { NSEvent::eventType(*event) };
// println!(
// "EVent type {:?} {} {}",
// event_type,
// self.mouse_down.get(),
// self.mouse_dragged.get()
// );
// NSMouseMoved after NSLeftMouseDragged without NSLeftMouseUp
if event_type == NSMouseMoved {
if self.mouse_down.get() {
unsafe {
// println!("Synthetizing up");
self.synthetize_mouse_up_event();
}
}
self.mouse_down.set(false);
self.mouse_dragged.set(false);
}
if event_type == NSLeftMouseDown {
self.mouse_down.set(true);
self.mouse_dragged.set(false);
} else if event_type == NSLeftMouseUp {
self.mouse_down.set(false);
self.mouse_dragged.set(false);
self.window_dragging.set(false);
} else if event_type == NSLeftMouseDragged
&& !self.mouse_down.get()
&& !self.mouse_dragged.get()
{
// first NSLeftMouseDragged, ignore it because sometimes window
// server sends it without subsequent NSLeftMouseUp
self.mouse_dragged.set(true);
} else if event_type == NSLeftMouseDragged
&& !self.mouse_down.get()
&& self.mouse_dragged.get()
&& !self.window_dragging.get()
{
// Second NSLeftMouseDragged without prior NSLeftMouseDown; This likely
// means user started dragging window while popup menu was opened.
// On this case we synthetize LeftMouseDown event at the location
// of lastest hitTest in IMContentView. While window server swallows
// the mouseDown event, it still generates hitTest at the location
// where user pressed the button to possibly initiate regular
// window drag.
unsafe {
let event = NSEvent::CGEvent(*event) as core_graphics::sys::CGEventRef;
let event = CGEventCreateCopy(event);
CGEventSetType(event, CGEventType::LeftMouseDown);
let location: CGPoint = msg_send![
NSWindow::contentView(*self.platform_window),
lastHitTestScreen
];
let location_win: CGPoint = msg_send![
NSWindow::contentView(*self.platform_window),
lastHitTestWindow
];
CGEventSetLocation(event, location);
CGEventSetWindowLocation(event, location_win);
println!("Synthetizing left mouse down");
let synthetized: id = msg_send![class!(NSEvent), eventWithCGEvent: event];
CFRelease(event as *mut _);
let () = msg_send![*self.platform_window, sendEvent: synthetized];
self.mouse_down.set(true);
self.mouse_dragged.set(false);
}
}
}
pub fn set_pending_effect(&self, effect: DragEffect) {
self.drag_context.borrow_mut().set_pending_effect(effect);
}
pub fn begin_drag_session(&self, request: DragRequest) -> PlatformResult<()> {
let last_down_event = self
.last_event
.borrow()
.get(&(NSLeftMouseDown as u64))
.cloned();
if let Some(last_down_event) = last_down_event {
autoreleasepool(|| unsafe {
self.drag_context.borrow().start_drag(
request,
self.platform_window.contentView(),
*self.platform_window,
*last_down_event,
);
self.synthetize_mouse_up_event();
});
Ok(())
} else {
Err(PlatformError::NoEventFound)
}
}
fn system_uptime() -> f64 {
unsafe {
let info = NSProcessInfo::processInfo(nil);
msg_send![info, systemUptime]
}
}
pub fn show_popup_menu<F>(&self, menu: Rc<PlatformMenu>, request: PopupMenuRequest, on_done: F)
where
F: FnOnce(PlatformResult<PopupMenuResponse>) + 'static,
{
unsafe {
// cocoa eats mouse up on popup menu
self.synthetize_mouse_up_event();
let mut position: NSPoint = request.position.into();
flip_position(self.platform_window.contentView(), &mut position);
let view = StrongPtr::retain(self.platform_window.contentView());
let menu = menu.menu.clone();
let on_done = RefCell::new(Some(Box::new(on_done)));
let weak = self.weak_self.clone_value();
let cb = move || {
let item_selected: BOOL = msg_send![*menu, popUpMenuPositioningItem:nil atLocation:position inView:view.clone()];
let on_done = on_done.take();
if let Some(s) = weak.upgrade() {
// When hiding menu NSApplication will for whatever reason replay
// 'queued' stale MouseEnter/Leave events.
s.ignore_enter_leave_until.replace(Self::system_uptime());
s.synthetize_mouse_move_if_needed();
}
if let Some(on_done) = on_done {
on_done(Ok(PopupMenuResponse {
item_selected: item_selected == YES,
}));
}
};