window_focus.rs 967 B

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