Skip to content

Commit

Permalink
macos: quit on lost focus only if another application takes focus away.
Browse files Browse the repository at this point in the history
Fixes #95 for mac only
  • Loading branch information
liias committed Oct 5, 2023
1 parent 7600b1b commit 562aedd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- macOS: Exit on lost focus when application looses focus not the window. #95

## [0.4.2] - 2023-09-25

### Fixed
Expand Down
18 changes: 15 additions & 3 deletions src/gui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ impl UIBrowser {
impl UIState {}

pub const URL_OPENED: Selector<druid::UrlOpenInfo> = Selector::new("url_opened");
pub const APP_LOST_FOCUS: Selector<druid::ApplicationLostFocus> = Selector::new("app_lost_focus");

pub const EXIT_APP: Selector<String> = Selector::new("browsers.exit_app");

Expand Down Expand Up @@ -443,15 +444,18 @@ impl AppDelegate<UIState> for UIDelegate {
data: &mut UIState,
_env: &Env,
) -> Option<Event> {
//let is_linux = cfg!(target_os = "linux");
let is_mac = cfg!(target_os = "macos");
// linux calls this even when just opening a context menu
//let close_on_lost_focus = !is_linux;
// mac calls this when opening About window
// mac is handled by application event instead now, which is fired
// when all windows of app loose focus
let quit_on_lost_focus = !is_mac && self.quit_on_lost_focus;

let should_exit = match event {
Event::KeyDown(KeyEvent {
key: KbKey::Escape, ..
}) => true,
Event::WindowLostFocus => self.quit_on_lost_focus,
Event::WindowLostFocus => quit_on_lost_focus,
_ => false,
};

Expand Down Expand Up @@ -552,6 +556,14 @@ impl AppDelegate<UIState> for UIDelegate {
// QUIT_APP doesn't always actually quit the app on macOS, so forcing exit until thats figured out
exit(0x0100);
Handled::Yes
} else if cmd.is(APP_LOST_FOCUS) {
info!("App lost focus");
if self.quit_on_lost_focus {
let sink = ctx.get_external_handle();
sink.submit_command(EXIT_APP, "".to_string(), Target::Global)
.unwrap();
}
Handled::Yes
} else if cmd.is(URL_OPENED) {
let url_open_info = cmd.get_unchecked(URL_OPENED);
data.url = url_open_info.url.clone();
Expand Down

0 comments on commit 562aedd

Please sign in to comment.