window_focus.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use dioxus::prelude::*;
  2. use dioxus_desktop::tao::event::WindowEvent;
  3. use dioxus_desktop::use_wry_event_handler;
  4. use dioxus_desktop::wry::application::event::Event as WryEvent;
  5. fn main() {
  6. dioxus_desktop::launch(app);
  7. }
  8. fn app(cx: Scope) -> Element {
  9. let focused = use_state(cx, || false);
  10. use_wry_event_handler(cx, {
  11. to_owned![focused];
  12. 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. });
  22. cx.render(rsx! {
  23. div{
  24. width: "100%",
  25. height: "100%",
  26. display: "flex",
  27. flex_direction: "column",
  28. align_items: "center",
  29. {
  30. if *focused.get() {
  31. "This window is focused!"
  32. } else {
  33. "This window is not focused!"
  34. }
  35. }
  36. }
  37. })
  38. }