Skip to content

Commit edad9f4

Browse files
authored
refactor(core): add RunEvent::WindowEvent (#3793)
1 parent d5c06f0 commit edad9f4

File tree

9 files changed

+115
-42
lines changed

9 files changed

+115
-42
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": minor
3+
"tauri-runtime-wry": minor
4+
---
5+
6+
**Breaking change:** Use the dedicated `WindowEvent` enum on `RunEvent`.

.changes/refactor-window-event.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** Removed `RunEvent::CloseRequested` and `RunEvent::WindowClosed` and added `RunEvent::WindowEvent`.

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,9 +2458,16 @@ fn handle_event_loop<T: UserEvent>(
24582458

24592459
{
24602460
let windows_lock = windows.lock().expect("poisoned webview collection");
2461-
if let Some(window_handle) = windows_lock.get(&window_id).map(|w| &w.inner) {
2461+
if let Some((label, window_handle)) =
2462+
windows_lock.get(&window_id).map(|w| (&w.label, &w.inner))
2463+
{
24622464
if let Some(event) = WindowEventWrapper::parse(window_handle, &event).0 {
2465+
let label = label.clone();
24632466
drop(windows_lock);
2467+
callback(RunEvent::WindowEvent {
2468+
label,
2469+
event: event.clone(),
2470+
});
24642471
for handler in window_event_listeners
24652472
.lock()
24662473
.unwrap()
@@ -2519,7 +2526,6 @@ fn handle_event_loop<T: UserEvent>(
25192526
Event::UserEvent(message) => match message {
25202527
Message::Window(id, WindowMessage::Close) => {
25212528
on_window_close(
2522-
callback,
25232529
id,
25242530
windows.lock().expect("poisoned webview collection"),
25252531
menu_event_listeners.clone(),
@@ -2574,19 +2580,17 @@ fn on_close_requested<'a, T: UserEvent>(
25742580
.values()
25752581
{
25762582
handler(&WindowEvent::CloseRequested {
2577-
label: label.clone(),
25782583
signal_tx: tx.clone(),
25792584
});
25802585
}
2581-
callback(RunEvent::CloseRequested {
2586+
callback(RunEvent::WindowEvent {
25822587
label,
2583-
signal_tx: tx,
2588+
event: WindowEvent::CloseRequested { signal_tx: tx },
25842589
});
25852590
if let Ok(true) = rx.try_recv() {
25862591
None
25872592
} else {
25882593
on_window_close(
2589-
callback,
25902594
window_id,
25912595
windows.lock().expect("poisoned webview collection"),
25922596
menu_event_listeners,
@@ -2597,17 +2601,14 @@ fn on_close_requested<'a, T: UserEvent>(
25972601
}
25982602
}
25992603

2600-
fn on_window_close<'a, T: UserEvent>(
2601-
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
2604+
fn on_window_close(
26022605
window_id: WebviewId,
2603-
mut windows: MutexGuard<'a, HashMap<WebviewId, WindowWrapper>>,
2606+
mut windows: MutexGuard<'_, HashMap<WebviewId, WindowWrapper>>,
26042607
menu_event_listeners: MenuEventListeners,
26052608
) -> Option<WindowWrapper> {
26062609
#[allow(unused_mut)]
26072610
let w = if let Some(mut webview) = windows.remove(&window_id) {
2608-
drop(windows);
26092611
menu_event_listeners.lock().unwrap().remove(&window_id);
2610-
callback(RunEvent::WindowClose(webview.label.clone()));
26112612
Some(webview)
26122613
} else {
26132614
None

core/tauri-runtime/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,13 @@ pub enum RunEvent<T: UserEvent> {
208208
ExitRequested {
209209
tx: Sender<ExitRequestedEventAction>,
210210
},
211-
/// Window close was requested by the user.
212-
CloseRequested {
211+
/// An event associated with a window.
212+
WindowEvent {
213213
/// The window label.
214214
label: String,
215-
/// A signal sender. If a `true` value is emitted, the window won't be closed.
216-
signal_tx: Sender<bool>,
215+
/// The detailed event.
216+
event: WindowEvent,
217217
},
218-
/// Window closed.
219-
WindowClose(String),
220218
/// Application ready.
221219
Ready,
222220
/// Sent if the event loop is being resumed.

core/tauri-runtime/src/window.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ pub mod dpi;
2828

2929
/// An event from a window.
3030
#[derive(Debug, Clone)]
31-
#[non_exhaustive]
3231
pub enum WindowEvent {
3332
/// The size of the window has changed. Contains the client area's new dimensions.
3433
Resized(dpi::PhysicalSize<u32>),
3534
/// The position of the window has changed. Contains the window's new position.
3635
Moved(dpi::PhysicalPosition<i32>),
3736
/// The window has been requested to close.
3837
CloseRequested {
39-
/// The window label.
40-
label: String,
4138
/// A signal sender. If a `true` value is emitted, the window won't be closed.
4239
signal_tx: Sender<bool>,
4340
},

core/tauri/src/app.rs

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
runtime::{
1717
http::{Request as HttpRequest, Response as HttpResponse},
1818
webview::{WebviewAttributes, WindowBuilder as _},
19-
window::{PendingWindow, WindowEvent},
19+
window::{PendingWindow, WindowEvent as RuntimeWindowEvent},
2020
Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent,
2121
},
2222
scope::FsScope,
@@ -32,6 +32,10 @@ use crate::{
3232
use crate::scope::ShellScope;
3333

3434
use tauri_macros::default_runtime;
35+
use tauri_runtime::window::{
36+
dpi::{PhysicalPosition, PhysicalSize},
37+
FileDropEvent,
38+
};
3539
use tauri_utils::PackageInfo;
3640

3741
use std::{
@@ -69,7 +73,7 @@ impl ExitRequestApi {
6973
}
7074

7175
/// Api exposed on the `CloseRequested` event.
72-
#[derive(Debug)]
76+
#[derive(Debug, Clone)]
7377
pub struct CloseRequestApi(Sender<bool>);
7478

7579
impl CloseRequestApi {
@@ -79,6 +83,66 @@ impl CloseRequestApi {
7983
}
8084
}
8185

86+
/// An event from a window.
87+
#[derive(Debug, Clone)]
88+
#[non_exhaustive]
89+
pub enum WindowEvent {
90+
/// The size of the window has changed. Contains the client area's new dimensions.
91+
Resized(PhysicalSize<u32>),
92+
/// The position of the window has changed. Contains the window's new position.
93+
Moved(PhysicalPosition<i32>),
94+
/// The window has been requested to close.
95+
#[non_exhaustive]
96+
CloseRequested {
97+
/// An API modify the behavior of the close requested event.
98+
api: CloseRequestApi,
99+
},
100+
/// The window has been destroyed.
101+
Destroyed,
102+
/// The window gained or lost focus.
103+
///
104+
/// The parameter is true if the window has gained focus, and false if it has lost focus.
105+
Focused(bool),
106+
/// The window's scale factor has changed.
107+
///
108+
/// The following user actions can cause DPI changes:
109+
///
110+
/// - Changing the display's resolution.
111+
/// - Changing the display's scale factor (e.g. in Control Panel on Windows).
112+
/// - Moving the window to a display with a different scale factor.
113+
#[non_exhaustive]
114+
ScaleFactorChanged {
115+
/// The new scale factor.
116+
scale_factor: f64,
117+
/// The window inner size.
118+
new_inner_size: PhysicalSize<u32>,
119+
},
120+
/// An event associated with the file drop action.
121+
FileDrop(FileDropEvent),
122+
}
123+
124+
impl From<RuntimeWindowEvent> for WindowEvent {
125+
fn from(event: RuntimeWindowEvent) -> Self {
126+
match event {
127+
RuntimeWindowEvent::Resized(size) => Self::Resized(size),
128+
RuntimeWindowEvent::Moved(position) => Self::Moved(position),
129+
RuntimeWindowEvent::CloseRequested { signal_tx } => Self::CloseRequested {
130+
api: CloseRequestApi(signal_tx),
131+
},
132+
RuntimeWindowEvent::Destroyed => Self::Destroyed,
133+
RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
134+
RuntimeWindowEvent::ScaleFactorChanged {
135+
scale_factor,
136+
new_inner_size,
137+
} => Self::ScaleFactorChanged {
138+
scale_factor,
139+
new_inner_size,
140+
},
141+
RuntimeWindowEvent::FileDrop(event) => Self::FileDrop(event),
142+
}
143+
}
144+
}
145+
82146
/// An application event, triggered from the event loop.
83147
#[derive(Debug)]
84148
#[non_exhaustive]
@@ -91,16 +155,14 @@ pub enum RunEvent {
91155
/// Event API
92156
api: ExitRequestApi,
93157
},
94-
/// Window close was requested by the user.
158+
/// An event associated with a window.
95159
#[non_exhaustive]
96-
CloseRequested {
160+
WindowEvent {
97161
/// The window label.
98162
label: String,
99-
/// Event API.
100-
api: CloseRequestApi,
163+
/// The detailed event.
164+
event: WindowEvent,
101165
},
102-
/// Window closed.
103-
WindowClosed(String),
104166
/// Application ready.
105167
Ready,
106168
/// Sent if the event loop is being resumed.
@@ -1428,7 +1490,11 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
14281490
manager: &WindowManager<R>,
14291491
callback: Option<&mut F>,
14301492
) {
1431-
if let RuntimeRunEvent::WindowClose(label) = &event {
1493+
if let RuntimeRunEvent::WindowEvent {
1494+
label,
1495+
event: RuntimeWindowEvent::Destroyed,
1496+
} = &event
1497+
{
14321498
manager.on_window_close(label);
14331499
}
14341500

@@ -1437,11 +1503,10 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
14371503
RuntimeRunEvent::ExitRequested { tx } => RunEvent::ExitRequested {
14381504
api: ExitRequestApi(tx),
14391505
},
1440-
RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {
1506+
RuntimeRunEvent::WindowEvent { label, event } => RunEvent::WindowEvent {
14411507
label,
1442-
api: CloseRequestApi(signal_tx),
1508+
event: event.into(),
14431509
},
1444-
RuntimeRunEvent::WindowClose(label) => RunEvent::WindowClosed(label),
14451510
RuntimeRunEvent::Ready => RunEvent::Ready,
14461511
RuntimeRunEvent::Resumed => RunEvent::Resumed,
14471512
RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,

core/tauri/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub use {
203203
pub use {
204204
self::app::{
205205
App, AppHandle, AssetResolver, Builder, CloseRequestApi, GlobalWindowEvent, PathResolver,
206-
RunEvent,
206+
RunEvent, WindowEvent,
207207
},
208208
self::hooks::{
209209
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokePayload, InvokeResolver,
@@ -214,7 +214,7 @@ pub use {
214214
webview::{WebviewAttributes, WindowBuilder},
215215
window::{
216216
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Pixel, Position, Size},
217-
FileDropEvent, WindowEvent,
217+
FileDropEvent,
218218
},
219219
ClipboardManager, GlobalShortcutManager, RunIteration, TrayIcon, UserAttentionType,
220220
},

core/tauri/src/manager.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ impl<R: Runtime> WindowManager<R> {
11471147
for handler in window_event_listeners.iter() {
11481148
handler(GlobalWindowEvent {
11491149
window: window_.clone(),
1150-
event: event.clone(),
1150+
event: event.clone().into(),
11511151
});
11521152
}
11531153
});
@@ -1274,10 +1274,7 @@ fn on_window_event<R: Runtime>(
12741274
match event {
12751275
WindowEvent::Resized(size) => window.emit(WINDOW_RESIZED_EVENT, size)?,
12761276
WindowEvent::Moved(position) => window.emit(WINDOW_MOVED_EVENT, position)?,
1277-
WindowEvent::CloseRequested {
1278-
label: _,
1279-
signal_tx,
1280-
} => {
1277+
WindowEvent::CloseRequested { signal_tx } => {
12811278
if window.has_js_listener(Some(window.label().into()), WINDOW_CLOSE_REQUESTED_EVENT) {
12821279
signal_tx.send(true).unwrap();
12831280
}
@@ -1328,7 +1325,6 @@ fn on_window_event<R: Runtime>(
13281325
FileDropEvent::Cancelled => window.emit("tauri://file-drop-cancelled", ())?,
13291326
_ => unimplemented!(),
13301327
},
1331-
_ => unimplemented!(),
13321328
}
13331329
Ok(())
13341330
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
1717
use serde::{Deserialize, Serialize};
1818
use tauri::{
1919
api::dialog::ask, http::ResponseBuilder, window::WindowBuilder, CustomMenuItem,
20-
GlobalShortcutManager, Manager, RunEvent, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowUrl,
20+
GlobalShortcutManager, Manager, RunEvent, SystemTray, SystemTrayEvent, SystemTrayMenu,
21+
WindowEvent, WindowUrl,
2122
};
2223

2324
#[derive(Clone, Serialize)]
@@ -227,7 +228,11 @@ fn main() {
227228
}
228229

229230
// Triggered when a window is trying to close
230-
RunEvent::CloseRequested { label, api, .. } => {
231+
RunEvent::WindowEvent {
232+
label,
233+
event: WindowEvent::CloseRequested { api, .. },
234+
..
235+
} => {
231236
let app_handle = app_handle.clone();
232237
let window = app_handle.get_window(&label).unwrap();
233238
// use the exposed close api, and prevent the event loop to close

0 commit comments

Comments
 (0)