|
@@ -6,23 +6,34 @@ use serde::Serialize;
|
|
|
|
|
|
/// The webview dispatcher.
|
|
/// The webview dispatcher.
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
-pub struct WebviewDispatcher<A: Clone>(A);
|
|
|
|
|
|
+pub struct WebviewDispatcher<A: Clone> {
|
|
|
|
+ dispatcher: A,
|
|
|
|
+ window_label: String,
|
|
|
|
+}
|
|
|
|
|
|
impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
- pub(crate) fn new(dispatcher: A) -> Self {
|
|
|
|
- Self(dispatcher)
|
|
|
|
|
|
+ pub(crate) fn new(dispatcher: A, window_label: String) -> Self {
|
|
|
|
+ Self {
|
|
|
|
+ dispatcher,
|
|
|
|
+ window_label,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// The label of the window tied to this dispatcher.
|
|
|
|
+ pub fn window_label(&self) -> &str {
|
|
|
|
+ &self.window_label
|
|
}
|
|
}
|
|
|
|
|
|
- /// Listen to an event.
|
|
|
|
|
|
+ /// Listen to a webview event.
|
|
pub fn listen<F: FnMut(Option<String>) + Send + 'static>(
|
|
pub fn listen<F: FnMut(Option<String>) + Send + 'static>(
|
|
&self,
|
|
&self,
|
|
event: impl AsRef<str>,
|
|
event: impl AsRef<str>,
|
|
handler: F,
|
|
handler: F,
|
|
) {
|
|
) {
|
|
- super::event::listen(event, handler)
|
|
|
|
|
|
+ super::event::listen(event, Some(self.window_label.to_string()), handler)
|
|
}
|
|
}
|
|
|
|
|
|
- /// Emits an event.
|
|
|
|
|
|
+ /// Emits an event to the webview.
|
|
pub fn emit<S: Serialize>(
|
|
pub fn emit<S: Serialize>(
|
|
&self,
|
|
&self,
|
|
event: impl AsRef<str>,
|
|
event: impl AsRef<str>,
|
|
@@ -31,83 +42,100 @@ impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
super::event::emit(&self, event, payload)
|
|
super::event::emit(&self, event, payload)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Emits an event from the webview.
|
|
pub(crate) fn on_event(&self, event: String, data: Option<String>) {
|
|
pub(crate) fn on_event(&self, event: String, data: Option<String>) {
|
|
- super::event::on_event(event, data)
|
|
|
|
|
|
+ super::event::on_event(event, Some(&self.window_label), data)
|
|
}
|
|
}
|
|
|
|
|
|
/// Evaluates a JS script.
|
|
/// Evaluates a JS script.
|
|
pub fn eval(&self, js: &str) {
|
|
pub fn eval(&self, js: &str) {
|
|
- self.0.send_message(Message::EvalScript(js.to_string()))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::EvalScript(js.to_string()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Updates the window resizable flag.
|
|
/// Updates the window resizable flag.
|
|
pub fn set_resizable(&self, resizable: bool) {
|
|
pub fn set_resizable(&self, resizable: bool) {
|
|
- self.0.send_message(Message::SetResizable(resizable))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetResizable(resizable))
|
|
}
|
|
}
|
|
|
|
|
|
/// Updates the window title.
|
|
/// Updates the window title.
|
|
pub fn set_title(&self, title: &str) {
|
|
pub fn set_title(&self, title: &str) {
|
|
- self.0.send_message(Message::SetTitle(title.to_string()))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetTitle(title.to_string()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Maximizes the window.
|
|
/// Maximizes the window.
|
|
pub fn maximize(&self) {
|
|
pub fn maximize(&self) {
|
|
- self.0.send_message(Message::Maximize)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Maximize)
|
|
}
|
|
}
|
|
|
|
|
|
/// Unmaximizes the window.
|
|
/// Unmaximizes the window.
|
|
pub fn unmaximize(&self) {
|
|
pub fn unmaximize(&self) {
|
|
- self.0.send_message(Message::Unmaximize)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Unmaximize)
|
|
}
|
|
}
|
|
|
|
|
|
/// Minimizes the window.
|
|
/// Minimizes the window.
|
|
pub fn minimize(&self) {
|
|
pub fn minimize(&self) {
|
|
- self.0.send_message(Message::Minimize)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Minimize)
|
|
}
|
|
}
|
|
|
|
|
|
/// Unminimizes the window.
|
|
/// Unminimizes the window.
|
|
pub fn unminimize(&self) {
|
|
pub fn unminimize(&self) {
|
|
- self.0.send_message(Message::Unminimize)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Unminimize)
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window visibility to true.
|
|
/// Sets the window visibility to true.
|
|
pub fn show(&self) {
|
|
pub fn show(&self) {
|
|
- self.0.send_message(Message::Show)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Show)
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window visibility to false.
|
|
/// Sets the window visibility to false.
|
|
pub fn hide(&self) {
|
|
pub fn hide(&self) {
|
|
- self.0.send_message(Message::Hide)
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Hide)
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window transparent flag.
|
|
/// Sets the window transparent flag.
|
|
pub fn set_transparent(&self, transparent: bool) {
|
|
pub fn set_transparent(&self, transparent: bool) {
|
|
- self.0.send_message(Message::SetTransparent(transparent))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetTransparent(transparent))
|
|
}
|
|
}
|
|
|
|
|
|
/// Whether the window should have borders and bars.
|
|
/// Whether the window should have borders and bars.
|
|
pub fn set_decorations(&self, decorations: bool) {
|
|
pub fn set_decorations(&self, decorations: bool) {
|
|
- self.0.send_message(Message::SetDecorations(decorations))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetDecorations(decorations))
|
|
}
|
|
}
|
|
|
|
|
|
/// Whether the window should always be on top of other windows.
|
|
/// Whether the window should always be on top of other windows.
|
|
pub fn set_always_on_top(&self, always_on_top: bool) {
|
|
pub fn set_always_on_top(&self, always_on_top: bool) {
|
|
- self.0.send_message(Message::SetAlwaysOnTop(always_on_top))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetAlwaysOnTop(always_on_top))
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window width.
|
|
/// Sets the window width.
|
|
pub fn set_width(&self, width: impl Into<f64>) {
|
|
pub fn set_width(&self, width: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetWidth(width.into()))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetWidth(width.into()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window height.
|
|
/// Sets the window height.
|
|
pub fn set_height(&self, height: impl Into<f64>) {
|
|
pub fn set_height(&self, height: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetHeight(height.into()))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetHeight(height.into()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Resizes the window.
|
|
/// Resizes the window.
|
|
pub fn resize(&self, width: impl Into<f64>, height: impl Into<f64>) {
|
|
pub fn resize(&self, width: impl Into<f64>, height: impl Into<f64>) {
|
|
- self.0.send_message(Message::Resize {
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::Resize {
|
|
width: width.into(),
|
|
width: width.into(),
|
|
height: height.into(),
|
|
height: height.into(),
|
|
})
|
|
})
|
|
@@ -115,7 +143,7 @@ impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
|
|
|
|
/// Sets the window min size.
|
|
/// Sets the window min size.
|
|
pub fn set_min_size(&self, min_width: impl Into<f64>, min_height: impl Into<f64>) {
|
|
pub fn set_min_size(&self, min_width: impl Into<f64>, min_height: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetMinSize {
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetMinSize {
|
|
min_width: min_width.into(),
|
|
min_width: min_width.into(),
|
|
min_height: min_height.into(),
|
|
min_height: min_height.into(),
|
|
})
|
|
})
|
|
@@ -123,7 +151,7 @@ impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
|
|
|
|
/// Sets the window max size.
|
|
/// Sets the window max size.
|
|
pub fn set_max_size(&self, max_width: impl Into<f64>, max_height: impl Into<f64>) {
|
|
pub fn set_max_size(&self, max_width: impl Into<f64>, max_height: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetMaxSize {
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetMaxSize {
|
|
max_width: max_width.into(),
|
|
max_width: max_width.into(),
|
|
max_height: max_height.into(),
|
|
max_height: max_height.into(),
|
|
})
|
|
})
|
|
@@ -131,17 +159,17 @@ impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
|
|
|
|
/// Sets the window x position.
|
|
/// Sets the window x position.
|
|
pub fn set_x(&self, x: impl Into<f64>) {
|
|
pub fn set_x(&self, x: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetX(x.into()))
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetX(x.into()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window y position.
|
|
/// Sets the window y position.
|
|
pub fn set_y(&self, y: impl Into<f64>) {
|
|
pub fn set_y(&self, y: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetY(y.into()))
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetY(y.into()))
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window position.
|
|
/// Sets the window position.
|
|
pub fn set_position(&self, x: impl Into<f64>, y: impl Into<f64>) {
|
|
pub fn set_position(&self, x: impl Into<f64>, y: impl Into<f64>) {
|
|
- self.0.send_message(Message::SetPosition {
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetPosition {
|
|
x: x.into(),
|
|
x: x.into(),
|
|
y: y.into(),
|
|
y: y.into(),
|
|
})
|
|
})
|
|
@@ -149,12 +177,14 @@ impl<A: ApplicationDispatcherExt> WebviewDispatcher<A> {
|
|
|
|
|
|
/// Sets the window fullscreen state.
|
|
/// Sets the window fullscreen state.
|
|
pub fn set_fullscreen(&self, fullscreen: bool) {
|
|
pub fn set_fullscreen(&self, fullscreen: bool) {
|
|
- self.0.send_message(Message::SetFullscreen(fullscreen))
|
|
|
|
|
|
+ self
|
|
|
|
+ .dispatcher
|
|
|
|
+ .send_message(Message::SetFullscreen(fullscreen))
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the window icon.
|
|
/// Sets the window icon.
|
|
pub fn set_icon(&self, icon: Icon) {
|
|
pub fn set_icon(&self, icon: Icon) {
|
|
- self.0.send_message(Message::SetIcon(icon))
|
|
|
|
|
|
+ self.dispatcher.send_message(Message::SetIcon(icon))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -173,6 +203,11 @@ impl<A: ApplicationDispatcherExt> WebviewManager<A> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Gets the map of the current webviews.
|
|
|
|
+ pub fn webviews(&self) -> &HashMap<String, WebviewDispatcher<A>> {
|
|
|
|
+ &self.dispatchers
|
|
|
|
+ }
|
|
|
|
+
|
|
/// Returns the label of the window associated with the current context.
|
|
/// Returns the label of the window associated with the current context.
|
|
pub fn current_window_label(&self) -> &str {
|
|
pub fn current_window_label(&self) -> &str {
|
|
&self.current_webview_window_label
|
|
&self.current_webview_window_label
|
|
@@ -190,4 +225,31 @@ impl<A: ApplicationDispatcherExt> WebviewManager<A> {
|
|
.get(window_label)
|
|
.get(window_label)
|
|
.ok_or(crate::Error::WebviewNotFound)
|
|
.ok_or(crate::Error::WebviewNotFound)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /// Listen to a global event.
|
|
|
|
+ /// An event from any webview will trigger the handler.
|
|
|
|
+ pub fn listen<F: FnMut(Option<String>) + Send + 'static>(
|
|
|
|
+ &self,
|
|
|
|
+ event: impl AsRef<str>,
|
|
|
|
+ handler: F,
|
|
|
|
+ ) {
|
|
|
|
+ super::event::listen(event, None, handler)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Emits an event to all webviews.
|
|
|
|
+ pub fn emit<S: Serialize + Clone>(
|
|
|
|
+ &self,
|
|
|
|
+ event: impl AsRef<str>,
|
|
|
|
+ payload: Option<S>,
|
|
|
|
+ ) -> crate::Result<()> {
|
|
|
|
+ for dispatcher in self.dispatchers.values() {
|
|
|
|
+ super::event::emit(&dispatcher, event.as_ref(), payload.clone())?;
|
|
|
|
+ }
|
|
|
|
+ Ok(())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Emits a global event from the webview.
|
|
|
|
+ pub(crate) fn on_event(&self, event: String, data: Option<String>) {
|
|
|
|
+ super::event::on_event(event, None, data)
|
|
|
|
+ }
|
|
}
|
|
}
|