Skip to content

Commit eb0312e

Browse files
feat(mobile): Propagate tao::Event::Suspended and tao::Event::Resumed to the window (feat #15181) (#15199)
* wip * docs: adds documentation for forwarded tao events * docs: adds change log * feat: updates js api build --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 4ef5797 commit eb0312e

8 files changed

Lines changed: 144 additions & 55 deletions

File tree

.changes/mobile_tao_events.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'tauri-runtime-wry': 'minor:feat'
3+
'tauri-runtime': 'minor:feat'
4+
'tauri': 'minor:feat'
5+
'@tauri-apps/api': 'minor:feat'
6+
---
7+
8+
Propagates the `Event::Suspended` and `Event::Resumed` events from `tao` when they are emitted on mobile targets.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4405,6 +4405,31 @@ fn handle_event_loop<T: UserEvent>(
44054405
Event::SceneRequested { scene, options } => {
44064406
callback(RunEvent::SceneRequested { scene, options });
44074407
}
4408+
#[cfg(mobile)]
4409+
e @ Event::Resumed | e @ Event::Suspended => {
4410+
let event = match e {
4411+
Event::Resumed => WindowEvent::Resumed,
4412+
Event::Suspended => WindowEvent::Suspended,
4413+
_ => unreachable!(),
4414+
};
4415+
4416+
let windows_ref = windows.0.borrow();
4417+
windows_ref.values().for_each(|window| {
4418+
let label = window.label.clone();
4419+
let window_event_listeners = window.window_event_listeners.clone();
4420+
let listeners = window_event_listeners.lock().unwrap();
4421+
for handler in listeners.values() {
4422+
handler(&event);
4423+
}
4424+
4425+
callback(RunEvent::WindowEvent {
4426+
label,
4427+
event: event.clone(),
4428+
});
4429+
});
4430+
4431+
drop(windows_ref);
4432+
}
44084433
_ => (),
44094434
}
44104435
}

crates/tauri-runtime/src/window.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ pub enum WindowEvent {
6262
///
6363
/// Applications might wish to react to this to change the theme of the content of the window when the system changes the window theme.
6464
ThemeChanged(Theme),
65+
66+
/// Emitted when the application has been suspended.
67+
///
68+
/// ## Platform-specific
69+
///
70+
/// - **Android**: This is triggered by `onPause` method of the Activity.
71+
/// - **iOS**: This is triggered by `applicationWillResignActive` method of the UIApplicationDelegate.
72+
/// - **Linux / macOS / Windows**: Unsupported.
73+
#[cfg(mobile)]
74+
Suspended,
75+
76+
/// Emitted when the application has been resumed.
77+
///
78+
/// ## Platform-specific
79+
///
80+
/// - **Android**: This is triggered by `onResume` method of the Activity. The first onResume() is ignored to match the iOS implementation, since that is called on activity creation.
81+
/// - **iOS**: This is triggered by `applicationWillEnterForeground` method of the UIApplicationDelegate.
82+
/// - **Linux / macOS / Windows**: Unsupported.
83+
#[cfg(mobile)]
84+
Resumed,
6585
}
6686

6787
/// An event from a window.

crates/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/src/app.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ pub enum WindowEvent {
149149
///
150150
/// - **Linux**: Not supported.
151151
ThemeChanged(Theme),
152+
/// Emitted when the application has been suspended.
153+
///
154+
/// ## Platform-specific
155+
///
156+
/// - **Android**: This is triggered by `onPause` method of the Activity.
157+
/// - **iOS**: This is triggered by `applicationWillResignActive` method of the UIApplicationDelegate.
158+
/// - **Linux / macOS / Windows**: Unsupported.
159+
#[cfg(mobile)]
160+
Suspended,
161+
/// Emitted when the application has been resumed.
162+
///
163+
/// ## Platform-specific
164+
///
165+
/// - **Android**: This is triggered by `onResume` method of the Activity. The first onResume() is ignored to match the iOS implementation, since that is called on activity creation.
166+
/// - **iOS**: This is triggered by `applicationWillEnterForeground` method of the UIApplicationDelegate.
167+
/// - **Linux / macOS / Windows**: Unsupported.
168+
#[cfg(mobile)]
169+
Resumed,
152170
}
153171

154172
impl From<RuntimeWindowEvent> for WindowEvent {
@@ -170,6 +188,10 @@ impl From<RuntimeWindowEvent> for WindowEvent {
170188
},
171189
RuntimeWindowEvent::DragDrop(event) => Self::DragDrop(event),
172190
RuntimeWindowEvent::ThemeChanged(theme) => Self::ThemeChanged(theme),
191+
#[cfg(mobile)]
192+
RuntimeWindowEvent::Suspended => Self::Suspended,
193+
#[cfg(mobile)]
194+
RuntimeWindowEvent::Resumed => Self::Resumed,
173195
}
174196
}
175197
}

crates/tauri/src/manager/window.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ pub(crate) const DRAG_ENTER_EVENT: EventName<&str> = EventName::from_str("tauri:
3737
pub(crate) const DRAG_OVER_EVENT: EventName<&str> = EventName::from_str("tauri://drag-over");
3838
pub(crate) const DRAG_DROP_EVENT: EventName<&str> = EventName::from_str("tauri://drag-drop");
3939
pub(crate) const DRAG_LEAVE_EVENT: EventName<&str> = EventName::from_str("tauri://drag-leave");
40+
#[cfg(mobile)]
41+
pub(crate) const WINDOW_SUSPENDED_EVENT: EventName<&str> = EventName::from_str("tauri://suspended");
42+
#[cfg(mobile)]
43+
pub(crate) const WINDOW_RESUMED_EVENT: EventName<&str> = EventName::from_str("tauri://resumed");
4044

4145
pub struct WindowManager<R: Runtime> {
4246
pub windows: Mutex<HashMap<String, Window<R>>>,
@@ -265,6 +269,10 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
265269
_ => unimplemented!(),
266270
},
267271
WindowEvent::ThemeChanged(theme) => window.emit_to_window(WINDOW_THEME_CHANGED, &theme)?,
272+
#[cfg(mobile)]
273+
WindowEvent::Suspended => window.emit_to_window(WINDOW_SUSPENDED_EVENT, &())?,
274+
#[cfg(mobile)]
275+
WindowEvent::Resumed => window.emit_to_window(WINDOW_RESUMED_EVENT, &())?,
268276
}
269277
Ok(())
270278
}

packages/api/src/event.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ enum TauriEvent {
6565
WINDOW_SCALE_FACTOR_CHANGED = 'tauri://scale-change',
6666
WINDOW_THEME_CHANGED = 'tauri://theme-changed',
6767
WINDOW_CREATED = 'tauri://window-created',
68+
WINDOW_SUSPENDED = 'tauri://suspended',
69+
WINDOW_RESUMED = 'tauri://resumed',
6870
WEBVIEW_CREATED = 'tauri://webview-created',
6971
DRAG_ENTER = 'tauri://drag-enter',
7072
DRAG_OVER = 'tauri://drag-over',

0 commit comments

Comments
 (0)