dioxus/packages/desktop/src/ipc.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
/// A message struct that manages the communication between the webview and the eventloop code
///
/// This needs to be serializable across the JS boundary, so the method names and structs are sensitive.
2022-12-31 17:19:21 +00:00
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct IpcMessage {
2022-02-13 18:56:11 +00:00
method: String,
params: serde_json::Value,
}
/// A set of known messages that we need to respond to
2024-01-04 23:21:30 +00:00
#[derive(Deserialize, Serialize, Debug, Clone)]
2024-01-05 00:48:53 +00:00
pub enum IpcMethod<'a> {
2024-01-04 23:21:30 +00:00
FileDialog,
UserEvent,
Query,
BrowserOpen,
Initialize,
Other(&'a str),
}
2022-02-13 18:56:11 +00:00
impl IpcMessage {
2024-01-05 00:48:53 +00:00
pub(crate) fn method(&self) -> IpcMethod {
2024-01-04 23:21:30 +00:00
match self.method.as_str() {
// todo: this is a misspelling, needs to be fixed
2024-01-05 00:48:53 +00:00
"file_diolog" => IpcMethod::FileDialog,
"user_event" => IpcMethod::UserEvent,
"query" => IpcMethod::Query,
"browser_open" => IpcMethod::BrowserOpen,
"initialize" => IpcMethod::Initialize,
_ => IpcMethod::Other(&self.method),
2024-01-04 23:21:30 +00:00
}
2022-02-13 18:56:11 +00:00
}
pub(crate) fn params(self) -> serde_json::Value {
self.params
}
}