Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(macos): add ApplicationShouldHandleReopen event. #515

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/ApplicationShouldHandleReopen-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": minor
---

Add `event::ApplicationShouldHandleReopen` for handle click on dock icon on macOS.
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Run the `cargo run --example <file_name>` to see how each example works.
- `window_icon`: add window icon.
- `window`: example that makes a window.
- `system_tray`: add icon to your system tray, click icon to start.
- `system_tray_no_menu`: like system_tray, but no menu when right click icon.
- `system_tray_no_menu`: like system_tray, but no menu when right click icon.
- `activate_event`: example of how to handle click on dock icon.
65 changes: 65 additions & 0 deletions examples/activate_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use tao::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};

#[allow(clippy::single_match)]
fn main() {
let event_loop = EventLoop::new();

let window = Some(
WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(tao::dpi::LogicalSize::new(128.0, 128.0))
.build(&event_loop)
.unwrap(),
);

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
println!("{:?}", event);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id: _,
..
} => {
// drop the window to fire the `Destroyed` event
if let Some(window) = &window{
window.set_visible(false)
}
}
Event::WindowEvent {
event: WindowEvent::Destroyed,
window_id: _,
..
} => {
// *control_flow = ControlFlow::Exit;
}
Event::MainEventsCleared => {
if let Some(w) = &window {
w.request_redraw();
}
}
// handle click on dock icon
Event::ApplicationShouldHandleReopen { has_visible_windows, should_handle } => {
if !has_visible_windows {
if let Some(window) = &window{
window.set_visible(true)
}
} else {
if let Some(window) = &window{
window.set_visible(false)
}
}
*should_handle = false
}
_ => {}
}
});
}
21 changes: 21 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ pub enum Event<'a, T: 'static> {
position: PhysicalPosition<f64>,
},

/// Emitted when dock icon has been clicked.
///
/// ## Platform-specific
///
/// - **iOS / Android / Linux / Windows:** Unsupported.
ApplicationShouldHandleReopen {
has_visible_windows: bool,
should_handle: &'a mut bool,
},

/// Emitted when a global shortcut is triggered.
///
/// ## Platform-specific
Expand Down Expand Up @@ -203,6 +213,9 @@ impl<T: Clone> Clone for Event<'static, T> {
position: *position,
},
GlobalShortcutEvent(accelerator_id) => GlobalShortcutEvent(*accelerator_id),
ApplicationShouldHandleReopen { .. } => {
unreachable!("ApplicationShouldHandleReopen cannot clone")
},
}
}
}
Expand Down Expand Up @@ -240,6 +253,13 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Ok(GlobalShortcutEvent(accelerator_id)),
ApplicationShouldHandleReopen {
has_visible_windows,
should_handle
} => Ok(ApplicationShouldHandleReopen {
has_visible_windows,
should_handle,
}),
}
}

Expand Down Expand Up @@ -279,6 +299,7 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Some(GlobalShortcutEvent(accelerator_id)),
ApplicationShouldHandleReopen { .. } => None,
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};
use std::{
cell::{RefCell, RefMut},
os::raw::c_void,
};

use cocoa::base::id;
use objc::{
declare::ClassDecl,
runtime::{Class, Object, Sel},
};
use std::{
cell::{RefCell, RefMut},
os::raw::c_void,
runtime::{BOOL, Class, NO, Object, Sel, YES},
};

use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};

static AUX_DELEGATE_STATE_NAME: &str = "auxState";

pub struct AuxDelegateState {
Expand Down Expand Up @@ -44,6 +45,10 @@ lazy_static! {
sel!(applicationWillTerminate:),
application_will_terminate as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(applicationShouldHandleReopen:hasVisibleWindows:),
application_should_handle_reopen as extern "C" fn(&Object, Sel, id, BOOL) -> BOOL,
);
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);

AppDelegateClass(decl.register())
Expand Down Expand Up @@ -92,3 +97,11 @@ extern "C" fn application_will_terminate(_: &Object, _: Sel, _: id) {
AppState::exit();
trace!("Completed `applicationWillTerminate`");
}

extern "C" fn application_should_handle_reopen(_: &Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL {
trace!("Triggered `applicationShouldHandleReopen`");
let should_handle =
AppState::should_handle_reopen(has_visible_windows == YES);
trace!("Completed `applicationShouldHandleReopen`");
if should_handle { YES } else { NO }
}
17 changes: 17 additions & 0 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ impl Handler {
}
}

fn handle_nonuser_event_without_wrapper(&self, event: Event<'_, Never>) {
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
callback.handle_nonuser_event(event, &mut *self.control_flow.lock().unwrap())
}
}

fn handle_user_events(&self) {
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
callback.handle_user_events(&mut *self.control_flow.lock().unwrap());
Expand Down Expand Up @@ -297,6 +303,17 @@ impl AppState {
HANDLER.set_in_callback(false);
}

pub fn should_handle_reopen(has_visible_windows: bool) -> bool {
let mut should_handle = true;

HANDLER.handle_nonuser_event_without_wrapper(Event::ApplicationShouldHandleReopen {
has_visible_windows,
should_handle: &mut should_handle,
});

should_handle
}

pub fn wakeup(panic_info: Weak<PanicInfo>) {
let panic_info = panic_info
.upgrade()
Expand Down