window.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! The Tauri window types and functions.
  5. pub(crate) mod menu;
  6. pub use menu::{MenuEvent, MenuHandle};
  7. use crate::{
  8. app::AppHandle,
  9. command::{CommandArg, CommandItem},
  10. event::{Event, EventHandler},
  11. hooks::InvokeResponder,
  12. manager::WindowManager,
  13. runtime::{
  14. monitor::Monitor as RuntimeMonitor,
  15. webview::{InvokePayload, WebviewAttributes, WindowBuilder},
  16. window::{
  17. dpi::{PhysicalPosition, PhysicalSize, Position, Size},
  18. DetachedWindow, PendingWindow, WindowEvent,
  19. },
  20. Dispatch, Icon, Runtime, UserAttentionType,
  21. },
  22. sealed::ManagerBase,
  23. sealed::RuntimeOrDispatch,
  24. utils::config::WindowUrl,
  25. Invoke, InvokeError, InvokeMessage, InvokeResolver, Manager, PageLoadPayload,
  26. };
  27. use serde::Serialize;
  28. use tauri_macros::default_runtime;
  29. use std::{
  30. hash::{Hash, Hasher},
  31. sync::Arc,
  32. };
  33. /// Monitor descriptor.
  34. #[derive(Debug, Clone, Serialize)]
  35. #[serde(rename_all = "camelCase")]
  36. pub struct Monitor {
  37. pub(crate) name: Option<String>,
  38. pub(crate) size: PhysicalSize<u32>,
  39. pub(crate) position: PhysicalPosition<i32>,
  40. pub(crate) scale_factor: f64,
  41. }
  42. impl From<RuntimeMonitor> for Monitor {
  43. fn from(monitor: RuntimeMonitor) -> Self {
  44. Self {
  45. name: monitor.name,
  46. size: monitor.size,
  47. position: monitor.position,
  48. scale_factor: monitor.scale_factor,
  49. }
  50. }
  51. }
  52. impl Monitor {
  53. /// Returns a human-readable name of the monitor.
  54. /// Returns None if the monitor doesn't exist anymore.
  55. pub fn name(&self) -> Option<&String> {
  56. self.name.as_ref()
  57. }
  58. /// Returns the monitor's resolution.
  59. pub fn size(&self) -> &PhysicalSize<u32> {
  60. &self.size
  61. }
  62. /// Returns the top-left corner position of the monitor relative to the larger full screen area.
  63. pub fn position(&self) -> &PhysicalPosition<i32> {
  64. &self.position
  65. }
  66. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  67. pub fn scale_factor(&self) -> f64 {
  68. self.scale_factor
  69. }
  70. }
  71. // TODO: expand these docs since this is a pretty important type
  72. /// A webview window managed by Tauri.
  73. ///
  74. /// This type also implements [`Manager`] which allows you to manage other windows attached to
  75. /// the same application.
  76. #[default_runtime(crate::Wry, wry)]
  77. #[derive(Debug)]
  78. pub struct Window<R: Runtime> {
  79. /// The webview window created by the runtime.
  80. window: DetachedWindow<R>,
  81. /// The manager to associate this webview window with.
  82. manager: WindowManager<R>,
  83. pub(crate) app_handle: AppHandle<R>,
  84. }
  85. #[cfg(any(windows, target_os = "macos"))]
  86. #[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "macos"))))]
  87. unsafe impl<R: Runtime> raw_window_handle::HasRawWindowHandle for Window<R> {
  88. #[cfg(windows)]
  89. fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
  90. let mut handle = raw_window_handle::Win32Handle::empty();
  91. handle.hwnd = self.hwnd().expect("failed to get window `hwnd`");
  92. raw_window_handle::RawWindowHandle::Win32(handle)
  93. }
  94. #[cfg(target_os = "macos")]
  95. fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
  96. let mut handle = raw_window_handle::AppKitHandle::empty();
  97. handle.ns_window = self
  98. .ns_window()
  99. .expect("failed to get window's `ns_window`");
  100. raw_window_handle::RawWindowHandle::AppKit(handle)
  101. }
  102. }
  103. impl<R: Runtime> Clone for Window<R> {
  104. fn clone(&self) -> Self {
  105. Self {
  106. window: self.window.clone(),
  107. manager: self.manager.clone(),
  108. app_handle: self.app_handle.clone(),
  109. }
  110. }
  111. }
  112. impl<R: Runtime> Hash for Window<R> {
  113. /// Only use the [`Window`]'s label to represent its hash.
  114. fn hash<H: Hasher>(&self, state: &mut H) {
  115. self.window.label.hash(state)
  116. }
  117. }
  118. impl<R: Runtime> Eq for Window<R> {}
  119. impl<R: Runtime> PartialEq for Window<R> {
  120. /// Only use the [`Window`]'s label to compare equality.
  121. fn eq(&self, other: &Self) -> bool {
  122. self.window.label.eq(&other.window.label)
  123. }
  124. }
  125. impl<R: Runtime> Manager<R> for Window<R> {}
  126. impl<R: Runtime> ManagerBase<R> for Window<R> {
  127. fn manager(&self) -> &WindowManager<R> {
  128. &self.manager
  129. }
  130. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  131. RuntimeOrDispatch::Dispatch(self.dispatcher())
  132. }
  133. fn app_handle(&self) -> AppHandle<R> {
  134. self.app_handle.clone()
  135. }
  136. }
  137. impl<'de, R: Runtime> CommandArg<'de, R> for Window<R> {
  138. /// Grabs the [`Window`] from the [`CommandItem`]. This will never fail.
  139. fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
  140. Ok(command.message.window())
  141. }
  142. }
  143. impl<R: Runtime> Window<R> {
  144. /// Create a new window that is attached to the manager.
  145. pub(crate) fn new(
  146. manager: WindowManager<R>,
  147. window: DetachedWindow<R>,
  148. app_handle: AppHandle<R>,
  149. ) -> Self {
  150. Self {
  151. window,
  152. manager,
  153. app_handle,
  154. }
  155. }
  156. /// Creates a new webview window.
  157. pub fn create_window<F>(
  158. &mut self,
  159. label: String,
  160. url: WindowUrl,
  161. setup: F,
  162. ) -> crate::Result<Window<R>>
  163. where
  164. F: FnOnce(
  165. <R::Dispatcher as Dispatch>::WindowBuilder,
  166. WebviewAttributes,
  167. ) -> (
  168. <R::Dispatcher as Dispatch>::WindowBuilder,
  169. WebviewAttributes,
  170. ),
  171. {
  172. let (window_builder, webview_attributes) = setup(
  173. <R::Dispatcher as Dispatch>::WindowBuilder::new(),
  174. WebviewAttributes::new(url),
  175. );
  176. self.create_new_window(PendingWindow::new(
  177. window_builder,
  178. webview_attributes,
  179. label,
  180. ))
  181. }
  182. pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {
  183. self.manager.invoke_responder()
  184. }
  185. /// The current window's dispatcher.
  186. pub(crate) fn dispatcher(&self) -> R::Dispatcher {
  187. self.window.dispatcher.clone()
  188. }
  189. /// Runs the given closure on the main thread.
  190. pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
  191. self
  192. .window
  193. .dispatcher
  194. .run_on_main_thread(f)
  195. .map_err(Into::into)
  196. }
  197. /// How to handle this window receiving an [`InvokeMessage`].
  198. pub fn on_message(self, command: String, payload: InvokePayload) -> crate::Result<()> {
  199. let manager = self.manager.clone();
  200. match command.as_str() {
  201. "__initialized" => {
  202. let payload: PageLoadPayload = serde_json::from_value(payload.inner)?;
  203. manager.run_on_page_load(self, payload);
  204. }
  205. _ => {
  206. let message = InvokeMessage::new(
  207. self.clone(),
  208. manager.state(),
  209. command.to_string(),
  210. payload.inner,
  211. );
  212. let resolver = InvokeResolver::new(self, payload.callback, payload.error);
  213. let invoke = Invoke { message, resolver };
  214. if manager.verify_invoke_key(payload.key) {
  215. if let Some(module) = &payload.tauri_module {
  216. let module = module.to_string();
  217. crate::endpoints::handle(module, invoke, manager.config(), manager.package_info());
  218. } else if command.starts_with("plugin:") {
  219. manager.extend_api(invoke);
  220. } else {
  221. manager.run_invoke_handler(invoke);
  222. }
  223. } else {
  224. panic!(
  225. r#"The invoke key "{}" is invalid. This means that an external, possible malicious script is trying to access the system interface."#,
  226. payload.key
  227. );
  228. }
  229. }
  230. }
  231. Ok(())
  232. }
  233. /// The label of this window.
  234. pub fn label(&self) -> &str {
  235. &self.window.label
  236. }
  237. /// Emits an event to both the JavaScript and the Rust listeners.
  238. pub fn emit_and_trigger<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
  239. self.trigger(event, Some(serde_json::to_string(&payload)?));
  240. self.emit(event, payload)
  241. }
  242. /// Emits an event to the JavaScript listeners on the current window.
  243. ///
  244. /// The event is only delivered to listeners that used the `appWindow.listen` method on the @tauri-apps/api `window` module.
  245. pub fn emit<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
  246. self.eval(&format!(
  247. "window['{}']({{event: {}, payload: {}}})",
  248. self.manager.event_emit_function_name(),
  249. serde_json::to_string(event)?,
  250. serde_json::to_value(payload)?,
  251. ))?;
  252. Ok(())
  253. }
  254. /// Emits an event to the JavaScript listeners on all windows except this one.
  255. ///
  256. /// The event is only delivered to listeners that used the `appWindow.listen` function from the `@tauri-apps/api `window` module.
  257. pub fn emit_others<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
  258. self.manager.emit_filter(event, payload, |w| w != self)
  259. }
  260. /// Listen to an event on this window.
  261. ///
  262. /// This listener only receives events that are triggered using the
  263. /// [`trigger`](Window#method.trigger) and [`emit_and_trigger`](Window#method.emit_and_trigger) methods or
  264. /// the `appWindow.emit` function from the @tauri-apps/api `window` module.
  265. pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
  266. where
  267. F: Fn(Event) + Send + 'static,
  268. {
  269. let label = self.window.label.clone();
  270. self.manager.listen(event.into(), Some(label), handler)
  271. }
  272. /// Unlisten to an event on this window.
  273. pub fn unlisten(&self, handler_id: EventHandler) {
  274. self.manager.unlisten(handler_id)
  275. }
  276. /// Listen to an event on this window a single time.
  277. pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
  278. where
  279. F: Fn(Event) + Send + 'static,
  280. {
  281. let label = self.window.label.clone();
  282. self.manager.once(event.into(), Some(label), handler)
  283. }
  284. /// Triggers an event to the Rust listeners on this window.
  285. ///
  286. /// The event is only delivered to listeners that used the [`listen`](Window#method.listen) method.
  287. pub fn trigger(&self, event: &str, data: Option<String>) {
  288. let label = self.window.label.clone();
  289. self.manager.trigger(event, Some(label), data)
  290. }
  291. /// Evaluates JavaScript on this window.
  292. pub fn eval(&self, js: &str) -> crate::Result<()> {
  293. self.window.dispatcher.eval_script(js).map_err(Into::into)
  294. }
  295. /// Registers a window event listener.
  296. pub fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) {
  297. self.window.dispatcher.on_window_event(f);
  298. }
  299. /// Registers a menu event listener.
  300. pub fn on_menu_event<F: Fn(MenuEvent) + Send + 'static>(&self, f: F) -> uuid::Uuid {
  301. let menu_ids = self.window.menu_ids.clone();
  302. self.window.dispatcher.on_menu_event(move |event| {
  303. f(MenuEvent {
  304. menu_item_id: menu_ids
  305. .lock()
  306. .unwrap()
  307. .get(&event.menu_item_id)
  308. .unwrap()
  309. .clone(),
  310. })
  311. })
  312. }
  313. pub(crate) fn register_js_listener(&self, event: String, id: u64) {
  314. self
  315. .window
  316. .js_event_listeners
  317. .lock()
  318. .unwrap()
  319. .entry(event)
  320. .or_insert_with(Default::default)
  321. .insert(id);
  322. }
  323. pub(crate) fn unregister_js_listener(&self, id: u64) {
  324. let mut empty = None;
  325. let mut js_listeners = self.window.js_event_listeners.lock().unwrap();
  326. for (event, ids) in js_listeners.iter_mut() {
  327. if ids.contains(&id) {
  328. ids.remove(&id);
  329. if ids.is_empty() {
  330. empty.replace(event.clone());
  331. }
  332. break;
  333. }
  334. }
  335. if let Some(event) = empty {
  336. js_listeners.remove(&event);
  337. }
  338. }
  339. pub(crate) fn has_js_listener(&self, event: &str) -> bool {
  340. self
  341. .window
  342. .js_event_listeners
  343. .lock()
  344. .unwrap()
  345. .contains_key(event)
  346. }
  347. // Getters
  348. /// Gets a handle to the window menu.
  349. pub fn menu_handle(&self) -> MenuHandle<R> {
  350. MenuHandle {
  351. ids: self.window.menu_ids.clone(),
  352. dispatcher: self.dispatcher(),
  353. }
  354. }
  355. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  356. pub fn scale_factor(&self) -> crate::Result<f64> {
  357. self.window.dispatcher.scale_factor().map_err(Into::into)
  358. }
  359. /// Returns the position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop.
  360. pub fn inner_position(&self) -> crate::Result<PhysicalPosition<i32>> {
  361. self.window.dispatcher.inner_position().map_err(Into::into)
  362. }
  363. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  364. pub fn outer_position(&self) -> crate::Result<PhysicalPosition<i32>> {
  365. self.window.dispatcher.outer_position().map_err(Into::into)
  366. }
  367. /// Returns the physical size of the window's client area.
  368. ///
  369. /// The client area is the content of the window, excluding the title bar and borders.
  370. pub fn inner_size(&self) -> crate::Result<PhysicalSize<u32>> {
  371. self.window.dispatcher.inner_size().map_err(Into::into)
  372. }
  373. /// Returns the physical size of the entire window.
  374. ///
  375. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  376. pub fn outer_size(&self) -> crate::Result<PhysicalSize<u32>> {
  377. self.window.dispatcher.outer_size().map_err(Into::into)
  378. }
  379. /// Gets the window's current fullscreen state.
  380. pub fn is_fullscreen(&self) -> crate::Result<bool> {
  381. self.window.dispatcher.is_fullscreen().map_err(Into::into)
  382. }
  383. /// Gets the window's current maximized state.
  384. pub fn is_maximized(&self) -> crate::Result<bool> {
  385. self.window.dispatcher.is_maximized().map_err(Into::into)
  386. }
  387. /// Gets the window’s current decoration state.
  388. pub fn is_decorated(&self) -> crate::Result<bool> {
  389. self.window.dispatcher.is_decorated().map_err(Into::into)
  390. }
  391. /// Gets the window’s current resizable state.
  392. pub fn is_resizable(&self) -> crate::Result<bool> {
  393. self.window.dispatcher.is_resizable().map_err(Into::into)
  394. }
  395. /// Gets the window's current vibility state.
  396. pub fn is_visible(&self) -> crate::Result<bool> {
  397. self.window.dispatcher.is_visible().map_err(Into::into)
  398. }
  399. /// Returns the monitor on which the window currently resides.
  400. ///
  401. /// Returns None if current monitor can't be detected.
  402. ///
  403. /// ## Platform-specific
  404. ///
  405. /// - **Linux:** Unsupported
  406. pub fn current_monitor(&self) -> crate::Result<Option<Monitor>> {
  407. self
  408. .window
  409. .dispatcher
  410. .current_monitor()
  411. .map(|m| m.map(Into::into))
  412. .map_err(Into::into)
  413. }
  414. /// Returns the primary monitor of the system.
  415. ///
  416. /// Returns None if it can't identify any monitor as a primary one.
  417. ///
  418. /// ## Platform-specific
  419. ///
  420. /// - **Linux:** Unsupported
  421. pub fn primary_monitor(&self) -> crate::Result<Option<Monitor>> {
  422. self
  423. .window
  424. .dispatcher
  425. .primary_monitor()
  426. .map(|m| m.map(Into::into))
  427. .map_err(Into::into)
  428. }
  429. /// Returns the list of all the monitors available on the system.
  430. ///
  431. /// ## Platform-specific
  432. ///
  433. /// - **Linux:** Unsupported
  434. pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
  435. self
  436. .window
  437. .dispatcher
  438. .available_monitors()
  439. .map(|m| m.into_iter().map(Into::into).collect())
  440. .map_err(Into::into)
  441. }
  442. /// Returns the native handle that is used by this window.
  443. #[cfg(target_os = "macos")]
  444. pub fn ns_window(&self) -> crate::Result<*mut std::ffi::c_void> {
  445. self.window.dispatcher.ns_window().map_err(Into::into)
  446. }
  447. /// Returns the native handle that is used by this window.
  448. #[cfg(windows)]
  449. pub fn hwnd(&self) -> crate::Result<*mut std::ffi::c_void> {
  450. self
  451. .window
  452. .dispatcher
  453. .hwnd()
  454. .map(|hwnd| hwnd as *mut _)
  455. .map_err(Into::into)
  456. }
  457. /// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
  458. ///
  459. /// Note that this can only be used on the main thread.
  460. #[cfg(any(
  461. target_os = "linux",
  462. target_os = "dragonfly",
  463. target_os = "freebsd",
  464. target_os = "netbsd",
  465. target_os = "openbsd"
  466. ))]
  467. pub fn gtk_window(&self) -> crate::Result<gtk::ApplicationWindow> {
  468. self.window.dispatcher.gtk_window().map_err(Into::into)
  469. }
  470. // Setters
  471. /// Centers the window.
  472. pub fn center(&self) -> crate::Result<()> {
  473. self.window.dispatcher.center().map_err(Into::into)
  474. }
  475. /// Requests user attention to the window, this has no effect if the application
  476. /// is already focused. How requesting for user attention manifests is platform dependent,
  477. /// see `UserAttentionType` for details.
  478. ///
  479. /// Providing `None` will unset the request for user attention. Unsetting the request for
  480. /// user attention might not be done automatically by the WM when the window receives input.
  481. ///
  482. /// ## Platform-specific
  483. ///
  484. /// - **macOS:** `None` has no effect.
  485. pub fn request_user_attention(
  486. &self,
  487. request_type: Option<UserAttentionType>,
  488. ) -> crate::Result<()> {
  489. self
  490. .window
  491. .dispatcher
  492. .request_user_attention(request_type)
  493. .map_err(Into::into)
  494. }
  495. /// Opens the dialog to prints the contents of the webview.
  496. /// Currently only supported on macOS on `wry`.
  497. /// `window.print()` works on all platforms.
  498. pub fn print(&self) -> crate::Result<()> {
  499. self.window.dispatcher.print().map_err(Into::into)
  500. }
  501. /// Determines if this window should be resizable.
  502. pub fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
  503. self
  504. .window
  505. .dispatcher
  506. .set_resizable(resizable)
  507. .map_err(Into::into)
  508. }
  509. /// Set this window's title.
  510. pub fn set_title(&self, title: &str) -> crate::Result<()> {
  511. self
  512. .window
  513. .dispatcher
  514. .set_title(title.to_string())
  515. .map_err(Into::into)
  516. }
  517. /// Maximizes this window.
  518. pub fn maximize(&self) -> crate::Result<()> {
  519. self.window.dispatcher.maximize().map_err(Into::into)
  520. }
  521. /// Un-maximizes this window.
  522. pub fn unmaximize(&self) -> crate::Result<()> {
  523. self.window.dispatcher.unmaximize().map_err(Into::into)
  524. }
  525. /// Minimizes this window.
  526. pub fn minimize(&self) -> crate::Result<()> {
  527. self.window.dispatcher.minimize().map_err(Into::into)
  528. }
  529. /// Un-minimizes this window.
  530. pub fn unminimize(&self) -> crate::Result<()> {
  531. self.window.dispatcher.unminimize().map_err(Into::into)
  532. }
  533. /// Show this window.
  534. pub fn show(&self) -> crate::Result<()> {
  535. self.window.dispatcher.show().map_err(Into::into)
  536. }
  537. /// Hide this window.
  538. pub fn hide(&self) -> crate::Result<()> {
  539. self.window.dispatcher.hide().map_err(Into::into)
  540. }
  541. /// Closes this window.
  542. /// # Panics
  543. ///
  544. /// - Panics if the event loop is not running yet, usually when called on the [`setup`](crate::Builder#method.setup) closure.
  545. /// - Panics when called on the main thread, usually on the [`run`](crate::App#method.run) closure.
  546. ///
  547. /// You can spawn a task to use the API using [`crate::async_runtime::spawn`] or [`std::thread::spawn`] to prevent the panic.
  548. pub fn close(&self) -> crate::Result<()> {
  549. self.window.dispatcher.close().map_err(Into::into)
  550. }
  551. /// Determines if this window should be [decorated].
  552. ///
  553. /// [decorated]: https://en.wikipedia.org/wiki/Window_(computing)#Window_decoration
  554. pub fn set_decorations(&self, decorations: bool) -> crate::Result<()> {
  555. self
  556. .window
  557. .dispatcher
  558. .set_decorations(decorations)
  559. .map_err(Into::into)
  560. }
  561. /// Determines if this window should always be on top of other windows.
  562. pub fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()> {
  563. self
  564. .window
  565. .dispatcher
  566. .set_always_on_top(always_on_top)
  567. .map_err(Into::into)
  568. }
  569. /// Resizes this window.
  570. pub fn set_size<S: Into<Size>>(&self, size: S) -> crate::Result<()> {
  571. self
  572. .window
  573. .dispatcher
  574. .set_size(size.into())
  575. .map_err(Into::into)
  576. }
  577. /// Sets this window's minimum size.
  578. pub fn set_min_size<S: Into<Size>>(&self, size: Option<S>) -> crate::Result<()> {
  579. self
  580. .window
  581. .dispatcher
  582. .set_min_size(size.map(|s| s.into()))
  583. .map_err(Into::into)
  584. }
  585. /// Sets this window's maximum size.
  586. pub fn set_max_size<S: Into<Size>>(&self, size: Option<S>) -> crate::Result<()> {
  587. self
  588. .window
  589. .dispatcher
  590. .set_max_size(size.map(|s| s.into()))
  591. .map_err(Into::into)
  592. }
  593. /// Sets this window's position.
  594. pub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> crate::Result<()> {
  595. self
  596. .window
  597. .dispatcher
  598. .set_position(position.into())
  599. .map_err(Into::into)
  600. }
  601. /// Determines if this window should be fullscreen.
  602. pub fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()> {
  603. self
  604. .window
  605. .dispatcher
  606. .set_fullscreen(fullscreen)
  607. .map_err(Into::into)
  608. }
  609. /// Bring the window to front and focus.
  610. pub fn set_focus(&self) -> crate::Result<()> {
  611. self.window.dispatcher.set_focus().map_err(Into::into)
  612. }
  613. /// Sets this window' icon.
  614. pub fn set_icon(&self, icon: Icon) -> crate::Result<()> {
  615. self.window.dispatcher.set_icon(icon).map_err(Into::into)
  616. }
  617. /// Whether to show the window icon in the task bar or not.
  618. pub fn set_skip_taskbar(&self, skip: bool) -> crate::Result<()> {
  619. self
  620. .window
  621. .dispatcher
  622. .set_skip_taskbar(skip)
  623. .map_err(Into::into)
  624. }
  625. /// Starts dragging the window.
  626. pub fn start_dragging(&self) -> crate::Result<()> {
  627. self.window.dispatcher.start_dragging().map_err(Into::into)
  628. }
  629. }