1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
//!
//! Access to system display information, including system resolution, display layout and
//! display layout change notification events.
//!
//! # Synopsis
//! ```
//! // init must be called once during startup, before any function to nw.Screen can be called
//! nw_sys::screen::init_once();
//!
//! let display_bounds_changed_callback = Callback::new(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayBoundsChanged: {:#?}", screen);
//! Ok(())
//! });
//!
//! let display_added_callback = Callback::new(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayAdded: {:#?}", screen);
//! Ok(())
//! });
//!
//! let display_removed_callback = Callback::new(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayRemoved: {:#?}", screen);
//! Ok(())
//! });
//!
//! // listen to screen events
//! nw_sys::screen::on("displayBoundsChanged", display_bounds_changed_callback.as_ref());
//! nw_sys::screen::on("displayAdded", display_added_callback.as_ref());
//! nw_sys::screen::on("displayRemoved", display_removed_callback.as_ref());
//!
//! // save callbacks somewhere
//! app.push_callback(display_bounds_changed_callback)?;
//! app.push_callback(display_added_callback)?;
//! app.push_callback(display_removed_callback)?;
//!
//! ```
//!
//! Screen
//! # Synopsis
//! ```
//! use workflow_wasm::prelude::*;
//!
//! // init must be called once during startup, before any function to nw.Screen can be called
//! nw_sys::screen::init_once();
//!
//! let display_bounds_changed_callback = callback!(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayBoundsChanged: {:#?}", screen);
//! Ok(())
//! });
//!
//! let display_added_callback = callback!(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayAdded: {:#?}", screen);
//! Ok(())
//! });
//!
//! let display_removed_callback = callback!(move |screen:JsValue|{
//! let screen: nw_sys::screen::ScreenInfo = screen.try_into()?;
//! log_info!("displayRemoved: {:#?}", screen);
//! Ok(())
//! });
//!
//! // listen to screen events
//! nw_sys::screen::on("displayBoundsChanged", display_bounds_changed_callback.into_js());
//! nw_sys::screen::on("displayAdded", display_added_callback.into_js());
//! nw_sys::screen::on("displayRemoved", display_removed_callback.into_js());
//!
//! // save callbacks somewhere
//! app.push_callback(display_bounds_changed_callback)?;
//! app.push_callback(display_added_callback)?;
//! app.push_callback(display_removed_callback)?;
//!
//! ```
use js_sys::{Array, Function};
use wasm_bindgen::prelude::*;
//use workflow_log::log_info;
//use crate::options::OptionsExt;
use crate::result::Result;
use crate::utils;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace=nw, js_name = Screen)]
#[derive(Debug, Clone)]
type ScreenLocal;
#[wasm_bindgen(js_namespace=["nw", "Screen"], js_name = Init)]
/// Init the Screen singleton object, you only need to call this once
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screeninit)
///
pub fn init();
#[wasm_bindgen(getter, static_method_of=ScreenLocal, js_namespace=["nw"], js_class=Screen, js_name = screens)]
fn screens_impl() -> Array;
#[wasm_bindgen(js_namespace=["nw", "Screen"], js_name = chooseDesktopMedia)]
fn choose_desktop_media_impl(sources: Array, callback: &Function);
#[wasm_bindgen(js_namespace=["nw", "Screen"], js_name = on)]
///
///
/// Interface for accessing display & monitor layout information. For usage example please refer to [nw_sys::screen](self)
///
/// ### Events:
/// - displayBoundsChanged (screen)
/// - displayAdded (screen)
/// - displayRemoved (screen)
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#event-displayboundschangedscreen)
///
pub fn on(event_name: &str, callback: &Function);
}
pub mod desktop_capture_monitor {
use js_sys::Function;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace=["nw", "Screen"], js_name = DesktopCaptureMonitor)]
#[derive(Debug, Clone)]
type DCM;
#[wasm_bindgen(getter, static_method_of=DCM, js_namespace=["nw", "Screen"], js_class=DesktopCaptureMonitor, js_name = started)]
/// Return Boolean of whether the DesktopCaptureMonitor is started.
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screendesktopcapturemonitorstarted)
///
pub fn started() -> bool;
#[wasm_bindgen(js_namespace=["nw", "Screen", "DesktopCaptureMonitor"], js_name = start)]
/// The DesktopCaptureMonitor will start monitoring the system
/// and trigger the the events. The screen may flicker
/// if while DesktopCaptureMonitor is running.
///
/// Example:
/// ```rust
/// nw::screen::desktop_capture_monitor::start(true, true);
/// ```
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screendesktopcapturemonitorstartshould_include_screens-should_include_windows)
///
pub fn start(should_include_screens: bool, should_include_windows: bool);
#[wasm_bindgen(js_namespace=["nw", "Screen", "DesktopCaptureMonitor"], js_name = stop)]
/// The `DesktopCaptureMonitor` will stop monitoring the system.
/// `DesktopCaptureMonitor` should be stopped after a stream is selected.
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screendesktopcapturemonitorstop)
///
pub fn stop();
#[wasm_bindgen(js_namespace=["nw", "Screen", "DesktopCaptureMonitor"], js_name = registerStream)]
/// Register and return a valid stream id which will be used into
/// chromeMediaSourceId in get_user_media constraints.
///
/// See Synopsis for the usage.
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screendesktopcapturemonitorregisterstreamid)
///
pub fn register_stream(id: &str) -> String;
#[wasm_bindgen(static_method_of=DCM, js_namespace=["nw", "Screen"], js_class=DesktopCaptureMonitor, js_name = on)]
/// Add event listener
///
/// ### Events:
/// - added (id, name, order, type, primary)
/// - removed (order)
/// - orderchanged (id, new_order, old_order)
/// - namechanged (id, name)
/// - thumbnailchanged (id, thumbnail)
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#event-added-id-name-order-type-primary)
///
pub fn on(event_name: &str, callback: &Function);
}
/// Return Boolean of whether the DesktopCaptureMonitor is started.
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screendesktopcapturemonitorstarted)
///
pub fn started() -> bool {
DCM::started()
}
/// Add event listener
///
/// ### Events:
/// - added (id, name, order, type, primary)
/// - removed (order)
/// - orderchanged (id, new_order, old_order)
/// - namechanged (id, name)
/// - thumbnailchanged (id, thumbnail)
///
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#event-added-id-name-order-type-primary)
///
pub fn on(event_name: &str, callback: &Function) {
DCM::on(event_name, callback)
}
}
static mut INIT: bool = false;
/// Return true is screen is initialized
pub fn is_initialized() -> bool {
unsafe { INIT }
}
/// Call the screen::init() if screen is not initialized yet
pub fn init_once() {
if !is_initialized() {
unsafe { INIT = true };
init();
}
}
/// Media source type
pub enum MediaSources {
Screen,
Window,
ScreenAndWindow,
}
/// Choose desktop media
///
/// Screen sharing by selection; Currently only working in Windows and OSX
/// and some linux distribution.
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screenchoosedesktopmedia-sources-callback)
///
pub fn choose_desktop_media(sources: MediaSources, callback: &Function) -> Result<()> {
let array = Array::new();
match sources {
MediaSources::Screen => {
array.push(&JsValue::from("screen"));
}
MediaSources::Window => {
array.push(&JsValue::from("window"));
}
MediaSources::ScreenAndWindow => {
array.push(&JsValue::from("screen"));
array.push(&JsValue::from("window"));
}
};
choose_desktop_media_impl(array, callback);
Ok(())
}
/// Get the array of screen (number of screen connected to the computer)
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Screen/#screenscreens)
///
pub fn screens() -> Result<Vec<ScreenInfo>> {
let mut result: Vec<ScreenInfo> = Vec::new();
let array = ScreenLocal::screens_impl();
for index in 0..array.length() {
let screen = array.get(index);
//log_info!("screen: {:#?}", screen);
result.push(screen.try_into()?);
}
Ok(result)
}
/// physical screen resolution, can be negative,
/// not necessarily start from 0,
/// depending on screen arrangement
#[derive(Debug)]
pub struct Bounds {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
/// useable area within the screen bound
#[derive(Debug)]
pub struct WorkArea {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
/// Screen Info
#[derive(Debug)]
pub struct ScreenInfo {
pub id: u64,
pub scale_factor: f64,
pub is_built_in: bool,
pub rotation: u64,
pub touch_support: u64,
pub bounds: Bounds,
pub work_area: WorkArea,
}
fn read_box(jsv: &JsValue, prop: &str) -> Result<(f64, f64, f64, f64)> {
let jsv = utils::try_get_js_value(jsv, prop)?;
let x = utils::try_get_f64_from_prop(&jsv, "x")?;
let y = utils::try_get_f64_from_prop(&jsv, "y")?;
let width = utils::try_get_f64_from_prop(&jsv, "width")?;
let height = utils::try_get_f64_from_prop(&jsv, "height")?;
Ok((x, y, width, height))
}
impl TryFrom<JsValue> for ScreenInfo {
type Error = crate::error::Error;
fn try_from(jsv: JsValue) -> std::result::Result<Self, Self::Error> {
let id = utils::try_get_u64_from_prop(&jsv, "id")?;
let scale_factor = utils::try_get_f64_from_prop(&jsv, "scaleFactor")?;
let is_built_in = utils::try_get_bool_from_prop(&jsv, "isBuiltIn")?;
let rotation = utils::try_get_u64_from_prop(&jsv, "rotation")?;
let touch_support = utils::try_get_u64_from_prop(&jsv, "touchSupport")?;
let (x, y, width, height) = read_box(&jsv, "bounds")?;
let bounds = Bounds {
x,
y,
width,
height,
};
let (x, y, width, height) = read_box(&jsv, "work_area")?;
let work_area = WorkArea {
x,
y,
width,
height,
};
let info = Self {
id,
scale_factor,
is_built_in,
rotation,
touch_support,
bounds,
work_area,
};
Ok(info)
}
}