lib.rs 22 KB

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