-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathwindow_focus.rs
45 lines (40 loc) · 1.42 KB
/
window_focus.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! Listen for window focus events using a wry event handler
//!
//! This example shows how to use the use_wry_event_handler hook to listen for window focus events.
//! We can intercept any Wry event, but in this case we're only interested in the WindowEvent::Focused event.
//!
//! This lets you do things like backgrounding tasks, pausing animations, or changing the UI when the window is focused or not.
use dioxus::desktop::tao::event::Event as WryEvent;
use dioxus::desktop::tao::event::WindowEvent;
use dioxus::desktop::use_wry_event_handler;
use dioxus::desktop::{Config, DefaultWindowCloseBehaviour};
use dioxus::prelude::*;
fn main() {
dioxus::LaunchBuilder::desktop()
.with_cfg(
Config::new()
.with_default_window_close_behaviour(DefaultWindowCloseBehaviour::WindowsCloses),
)
.launch(app)
}
fn app() -> Element {
let mut focused = use_signal(|| true);
use_wry_event_handler(move |event, _| {
if let WryEvent::WindowEvent {
event: WindowEvent::Focused(new_focused),
..
} = event
{
focused.set(*new_focused)
}
});
rsx! {
div { width: "100%", height: "100%", display: "flex", flex_direction: "column", align_items: "center",
if focused() {
"This window is focused!"
} else {
"This window is not focused!"
}
}
}
}