window_focus.rs 948 B

123456789101112131415161718192021222324252627282930313233
  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. LaunchBuilder::new(app)
  8. .cfg(Config::new().with_close_behaviour(WindowCloseBehaviour::CloseWindow))
  9. .launch_desktop()
  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 { width: "100%", height: "100%", display: "flex", flex_direction: "column", align_items: "center",
  22. if focused() {
  23. "This window is focused!"
  24. } else {
  25. "This window is not focused!"
  26. }
  27. }
  28. }
  29. }