window_focus.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use dioxus::prelude::*;
  2. use dioxus_desktop::tao::event::Event as WryEvent;
  3. use dioxus_desktop::tao::event::WindowEvent;
  4. use dioxus_desktop::use_wry_event_handler;
  5. use dioxus_desktop::{Config, WindowCloseBehaviour};
  6. fn main() {
  7. let cfg = Config::new().with_close_behaviour(WindowCloseBehaviour::CloseWindow);
  8. dioxus_desktop::launch_cfg(app, cfg);
  9. }
  10. fn app() -> Element {
  11. let focused = use_signal(|| false);
  12. use_wry_event_handler(move |event, _| {
  13. if let WryEvent::WindowEvent {
  14. event: WindowEvent::Focused(new_focused),
  15. ..
  16. } = event
  17. {
  18. focused.set(*new_focused);
  19. }
  20. });
  21. rsx! {
  22. div{
  23. width: "100%",
  24. height: "100%",
  25. display: "flex",
  26. flex_direction: "column",
  27. align_items: "center",
  28. {
  29. if *focused.get() {
  30. "This window is focused!"
  31. } else {
  32. "This window is not focused!"
  33. }
  34. }
  35. }
  36. }
  37. }