lib.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! [![](https://github.com/tauri-apps/tauri/raw/dev/.github/splash.png)](https://tauri.app)
  5. //!
  6. //! Internal runtime between Tauri and the underlying webview runtime.
  7. #![doc(
  8. html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
  9. html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
  10. )]
  11. #![cfg_attr(docsrs, feature(doc_cfg))]
  12. use raw_window_handle::DisplayHandle;
  13. use serde::Deserialize;
  14. use std::{borrow::Cow, fmt::Debug, sync::mpsc::Sender};
  15. use tauri_utils::Theme;
  16. use url::Url;
  17. use webview::{DetachedWebview, PendingWebview};
  18. /// Types useful for interacting with a user's monitors.
  19. pub mod monitor;
  20. pub mod webview;
  21. pub mod window;
  22. use monitor::Monitor;
  23. use window::{
  24. dpi::{PhysicalPosition, PhysicalSize, Position, Size},
  25. CursorIcon, DetachedWindow, PendingWindow, RawWindow, WebviewEvent, WindowEvent,
  26. };
  27. use window::{WindowBuilder, WindowId};
  28. use http::{
  29. header::{InvalidHeaderName, InvalidHeaderValue},
  30. method::InvalidMethod,
  31. status::InvalidStatusCode,
  32. };
  33. pub type WindowEventId = u32;
  34. pub type WebviewEventId = u32;
  35. /// Progress bar status.
  36. #[derive(Debug, Clone, Copy, Deserialize)]
  37. #[serde(rename_all = "camelCase")]
  38. pub enum ProgressBarStatus {
  39. /// Hide progress bar.
  40. None,
  41. /// Normal state.
  42. Normal,
  43. /// Indeterminate state. **Treated as Normal on Linux and macOS**
  44. Indeterminate,
  45. /// Paused state. **Treated as Normal on Linux**
  46. Paused,
  47. /// Error state. **Treated as Normal on Linux**
  48. Error,
  49. }
  50. /// Progress Bar State
  51. #[derive(Debug, Deserialize)]
  52. #[serde(rename_all = "camelCase")]
  53. pub struct ProgressBarState {
  54. /// The progress bar status.
  55. pub status: Option<ProgressBarStatus>,
  56. /// The progress bar progress. This can be a value ranging from `0` to `100`
  57. pub progress: Option<u64>,
  58. /// The `.desktop` filename with the Unity desktop window manager, for example `myapp.desktop` **Linux Only**
  59. pub desktop_filename: Option<String>,
  60. }
  61. /// Type of user attention requested on a window.
  62. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  63. #[serde(tag = "type")]
  64. pub enum UserAttentionType {
  65. /// ## Platform-specific
  66. /// - **macOS:** Bounces the dock icon until the application is in focus.
  67. /// - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
  68. Critical,
  69. /// ## Platform-specific
  70. /// - **macOS:** Bounces the dock icon once.
  71. /// - **Windows:** Flashes the taskbar button until the application is in focus.
  72. Informational,
  73. }
  74. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  75. #[serde(tag = "type")]
  76. pub enum DeviceEventFilter {
  77. /// Always filter out device events.
  78. Always,
  79. /// Filter out device events while the window is not focused.
  80. Unfocused,
  81. /// Report all device events regardless of window focus.
  82. Never,
  83. }
  84. impl Default for DeviceEventFilter {
  85. fn default() -> Self {
  86. Self::Unfocused
  87. }
  88. }
  89. /// Defines the orientation that a window resize will be performed.
  90. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  91. pub enum ResizeDirection {
  92. East,
  93. North,
  94. NorthEast,
  95. NorthWest,
  96. South,
  97. SouthEast,
  98. SouthWest,
  99. West,
  100. }
  101. #[derive(Debug, thiserror::Error)]
  102. #[non_exhaustive]
  103. pub enum Error {
  104. /// Failed to create webview.
  105. #[error("failed to create webview: {0}")]
  106. CreateWebview(Box<dyn std::error::Error + Send + Sync>),
  107. /// Failed to create window.
  108. #[error("failed to create window")]
  109. CreateWindow,
  110. /// The given window label is invalid.
  111. #[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")]
  112. InvalidWindowLabel,
  113. /// Failed to send message to webview.
  114. #[error("failed to send message to the webview")]
  115. FailedToSendMessage,
  116. /// Failed to receive message from webview.
  117. #[error("failed to receive message from webview")]
  118. FailedToReceiveMessage,
  119. /// Failed to serialize/deserialize.
  120. #[error("JSON error: {0}")]
  121. Json(#[from] serde_json::Error),
  122. /// Failed to load window icon.
  123. #[error("invalid icon: {0}")]
  124. InvalidIcon(Box<dyn std::error::Error + Send + Sync>),
  125. /// Failed to get monitor on window operation.
  126. #[error("failed to get monitor")]
  127. FailedToGetMonitor,
  128. #[error("Invalid header name: {0}")]
  129. InvalidHeaderName(#[from] InvalidHeaderName),
  130. #[error("Invalid header value: {0}")]
  131. InvalidHeaderValue(#[from] InvalidHeaderValue),
  132. #[error("Invalid status code: {0}")]
  133. InvalidStatusCode(#[from] InvalidStatusCode),
  134. #[error("Invalid method: {0}")]
  135. InvalidMethod(#[from] InvalidMethod),
  136. #[error("Infallible error, something went really wrong: {0}")]
  137. Infallible(#[from] std::convert::Infallible),
  138. #[error("the event loop has been closed")]
  139. EventLoopClosed,
  140. #[error("Invalid proxy url")]
  141. InvalidProxyUrl,
  142. #[error("window not found")]
  143. WindowNotFound,
  144. }
  145. /// Result type.
  146. pub type Result<T> = std::result::Result<T, Error>;
  147. /// Window icon.
  148. #[derive(Debug, Clone)]
  149. pub struct Icon<'a> {
  150. /// RGBA bytes of the icon.
  151. pub rgba: Cow<'a, [u8]>,
  152. /// Icon width.
  153. pub width: u32,
  154. /// Icon height.
  155. pub height: u32,
  156. }
  157. /// A type that can be used as an user event.
  158. pub trait UserEvent: Debug + Clone + Send + 'static {}
  159. impl<T: Debug + Clone + Send + 'static> UserEvent for T {}
  160. /// Event triggered on the event loop run.
  161. #[derive(Debug)]
  162. #[non_exhaustive]
  163. pub enum RunEvent<T: UserEvent> {
  164. /// Event loop is exiting.
  165. Exit,
  166. /// Event loop is about to exit
  167. ExitRequested {
  168. /// The exit code.
  169. code: Option<i32>,
  170. tx: Sender<ExitRequestedEventAction>,
  171. },
  172. /// An event associated with a window.
  173. WindowEvent {
  174. /// The window label.
  175. label: String,
  176. /// The detailed event.
  177. event: WindowEvent,
  178. },
  179. /// An event associated with a webview.
  180. WebviewEvent {
  181. /// The webview label.
  182. label: String,
  183. /// The detailed event.
  184. event: WebviewEvent,
  185. },
  186. /// Application ready.
  187. Ready,
  188. /// Sent if the event loop is being resumed.
  189. Resumed,
  190. /// Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.
  191. ///
  192. /// 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.
  193. MainEventsCleared,
  194. /// Emitted when the user wants to open the specified resource with the app.
  195. #[cfg(any(target_os = "macos", target_os = "ios"))]
  196. Opened { urls: Vec<url::Url> },
  197. /// A custom event defined by the user.
  198. UserEvent(T),
  199. }
  200. /// Action to take when the event loop is about to exit
  201. #[derive(Debug)]
  202. pub enum ExitRequestedEventAction {
  203. /// Prevent the event loop from exiting
  204. Prevent,
  205. }
  206. /// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
  207. #[cfg(target_os = "macos")]
  208. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  209. #[non_exhaustive]
  210. pub enum ActivationPolicy {
  211. /// Corresponds to NSApplicationActivationPolicyRegular.
  212. Regular,
  213. /// Corresponds to NSApplicationActivationPolicyAccessory.
  214. Accessory,
  215. /// Corresponds to NSApplicationActivationPolicyProhibited.
  216. Prohibited,
  217. }
  218. /// A [`Send`] handle to the runtime.
  219. pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  220. type Runtime: Runtime<T, Handle = Self>;
  221. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  222. fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy;
  223. /// Sets the activation policy for the application.
  224. #[cfg(target_os = "macos")]
  225. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  226. fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()>;
  227. /// Requests an exit of the event loop.
  228. fn request_exit(&self, code: i32) -> Result<()>;
  229. /// Create a new window.
  230. fn create_window<F: Fn(RawWindow) + Send + 'static>(
  231. &self,
  232. pending: PendingWindow<T, Self::Runtime>,
  233. before_window_creation: Option<F>,
  234. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  235. /// Create a new webview.
  236. fn create_webview(
  237. &self,
  238. window_id: WindowId,
  239. pending: PendingWebview<T, Self::Runtime>,
  240. ) -> Result<DetachedWebview<T, Self::Runtime>>;
  241. /// Run a task on the main thread.
  242. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  243. fn display_handle(&self) -> std::result::Result<DisplayHandle, raw_window_handle::HandleError>;
  244. fn primary_monitor(&self) -> Option<Monitor>;
  245. fn available_monitors(&self) -> Vec<Monitor>;
  246. /// Shows the application, but does not automatically focus it.
  247. #[cfg(target_os = "macos")]
  248. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  249. fn show(&self) -> Result<()>;
  250. /// Hides the application.
  251. #[cfg(target_os = "macos")]
  252. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  253. fn hide(&self) -> Result<()>;
  254. /// Finds an Android class in the project scope.
  255. #[cfg(target_os = "android")]
  256. fn find_class<'a>(
  257. &self,
  258. env: &mut jni::JNIEnv<'a>,
  259. activity: &jni::objects::JObject<'_>,
  260. name: impl Into<String>,
  261. ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error>;
  262. /// Dispatch a closure to run on the Android context.
  263. ///
  264. /// The closure takes the JNI env, the Android activity instance and the possibly null webview.
  265. #[cfg(target_os = "android")]
  266. fn run_on_android_context<F>(&self, f: F)
  267. where
  268. F: FnOnce(&mut jni::JNIEnv, &jni::objects::JObject, &jni::objects::JObject) + Send + 'static;
  269. }
  270. pub trait EventLoopProxy<T: UserEvent>: Debug + Clone + Send + Sync {
  271. fn send_event(&self, event: T) -> Result<()>;
  272. }
  273. #[derive(Default)]
  274. pub struct RuntimeInitArgs {
  275. #[cfg(windows)]
  276. pub msg_hook: Option<Box<dyn FnMut(*const std::ffi::c_void) -> bool + 'static>>,
  277. }
  278. /// The webview runtime interface.
  279. pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
  280. /// The window message dispatcher.
  281. type WindowDispatcher: WindowDispatch<T, Runtime = Self>;
  282. /// The webview message dispatcher.
  283. type WebviewDispatcher: WebviewDispatch<T, Runtime = Self>;
  284. /// The runtime handle type.
  285. type Handle: RuntimeHandle<T, Runtime = Self>;
  286. /// The proxy type.
  287. type EventLoopProxy: EventLoopProxy<T>;
  288. /// Creates a new webview runtime. Must be used on the main thread.
  289. fn new(args: RuntimeInitArgs) -> Result<Self>;
  290. /// Creates a new webview runtime on any thread.
  291. #[cfg(any(windows, target_os = "linux"))]
  292. #[cfg_attr(docsrs, doc(cfg(any(windows, target_os = "linux"))))]
  293. fn new_any_thread(args: RuntimeInitArgs) -> Result<Self>;
  294. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  295. fn create_proxy(&self) -> Self::EventLoopProxy;
  296. /// Gets a runtime handle.
  297. fn handle(&self) -> Self::Handle;
  298. /// Create a new window.
  299. fn create_window<F: Fn(RawWindow) + Send + 'static>(
  300. &self,
  301. pending: PendingWindow<T, Self>,
  302. after_window_creation: Option<F>,
  303. ) -> Result<DetachedWindow<T, Self>>;
  304. /// Create a new webview.
  305. fn create_webview(
  306. &self,
  307. window_id: WindowId,
  308. pending: PendingWebview<T, Self>,
  309. ) -> Result<DetachedWebview<T, Self>>;
  310. fn primary_monitor(&self) -> Option<Monitor>;
  311. fn available_monitors(&self) -> Vec<Monitor>;
  312. /// Sets the activation policy for the application.
  313. #[cfg(target_os = "macos")]
  314. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  315. fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
  316. /// Shows the application, but does not automatically focus it.
  317. #[cfg(target_os = "macos")]
  318. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  319. fn show(&self);
  320. /// Hides the application.
  321. #[cfg(target_os = "macos")]
  322. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  323. fn hide(&self);
  324. /// Change the device event filter mode.
  325. ///
  326. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  327. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  328. /// the filter to explicitly capture them again.
  329. ///
  330. /// ## Platform-specific
  331. ///
  332. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  333. ///
  334. /// [`tao`]: https://crates.io/crates/tao
  335. fn set_device_event_filter(&mut self, filter: DeviceEventFilter);
  336. /// Runs an iteration of the runtime event loop and returns control flow to the caller.
  337. #[cfg(desktop)]
  338. fn run_iteration<F: FnMut(RunEvent<T>) + 'static>(&mut self, callback: F);
  339. /// Run the webview runtime.
  340. fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F);
  341. }
  342. /// Webview dispatcher. A thread-safe handle to the webview APIs.
  343. pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  344. /// The runtime this [`WebviewDispatch`] runs under.
  345. type Runtime: Runtime<T>;
  346. /// Run a task on the main thread.
  347. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  348. /// Registers a webview event handler.
  349. fn on_webview_event<F: Fn(&WebviewEvent) + Send + 'static>(&self, f: F) -> WebviewEventId;
  350. /// Runs a closure with the platform webview object as argument.
  351. fn with_webview<F: FnOnce(Box<dyn std::any::Any>) + Send + 'static>(&self, f: F) -> Result<()>;
  352. /// Open the web inspector which is usually called devtools.
  353. #[cfg(any(debug_assertions, feature = "devtools"))]
  354. fn open_devtools(&self);
  355. /// Close the web inspector which is usually called devtools.
  356. #[cfg(any(debug_assertions, feature = "devtools"))]
  357. fn close_devtools(&self);
  358. /// Gets the devtools window's current open state.
  359. #[cfg(any(debug_assertions, feature = "devtools"))]
  360. fn is_devtools_open(&self) -> Result<bool>;
  361. // GETTERS
  362. /// Returns the webview's current URL.
  363. fn url(&self) -> Result<Url>;
  364. /// Returns the position of the top-left hand corner of the webviews's client area relative to the top-left hand corner of the window.
  365. fn position(&self) -> Result<PhysicalPosition<i32>>;
  366. /// Returns the physical size of the webviews's client area.
  367. fn size(&self) -> Result<PhysicalSize<u32>>;
  368. // SETTER
  369. /// Navigate to the given URL.
  370. fn navigate(&self, url: Url) -> Result<()>;
  371. /// Opens the dialog to prints the contents of the webview.
  372. fn print(&self) -> Result<()>;
  373. /// Closes the webview.
  374. fn close(&self) -> Result<()>;
  375. /// Resizes the webview.
  376. fn set_size(&self, size: Size) -> Result<()>;
  377. /// Updates the webview position.
  378. fn set_position(&self, position: Position) -> Result<()>;
  379. /// Bring the window to front and focus the webview.
  380. fn set_focus(&self) -> Result<()>;
  381. /// Executes javascript on the window this [`WindowDispatch`] represents.
  382. fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
  383. /// Moves the webview to the given window.
  384. fn reparent(&self, window_id: WindowId) -> Result<()>;
  385. /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
  386. fn set_auto_resize(&self, auto_resize: bool) -> Result<()>;
  387. }
  388. /// Window dispatcher. A thread-safe handle to the window APIs.
  389. pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  390. /// The runtime this [`WindowDispatch`] runs under.
  391. type Runtime: Runtime<T>;
  392. /// The window builder type.
  393. type WindowBuilder: WindowBuilder;
  394. /// Run a task on the main thread.
  395. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  396. /// Registers a window event handler.
  397. fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> WindowEventId;
  398. // GETTERS
  399. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  400. fn scale_factor(&self) -> Result<f64>;
  401. /// 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.
  402. fn inner_position(&self) -> Result<PhysicalPosition<i32>>;
  403. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  404. fn outer_position(&self) -> Result<PhysicalPosition<i32>>;
  405. /// Returns the physical size of the window's client area.
  406. ///
  407. /// The client area is the content of the window, excluding the title bar and borders.
  408. fn inner_size(&self) -> Result<PhysicalSize<u32>>;
  409. /// Returns the physical size of the entire window.
  410. ///
  411. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  412. fn outer_size(&self) -> Result<PhysicalSize<u32>>;
  413. /// Gets the window's current fullscreen state.
  414. fn is_fullscreen(&self) -> Result<bool>;
  415. /// Gets the window's current minimized state.
  416. fn is_minimized(&self) -> Result<bool>;
  417. /// Gets the window's current maximized state.
  418. fn is_maximized(&self) -> Result<bool>;
  419. /// Gets the window's current focus state.
  420. fn is_focused(&self) -> Result<bool>;
  421. /// Gets the window’s current decoration state.
  422. fn is_decorated(&self) -> Result<bool>;
  423. /// Gets the window’s current resizable state.
  424. fn is_resizable(&self) -> Result<bool>;
  425. /// Gets the window's native maximize button state.
  426. ///
  427. /// ## Platform-specific
  428. ///
  429. /// - **Linux / iOS / Android:** Unsupported.
  430. fn is_maximizable(&self) -> Result<bool>;
  431. /// Gets the window's native minimize button state.
  432. ///
  433. /// ## Platform-specific
  434. ///
  435. /// - **Linux / iOS / Android:** Unsupported.
  436. fn is_minimizable(&self) -> Result<bool>;
  437. /// Gets the window's native close button state.
  438. ///
  439. /// ## Platform-specific
  440. ///
  441. /// - **iOS / Android:** Unsupported.
  442. fn is_closable(&self) -> Result<bool>;
  443. /// Gets the window's current visibility state.
  444. fn is_visible(&self) -> Result<bool>;
  445. /// Gets the window's current title.
  446. fn title(&self) -> Result<String>;
  447. /// Returns the monitor on which the window currently resides.
  448. ///
  449. /// Returns None if current monitor can't be detected.
  450. fn current_monitor(&self) -> Result<Option<Monitor>>;
  451. /// Returns the primary monitor of the system.
  452. ///
  453. /// Returns None if it can't identify any monitor as a primary one.
  454. fn primary_monitor(&self) -> Result<Option<Monitor>>;
  455. /// Returns the list of all the monitors available on the system.
  456. fn available_monitors(&self) -> Result<Vec<Monitor>>;
  457. /// Returns the `ApplicationWindow` from gtk crate that is used by this window.
  458. #[cfg(any(
  459. target_os = "linux",
  460. target_os = "dragonfly",
  461. target_os = "freebsd",
  462. target_os = "netbsd",
  463. target_os = "openbsd"
  464. ))]
  465. fn gtk_window(&self) -> Result<gtk::ApplicationWindow>;
  466. /// Returns the vertical [`gtk::Box`] that is added by default as the sole child of this window.
  467. #[cfg(any(
  468. target_os = "linux",
  469. target_os = "dragonfly",
  470. target_os = "freebsd",
  471. target_os = "netbsd",
  472. target_os = "openbsd"
  473. ))]
  474. fn default_vbox(&self) -> Result<gtk::Box>;
  475. /// Raw window handle.
  476. fn window_handle(
  477. &self,
  478. ) -> std::result::Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError>;
  479. /// Returns the current window theme.
  480. fn theme(&self) -> Result<Theme>;
  481. // SETTERS
  482. /// Centers the window.
  483. fn center(&self) -> Result<()>;
  484. /// Requests user attention to the window.
  485. ///
  486. /// Providing `None` will unset the request for user attention.
  487. fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()>;
  488. /// Create a new window.
  489. fn create_window<F: Fn(RawWindow) + Send + 'static>(
  490. &mut self,
  491. pending: PendingWindow<T, Self::Runtime>,
  492. after_window_creation: Option<F>,
  493. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  494. /// Create a new webview.
  495. fn create_webview(
  496. &mut self,
  497. pending: PendingWebview<T, Self::Runtime>,
  498. ) -> Result<DetachedWebview<T, Self::Runtime>>;
  499. /// Updates the window resizable flag.
  500. fn set_resizable(&self, resizable: bool) -> Result<()>;
  501. /// Updates the window's native maximize button state.
  502. ///
  503. /// ## Platform-specific
  504. ///
  505. /// - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  506. /// - **Linux / iOS / Android:** Unsupported.
  507. fn set_maximizable(&self, maximizable: bool) -> Result<()>;
  508. /// Updates the window's native minimize button state.
  509. ///
  510. /// ## Platform-specific
  511. ///
  512. /// - **Linux / iOS / Android:** Unsupported.
  513. fn set_minimizable(&self, minimizable: bool) -> Result<()>;
  514. /// Updates the window's native close button state.
  515. ///
  516. /// ## Platform-specific
  517. ///
  518. /// - **Linux:** "GTK+ will do its best to convince the window manager not to show a close button.
  519. /// Depending on the system, this function may not have any effect when called on a window that is already visible"
  520. /// - **iOS / Android:** Unsupported.
  521. fn set_closable(&self, closable: bool) -> Result<()>;
  522. /// Updates the window title.
  523. fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
  524. /// Maximizes the window.
  525. fn maximize(&self) -> Result<()>;
  526. /// Unmaximizes the window.
  527. fn unmaximize(&self) -> Result<()>;
  528. /// Minimizes the window.
  529. fn minimize(&self) -> Result<()>;
  530. /// Unminimizes the window.
  531. fn unminimize(&self) -> Result<()>;
  532. /// Shows the window.
  533. fn show(&self) -> Result<()>;
  534. /// Hides the window.
  535. fn hide(&self) -> Result<()>;
  536. /// Closes the window.
  537. fn close(&self) -> Result<()>;
  538. /// Destroys the window.
  539. fn destroy(&self) -> Result<()>;
  540. /// Updates the decorations flag.
  541. fn set_decorations(&self, decorations: bool) -> Result<()>;
  542. /// Updates the shadow flag.
  543. fn set_shadow(&self, enable: bool) -> Result<()>;
  544. /// Updates the window alwaysOnBottom flag.
  545. fn set_always_on_bottom(&self, always_on_bottom: bool) -> Result<()>;
  546. /// Updates the window alwaysOnTop flag.
  547. fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;
  548. /// Updates the window visibleOnAllWorkspaces flag.
  549. fn set_visible_on_all_workspaces(&self, visible_on_all_workspaces: bool) -> Result<()>;
  550. /// Prevents the window contents from being captured by other apps.
  551. fn set_content_protected(&self, protected: bool) -> Result<()>;
  552. /// Resizes the window.
  553. fn set_size(&self, size: Size) -> Result<()>;
  554. /// Updates the window min size.
  555. fn set_min_size(&self, size: Option<Size>) -> Result<()>;
  556. /// Updates the window max size.
  557. fn set_max_size(&self, size: Option<Size>) -> Result<()>;
  558. /// Updates the window position.
  559. fn set_position(&self, position: Position) -> Result<()>;
  560. /// Updates the window fullscreen state.
  561. fn set_fullscreen(&self, fullscreen: bool) -> Result<()>;
  562. /// Bring the window to front and focus.
  563. fn set_focus(&self) -> Result<()>;
  564. /// Updates the window icon.
  565. fn set_icon(&self, icon: Icon) -> Result<()>;
  566. /// Whether to hide the window icon from the taskbar or not.
  567. fn set_skip_taskbar(&self, skip: bool) -> Result<()>;
  568. /// Grabs the cursor, preventing it from leaving the window.
  569. ///
  570. /// There's no guarantee that the cursor will be hidden. You should
  571. /// hide it by yourself if you want so.
  572. fn set_cursor_grab(&self, grab: bool) -> Result<()>;
  573. /// Modifies the cursor's visibility.
  574. ///
  575. /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
  576. fn set_cursor_visible(&self, visible: bool) -> Result<()>;
  577. // Modifies the cursor icon of the window.
  578. fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>;
  579. /// Changes the position of the cursor in window coordinates.
  580. fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>;
  581. /// Ignores the window cursor events.
  582. fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()>;
  583. /// Starts dragging the window.
  584. fn start_dragging(&self) -> Result<()>;
  585. /// Starts resize-dragging the window.
  586. fn start_resize_dragging(&self, direction: ResizeDirection) -> Result<()>;
  587. /// Sets the taskbar progress state.
  588. ///
  589. /// ## Platform-specific
  590. ///
  591. /// - **Linux / macOS**: Progress bar is app-wide and not specific to this window. Only supported desktop environments with `libunity` (e.g. GNOME).
  592. /// - **iOS / Android:** Unsupported.
  593. fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()>;
  594. }