Merge pull request #318 from overlisted/eval-stuff

Eval stuff
This commit is contained in:
Jon Kelley authored and GitHub committed 2022-04-04 13:01:35 -04:00
commit 56ea9e42fa
7 files changed
+83 -4

No files matched your search

+18
View File
@@ -119,6 +119,11 @@ impl DesktopContext {
pub fn devtool(&self) {
let _ = self.proxy.send_event(DevTool);
}
/// run (evaluate) a script in the WebView context
pub fn eval(&self, script: impl std::string::ToString) {
let _ = self.proxy.send_event(Eval(script.to_string()));
}
}
#[derive(Debug)]
@@ -144,6 +149,8 @@ pub enum UserWindowEvent {
SetDecorations(bool),
DevTool,
Eval(String),
}
pub(super) fn handler(
@@ -185,5 +192,16 @@ pub(super) fn handler(
SetDecorations(state) => window.set_decorations(state),
DevTool => webview.devtool(),
Eval(code) => webview
.evaluate_script(code.as_str())
.expect("eval shouldn't panic"),
}
}
/// Get a closure that executes any JavaScript in the WebView context.
pub fn use_eval<S: std::string::ToString>(cx: &ScopeState) -> &dyn Fn(S) {
let desktop = use_window(&cx).clone();
cx.use_hook(|_| move |script| desktop.eval(script))
}
+1 -1
View File
@@ -11,7 +11,7 @@ mod events;
mod protocol;
use desktop_context::UserWindowEvent;
pub use desktop_context::{use_window, DesktopContext};
pub use desktop_context::{use_eval, use_window, DesktopContext};
pub use wry;
pub use wry::application as tao;