lib.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. #[error("Invalid header name: {0}")]
  230. InvalidHeaderName(#[from] InvalidHeaderName),
  231. #[error("Invalid header value: {0}")]
  232. InvalidHeaderValue(#[from] InvalidHeaderValue),
  233. #[error("Invalid uri: {0}")]
  234. InvalidUri(#[from] InvalidUri),
  235. #[error("Invalid status code: {0}")]
  236. InvalidStatusCode(#[from] InvalidStatusCode),
  237. #[error("Invalid method: {0}")]
  238. InvalidMethod(#[from] InvalidMethod),
  239. #[error("Infallible error, something went really wrong: {0}")]
  240. Infallible(#[from] std::convert::Infallible),
  241. #[error("the event loop has been closed")]
  242. EventLoopClosed,
  243. }
  244. /// Result type.
  245. pub type Result<T> = std::result::Result<T, Error>;
  246. /// Window icon.
  247. #[derive(Debug, Clone)]
  248. pub struct Icon {
  249. /// RGBA bytes of the icon.
  250. pub rgba: Vec<u8>,
  251. /// Icon width.
  252. pub width: u32,
  253. /// Icon height.
  254. pub height: u32,
  255. }
  256. /// A type that can be used as an user event.
  257. pub trait UserEvent: Debug + Clone + Send + 'static {}
  258. impl<T: Debug + Clone + Send + 'static> UserEvent for T {}
  259. /// Event triggered on the event loop run.
  260. #[non_exhaustive]
  261. pub enum RunEvent<T: UserEvent> {
  262. /// Event loop is exiting.
  263. Exit,
  264. /// Event loop is about to exit
  265. ExitRequested {
  266. tx: Sender<ExitRequestedEventAction>,
  267. },
  268. /// An event associated with a window.
  269. WindowEvent {
  270. /// The window label.
  271. label: String,
  272. /// The detailed event.
  273. event: WindowEvent,
  274. },
  275. /// Application ready.
  276. Ready,
  277. /// Sent if the event loop is being resumed.
  278. Resumed,
  279. /// Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.
  280. ///
  281. /// 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.
  282. MainEventsCleared,
  283. /// A custom event defined by the user.
  284. UserEvent(T),
  285. }
  286. /// Action to take when the event loop is about to exit
  287. #[derive(Debug)]
  288. pub enum ExitRequestedEventAction {
  289. /// Prevent the event loop from exiting
  290. Prevent,
  291. }
  292. /// A system tray event.
  293. #[derive(Debug)]
  294. pub enum SystemTrayEvent {
  295. MenuItemClick(u16),
  296. LeftClick {
  297. position: PhysicalPosition<f64>,
  298. size: PhysicalSize<f64>,
  299. },
  300. RightClick {
  301. position: PhysicalPosition<f64>,
  302. size: PhysicalSize<f64>,
  303. },
  304. DoubleClick {
  305. position: PhysicalPosition<f64>,
  306. size: PhysicalSize<f64>,
  307. },
  308. }
  309. /// Metadata for a runtime event loop iteration on `run_iteration`.
  310. #[derive(Debug, Clone, Default)]
  311. pub struct RunIteration {
  312. pub window_count: usize,
  313. }
  314. /// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
  315. #[cfg(target_os = "macos")]
  316. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  317. #[non_exhaustive]
  318. pub enum ActivationPolicy {
  319. /// Corresponds to NSApplicationActivationPolicyRegular.
  320. Regular,
  321. /// Corresponds to NSApplicationActivationPolicyAccessory.
  322. Accessory,
  323. /// Corresponds to NSApplicationActivationPolicyProhibited.
  324. Prohibited,
  325. }
  326. /// A [`Send`] handle to the runtime.
  327. pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  328. type Runtime: Runtime<T, Handle = Self>;
  329. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  330. fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy;
  331. /// Create a new webview window.
  332. fn create_window(
  333. &self,
  334. pending: PendingWindow<T, Self::Runtime>,
  335. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  336. /// Run a task on the main thread.
  337. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  338. /// Adds an icon to the system tray with the specified menu items.
  339. #[cfg(all(desktop, feature = "system-tray"))]
  340. #[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "system-tray"))))]
  341. fn system_tray(
  342. &self,
  343. system_tray: SystemTray,
  344. ) -> Result<<Self::Runtime as Runtime<T>>::TrayHandler>;
  345. fn raw_display_handle(&self) -> RawDisplayHandle;
  346. /// Shows the application, but does not automatically focus it.
  347. #[cfg(target_os = "macos")]
  348. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  349. fn show(&self) -> Result<()>;
  350. /// Hides the application.
  351. #[cfg(target_os = "macos")]
  352. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  353. fn hide(&self) -> Result<()>;
  354. /// Finds an Android class in the project scope.
  355. #[cfg(target_os = "android")]
  356. fn find_class<'a>(
  357. &'a self,
  358. env: jni::JNIEnv<'a>,
  359. activity: jni::objects::JObject<'a>,
  360. name: impl Into<String>,
  361. ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error>;
  362. /// Dispatch a closure to run on the Android context.
  363. ///
  364. /// The closure takes the JNI env, the Android activity instance and the possibly null webview.
  365. #[cfg(target_os = "android")]
  366. fn run_on_android_context<F>(&self, f: F)
  367. where
  368. F: FnOnce(jni::JNIEnv<'_>, jni::objects::JObject<'_>, jni::objects::JObject<'_>)
  369. + Send
  370. + 'static;
  371. }
  372. /// A global shortcut manager.
  373. #[cfg(all(desktop, feature = "global-shortcut"))]
  374. pub trait GlobalShortcutManager: Debug + Clone + Send + Sync {
  375. /// Whether the application has registered the given `accelerator`.
  376. fn is_registered(&self, accelerator: &str) -> Result<bool>;
  377. /// Register a global shortcut of `accelerator`.
  378. fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()>;
  379. /// Unregister all accelerators registered by the manager instance.
  380. fn unregister_all(&mut self) -> Result<()>;
  381. /// Unregister the provided `accelerator`.
  382. fn unregister(&mut self, accelerator: &str) -> Result<()>;
  383. }
  384. pub trait EventLoopProxy<T: UserEvent>: Debug + Clone + Send + Sync {
  385. fn send_event(&self, event: T) -> Result<()>;
  386. }
  387. /// The webview runtime interface.
  388. pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
  389. /// The message dispatcher.
  390. type Dispatcher: Dispatch<T, Runtime = Self>;
  391. /// The runtime handle type.
  392. type Handle: RuntimeHandle<T, Runtime = Self>;
  393. /// The global shortcut manager type.
  394. #[cfg(all(desktop, feature = "global-shortcut"))]
  395. type GlobalShortcutManager: GlobalShortcutManager;
  396. /// The tray handler type.
  397. #[cfg(all(desktop, feature = "system-tray"))]
  398. type TrayHandler: menu::TrayHandle;
  399. /// The proxy type.
  400. type EventLoopProxy: EventLoopProxy<T>;
  401. /// Creates a new webview runtime. Must be used on the main thread.
  402. fn new() -> Result<Self>;
  403. /// Creates a new webview runtime on any thread.
  404. #[cfg(any(windows, target_os = "linux"))]
  405. #[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "linux"))))]
  406. fn new_any_thread() -> Result<Self>;
  407. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  408. fn create_proxy(&self) -> Self::EventLoopProxy;
  409. /// Gets a runtime handle.
  410. fn handle(&self) -> Self::Handle;
  411. /// Gets the global shortcut manager.
  412. #[cfg(all(desktop, feature = "global-shortcut"))]
  413. fn global_shortcut_manager(&self) -> Self::GlobalShortcutManager;
  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. /// Runs a closure with the platform webview object as argument.
  467. fn with_webview<F: FnOnce(Box<dyn std::any::Any>) + Send + 'static>(&self, f: F) -> Result<()>;
  468. /// Open the web inspector which is usually called devtools.
  469. #[cfg(any(debug_assertions, feature = "devtools"))]
  470. fn open_devtools(&self);
  471. /// Close the web inspector which is usually called devtools.
  472. #[cfg(any(debug_assertions, feature = "devtools"))]
  473. fn close_devtools(&self);
  474. /// Gets the devtools window's current open state.
  475. #[cfg(any(debug_assertions, feature = "devtools"))]
  476. fn is_devtools_open(&self) -> Result<bool>;
  477. // GETTERS
  478. /// Returns the webview's current URL.
  479. fn url(&self) -> Result<Url>;
  480. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  481. fn scale_factor(&self) -> Result<f64>;
  482. /// 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.
  483. fn inner_position(&self) -> Result<PhysicalPosition<i32>>;
  484. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  485. fn outer_position(&self) -> Result<PhysicalPosition<i32>>;
  486. /// Returns the physical size of the window's client area.
  487. ///
  488. /// The client area is the content of the window, excluding the title bar and borders.
  489. fn inner_size(&self) -> Result<PhysicalSize<u32>>;
  490. /// Returns the physical size of the entire window.
  491. ///
  492. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  493. fn outer_size(&self) -> Result<PhysicalSize<u32>>;
  494. /// Gets the window's current fullscreen state.
  495. fn is_fullscreen(&self) -> Result<bool>;
  496. /// Gets the window's current minimized state.
  497. fn is_minimized(&self) -> Result<bool>;
  498. /// Gets the window's current maximized state.
  499. fn is_maximized(&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 current visibility state.
  505. fn is_visible(&self) -> Result<bool>;
  506. /// Gets the window's current title.
  507. fn title(&self) -> Result<String>;
  508. /// Gets the window menu current visibility state.
  509. fn is_menu_visible(&self) -> Result<bool>;
  510. /// Returns the monitor on which the window currently resides.
  511. ///
  512. /// Returns None if current monitor can't be detected.
  513. fn current_monitor(&self) -> Result<Option<Monitor>>;
  514. /// Returns the primary monitor of the system.
  515. ///
  516. /// Returns None if it can't identify any monitor as a primary one.
  517. fn primary_monitor(&self) -> Result<Option<Monitor>>;
  518. /// Returns the list of all the monitors available on the system.
  519. fn available_monitors(&self) -> Result<Vec<Monitor>>;
  520. /// Returns the `ApplicationWindow` from gtk crate that is used by this window.
  521. #[cfg(any(
  522. target_os = "linux",
  523. target_os = "dragonfly",
  524. target_os = "freebsd",
  525. target_os = "netbsd",
  526. target_os = "openbsd"
  527. ))]
  528. fn gtk_window(&self) -> Result<gtk::ApplicationWindow>;
  529. fn raw_window_handle(&self) -> Result<raw_window_handle::RawWindowHandle>;
  530. /// Returns the current window theme.
  531. fn theme(&self) -> Result<Theme>;
  532. // SETTERS
  533. /// Centers the window.
  534. fn center(&self) -> Result<()>;
  535. /// Opens the dialog to prints the contents of the webview.
  536. fn print(&self) -> Result<()>;
  537. /// Requests user attention to the window.
  538. ///
  539. /// Providing `None` will unset the request for user attention.
  540. fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()>;
  541. /// Create a new webview window.
  542. fn create_window(
  543. &mut self,
  544. pending: PendingWindow<T, Self::Runtime>,
  545. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  546. /// Updates the window resizable flag.
  547. fn set_resizable(&self, resizable: bool) -> Result<()>;
  548. /// Updates the window title.
  549. fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
  550. /// Maximizes the window.
  551. fn maximize(&self) -> Result<()>;
  552. /// Unmaximizes the window.
  553. fn unmaximize(&self) -> Result<()>;
  554. /// Minimizes the window.
  555. fn minimize(&self) -> Result<()>;
  556. /// Unminimizes the window.
  557. fn unminimize(&self) -> Result<()>;
  558. /// Shows the window menu.
  559. fn show_menu(&self) -> Result<()>;
  560. /// Hides the window menu.
  561. fn hide_menu(&self) -> Result<()>;
  562. /// Shows the window.
  563. fn show(&self) -> Result<()>;
  564. /// Hides the window.
  565. fn hide(&self) -> Result<()>;
  566. /// Closes the window.
  567. fn close(&self) -> Result<()>;
  568. /// Updates the decorations flag.
  569. fn set_decorations(&self, decorations: bool) -> Result<()>;
  570. /// Updates the shadow flag.
  571. fn set_shadow(&self, enable: bool) -> Result<()>;
  572. /// Updates the window alwaysOnTop flag.
  573. fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;
  574. /// Prevents the window contents from being captured by other apps.
  575. fn set_content_protected(&self, protected: bool) -> Result<()>;
  576. /// Resizes the window.
  577. fn set_size(&self, size: Size) -> Result<()>;
  578. /// Updates the window min size.
  579. fn set_min_size(&self, size: Option<Size>) -> Result<()>;
  580. /// Updates the window max size.
  581. fn set_max_size(&self, size: Option<Size>) -> Result<()>;
  582. /// Updates the window position.
  583. fn set_position(&self, position: Position) -> Result<()>;
  584. /// Updates the window fullscreen state.
  585. fn set_fullscreen(&self, fullscreen: bool) -> Result<()>;
  586. /// Bring the window to front and focus.
  587. fn set_focus(&self) -> Result<()>;
  588. /// Updates the window icon.
  589. fn set_icon(&self, icon: Icon) -> Result<()>;
  590. /// Whether to hide the window icon from the taskbar or not.
  591. fn set_skip_taskbar(&self, skip: bool) -> Result<()>;
  592. /// Grabs the cursor, preventing it from leaving the window.
  593. ///
  594. /// There's no guarantee that the cursor will be hidden. You should
  595. /// hide it by yourself if you want so.
  596. fn set_cursor_grab(&self, grab: bool) -> Result<()>;
  597. /// Modifies the cursor's visibility.
  598. ///
  599. /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
  600. fn set_cursor_visible(&self, visible: bool) -> Result<()>;
  601. // Modifies the cursor icon of the window.
  602. fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>;
  603. /// Changes the position of the cursor in window coordinates.
  604. fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>;
  605. /// Ignores the window cursor events.
  606. fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()>;
  607. /// Starts dragging the window.
  608. fn start_dragging(&self) -> Result<()>;
  609. /// Executes javascript on the window this [`Dispatch`] represents.
  610. fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
  611. /// Applies the specified `update` to the menu item associated with the given `id`.
  612. fn update_menu_item(&self, id: u16, update: menu::MenuUpdate) -> Result<()>;
  613. }