Struct WindowIpc

Source
pub struct WindowIpc { /* private fields */ }
Expand description

The Window’s IPC interface, used for evalling, events, etc.

Implementations§

Source§

impl WindowIpc

Source

pub fn new(ipc: Arc<Mutex<Option<BrowserIpc>>>) -> Self

Source

pub fn block_until_initialized(&self) -> Result<(), CrowserError>

Wait until the IPC layer has made a connection with the window.

Examples found in repository?
examples/javascript.rs (line 17)
3fn main() -> Result<(), CrowserError> {
4  let mut profile_dir = std::env::current_dir()?;
5  profile_dir.push("example_profiles");
6
7  let config = RemoteConfig {
8    url: "https://example.com".to_string(),
9  };
10
11  let mut window = Window::new(config, None, profile_dir)?;
12  let ipc = window.ipc();
13
14  window.clear_profile().unwrap_or_default();
15
16  std::thread::spawn(move || {
17    ipc.block_until_initialized().unwrap_or_default();
18
19    let two = ipc.eval("1 + 1").unwrap();
20    println!("1 + 1 = {:?}", two);
21
22    std::thread::sleep(std::time::Duration::from_secs(2));
23
24    ipc.eval("alert('Hello from Crowser!')").unwrap_or_default();
25  });
26
27  window.create()?;
28
29  Ok(())
30}
More examples
Hide additional examples
examples/commands.rs (line 17)
3fn main() -> Result<(), CrowserError> {
4  let mut profile_dir = std::env::current_dir()?;
5  profile_dir.push("example_profiles");
6
7  let config = RemoteConfig {
8    url: "https://example.com".to_string(),
9  };
10
11  let mut window = Window::new(config, None, profile_dir)?;
12  let ipc = window.ipc();
13
14  window.clear_profile().unwrap_or_default();
15
16  std::thread::spawn(move || {
17    ipc.block_until_initialized().unwrap_or_default();
18
19    ipc
20      .register_command("hello", |_| {
21        println!("Got hello command");
22        Ok(serde_json::json!("Hello from Crowser!"))
23      })
24      .unwrap_or_default();
25
26    std::thread::sleep(std::time::Duration::from_secs(1));
27
28    // Eval some JS that calls that command
29    let result = ipc
30      .eval("window.__CROWSER.ipc.invoke('hello')")
31      .unwrap_or_default();
32    println!("Result: {:?}", result);
33  });
34
35  window.create()?;
36
37  Ok(())
38}
Source

pub fn eval(&self, script: impl AsRef<str>) -> Result<Value, CrowserError>

Eval JavaScript in the window.

Examples found in repository?
examples/javascript.rs (line 19)
3fn main() -> Result<(), CrowserError> {
4  let mut profile_dir = std::env::current_dir()?;
5  profile_dir.push("example_profiles");
6
7  let config = RemoteConfig {
8    url: "https://example.com".to_string(),
9  };
10
11  let mut window = Window::new(config, None, profile_dir)?;
12  let ipc = window.ipc();
13
14  window.clear_profile().unwrap_or_default();
15
16  std::thread::spawn(move || {
17    ipc.block_until_initialized().unwrap_or_default();
18
19    let two = ipc.eval("1 + 1").unwrap();
20    println!("1 + 1 = {:?}", two);
21
22    std::thread::sleep(std::time::Duration::from_secs(2));
23
24    ipc.eval("alert('Hello from Crowser!')").unwrap_or_default();
25  });
26
27  window.create()?;
28
29  Ok(())
30}
More examples
Hide additional examples
examples/commands.rs (line 30)
3fn main() -> Result<(), CrowserError> {
4  let mut profile_dir = std::env::current_dir()?;
5  profile_dir.push("example_profiles");
6
7  let config = RemoteConfig {
8    url: "https://example.com".to_string(),
9  };
10
11  let mut window = Window::new(config, None, profile_dir)?;
12  let ipc = window.ipc();
13
14  window.clear_profile().unwrap_or_default();
15
16  std::thread::spawn(move || {
17    ipc.block_until_initialized().unwrap_or_default();
18
19    ipc
20      .register_command("hello", |_| {
21        println!("Got hello command");
22        Ok(serde_json::json!("Hello from Crowser!"))
23      })
24      .unwrap_or_default();
25
26    std::thread::sleep(std::time::Duration::from_secs(1));
27
28    // Eval some JS that calls that command
29    let result = ipc
30      .eval("window.__CROWSER.ipc.invoke('hello')")
31      .unwrap_or_default();
32    println!("Result: {:?}", result);
33  });
34
35  window.create()?;
36
37  Ok(())
38}
Source

pub fn listen( &self, name: impl AsRef<str>, callback: fn(Value) -> Result<Value, CrowserError>, ) -> Result<(), CrowserError>

Listen for events from the window.

Source

pub fn register_command( &self, name: impl AsRef<str>, callback: fn(Value) -> Result<Value, CrowserError>, ) -> Result<(), CrowserError>

Register commands that the Window can emit.

Examples found in repository?
examples/commands.rs (lines 20-23)
3fn main() -> Result<(), CrowserError> {
4  let mut profile_dir = std::env::current_dir()?;
5  profile_dir.push("example_profiles");
6
7  let config = RemoteConfig {
8    url: "https://example.com".to_string(),
9  };
10
11  let mut window = Window::new(config, None, profile_dir)?;
12  let ipc = window.ipc();
13
14  window.clear_profile().unwrap_or_default();
15
16  std::thread::spawn(move || {
17    ipc.block_until_initialized().unwrap_or_default();
18
19    ipc
20      .register_command("hello", |_| {
21        println!("Got hello command");
22        Ok(serde_json::json!("Hello from Crowser!"))
23      })
24      .unwrap_or_default();
25
26    std::thread::sleep(std::time::Duration::from_secs(1));
27
28    // Eval some JS that calls that command
29    let result = ipc
30      .eval("window.__CROWSER.ipc.invoke('hello')")
31      .unwrap_or_default();
32    println!("Result: {:?}", result);
33  });
34
35  window.create()?;
36
37  Ok(())
38}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V