pub struct UI { /* private fields */ }
Expand description
The browser window
Implementations
sourceimpl UI
impl UI
sourcepub fn wait_finish(&self)
pub fn wait_finish(&self)
Wait for the browser to be closed
sourcepub fn load(&self, content: Content<'_>) -> Result<(), JSError>
pub fn load(&self, content: Content<'_>) -> Result<(), JSError>
Load content in the browser. It returns Err if it fails.
sourcepub fn bind<F>(&self, name: &str, f: F) -> Result<(), JSError> where
F: Fn(&[JSObject]) -> JSResult + Sync + Send + 'static,
pub fn bind<F>(&self, name: &str, f: F) -> Result<(), JSError> where
F: Fn(&[JSObject]) -> JSResult + Sync + Send + 'static,
Bind a rust function so that JS code can use it. It returns Err if it fails. The rust function will be executed in a new thread and can be called asynchronously from Javascript
Arguments
name
- Name of the functionf
- The function. It should take a list ofJSObject
as argument and return aJSResult
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
use serde_json::to_value;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.bind("add",|args| {
let mut sum = 0;
for arg in args {
match arg.as_i64() {
Some(i) => sum+=i,
None => return Err(to_value("Not number").unwrap())
}
}
Ok(to_value(sum).unwrap())
}).expect("Unable to bind function");
assert_eq!(ui.eval("(async () => await add(1,2,3))();").unwrap(), 6);
assert!(ui.eval("(async () => await add(1,2,'hi'))();").is_err());
sourcepub fn bind_async<F>(&self, name: &str, f: F) -> Result<(), JSError> where
F: Fn(BindingContext) + Sync + Send + 'static,
pub fn bind_async<F>(&self, name: &str, f: F) -> Result<(), JSError> where
F: Fn(BindingContext) + Sync + Send + 'static,
Bind a rust function callable from JS that can complete asynchronously. If you are using
[tokio
], you will probably want to be using [Self::bind_tokio()
] instead.
Unlike bind()
, this passes ownership of the arguments to the callback function f
, and
allows completing the javascript implementation after returning from f
. This makes async
behavior much simpler to implement.
For efficency, f
will be executed in the message processing loop, and therefore should
avoid blocking by moving work onto another thread, for example with an async runtime
spawn method.
Arguments
name
- Name of the functionf
- The function. It should take aBindingContext
that gives access to the arguments and allows returning results.
Examples
bind()
approximately performs the following:
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
use serde_json::to_value;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.bind_async("add", |context| {
std::thread::spawn(|| {
// imagine this is very expensive, or hits a network...
let mut sum = 0;
for arg in context.args() {
match arg.as_i64() {
Some(i) => sum+=i,
None => return context.err(to_value("Not number").unwrap())
}
}
context.complete(Ok(to_value(sum).unwrap()));
});
}).expect("Unable to bind function");
assert_eq!(ui.eval("(async () => await add(1,2,3))();").unwrap(), 6);
assert!(ui.eval("(async () => await add(1,2,'hi'))();").is_err());
sourcepub fn eval(&self, js: &str) -> JSResult
pub fn eval(&self, js: &str) -> JSResult
Evaluates js code and returns the result.
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
assert_eq!(ui.eval("1+1").unwrap(), 2);
assert_eq!(ui.eval("'Hello'+' World'").unwrap(), "Hello World");
assert!(ui.eval("xfgch").is_err());
sourcepub fn load_js(&self, script: &str) -> Result<(), JSError>
pub fn load_js(&self, script: &str) -> Result<(), JSError>
Evaluates js code and adds functions before document loads. Loaded js is unloaded on reload.
Arguments
script
- Javascript that should be loaded
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.load_js("function loadedFunction() { return 'This function was loaded from rust'; }").expect("Unable to load js");
assert_eq!(ui.eval("loadedFunction()").unwrap(), "This function was loaded from rust");
sourcepub fn load_css(&self, css: &str) -> Result<(), JSError>
pub fn load_css(&self, css: &str) -> Result<(), JSError>
Loads CSS into current window. Loaded CSS is unloaded on reload.
Arguments
css
- CSS that should be loaded
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.load_css("body {display: none;}").expect("Unable to load css");
Trait Implementations
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more