workflow_nw/window.rs
1use crate::result::Result;
2use nw_sys::prelude::*;
3use wasm_bindgen::prelude::*;
4use workflow_core::channel::oneshot;
5use workflow_wasm::callback::Callback;
6
7/// Asynchronously retrieves all currently open NW.js windows belonging to the
8/// application.
9pub async fn get_all_async() -> Result<Vec<Window>> {
10 let (sender, receiver) = oneshot();
11 let callback = Callback::new(move |windows: JsValue| {
12 let array = windows
13 .dyn_into::<js_sys::Array>()
14 .expect("nw_sys::window::get_all_async() error converting to window array");
15 let windows = array
16 .to_vec()
17 .into_iter()
18 .map(|window: JsValue| window.unchecked_into::<Window>())
19 .collect::<Vec<Window>>();
20
21 sender.try_send(windows).unwrap();
22 });
23
24 nw_sys::window::get_all(callback.as_ref());
25 let result = receiver.recv().await?;
26
27 Ok(result)
28}
29
30// this can be used to test...
31// pub async fn get_all_async() -> Result<Vec<Window>> {
32// let (sender, receiver) = oneshot();
33// let callback = Callback::new_with_args_2(move |windows: JsValue, v2: JsValue| {
34// log_info!("windows: {:?}", windows);
35// log_info!("v2: {:?}", v2);
36
37// sender.try_send(Sendable(windows)).unwrap();
38// });
39
40// nw_sys::window::get_all(callback.as_ref());
41// let windows = receiver.recv().await?;
42// let length = Reflect::get(&windows, &"length".into())?;
43// log_info!("length: {:?}", length);
44// let length = length.as_f64().unwrap() as usize;
45// let result = (0..length)
46// .into_iter()
47// .map(|index| {
48// Reflect::get(&windows, &index.into())
49// .expect("failed to get nw window")
50// .unchecked_into::<Window>()
51// })
52// .collect::<Vec<_>>();
53
54// Ok(result)
55// }