dioxus/examples/window_focus.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! 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.
2024-01-20 00:36:40 +00:00
use dioxus::desktop::tao::event::Event as WryEvent;
use dioxus::desktop::tao::event::WindowEvent;
use dioxus::desktop::use_wry_event_handler;
2024-03-19 05:38:33 +00:00
use dioxus::desktop::{Config, WindowCloseBehaviour};
2023-01-13 00:48:23 +00:00
use dioxus::prelude::*;
fn main() {
LaunchBuilder::desktop()
2024-03-19 05:38:33 +00:00
.with_cfg(Config::new().with_close_behaviour(WindowCloseBehaviour::CloseWindow))
2024-01-18 20:32:01 +00:00
.launch(app)
2023-01-13 00:48:23 +00:00
}
fn app() -> Element {
2024-01-20 08:11:55 +00:00
let mut focused = use_signal(|| true);
2023-01-13 00:48:23 +00:00
2024-01-31 02:29:49 +00:00
use_wry_event_handler(move |event, _| {
if let WryEvent::WindowEvent {
2024-01-14 05:12:21 +00:00
event: WindowEvent::Focused(new_focused),
..
2024-01-31 02:29:49 +00:00
} = event
{
focused.set(*new_focused)
}
2023-01-13 00:48:23 +00:00
});
2024-01-16 19:18:46 +00:00
rsx! {
div { width: "100%", height: "100%", display: "flex", flex_direction: "column", align_items: "center",
if focused() {
2024-01-15 21:06:05 +00:00
"This window is focused!"
} else {
"This window is not focused!"
2023-01-13 00:48:23 +00:00
}
}
2024-01-14 05:12:21 +00:00
}
2023-01-13 00:48:23 +00:00
}