lib.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Internal runtime between Tauri and the underlying webview runtime.
  5. #![cfg_attr(doc_cfg, feature(doc_cfg))]
  6. use raw_window_handle::RawDisplayHandle;
  7. use serde::Deserialize;
  8. use std::{fmt::Debug, sync::mpsc::Sender};
  9. use tauri_utils::Theme;
  10. use url::Url;
  11. use uuid::Uuid;
  12. pub mod http;
  13. /// Create window and system tray menus.
  14. pub mod menu;
  15. /// Types useful for interacting with a user's monitors.
  16. pub mod monitor;
  17. pub mod webview;
  18. pub mod window;
  19. use monitor::Monitor;
  20. use webview::WindowBuilder;
  21. use window::{
  22. dpi::{PhysicalPosition, PhysicalSize, Position, Size},
  23. CursorIcon, DetachedWindow, PendingWindow, WindowEvent,
  24. };
  25. use crate::http::{
  26. header::{InvalidHeaderName, InvalidHeaderValue},
  27. method::InvalidMethod,
  28. status::InvalidStatusCode,
  29. InvalidUri,
  30. };
  31. #[cfg(all(desktop, feature = "system-tray"))]
  32. use std::fmt;
  33. pub type TrayId = u16;
  34. pub type TrayEventHandler = dyn Fn(&SystemTrayEvent) + Send + 'static;
  35. #[cfg(all(desktop, feature = "system-tray"))]
  36. #[non_exhaustive]
  37. pub struct SystemTray {
  38. pub id: TrayId,
  39. pub icon: Option<Icon>,
  40. pub menu: Option<menu::SystemTrayMenu>,
  41. #[cfg(target_os = "macos")]
  42. pub icon_as_template: bool,
  43. #[cfg(target_os = "macos")]
  44. pub menu_on_left_click: bool,
  45. #[cfg(target_os = "macos")]
  46. pub title: Option<String>,
  47. pub on_event: Option<Box<TrayEventHandler>>,
  48. pub tooltip: Option<String>,
  49. }
  50. #[cfg(all(desktop, feature = "system-tray"))]
  51. impl fmt::Debug for SystemTray {
  52. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  53. let mut d = f.debug_struct("SystemTray");
  54. d.field("id", &self.id)
  55. .field("icon", &self.icon)
  56. .field("menu", &self.menu);
  57. #[cfg(target_os = "macos")]
  58. {
  59. d.field("icon_as_template", &self.icon_as_template)
  60. .field("menu_on_left_click", &self.menu_on_left_click)
  61. .field("title", &self.title);
  62. }
  63. d.finish()
  64. }
  65. }
  66. #[cfg(all(desktop, feature = "system-tray"))]
  67. impl Clone for SystemTray {
  68. fn clone(&self) -> Self {
  69. Self {
  70. id: self.id,
  71. icon: self.icon.clone(),
  72. menu: self.menu.clone(),
  73. on_event: None,
  74. #[cfg(target_os = "macos")]
  75. icon_as_template: self.icon_as_template,
  76. #[cfg(target_os = "macos")]
  77. menu_on_left_click: self.menu_on_left_click,
  78. #[cfg(target_os = "macos")]
  79. title: self.title.clone(),
  80. tooltip: self.tooltip.clone(),
  81. }
  82. }
  83. }
  84. #[cfg(all(desktop, feature = "system-tray"))]
  85. impl Default for SystemTray {
  86. fn default() -> Self {
  87. Self {
  88. id: rand::random(),
  89. icon: None,
  90. menu: None,
  91. #[cfg(target_os = "macos")]
  92. icon_as_template: false,
  93. #[cfg(target_os = "macos")]
  94. menu_on_left_click: false,
  95. #[cfg(target_os = "macos")]
  96. title: None,
  97. on_event: None,
  98. tooltip: None,
  99. }
  100. }
  101. }
  102. #[cfg(all(desktop, feature = "system-tray"))]
  103. impl SystemTray {
  104. /// Creates a new system tray that only renders an icon.
  105. pub fn new() -> Self {
  106. Default::default()
  107. }
  108. pub fn menu(&self) -> Option<&menu::SystemTrayMenu> {
  109. self.menu.as_ref()
  110. }
  111. /// Sets the tray id.
  112. #[must_use]
  113. pub fn with_id(mut self, id: TrayId) -> Self {
  114. self.id = id;
  115. self
  116. }
  117. /// Sets the tray icon.
  118. #[must_use]
  119. pub fn with_icon(mut self, icon: Icon) -> Self {
  120. self.icon.replace(icon);
  121. self
  122. }
  123. /// Sets the tray icon as template.
  124. #[cfg(target_os = "macos")]
  125. #[must_use]
  126. pub fn with_icon_as_template(mut self, is_template: bool) -> Self {
  127. self.icon_as_template = is_template;
  128. self
  129. }
  130. /// Sets whether the menu should appear when the tray receives a left click. Defaults to `true`.
  131. #[cfg(target_os = "macos")]
  132. #[must_use]
  133. pub fn with_menu_on_left_click(mut self, menu_on_left_click: bool) -> Self {
  134. self.menu_on_left_click = menu_on_left_click;
  135. self
  136. }
  137. #[cfg(target_os = "macos")]
  138. #[must_use]
  139. pub fn with_title(mut self, title: &str) -> Self {
  140. self.title = Some(title.to_owned());
  141. self
  142. }
  143. /// Sets the tray icon tooltip.
  144. ///
  145. /// ## Platform-specific:
  146. ///
  147. /// - **Linux:** Unsupported
  148. #[must_use]
  149. pub fn with_tooltip(mut self, tooltip: &str) -> Self {
  150. self.tooltip = Some(tooltip.to_owned());
  151. self
  152. }
  153. /// Sets the menu to show when the system tray is right clicked.
  154. #[must_use]
  155. pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {
  156. self.menu.replace(menu);
  157. self
  158. }
  159. #[must_use]
  160. pub fn on_event<F: Fn(&SystemTrayEvent) + Send + 'static>(mut self, f: F) -> Self {
  161. self.on_event.replace(Box::new(f));
  162. self
  163. }
  164. }
  165. /// Type of user attention requested on a window.
  166. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  167. #[serde(tag = "type")]
  168. pub enum UserAttentionType {
  169. /// ## Platform-specific
  170. /// - **macOS:** Bounces the dock icon until the application is in focus.
  171. /// - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
  172. Critical,
  173. /// ## Platform-specific
  174. /// - **macOS:** Bounces the dock icon once.
  175. /// - **Windows:** Flashes the taskbar button until the application is in focus.
  176. Informational,
  177. }
  178. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  179. #[serde(tag = "type")]
  180. pub enum DeviceEventFilter {
  181. /// Always filter out device events.
  182. Always,
  183. /// Filter out device events while the window is not focused.
  184. Unfocused,
  185. /// Report all device events regardless of window focus.
  186. Never,
  187. }
  188. impl Default for DeviceEventFilter {
  189. fn default() -> Self {
  190. Self::Unfocused
  191. }
  192. }
  193. #[derive(Debug, thiserror::Error)]
  194. #[non_exhaustive]
  195. pub enum Error {
  196. /// Failed to create webview.
  197. #[error("failed to create webview: {0}")]
  198. CreateWebview(Box<dyn std::error::Error + Send + Sync>),
  199. /// Failed to create window.
  200. #[error("failed to create window")]
  201. CreateWindow,
  202. /// The given window label is invalid.
  203. #[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")]
  204. InvalidWindowLabel,
  205. /// Failed to send message to webview.
  206. #[error("failed to send message to the webview")]
  207. FailedToSendMessage,
  208. /// Failed to receive message from webview.
  209. #[error("failed to receive message from webview")]
  210. FailedToReceiveMessage,
  211. /// Failed to serialize/deserialize.
  212. #[error("JSON error: {0}")]
  213. Json(#[from] serde_json::Error),
  214. /// Encountered an error creating the app system tray.
  215. #[cfg(all(desktop, feature = "system-tray"))]
  216. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  217. #[error("error encountered during tray setup: {0}")]
  218. SystemTray(Box<dyn std::error::Error + Send + Sync>),
  219. /// Failed to load window icon.
  220. #[error("invalid icon: {0}")]
  221. InvalidIcon(Box<dyn std::error::Error + Send + Sync>),
  222. /// Failed to get monitor on window operation.
  223. #[error("failed to get monitor")]
  224. FailedToGetMonitor,
  225. /// Global shortcut error.
  226. #[cfg(all(desktop, feature = "global-shortcut"))]
  227. #[error(transparent)]
  228. GlobalShortcut(Box<dyn std::error::Error + Send + Sync>),
  229. #[cfg(all(desktop, feature = "clipboard"))]
  230. #[error(transparent)]
  231. Clipboard(Box<dyn std::error::Error + Send + Sync>),
  232. #[error("Invalid header name: {0}")]
  233. InvalidHeaderName(#[from] InvalidHeaderName),
  234. #[error("Invalid header value: {0}")]
  235. InvalidHeaderValue(#[from] InvalidHeaderValue),
  236. #[error("Invalid uri: {0}")]
  237. InvalidUri(#[from] InvalidUri),
  238. #[error("Invalid status code: {0}")]
  239. InvalidStatusCode(#[from] InvalidStatusCode),
  240. #[error("Invalid method: {0}")]
  241. InvalidMethod(#[from] InvalidMethod),
  242. #[error("Infallible error, something went really wrong: {0}")]
  243. Infallible(#[from] std::convert::Infallible),
  244. #[error("the event loop has been closed")]
  245. EventLoopClosed,
  246. }
  247. /// Result type.
  248. pub type Result<T> = std::result::Result<T, Error>;
  249. /// Window icon.
  250. #[derive(Debug, Clone)]
  251. pub struct Icon {
  252. /// RGBA bytes of the icon.
  253. pub rgba: Vec<u8>,
  254. /// Icon width.
  255. pub width: u32,
  256. /// Icon height.
  257. pub height: u32,
  258. }
  259. /// A type that can be used as an user event.
  260. pub trait UserEvent: Debug + Clone + Send + 'static {}
  261. impl<T: Debug + Clone + Send + 'static> UserEvent for T {}
  262. /// Event triggered on the event loop run.
  263. #[non_exhaustive]
  264. pub enum RunEvent<T: UserEvent> {
  265. /// Event loop is exiting.
  266. Exit,
  267. /// Event loop is about to exit
  268. ExitRequested {
  269. tx: Sender<ExitRequestedEventAction>,
  270. },
  271. /// An event associated with a window.
  272. WindowEvent {
  273. /// The window label.
  274. label: String,
  275. /// The detailed event.
  276. event: WindowEvent,
  277. },
  278. /// Application ready.
  279. Ready,
  280. /// Sent if the event loop is being resumed.
  281. Resumed,
  282. /// Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.
  283. ///
  284. /// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the “main body” of your event loop.
  285. MainEventsCleared,
  286. /// A custom event defined by the user.
  287. UserEvent(T),
  288. }
  289. /// Action to take when the event loop is about to exit
  290. #[derive(Debug)]
  291. pub enum ExitRequestedEventAction {
  292. /// Prevent the event loop from exiting
  293. Prevent,
  294. }
  295. /// A system tray event.
  296. #[derive(Debug)]
  297. pub enum SystemTrayEvent {
  298. MenuItemClick(u16),
  299. LeftClick {
  300. position: PhysicalPosition<f64>,
  301. size: PhysicalSize<f64>,
  302. },
  303. RightClick {
  304. position: PhysicalPosition<f64>,
  305. size: PhysicalSize<f64>,
  306. },
  307. DoubleClick {
  308. position: PhysicalPosition<f64>,
  309. size: PhysicalSize<f64>,
  310. },
  311. }
  312. /// Metadata for a runtime event loop iteration on `run_iteration`.
  313. #[derive(Debug, Clone, Default)]
  314. pub struct RunIteration {
  315. pub window_count: usize,
  316. }
  317. /// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
  318. #[cfg(target_os = "macos")]
  319. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  320. #[non_exhaustive]
  321. pub enum ActivationPolicy {
  322. /// Corresponds to NSApplicationActivationPolicyRegular.
  323. Regular,
  324. /// Corresponds to NSApplicationActivationPolicyAccessory.
  325. Accessory,
  326. /// Corresponds to NSApplicationActivationPolicyProhibited.
  327. Prohibited,
  328. }
  329. /// A [`Send`] handle to the runtime.
  330. pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  331. type Runtime: Runtime<T, Handle = Self>;
  332. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  333. fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy;
  334. /// Create a new webview window.
  335. fn create_window(
  336. &self,
  337. pending: PendingWindow<T, Self::Runtime>,
  338. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  339. /// Run a task on the main thread.
  340. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  341. /// Adds an icon to the system tray with the specified menu items.
  342. #[cfg(all(desktop, feature = "system-tray"))]
  343. #[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "system-tray"))))]
  344. fn system_tray(
  345. &self,
  346. system_tray: SystemTray,
  347. ) -> Result<<Self::Runtime as Runtime<T>>::TrayHandler>;
  348. fn raw_display_handle(&self) -> RawDisplayHandle;
  349. /// Shows the application, but does not automatically focus it.
  350. #[cfg(target_os = "macos")]
  351. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  352. fn show(&self) -> Result<()>;
  353. /// Hides the application.
  354. #[cfg(target_os = "macos")]
  355. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  356. fn hide(&self) -> Result<()>;
  357. }
  358. /// A global shortcut manager.
  359. #[cfg(all(desktop, feature = "global-shortcut"))]
  360. pub trait GlobalShortcutManager: Debug + Clone + Send + Sync {
  361. /// Whether the application has registered the given `accelerator`.
  362. fn is_registered(&self, accelerator: &str) -> Result<bool>;
  363. /// Register a global shortcut of `accelerator`.
  364. fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()>;
  365. /// Unregister all accelerators registered by the manager instance.
  366. fn unregister_all(&mut self) -> Result<()>;
  367. /// Unregister the provided `accelerator`.
  368. fn unregister(&mut self, accelerator: &str) -> Result<()>;
  369. }
  370. /// Clipboard manager.
  371. #[cfg(feature = "clipboard")]
  372. pub trait ClipboardManager: Debug + Clone + Send + Sync {
  373. /// Writes the text into the clipboard as plain text.
  374. fn write_text<T: Into<String>>(&mut self, text: T) -> Result<()>;
  375. /// Read the content in the clipboard as plain text.
  376. fn read_text(&self) -> Result<Option<String>>;
  377. }
  378. pub trait EventLoopProxy<T: UserEvent>: Debug + Clone + Send + Sync {
  379. fn send_event(&self, event: T) -> Result<()>;
  380. }
  381. /// The webview runtime interface.
  382. pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
  383. /// The message dispatcher.
  384. type Dispatcher: Dispatch<T, Runtime = Self>;
  385. /// The runtime handle type.
  386. type Handle: RuntimeHandle<T, Runtime = Self>;
  387. /// The global shortcut manager type.
  388. #[cfg(all(desktop, feature = "global-shortcut"))]
  389. type GlobalShortcutManager: GlobalShortcutManager;
  390. /// The clipboard manager type.
  391. #[cfg(feature = "clipboard")]
  392. type ClipboardManager: ClipboardManager;
  393. /// The tray handler type.
  394. #[cfg(all(desktop, feature = "system-tray"))]
  395. type TrayHandler: menu::TrayHandle;
  396. /// The proxy type.
  397. type EventLoopProxy: EventLoopProxy<T>;
  398. /// Creates a new webview runtime. Must be used on the main thread.
  399. fn new() -> Result<Self>;
  400. /// Creates a new webview runtime on any thread.
  401. #[cfg(any(windows, target_os = "linux"))]
  402. #[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "linux"))))]
  403. fn new_any_thread() -> Result<Self>;
  404. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  405. fn create_proxy(&self) -> Self::EventLoopProxy;
  406. /// Gets a runtime handle.
  407. fn handle(&self) -> Self::Handle;
  408. /// Gets the global shortcut manager.
  409. #[cfg(all(desktop, feature = "global-shortcut"))]
  410. fn global_shortcut_manager(&self) -> Self::GlobalShortcutManager;
  411. /// Gets the clipboard manager.
  412. #[cfg(feature = "clipboard")]
  413. fn clipboard_manager(&self) -> Self::ClipboardManager;
  414. /// Create a new webview window.
  415. fn create_window(&self, pending: PendingWindow<T, Self>) -> Result<DetachedWindow<T, Self>>;
  416. /// Adds the icon to the system tray with the specified menu items.
  417. #[cfg(all(desktop, feature = "system-tray"))]
  418. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  419. fn system_tray(&self, system_tray: SystemTray) -> Result<Self::TrayHandler>;
  420. /// Registers a system tray event handler.
  421. #[cfg(all(desktop, feature = "system-tray"))]
  422. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  423. fn on_system_tray_event<F: Fn(TrayId, &SystemTrayEvent) + Send + 'static>(&mut self, f: F);
  424. /// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
  425. #[cfg(target_os = "macos")]
  426. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  427. fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
  428. /// Shows the application, but does not automatically focus it.
  429. #[cfg(target_os = "macos")]
  430. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  431. fn show(&self);
  432. /// Hides the application.
  433. #[cfg(target_os = "macos")]
  434. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  435. fn hide(&self);
  436. /// Change the device event filter mode.
  437. ///
  438. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  439. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  440. /// the filter to explicitly capture them again.
  441. ///
  442. /// ## Platform-specific
  443. ///
  444. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  445. ///
  446. /// [`tao`]: https://crates.io/crates/tao
  447. fn set_device_event_filter(&mut self, filter: DeviceEventFilter);
  448. /// Runs the one step of the webview runtime event loop and returns control flow to the caller.
  449. #[cfg(desktop)]
  450. fn run_iteration<F: Fn(RunEvent<T>) + 'static>(&mut self, callback: F) -> RunIteration;
  451. /// Run the webview runtime.
  452. fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F);
  453. }
  454. /// Webview dispatcher. A thread-safe handle to the webview API.
  455. pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  456. /// The runtime this [`Dispatch`] runs under.
  457. type Runtime: Runtime<T>;
  458. /// The window builder type.
  459. type WindowBuilder: WindowBuilder;
  460. /// Run a task on the main thread.
  461. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  462. /// Registers a window event handler.
  463. fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> Uuid;
  464. /// Registers a window event handler.
  465. fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid;
  466. /// Open the web inspector which is usually called devtools.
  467. #[cfg(any(debug_assertions, feature = "devtools"))]
  468. fn open_devtools(&self);
  469. /// Close the web inspector which is usually called devtools.
  470. #[cfg(any(debug_assertions, feature = "devtools"))]
  471. fn close_devtools(&self);
  472. /// Gets the devtools window's current open state.
  473. #[cfg(any(debug_assertions, feature = "devtools"))]
  474. fn is_devtools_open(&self) -> Result<bool>;
  475. // GETTERS
  476. /// Returns the webview's current URL.
  477. fn url(&self) -> Result<Url>;
  478. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  479. fn scale_factor(&self) -> Result<f64>;
  480. /// 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.
  481. fn inner_position(&self) -> Result<PhysicalPosition<i32>>;
  482. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  483. fn outer_position(&self) -> Result<PhysicalPosition<i32>>;
  484. /// Returns the physical size of the window's client area.
  485. ///
  486. /// The client area is the content of the window, excluding the title bar and borders.
  487. fn inner_size(&self) -> Result<PhysicalSize<u32>>;
  488. /// Returns the physical size of the entire window.
  489. ///
  490. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  491. fn outer_size(&self) -> Result<PhysicalSize<u32>>;
  492. /// Gets the window's current fullscreen state.
  493. fn is_fullscreen(&self) -> Result<bool>;
  494. /// Gets the window's current minimized state.
  495. fn is_minimized(&self) -> Result<bool>;
  496. /// Gets the window's current maximized state.
  497. fn is_maximized(&self) -> Result<bool>;
  498. /// Gets the window's current focus state.
  499. fn is_focused(&self) -> Result<bool>;
  500. /// Gets the window’s current decoration state.
  501. fn is_decorated(&self) -> Result<bool>;
  502. /// Gets the window’s current resizable state.
  503. fn is_resizable(&self) -> Result<bool>;
  504. /// Gets the window's native maximize button state.
  505. ///
  506. /// ## Platform-specific
  507. ///
  508. /// - **Linux / iOS / Android:** Unsupported.
  509. fn is_maximizable(&self) -> Result<bool>;
  510. /// Gets the window's native minize button state.
  511. ///
  512. /// ## Platform-specific
  513. ///
  514. /// - **Linux / iOS / Android:** Unsupported.
  515. fn is_minimizable(&self) -> Result<bool>;
  516. /// Gets the window's native close button state.
  517. ///
  518. /// ## Platform-specific
  519. ///
  520. /// - **iOS / Android:** Unsupported.
  521. fn is_closable(&self) -> Result<bool>;
  522. /// Gets the window's current visibility state.
  523. fn is_visible(&self) -> Result<bool>;
  524. /// Gets the window's current title.
  525. fn title(&self) -> Result<String>;
  526. /// Gets the window menu current visibility state.
  527. fn is_menu_visible(&self) -> Result<bool>;
  528. /// Returns the monitor on which the window currently resides.
  529. ///
  530. /// Returns None if current monitor can't be detected.
  531. fn current_monitor(&self) -> Result<Option<Monitor>>;
  532. /// Returns the primary monitor of the system.
  533. ///
  534. /// Returns None if it can't identify any monitor as a primary one.
  535. fn primary_monitor(&self) -> Result<Option<Monitor>>;
  536. /// Returns the list of all the monitors available on the system.
  537. fn available_monitors(&self) -> Result<Vec<Monitor>>;
  538. /// Returns the `ApplicationWindow` from gtk crate that is used by this window.
  539. #[cfg(any(
  540. target_os = "linux",
  541. target_os = "dragonfly",
  542. target_os = "freebsd",
  543. target_os = "netbsd",
  544. target_os = "openbsd"
  545. ))]
  546. fn gtk_window(&self) -> Result<gtk::ApplicationWindow>;
  547. fn raw_window_handle(&self) -> Result<raw_window_handle::RawWindowHandle>;
  548. /// Returns the current window theme.
  549. fn theme(&self) -> Result<Theme>;
  550. // SETTERS
  551. /// Centers the window.
  552. fn center(&self) -> Result<()>;
  553. /// Opens the dialog to prints the contents of the webview.
  554. fn print(&self) -> Result<()>;
  555. /// Requests user attention to the window.
  556. ///
  557. /// Providing `None` will unset the request for user attention.
  558. fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()>;
  559. /// Create a new webview window.
  560. fn create_window(
  561. &mut self,
  562. pending: PendingWindow<T, Self::Runtime>,
  563. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  564. /// Updates the window resizable flag.
  565. fn set_resizable(&self, resizable: bool) -> Result<()>;
  566. /// Updates the window's native maximize button state.
  567. ///
  568. /// ## Platform-specific
  569. ///
  570. /// - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  571. /// - **Linux / iOS / Android:** Unsupported.
  572. fn set_maximizable(&self, maximizable: bool) -> Result<()>;
  573. /// Updates the window's native minimize button state.
  574. ///
  575. /// ## Platform-specific
  576. ///
  577. /// - **Linux / iOS / Android:** Unsupported.
  578. fn set_minimizable(&self, minimizable: bool) -> Result<()>;
  579. /// Updates the window's native close button state.
  580. ///
  581. /// ## Platform-specific
  582. ///
  583. /// - **Linux:** "GTK+ will do its best to convince the window manager not to show a close button.
  584. /// Depending on the system, this function may not have any effect when called on a window that is already visible"
  585. /// - **iOS / Android:** Unsupported.
  586. fn set_closable(&self, closable: bool) -> Result<()>;
  587. /// Updates the window title.
  588. fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
  589. /// Maximizes the window.
  590. fn maximize(&self) -> Result<()>;
  591. /// Unmaximizes the window.
  592. fn unmaximize(&self) -> Result<()>;
  593. /// Minimizes the window.
  594. fn minimize(&self) -> Result<()>;
  595. /// Unminimizes the window.
  596. fn unminimize(&self) -> Result<()>;
  597. /// Shows the window menu.
  598. fn show_menu(&self) -> Result<()>;
  599. /// Hides the window menu.
  600. fn hide_menu(&self) -> Result<()>;
  601. /// Shows the window.
  602. fn show(&self) -> Result<()>;
  603. /// Hides the window.
  604. fn hide(&self) -> Result<()>;
  605. /// Closes the window.
  606. fn close(&self) -> Result<()>;
  607. /// Updates the hasDecorations flag.
  608. fn set_decorations(&self, decorations: bool) -> Result<()>;
  609. /// Updates the window alwaysOnTop flag.
  610. fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;
  611. /// Prevents the window contents from being captured by other apps.
  612. fn set_content_protected(&self, protected: bool) -> Result<()>;
  613. /// Resizes the window.
  614. fn set_size(&self, size: Size) -> Result<()>;
  615. /// Updates the window min size.
  616. fn set_min_size(&self, size: Option<Size>) -> Result<()>;
  617. /// Updates the window max size.
  618. fn set_max_size(&self, size: Option<Size>) -> Result<()>;
  619. /// Updates the window position.
  620. fn set_position(&self, position: Position) -> Result<()>;
  621. /// Updates the window fullscreen state.
  622. fn set_fullscreen(&self, fullscreen: bool) -> Result<()>;
  623. /// Bring the window to front and focus.
  624. fn set_focus(&self) -> Result<()>;
  625. /// Updates the window icon.
  626. fn set_icon(&self, icon: Icon) -> Result<()>;
  627. /// Whether to hide the window icon from the taskbar or not.
  628. fn set_skip_taskbar(&self, skip: bool) -> Result<()>;
  629. /// Grabs the cursor, preventing it from leaving the window.
  630. ///
  631. /// There's no guarantee that the cursor will be hidden. You should
  632. /// hide it by yourself if you want so.
  633. fn set_cursor_grab(&self, grab: bool) -> Result<()>;
  634. /// Modifies the cursor's visibility.
  635. ///
  636. /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
  637. fn set_cursor_visible(&self, visible: bool) -> Result<()>;
  638. // Modifies the cursor icon of the window.
  639. fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>;
  640. /// Changes the position of the cursor in window coordinates.
  641. fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>;
  642. /// Ignores the window cursor events.
  643. fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()>;
  644. /// Starts dragging the window.
  645. fn start_dragging(&self) -> Result<()>;
  646. /// Executes javascript on the window this [`Dispatch`] represents.
  647. fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
  648. /// Applies the specified `update` to the menu item associated with the given `id`.
  649. fn update_menu_item(&self, id: u16, update: menu::MenuUpdate) -> Result<()>;
  650. }