ipc.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /// Poll the virtualdom
  12. Poll(WindowId),
  13. /// Handle an ipc message eminating from the window.postMessage of a given webview
  14. Ipc { id: WindowId, msg: IpcMessage },
  15. /// Handle a hotreload event, basically telling us to update our templates
  16. #[cfg(all(feature = "devtools", debug_assertions))]
  17. HotReloadEvent(dioxus_devtools::DevserverMsg),
  18. /// Create a new window
  19. NewWindow,
  20. /// Close a given window (could be any window!)
  21. CloseWindow(WindowId),
  22. /// Gracefully shutdown the entire app
  23. Shutdown,
  24. }
  25. /// A message struct that manages the communication between the webview and the eventloop code
  26. ///
  27. /// This needs to be serializable across the JS boundary, so the method names and structs are sensitive.
  28. #[derive(Deserialize, Serialize, Debug, Clone)]
  29. pub struct IpcMessage {
  30. method: String,
  31. params: serde_json::Value,
  32. }
  33. /// A set of known messages that we need to respond to
  34. #[derive(Deserialize, Serialize, Debug, Clone)]
  35. pub enum IpcMethod<'a> {
  36. FileDialog,
  37. UserEvent,
  38. Query,
  39. BrowserOpen,
  40. Initialize,
  41. Other(&'a str),
  42. }
  43. impl IpcMessage {
  44. pub(crate) fn method(&self) -> IpcMethod {
  45. match self.method.as_str() {
  46. "file_dialog" => IpcMethod::FileDialog,
  47. "user_event" => IpcMethod::UserEvent,
  48. "query" => IpcMethod::Query,
  49. "browser_open" => IpcMethod::BrowserOpen,
  50. "initialize" => IpcMethod::Initialize,
  51. _ => IpcMethod::Other(&self.method),
  52. }
  53. }
  54. pub(crate) fn params(self) -> serde_json::Value {
  55. self.params
  56. }
  57. }