1
0

window_focus.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. 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(cx: Scope) -> Element {
  11. let focused = use_state(cx, || false);
  12. use_wry_event_handler(cx, {
  13. to_owned![focused];
  14. move |event, _| {
  15. if let WryEvent::WindowEvent {
  16. event: WindowEvent::Focused(new_focused),
  17. ..
  18. } = event
  19. {
  20. focused.set(*new_focused);
  21. }
  22. }
  23. });
  24. cx.render(rsx! {
  25. div{
  26. width: "100%",
  27. height: "100%",
  28. display: "flex",
  29. flex_direction: "column",
  30. align_items: "center",
  31. {
  32. if *focused.get() {
  33. "This window is focused!"
  34. } else {
  35. "This window is not focused!"
  36. }
  37. }
  38. }
  39. })
  40. }