Skip to content

Commit 28c6b7a

Browse files
authored
feat: add Event::Ready (#2433)
1 parent 397710b commit 28c6b7a

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

.changes/tauri-ready-event.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": minor
3+
"tauri-runtime": minor
4+
"tauri-runtime-wry": minor
5+
---
6+
7+
Add `Event::Ready` on the `run()` callback. Triggered once when the event loop is ready.

core/tauri-runtime-wry/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use wry::{
4444
PhysicalPosition as WryPhysicalPosition, PhysicalSize as WryPhysicalSize,
4545
Position as WryPosition, Size as WrySize,
4646
},
47-
event::{Event, WindowEvent as WryWindowEvent},
47+
event::{Event, StartCause, WindowEvent as WryWindowEvent},
4848
event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
4949
global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
5050
menu::{
@@ -1781,6 +1781,10 @@ fn handle_event_loop(
17811781
*control_flow = ControlFlow::Wait;
17821782

17831783
match event {
1784+
Event::NewEvents(StartCause::Init) => {
1785+
callback(RunEvent::Ready);
1786+
}
1787+
17841788
Event::GlobalShortcutEvent(accelerator_id) => {
17851789
for (id, handler) in &*global_shortcut_manager_handle.listeners.lock().unwrap() {
17861790
if accelerator_id == *id {

core/tauri-runtime/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ pub enum RunEvent {
184184
},
185185
/// Window closed.
186186
WindowClose(String),
187+
/// Application ready.
188+
Ready,
187189
}
188190

189191
/// Action to take when the event loop is about to exit

core/tauri/src/app.rs

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ pub enum Event {
9595
},
9696
/// Window closed.
9797
WindowClosed(String),
98+
/// Application ready.
99+
Ready,
98100
}
99101

100102
/// A menu event that was triggered on a window.
@@ -438,6 +440,7 @@ impl<R: Runtime> App<R> {
438440
api: CloseRequestApi(signal_tx),
439441
},
440442
RunEvent::WindowClose(label) => Event::WindowClosed(label),
443+
RunEvent::Ready => Event::Ready,
441444
_ => unimplemented!(),
442445
},
443446
);

examples/api/src-tauri/src/main.rs

+38-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use std::path::PathBuf;
1515

1616
use serde::Serialize;
1717
use tauri::{
18-
api::dialog::ask, CustomMenuItem, Event, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu,
19-
WindowBuilder, WindowUrl,
18+
api::dialog::ask, async_runtime, CustomMenuItem, Event, GlobalShortcutManager, Manager,
19+
SystemTray, SystemTrayEvent, SystemTrayMenu, WindowBuilder, WindowUrl,
2020
};
2121

2222
#[derive(Serialize)]
@@ -55,7 +55,8 @@ fn main() {
5555
.add_item(CustomMenuItem::new("toggle", "Toggle"))
5656
.add_item(CustomMenuItem::new("new", "New window"))
5757
.add_item(CustomMenuItem::new("icon_1", "Tray Icon 1"))
58-
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2")),
58+
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2"))
59+
.add_item(CustomMenuItem::new("exit_app", "Quit")),
5960
),
6061
)
6162
.on_system_tray_event(|app, event| match event {
@@ -71,6 +72,10 @@ fn main() {
7172
SystemTrayEvent::MenuItemClick { id, .. } => {
7273
let item_handle = app.tray_handle().get_item(&id);
7374
match id.as_str() {
75+
"exit_app" => {
76+
// exit the app
77+
app.exit(0);
78+
}
7479
"toggle" => {
7580
let window = app.get_window("main").unwrap();
7681
let new_title = if window.is_visible().unwrap() {
@@ -157,15 +162,42 @@ fn main() {
157162
#[cfg(target_os = "macos")]
158163
app.set_activation_policy(tauri::ActivationPolicy::Regular);
159164

160-
app.run(|app_handle, e| {
161-
if let Event::CloseRequested { label, api, .. } = e {
162-
api.prevent_close();
165+
app.run(|app_handle, e| match e {
166+
// Application is ready (triggered only once)
167+
Event::Ready => {
163168
let app_handle = app_handle.clone();
169+
// launch a new thread so it doesnt block any channel
170+
async_runtime::spawn(async move {
171+
let app_handle = app_handle.clone();
172+
app_handle
173+
.global_shortcut_manager()
174+
.register("CmdOrCtrl+1", move || {
175+
let app_handle = app_handle.clone();
176+
let window = app_handle.get_window("main").unwrap();
177+
window.set_title("New title!").unwrap();
178+
})
179+
.unwrap();
180+
});
181+
}
182+
183+
// Triggered when a window is trying to close
184+
Event::CloseRequested { label, api, .. } => {
185+
let app_handle = app_handle.clone();
186+
// use the exposed close api, and prevent the event loop to close
187+
api.prevent_close();
188+
// ask the user if he wants to quit
164189
ask("Tauri API", "Are you sure?", move |answer| {
165190
if answer {
166191
app_handle.get_window(&label).unwrap().close().unwrap();
167192
}
168193
});
169194
}
195+
196+
// Keep the event loop running even if all windows are closed
197+
// This allow us to catch system tray events when there is no window
198+
Event::ExitRequested { api, .. } => {
199+
api.prevent_exit();
200+
}
201+
_ => {}
170202
})
171203
}

0 commit comments

Comments
 (0)