dioxus/examples/window_focus.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

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