Skip to content

Commit

Permalink
confirm before quit on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Sep 24, 2024
1 parent 4ccd0ce commit 1a65dd6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion frontends/rioterm/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ impl Application<'_> {
event_proxy.clone(),
);
let scheduler = Scheduler::new(proxy);

event_loop.listen_device_events(DeviceEvents::Never);

#[cfg(target_os = "macos")]
event_loop.set_confirm_before_quit(config.confirm_before_quit);

Application {
config,
event_proxy,
Expand Down
10 changes: 10 additions & 0 deletions rio-window/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ impl<T> EventLoop<T> {
.listen_device_events(allowed);
}

#[inline]
#[cfg(target_os = "macos")]
pub fn set_confirm_before_quit(&self, confirmation: bool) {
let _span =
tracing::debug_span!("rio_window::EventLoop::set_confirm_before_quit")
.entered();

self.event_loop.set_confirm_before_quit(confirmation)
}

/// Sets the [`ControlFlow`].
pub fn set_control_flow(&self, control_flow: ControlFlow) {
self.event_loop
Expand Down
13 changes: 12 additions & 1 deletion rio-window/src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Default for Policy {
pub(super) struct State {
activation_policy: Policy,
default_menu: bool,
set_confirm_before_quit: Cell<bool>,
activate_ignoring_other_apps: bool,
event_handler: EventHandler,
stop_on_launch: Cell<bool>,
Expand Down Expand Up @@ -92,6 +93,10 @@ declare_class!(
unsafe impl NSApplicationDelegate for ApplicationDelegate {
#[method(applicationShouldTerminate:)]
fn should_terminate(&self, _sender: Option<&AnyObject>) -> u64 {
if !self.ivars().set_confirm_before_quit.get() {
return NSApplicationTerminateReply::Now as u64;
}

use objc::runtime::Object;
use objc::msg_send;
use objc::sel;
Expand Down Expand Up @@ -349,6 +354,12 @@ impl ApplicationDelegate {
self.ivars().event_handler.set(handler, closure)
}

/// Place the event handler in the application delegate for the duration
/// of the given closure.
pub fn set_confirm_before_quit(&self, confirmation: bool) {
self.ivars().set_confirm_before_quit.set(confirmation)
}

/// If `pump_events` is called to progress the event loop then we
/// bootstrap the event loop via `-[NSApplication run]` but will use
/// `CFRunLoopRunInMode` for subsequent calls to `pump_events`.
Expand Down Expand Up @@ -677,4 +688,4 @@ fn min_timeout(a: Option<Instant>, b: Option<Instant>) -> Option<Instant> {
a.map_or(b, |a_timeout| {
b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))
})
}
}
4 changes: 4 additions & 0 deletions rio-window/src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ impl<T> EventLoop<T> {
&self.window_target
}

pub fn set_confirm_before_quit(&self, confirmation: bool) {
self.delegate.set_confirm_before_quit(confirmation);
}

pub fn run<F>(mut self, handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<T>, &RootWindowTarget),
Expand Down

0 comments on commit 1a65dd6

Please sign in to comment.