window_focus.rs 974 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. Config::new()
  8. .with_close_behaviour(WindowCloseBehaviour::CloseWindow)
  9. .launch(app)
  10. }
  11. fn app() -> Element {
  12. let mut focused = use_signal(|| false);
  13. use_wry_event_handler(move |event, _| match event {
  14. WryEvent::WindowEvent {
  15. event: WindowEvent::Focused(new_focused),
  16. ..
  17. } => focused.set(*new_focused),
  18. _ => {}
  19. });
  20. rsx! {
  21. div {
  22. width: "100%",
  23. height: "100%",
  24. display: "flex",
  25. flex_direction: "column",
  26. align_items: "center",
  27. if focused() {
  28. "This window is focused!"
  29. } else {
  30. "This window is not focused!"
  31. }
  32. }
  33. }
  34. }