Skip to content

Commit

Permalink
Improve ActiveApplicationImpl on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Sologub committed May 28, 2024
1 parent b1c627e commit e1e0660
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
50 changes: 45 additions & 5 deletions lib/src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ use std::{
rc::Weak,
};

use objc2::{declare_class, msg_send_id, mutability, rc::Id, ClassType, DeclaredClass};
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate};
use objc2::{
declare_class,
msg_send_id,
mutability,
rc::{autoreleasepool, Id},
ClassType,
DeclaredClass,
};
use objc2_app_kit::{NSApp, NSApplicationActivationPolicy, NSApplicationDelegate};
use objc2_foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol};

use super::panicinfo::PanicInfo;
use crate::{ActiveApplication, Event, EventHandler, LifeCycle};
use crate::{platform::Wrapper, ActiveApplication, Event, EventHandler, Icon, LifeCycle, Menu};

#[derive(Debug)]
pub(super) struct ActivationPolicy(NSApplicationActivationPolicy);
Expand Down Expand Up @@ -65,7 +72,7 @@ declare_class!(
#[method(applicationDidFinishLaunching:)]
fn did_finish_launching(&self, _notification: &NSNotification) {
let mtm = MainThreadMarker::from(self);
let app = NSApplication::sharedApplication(mtm);
let app = NSApp(mtm);
// We need to delay setting the activation policy and activating the app
// until `applicationDidFinishLaunching` has been called. Otherwise the
// menu bar is initially unresponsive on macOS 10.15.
Expand Down Expand Up @@ -101,7 +108,7 @@ impl AppDelegate {
}

pub(super) fn get(mtm: MainThreadMarker) -> Id<Self> {
let app = NSApplication::sharedApplication(mtm);
let app = NSApp(mtm);
let delegate =
unsafe { app.delegate() }.expect("a delegate was not configured on the application");
if delegate.is_kind_of::<Self>() {
Expand Down Expand Up @@ -152,4 +159,37 @@ impl AppDelegate {
self.ivars().handler.borrow_mut().on_event(app, event);
}
}

#[inline]
pub(super) fn set_menu(&self, menu: Option<&Menu>) {
let mtm = MainThreadMarker::from(self);
let app = NSApp(mtm);
if let Some(menu) = menu {
app.setMainMenu(Some(&menu.get_impl().get_native()));
} else {
app.setMainMenu(None);
}
}

#[inline]
pub(super) fn set_icon(&self, icon: Option<&Icon>) {
let mtm = MainThreadMarker::from(self);
let app = NSApp(mtm);
match icon {
Some(icon) => {
let ns_image = icon.get_impl().get_native();
unsafe { app.setApplicationIconImage(Some(&ns_image)) };
}
None => unsafe { app.setApplicationIconImage(None) },
}
}

#[inline]
pub(super) fn stop(&self) {
autoreleasepool(|_| {
let mtm = MainThreadMarker::from(self);
let app = NSApp(mtm);
app.stop(None);
});
}
}
33 changes: 10 additions & 23 deletions lib/src/platform_impl/macos/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use objc2::{
rc::{autoreleasepool, Id},
runtime::ProtocolObject,
};
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy};
use objc2_app_kit::{NSApp, NSApplication, NSApplicationActivationPolicy};
use objc2_foundation::{MainThreadBound, MainThreadMarker};

use super::{
Expand Down Expand Up @@ -94,34 +94,22 @@ impl ActiveApplicationImpl {
impl ActiveApplicationApi for ActiveApplicationImpl {
#[inline]
fn set_menu(&mut self, menu: Option<&Menu>) {
let mtm = self.context.get_impl().mtm();
let app = NSApplication::sharedApplication(mtm);
if let Some(menu) = menu {
app.setMainMenu(Some(&menu.get_impl().get_native()));
} else {
app.setMainMenu(None);
}
self.delegate.get_on_main(|delegate| {
delegate.set_menu(menu);
});
}

#[inline]
fn set_icon(&mut self, icon: Option<&Icon>) {
let mtm = self.context.get_impl().mtm();
let app = NSApplication::sharedApplication(mtm);
match icon {
Some(icon) => {
let ns_image = icon.get_impl().get_native();
unsafe { app.setApplicationIconImage(Some(&ns_image)) };
}
None => unsafe { app.setApplicationIconImage(None) },
}
self.delegate.get_on_main(|delegate| {
delegate.set_icon(icon);
});
}

#[inline]
fn stop(&mut self) {
autoreleasepool(|_| {
let mtm = self.context.get_impl().mtm();
let app = NSApplication::sharedApplication(mtm);
app.stop(None);
self.delegate.get_on_main(|delegate| {
delegate.stop();
});
}
}
Expand Down Expand Up @@ -153,7 +141,7 @@ impl ApplicationApi for ApplicationImpl {
#[inline]
fn run(&mut self, handler: impl EventHandler + 'static) {
let mtm = self.context.get_impl().mtm();
let ns_app = NSApplication::sharedApplication(mtm);
let ns_app = NSApp(mtm);
ns_app.setActivationPolicy(NSApplicationActivationPolicy::Regular);

// configure the application delegate
Expand All @@ -172,7 +160,6 @@ impl ApplicationApi for ApplicationImpl {
setup_control_flow_observers(Rc::downgrade(&panic_info));

autoreleasepool(|_| {
// SAFETY: We do not run the application re-entrantly
unsafe { ns_app.run() };
});
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/platform_impl/macos/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ impl IconImpl {
pub(super) fn get_native(&self) -> &Id<NSImage> { self.native.get(self.mtm) }
}

unsafe impl Sync for IconImpl {}
unsafe impl Send for IconImpl {}

impl IconApi for IconImpl {
#[inline]
fn from_data(
Expand Down
3 changes: 3 additions & 0 deletions lib/src/platform_impl/macos/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ impl MenuImpl {
pub(super) fn get_native(&self) -> &Id<NSMenu> { self.native.get(self.mtm) }
}

unsafe impl Sync for Menu {}
unsafe impl Send for Menu {}

impl MenuApi for MenuImpl {
#[inline]
fn new(ctx: &impl ContextOwner, items: Vec<MenuItem>) -> Self {
Expand Down

0 comments on commit e1e0660

Please sign in to comment.