desktop_context.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use wry::application::event_loop::EventLoopProxy;
  2. use crate::UserWindowEvent;
  3. type ProxyType = EventLoopProxy<UserWindowEvent>;
  4. /// Desktop-Window handle api context
  5. ///
  6. /// you can use this context control some window event
  7. ///
  8. /// you can use `cx.consume_context::<DesktopContext>` to get this context
  9. ///
  10. /// ```rust
  11. /// let desktop = cx.consume_context::<DesktopContext>().unwrap();
  12. /// ```
  13. #[derive(Clone)]
  14. pub struct DesktopContext {
  15. proxy: ProxyType,
  16. }
  17. impl DesktopContext {
  18. pub(crate) fn new(proxy: ProxyType) -> Self {
  19. Self { proxy }
  20. }
  21. /// trigger the drag-window event
  22. ///
  23. /// Moves the window with the left mouse button until the button is released.
  24. ///
  25. /// you need use it in `onmousedown` event:
  26. /// ```rust
  27. /// onmousedown: move |_| { desktop.drag_window(); }
  28. /// ```
  29. pub fn drag_window(&self) {
  30. let _ = self.proxy.send_event(UserWindowEvent::DragWindow);
  31. }
  32. /// set window minimize state
  33. pub fn minimize(&self, minimized: bool) {
  34. let _ = self.proxy.send_event(UserWindowEvent::Minimize(minimized));
  35. }
  36. /// set window maximize state
  37. pub fn maximize(&self, maximized: bool) {
  38. let _ = self.proxy.send_event(UserWindowEvent::Maximize(maximized));
  39. }
  40. /// close window
  41. pub fn close(&self) {
  42. let _ = self.proxy.send_event(UserWindowEvent::CloseWindow);
  43. }
  44. }