Skip to content

Commit cd1b688

Browse files
move NSApplication init to window code
1 parent 4207377 commit cd1b688

5 files changed

Lines changed: 46 additions & 35 deletions

File tree

desktop/src/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub(crate) struct App {
4444
}
4545

4646
impl App {
47+
pub(crate) fn init() {
48+
Window::init();
49+
}
50+
4751
pub(crate) fn new(
4852
cef_context: Box<dyn cef::CefContext>,
4953
cef_view_info_sender: Sender<cef::ViewInfoUpdate>,

desktop/src/lib.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,9 @@ use cef::CefHandler;
2323
use cli::Cli;
2424
use event::CreateAppEventSchedulerEventLoopExt;
2525

26-
mod mac_app_impl {
27-
use objc2::{ClassType, define_class, msg_send};
28-
use objc2_app_kit::{NSApplication, NSEvent, NSEventType, NSResponder};
29-
use objc2_foundation::NSObject;
30-
31-
define_class!(
32-
#[unsafe(super(NSApplication, NSResponder, NSObject))]
33-
#[name = "GraphiteApplication"]
34-
pub(super) struct GraphiteApplication;
35-
36-
impl GraphiteApplication {
37-
#[unsafe(method(sendEvent:))]
38-
fn send_event(&self, event: &NSEvent) {
39-
if event.r#type() == NSEventType::KeyDown && let Some(key_window) = self.keyWindow() {
40-
unsafe { msg_send![&key_window, sendEvent: event] }
41-
} else {
42-
unsafe { msg_send![super(self), sendEvent: event] }
43-
}
44-
}
45-
}
46-
);
47-
48-
impl GraphiteApplication {
49-
pub(super) fn init() {
50-
unsafe {
51-
let _: &NSApplication = msg_send![GraphiteApplication::class(), sharedApplication];
52-
}
53-
}
54-
}
55-
}
56-
5726
pub fn start() {
5827
tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init();
5928

60-
mac_app_impl::GraphiteApplication::init();
61-
6229
let cef_context_builder = cef::CefContextBuilder::<CefHandler>::new();
6330

6431
if cef_context_builder.is_sub_process() {
@@ -69,6 +36,8 @@ pub fn start() {
6936
return;
7037
}
7138

39+
App::init();
40+
7241
let cli = Cli::parse();
7342

7443
let wgpu_context = futures::executor::block_on(gpu_context::create_wgpu_context());

desktop/src/window.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use winit::window::{Window as WinitWindow, WindowAttributes};
44

55
use crate::consts::APP_NAME;
66
use crate::event::AppEventScheduler;
7+
use crate::window::mac::NativeWindowImpl;
78
use crate::wrapper::messages::MenuItem;
89

910
pub(crate) trait NativeWindow {
11+
fn init();
1012
fn configure(attributes: WindowAttributes, event_loop: &dyn ActiveEventLoop) -> WindowAttributes;
1113
fn new(window: &dyn WinitWindow, app_event_scheduler: AppEventScheduler) -> Self;
1214
fn update_menu(&self, _entries: Vec<MenuItem>) {}
@@ -34,6 +36,10 @@ pub(crate) struct Window {
3436
}
3537

3638
impl Window {
39+
pub(crate) fn init() {
40+
NativeWindowImpl::init();
41+
}
42+
3743
pub(crate) fn new(event_loop: &dyn ActiveEventLoop, app_event_scheduler: AppEventScheduler) -> Self {
3844
let mut attributes = WindowAttributes::default()
3945
.with_title(APP_NAME)

desktop/src/window/mac.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ use winit::window::{Window, WindowAttributes};
55
use crate::event::AppEventScheduler;
66
use crate::wrapper::messages::MenuItem;
77

8+
mod app;
9+
mod menu;
10+
811
pub(super) struct NativeWindowImpl {
912
menu: menu::Menu,
1013
}
1114

1215
impl super::NativeWindow for NativeWindowImpl {
16+
fn init() {
17+
app::init();
18+
}
19+
1320
fn configure(attributes: WindowAttributes, _event_loop: &dyn ActiveEventLoop) -> WindowAttributes {
1421
let mac_window = WindowAttributesMacOS::default()
1522
.with_titlebar_transparent(true)
@@ -28,5 +35,3 @@ impl super::NativeWindow for NativeWindowImpl {
2835
self.menu.update(entries);
2936
}
3037
}
31-
32-
mod menu;

desktop/src/window/mac/app.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use objc2::{ClassType, define_class, msg_send};
2+
use objc2_app_kit::{NSApplication, NSEvent, NSEventType, NSResponder};
3+
use objc2_foundation::NSObject;
4+
5+
pub(super) fn init() {
6+
unsafe {
7+
let _: &NSApplication = msg_send![GraphiteApplication::class(), sharedApplication];
8+
}
9+
}
10+
11+
define_class!(
12+
#[unsafe(super(NSApplication, NSResponder, NSObject))]
13+
#[name = "GraphiteApplication"]
14+
pub(super) struct GraphiteApplication;
15+
16+
impl GraphiteApplication {
17+
#[unsafe(method(sendEvent:))]
18+
fn send_event(&self, event: &NSEvent) {
19+
// Route keyDown events straight to the key window to skip native menu shortcut handling.
20+
if event.r#type() == NSEventType::KeyDown && let Some(key_window) = self.keyWindow() {
21+
unsafe { msg_send![&key_window, sendEvent: event] }
22+
} else {
23+
unsafe { msg_send![super(self), sendEvent: event] }
24+
}
25+
}
26+
}
27+
);

0 commit comments

Comments
 (0)