ipc.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use serde::{Deserialize, Serialize};
  2. use tao::window::WindowId;
  3. #[non_exhaustive]
  4. #[derive(Debug, Clone)]
  5. pub enum UserWindowEvent {
  6. /// A global hotkey event
  7. #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
  8. GlobalHotKeyEvent(global_hotkey::GlobalHotKeyEvent),
  9. #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
  10. MudaMenuEvent(muda::MenuEvent),
  11. #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
  12. TrayIconEvent(tray_icon::TrayIconEvent),
  13. #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
  14. TrayMenuEvent(tray_icon::menu::MenuEvent),
  15. /// Poll the virtualdom
  16. Poll(WindowId),
  17. /// Handle an ipc message eminating from the window.postMessage of a given webview
  18. Ipc {
  19. id: WindowId,
  20. msg: IpcMessage,
  21. },
  22. /// Handle a hotreload event, basically telling us to update our templates
  23. #[cfg(all(feature = "devtools", debug_assertions))]
  24. HotReloadEvent(dioxus_devtools::DevserverMsg),
  25. // Windows-only drag-n-drop fix events.
  26. WindowsDragDrop(WindowId),
  27. WindowsDragOver(WindowId, i32, i32),
  28. WindowsDragLeave(WindowId),
  29. /// Create a new window
  30. NewWindow,
  31. /// Close a given window (could be any window!)
  32. CloseWindow(WindowId),
  33. /// Gracefully shutdown the entire app
  34. Shutdown,
  35. }
  36. /// A message struct that manages the communication between the webview and the eventloop code
  37. ///
  38. /// This needs to be serializable across the JS boundary, so the method names and structs are sensitive.
  39. #[derive(Deserialize, Serialize, Debug, Clone)]
  40. pub struct IpcMessage {
  41. method: String,
  42. params: serde_json::Value,
  43. }
  44. /// A set of known messages that we need to respond to
  45. #[derive(Deserialize, Serialize, Debug, Clone)]
  46. pub enum IpcMethod<'a> {
  47. FileDialog,
  48. UserEvent,
  49. Query,
  50. BrowserOpen,
  51. Initialize,
  52. Other(&'a str),
  53. }
  54. impl IpcMessage {
  55. pub(crate) fn method(&self) -> IpcMethod {
  56. match self.method.as_str() {
  57. "file_dialog" => IpcMethod::FileDialog,
  58. "user_event" => IpcMethod::UserEvent,
  59. "query" => IpcMethod::Query,
  60. "browser_open" => IpcMethod::BrowserOpen,
  61. "initialize" => IpcMethod::Initialize,
  62. _ => IpcMethod::Other(&self.method),
  63. }
  64. }
  65. pub(crate) fn params(self) -> serde_json::Value {
  66. self.params
  67. }
  68. }