Skip to content

Commit 2775156

Browse files
authored
MacOS: Only activate after the application has finished launching (#1903)
* MacOS: Only activate after the application has finished launching This fixes the main menu not responding until you refocus, at least from what I can tell - though we might have to do something similar to linebender/druid#994 to fix it fully? * MacOS: Remove activation hack * Stop unnecessarily calling `makeKeyWindow` on initially hidden windows You can't make hidden windows the key window * Add new, simpler activation hack For activating multiple windows created before the application finished launching
1 parent 45aacd8 commit 2775156

File tree

8 files changed

+50
-289
lines changed

8 files changed

+50
-289
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- On macOS, wait with activating the application until the application has initialized.
34
- On macOS, fix creating new windows when the application has a main menu.
45
- On Windows, fix fractional deltas for mouse wheel device events.
56
- On macOS, fix segmentation fault after dropping the main window.

src/platform_impl/macos/activation_hack.rs

Lines changed: 0 additions & 208 deletions
This file was deleted.

src/platform_impl/macos/app.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::collections::VecDeque;
22

33
use cocoa::{
44
appkit::{self, NSEvent},
5-
base::{id, nil},
5+
base::id,
66
};
77
use objc::{
88
declare::ClassDecl,
99
runtime::{Class, Object, Sel},
1010
};
1111

12-
use super::{activation_hack, app_state::AppState, event::EventWrapper, util, DEVICE_ID};
12+
use super::{app_state::AppState, event::EventWrapper, util, DEVICE_ID};
1313
use crate::event::{DeviceEvent, ElementState, Event};
1414

1515
pub struct AppClass(pub *const Class);
@@ -49,14 +49,14 @@ extern "C" fn send_event(this: &Object, _sel: Sel, event: id) {
4949
let key_window: id = msg_send![this, keyWindow];
5050
let _: () = msg_send![key_window, sendEvent: event];
5151
} else {
52-
maybe_dispatch_device_event(this, event);
52+
maybe_dispatch_device_event(event);
5353
let superclass = util::superclass(this);
5454
let _: () = msg_send![super(this, superclass), sendEvent: event];
5555
}
5656
}
5757
}
5858

59-
unsafe fn maybe_dispatch_device_event(this: &Object, event: id) {
59+
unsafe fn maybe_dispatch_device_event(event: id) {
6060
let event_type = event.eventType();
6161
match event_type {
6262
appkit::NSMouseMoved
@@ -98,21 +98,6 @@ unsafe fn maybe_dispatch_device_event(this: &Object, event: id) {
9898
}
9999

100100
AppState::queue_events(events);
101-
102-
// Notify the delegate when the first mouse move occurs. This is
103-
// used for the unbundled app activation hack, which needs to know
104-
// if any mouse motions occurred prior to the app activating.
105-
let delegate: id = msg_send![this, delegate];
106-
assert_ne!(delegate, nil);
107-
if !activation_hack::State::get_mouse_moved(&*delegate) {
108-
activation_hack::State::set_mouse_moved(&*delegate, true);
109-
let () = msg_send![
110-
delegate,
111-
performSelector: sel!(activationHackMouseMoved:)
112-
withObject: nil
113-
afterDelay: 0.0
114-
];
115-
}
116101
}
117102
appkit::NSLeftMouseDown | appkit::NSRightMouseDown | appkit::NSOtherMouseDown => {
118103
let mut events = VecDeque::with_capacity(1);
Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use super::{activation_hack, app_state::AppState};
1+
use super::app_state::AppState;
22
use cocoa::base::id;
33
use objc::{
44
declare::ClassDecl,
55
runtime::{Class, Object, Sel},
66
};
7-
use std::os::raw::c_void;
87

98
pub struct AppDelegateClass(pub *const Class);
109
unsafe impl Send for AppDelegateClass {}
@@ -15,67 +14,17 @@ lazy_static! {
1514
let superclass = class!(NSResponder);
1615
let mut decl = ClassDecl::new("WinitAppDelegate", superclass).unwrap();
1716

18-
decl.add_class_method(sel!(new), new as extern "C" fn(&Class, Sel) -> id);
19-
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(&Object, Sel));
2017
decl.add_method(
2118
sel!(applicationDidFinishLaunching:),
2219
did_finish_launching as extern "C" fn(&Object, Sel, id),
2320
);
24-
decl.add_method(
25-
sel!(applicationDidBecomeActive:),
26-
did_become_active as extern "C" fn(&Object, Sel, id),
27-
);
28-
decl.add_method(
29-
sel!(applicationDidResignActive:),
30-
did_resign_active as extern "C" fn(&Object, Sel, id),
31-
);
32-
33-
decl.add_ivar::<*mut c_void>(activation_hack::State::name());
34-
decl.add_method(
35-
sel!(activationHackMouseMoved:),
36-
activation_hack::mouse_moved as extern "C" fn(&Object, Sel, id),
37-
);
3821

3922
AppDelegateClass(decl.register())
4023
};
4124
}
4225

43-
extern "C" fn new(class: &Class, _: Sel) -> id {
44-
unsafe {
45-
let this: id = msg_send![class, alloc];
46-
let this: id = msg_send![this, init];
47-
(*this).set_ivar(
48-
activation_hack::State::name(),
49-
activation_hack::State::new(),
50-
);
51-
this
52-
}
53-
}
54-
55-
extern "C" fn dealloc(this: &Object, _: Sel) {
56-
unsafe {
57-
activation_hack::State::free(activation_hack::State::get_ptr(this));
58-
}
59-
}
60-
6126
extern "C" fn did_finish_launching(_: &Object, _: Sel, _: id) {
6227
trace!("Triggered `applicationDidFinishLaunching`");
6328
AppState::launched();
6429
trace!("Completed `applicationDidFinishLaunching`");
6530
}
66-
67-
extern "C" fn did_become_active(this: &Object, _: Sel, _: id) {
68-
trace!("Triggered `applicationDidBecomeActive`");
69-
unsafe {
70-
activation_hack::State::set_activated(this, true);
71-
}
72-
trace!("Completed `applicationDidBecomeActive`");
73-
}
74-
75-
extern "C" fn did_resign_active(this: &Object, _: Sel, _: id) {
76-
trace!("Triggered `applicationDidResignActive`");
77-
unsafe {
78-
activation_hack::refocus(this);
79-
}
80-
trace!("Completed `applicationDidResignActive`");
81-
}

0 commit comments

Comments
 (0)