window_focus.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Listen for window focus events using a wry event handler
  2. //!
  3. //! This example shows how to use the use_wry_event_handler hook to listen for window focus events.
  4. //! We can intercept any Wry event, but in this case we're only interested in the WindowEvent::Focused event.
  5. //!
  6. //! This lets you do things like backgrounding tasks, pausing animations, or changing the UI when the window is focused or not.
  7. use dioxus::desktop::tao::event::Event as WryEvent;
  8. use dioxus::desktop::tao::event::WindowEvent;
  9. use dioxus::desktop::use_wry_event_handler;
  10. use dioxus::prelude::*;
  11. fn main() {
  12. dioxus::launch(app);
  13. }
  14. fn app() -> Element {
  15. let mut focused = use_signal(|| true);
  16. use_wry_event_handler(move |event, _| {
  17. if let WryEvent::WindowEvent {
  18. event: WindowEvent::Focused(new_focused),
  19. ..
  20. } = event
  21. {
  22. focused.set(*new_focused)
  23. }
  24. });
  25. rsx! {
  26. div { width: "100%", height: "100%", display: "flex", flex_direction: "column", align_items: "center",
  27. if focused() {
  28. "This window is focused!"
  29. } else {
  30. "This window is not focused!"
  31. }
  32. }
  33. }
  34. }