Skip to content

Commit 08a3dfc

Browse files
I-Infomadsmtm
andauthoredApr 22, 2024
feat: Event::Reopen support for macOS (#5)
* feat: `Reopen` event support for macOS * chore: update docs Co-authored-by: Mads Marquart <mads@marquart.dk> * fix: remove redundant enum variants * fix: adapt code --------- Co-authored-by: Mads Marquart <mads@marquart.dk>
1 parent 56b32eb commit 08a3dfc

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ smol_str = "0.2.0"
7272
[dev-dependencies]
7373
image = { version = "0.24.0", default-features = false, features = ["png"] }
7474
simple_logger = { version = "4.2.0", default_features = false }
75-
winit = { path = ".", features = ["rwh_05"] }
75+
# winit = { path = ".", features = ["rwh_05"] }
7676

7777
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies]
7878
softbuffer = "0.3.0"

‎src/event.rs

+19
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ pub enum Event<T: 'static> {
251251
///
252252
/// - **macOS / Wayland / Windows / Orbital:** Unsupported.
253253
MemoryWarning,
254+
255+
/// Emitted when the application has received a reopen request from OS.
256+
///
257+
/// ## Platform-specific
258+
///
259+
/// ### macOS
260+
///
261+
/// On macOS, the `Reopen` event is emitted in response to an [`applicationShouldHandleReopen`]
262+
/// callback, which is usually called whenever the Finder reactivates an already running
263+
/// application because the user double-clicked it again or used the dock to activate it.
264+
/// Usually, the user would expect you to create a new window if there isn't any.
265+
///
266+
/// [`applicationShouldHandleReopen`]: https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen
267+
///
268+
/// ### Others
269+
///
270+
/// - **Android / iOS / Web / Wayland / Windows / Orbital:** Unsupported.
271+
Reopen,
254272
}
255273

256274
impl<T> Event<T> {
@@ -267,6 +285,7 @@ impl<T> Event<T> {
267285
Suspended => Ok(Suspended),
268286
Resumed => Ok(Resumed),
269287
MemoryWarning => Ok(MemoryWarning),
288+
Reopen => Ok(Reopen),
270289
}
271290
}
272291
}

‎src/platform_impl/macos/app_delegate.rs

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use objc2::{declare_class, msg_send, msg_send_id, mutability, ClassType};
88

99
use super::app_state::AppState;
1010
use super::appkit::NSApplicationActivationPolicy;
11+
use crate::event::Event;
1112

1213
declare_class!(
1314
#[derive(Debug)]
@@ -58,6 +59,18 @@ declare_class!(
5859
// TODO: Notify every window that it will be destroyed, like done in iOS?
5960
AppState::internal_exit();
6061
}
62+
63+
#[method(applicationShouldHandleReopen:hasVisibleWindows:)]
64+
fn should_handle_reopen(
65+
&self,
66+
_sender: &Option<&AnyObject>,
67+
_has_visible_windows: bool,
68+
) -> bool {
69+
trace_scope!("applicationShouldHandleReopen:hasVisibleWindows:");
70+
AppState::queue_event(Event::Reopen);
71+
// return true to preserve the default behavior, such as showing the minimized window.
72+
true
73+
}
6174
}
6275
);
6376

‎src/platform_impl/windows/window.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ use windows_sys::Win32::{
4949
MENU_ITEM_STATE, MFS_DISABLED, MFS_ENABLED, MF_BYCOMMAND, NID_READY, PM_NOREMOVE,
5050
SC_CLOSE, SC_MAXIMIZE, SC_MINIMIZE, SC_MOVE, SC_RESTORE, SC_SIZE, SM_DIGITIZER,
5151
SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER, TPM_LEFTALIGN,
52-
TPM_RETURNCMD, WDA_EXCLUDEFROMCAPTURE, WDA_NONE, WM_NCLBUTTONDOWN,
53-
WM_SYSCOMMAND, WNDCLASSEXW,
52+
TPM_RETURNCMD, WDA_EXCLUDEFROMCAPTURE, WDA_NONE, WM_NCLBUTTONDOWN, WM_SYSCOMMAND,
53+
WNDCLASSEXW,
5454
},
5555
},
5656
};

0 commit comments

Comments
 (0)