mod.rs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! The Tauri window types and functions.
  5. pub(crate) mod plugin;
  6. use tauri_runtime::{
  7. dpi::{PhysicalPosition, PhysicalSize},
  8. webview::PendingWebview,
  9. };
  10. pub use tauri_utils::{config::Color, WindowEffect as Effect, WindowEffectState as EffectState};
  11. #[cfg(desktop)]
  12. pub use crate::runtime::ProgressBarStatus;
  13. use crate::{
  14. app::AppHandle,
  15. event::{Event, EventId, EventTarget},
  16. ipc::{CommandArg, CommandItem, InvokeError},
  17. manager::AppManager,
  18. runtime::{
  19. monitor::Monitor as RuntimeMonitor,
  20. window::{DetachedWindow, PendingWindow, WindowBuilder as _},
  21. RuntimeHandle, WindowDispatch,
  22. },
  23. sealed::{ManagerBase, RuntimeOrDispatch},
  24. utils::config::{WindowConfig, WindowEffectsConfig},
  25. webview::WebviewBuilder,
  26. Emitter, EventLoopMessage, Listener, Manager, ResourceTable, Runtime, Theme, Webview,
  27. WindowEvent,
  28. };
  29. #[cfg(desktop)]
  30. use crate::{
  31. image::Image,
  32. menu::{ContextMenu, Menu, MenuId},
  33. runtime::{
  34. dpi::{Position, Size},
  35. UserAttentionType,
  36. },
  37. CursorIcon,
  38. };
  39. use serde::Serialize;
  40. #[cfg(windows)]
  41. use windows::Win32::Foundation::HWND;
  42. use tauri_macros::default_runtime;
  43. use std::{
  44. fmt,
  45. hash::{Hash, Hasher},
  46. sync::{Arc, Mutex, MutexGuard},
  47. };
  48. /// Monitor descriptor.
  49. #[derive(Debug, Clone, Serialize)]
  50. #[serde(rename_all = "camelCase")]
  51. pub struct Monitor {
  52. pub(crate) name: Option<String>,
  53. pub(crate) size: PhysicalSize<u32>,
  54. pub(crate) position: PhysicalPosition<i32>,
  55. pub(crate) scale_factor: f64,
  56. }
  57. impl From<RuntimeMonitor> for Monitor {
  58. fn from(monitor: RuntimeMonitor) -> Self {
  59. Self {
  60. name: monitor.name,
  61. size: monitor.size,
  62. position: monitor.position,
  63. scale_factor: monitor.scale_factor,
  64. }
  65. }
  66. }
  67. impl Monitor {
  68. /// Returns a human-readable name of the monitor.
  69. /// Returns None if the monitor doesn't exist anymore.
  70. pub fn name(&self) -> Option<&String> {
  71. self.name.as_ref()
  72. }
  73. /// Returns the monitor's resolution.
  74. pub fn size(&self) -> &PhysicalSize<u32> {
  75. &self.size
  76. }
  77. /// Returns the top-left corner position of the monitor relative to the larger full screen area.
  78. pub fn position(&self) -> &PhysicalPosition<i32> {
  79. &self.position
  80. }
  81. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  82. pub fn scale_factor(&self) -> f64 {
  83. self.scale_factor
  84. }
  85. }
  86. macro_rules! unstable_struct {
  87. (#[doc = $doc:expr] $($tokens:tt)*) => {
  88. #[cfg(feature = "unstable")]
  89. #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
  90. #[doc = $doc]
  91. pub $($tokens)*
  92. #[cfg(not(feature = "unstable"))]
  93. pub(crate) $($tokens)*
  94. }
  95. }
  96. unstable_struct!(
  97. #[doc = "A builder for a window managed by Tauri."]
  98. struct WindowBuilder<'a, R: Runtime, M: Manager<R>> {
  99. manager: &'a M,
  100. pub(crate) label: String,
  101. pub(crate) window_builder:
  102. <R::WindowDispatcher as WindowDispatch<EventLoopMessage>>::WindowBuilder,
  103. #[cfg(desktop)]
  104. pub(crate) menu: Option<Menu<R>>,
  105. #[cfg(desktop)]
  106. on_menu_event: Option<crate::app::GlobalMenuEventListener<Window<R>>>,
  107. window_effects: Option<WindowEffectsConfig>,
  108. }
  109. );
  110. impl<'a, R: Runtime, M: Manager<R>> fmt::Debug for WindowBuilder<'a, R, M> {
  111. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  112. f.debug_struct("WindowBuilder")
  113. .field("label", &self.label)
  114. .field("window_builder", &self.window_builder)
  115. .finish()
  116. }
  117. }
  118. #[cfg_attr(not(feature = "unstable"), allow(dead_code))]
  119. impl<'a, R: Runtime, M: Manager<R>> WindowBuilder<'a, R, M> {
  120. /// Initializes a window builder with the given window label.
  121. ///
  122. /// # Known issues
  123. ///
  124. /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue].
  125. /// You should use `async` commands when creating windows.
  126. ///
  127. /// # Examples
  128. ///
  129. /// - Create a window in the setup hook:
  130. ///
  131. #[cfg_attr(
  132. feature = "unstable",
  133. doc = r####"
  134. ```
  135. tauri::Builder::default()
  136. .setup(|app| {
  137. let window = tauri::window::WindowBuilder::new(app, "label")
  138. .build()?;
  139. Ok(())
  140. });
  141. ```
  142. "####
  143. )]
  144. /// - Create a window in a separate thread:
  145. ///
  146. #[cfg_attr(
  147. feature = "unstable",
  148. doc = r####"
  149. ```
  150. tauri::Builder::default()
  151. .setup(|app| {
  152. let handle = app.handle().clone();
  153. std::thread::spawn(move || {
  154. let window = tauri::window::WindowBuilder::new(&handle, "label")
  155. .build()
  156. .unwrap();
  157. });
  158. Ok(())
  159. });
  160. ```
  161. "####
  162. )]
  163. ///
  164. /// - Create a window in a command:
  165. ///
  166. #[cfg_attr(
  167. feature = "unstable",
  168. doc = r####"
  169. ```
  170. #[tauri::command]
  171. async fn create_window(app: tauri::AppHandle) {
  172. let window = tauri::window::WindowBuilder::new(&app, "label")
  173. .build()
  174. .unwrap();
  175. }
  176. ```
  177. "####
  178. )]
  179. ///
  180. /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
  181. pub fn new<L: Into<String>>(manager: &'a M, label: L) -> Self {
  182. Self {
  183. manager,
  184. label: label.into(),
  185. window_builder: <R::WindowDispatcher as WindowDispatch<EventLoopMessage>>::WindowBuilder::new(
  186. ),
  187. #[cfg(desktop)]
  188. menu: None,
  189. #[cfg(desktop)]
  190. on_menu_event: None,
  191. window_effects: None,
  192. }
  193. }
  194. /// Initializes a window builder from a [`WindowConfig`] from tauri.conf.json.
  195. /// Keep in mind that you can't create 2 windows with the same `label` so make sure
  196. /// that the initial window was closed or change the label of the new [`WindowBuilder`].
  197. ///
  198. /// # Known issues
  199. ///
  200. /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue].
  201. /// You should use `async` commands when creating windows.
  202. ///
  203. /// # Examples
  204. ///
  205. /// - Create a window in a command:
  206. ///
  207. #[cfg_attr(
  208. feature = "unstable",
  209. doc = r####"
  210. ```
  211. #[tauri::command]
  212. async fn reopen_window(app: tauri::AppHandle) {
  213. let window = tauri::window::WindowBuilder::from_config(&app, &app.config().app.windows.get(0).unwrap().clone())
  214. .unwrap()
  215. .build()
  216. .unwrap();
  217. }
  218. ```
  219. "####
  220. )]
  221. ///
  222. /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
  223. pub fn from_config(manager: &'a M, config: &WindowConfig) -> crate::Result<Self> {
  224. #[cfg_attr(not(windows), allow(unused_mut))]
  225. let mut builder = Self {
  226. manager,
  227. label: config.label.clone(),
  228. window_effects: config.window_effects.clone(),
  229. window_builder:
  230. <R::WindowDispatcher as WindowDispatch<EventLoopMessage>>::WindowBuilder::with_config(
  231. config,
  232. ),
  233. #[cfg(desktop)]
  234. menu: None,
  235. #[cfg(desktop)]
  236. on_menu_event: None,
  237. };
  238. #[cfg(desktop)]
  239. if let Some(parent) = &config.parent {
  240. let window = manager
  241. .manager()
  242. .get_window(parent)
  243. .ok_or(crate::Error::WindowNotFound)?;
  244. builder = builder.parent(&window)?;
  245. }
  246. Ok(builder)
  247. }
  248. /// Registers a global menu event listener.
  249. ///
  250. /// Note that this handler is called for any menu event,
  251. /// whether it is coming from this window, another window or from the tray icon menu.
  252. ///
  253. /// Also note that this handler will not be called if
  254. /// the window used to register it was closed.
  255. ///
  256. /// # Examples
  257. #[cfg_attr(
  258. feature = "unstable",
  259. doc = r####"
  260. ```
  261. use tauri::menu::{Menu, Submenu, MenuItem};
  262. tauri::Builder::default()
  263. .setup(|app| {
  264. let handle = app.handle();
  265. let save_menu_item = MenuItem::new(handle, "Save", true, None::<&str>)?;
  266. let menu = Menu::with_items(handle, &[
  267. &Submenu::with_items(handle, "File", true, &[
  268. &save_menu_item,
  269. ])?,
  270. ])?;
  271. let window = tauri::window::WindowBuilder::new(app, "editor")
  272. .menu(menu)
  273. .on_menu_event(move |window, event| {
  274. if event.id == save_menu_item.id() {
  275. // save menu item
  276. }
  277. })
  278. .build()
  279. .unwrap();
  280. ///
  281. Ok(())
  282. });
  283. ```"####
  284. )]
  285. #[cfg(desktop)]
  286. pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
  287. mut self,
  288. f: F,
  289. ) -> Self {
  290. self.on_menu_event.replace(Box::new(f));
  291. self
  292. }
  293. /// Creates this window with a webview with it.
  294. #[cfg_attr(
  295. feature = "tracing",
  296. tracing::instrument(name = "webview::create", skip_all)
  297. )]
  298. pub(crate) fn with_webview(
  299. self,
  300. webview: WebviewBuilder<R>,
  301. ) -> crate::Result<(Window<R>, Webview<R>)> {
  302. let pending_webview = webview.into_pending_webview(self.manager, &self.label)?;
  303. let window = self.build_internal(Some(pending_webview))?;
  304. let webview = window.webviews().first().unwrap().clone();
  305. Ok((window, webview))
  306. }
  307. /// Creates a new window.
  308. pub fn build(self) -> crate::Result<Window<R>> {
  309. self.build_internal(None)
  310. }
  311. /// Creates a new window with an optional webview.
  312. fn build_internal(
  313. self,
  314. webview: Option<PendingWebview<EventLoopMessage, R>>,
  315. ) -> crate::Result<Window<R>> {
  316. let mut pending = PendingWindow::new(self.window_builder.clone(), self.label.clone())?;
  317. if let Some(webview) = webview {
  318. pending.set_webview(webview);
  319. }
  320. let app_manager = self.manager.manager();
  321. let pending = app_manager.window.prepare_window(pending)?;
  322. #[cfg(desktop)]
  323. let window_menu = {
  324. let is_app_wide = self.menu.is_none();
  325. self
  326. .menu
  327. .or_else(|| self.manager.app_handle().menu())
  328. .map(|menu| WindowMenu { is_app_wide, menu })
  329. };
  330. #[cfg(desktop)]
  331. let handler = app_manager
  332. .menu
  333. .prepare_window_menu_creation_handler(window_menu.as_ref(), self.window_builder.get_theme());
  334. #[cfg(not(desktop))]
  335. #[allow(clippy::type_complexity)]
  336. let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;
  337. let window = match &mut self.manager.runtime() {
  338. RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending, handler),
  339. RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending, handler),
  340. RuntimeOrDispatch::Dispatch(dispatcher) => dispatcher.create_window(pending, handler),
  341. }
  342. .map(|detached_window| {
  343. let window = app_manager.window.attach_window(
  344. self.manager.app_handle().clone(),
  345. detached_window.clone(),
  346. #[cfg(desktop)]
  347. window_menu,
  348. );
  349. if let Some(webview) = detached_window.webview {
  350. app_manager.webview.attach_webview(window.clone(), webview);
  351. }
  352. window
  353. })?;
  354. #[cfg(desktop)]
  355. if let Some(handler) = self.on_menu_event {
  356. window.on_menu_event(handler);
  357. }
  358. if let Some(effects) = self.window_effects {
  359. crate::vibrancy::set_window_effects(&window, Some(effects))?;
  360. }
  361. let app_manager = self.manager.manager_owned();
  362. let window_label = window.label().to_string();
  363. // run on the main thread to fix a deadlock on webview.eval if the tracing feature is enabled
  364. let _ = window.run_on_main_thread(move || {
  365. let _ = app_manager.emit(
  366. "tauri://window-created",
  367. Some(crate::webview::CreatedEvent {
  368. label: window_label,
  369. }),
  370. );
  371. });
  372. Ok(window)
  373. }
  374. }
  375. /// Desktop APIs.
  376. #[cfg(desktop)]
  377. #[cfg_attr(not(feature = "unstable"), allow(dead_code))]
  378. impl<'a, R: Runtime, M: Manager<R>> WindowBuilder<'a, R, M> {
  379. /// Sets the menu for the window.
  380. #[must_use]
  381. pub fn menu(mut self, menu: Menu<R>) -> Self {
  382. self.menu.replace(menu);
  383. self
  384. }
  385. /// Show window in the center of the screen.
  386. #[must_use]
  387. pub fn center(mut self) -> Self {
  388. self.window_builder = self.window_builder.center();
  389. self
  390. }
  391. /// The initial position of the window's.
  392. #[must_use]
  393. pub fn position(mut self, x: f64, y: f64) -> Self {
  394. self.window_builder = self.window_builder.position(x, y);
  395. self
  396. }
  397. /// Window size.
  398. #[must_use]
  399. pub fn inner_size(mut self, width: f64, height: f64) -> Self {
  400. self.window_builder = self.window_builder.inner_size(width, height);
  401. self
  402. }
  403. /// Window min inner size.
  404. #[must_use]
  405. pub fn min_inner_size(mut self, min_width: f64, min_height: f64) -> Self {
  406. self.window_builder = self.window_builder.min_inner_size(min_width, min_height);
  407. self
  408. }
  409. /// Window max inner size.
  410. #[must_use]
  411. pub fn max_inner_size(mut self, max_width: f64, max_height: f64) -> Self {
  412. self.window_builder = self.window_builder.max_inner_size(max_width, max_height);
  413. self
  414. }
  415. /// Window inner size constraints.
  416. #[must_use]
  417. pub fn inner_size_constraints(
  418. mut self,
  419. constraints: tauri_runtime::window::WindowSizeConstraints,
  420. ) -> Self {
  421. self.window_builder = self.window_builder.inner_size_constraints(constraints);
  422. self
  423. }
  424. /// Whether the window is resizable or not.
  425. /// When resizable is set to false, native window's maximize button is automatically disabled.
  426. #[must_use]
  427. pub fn resizable(mut self, resizable: bool) -> Self {
  428. self.window_builder = self.window_builder.resizable(resizable);
  429. self
  430. }
  431. /// Whether the window's native maximize button is enabled or not.
  432. /// If resizable is set to false, this setting is ignored.
  433. ///
  434. /// ## Platform-specific
  435. ///
  436. /// - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  437. /// - **Linux / iOS / Android:** Unsupported.
  438. #[must_use]
  439. pub fn maximizable(mut self, maximizable: bool) -> Self {
  440. self.window_builder = self.window_builder.maximizable(maximizable);
  441. self
  442. }
  443. /// Whether the window's native minimize button is enabled or not.
  444. ///
  445. /// ## Platform-specific
  446. ///
  447. /// - **Linux / iOS / Android:** Unsupported.
  448. #[must_use]
  449. pub fn minimizable(mut self, minimizable: bool) -> Self {
  450. self.window_builder = self.window_builder.minimizable(minimizable);
  451. self
  452. }
  453. /// Whether the window's native close button is enabled or not.
  454. ///
  455. /// ## Platform-specific
  456. ///
  457. /// - **Linux:** "GTK+ will do its best to convince the window manager not to show a close button.
  458. /// Depending on the system, this function may not have any effect when called on a window that is already visible"
  459. /// - **iOS / Android:** Unsupported.
  460. #[must_use]
  461. pub fn closable(mut self, closable: bool) -> Self {
  462. self.window_builder = self.window_builder.closable(closable);
  463. self
  464. }
  465. /// The title of the window in the title bar.
  466. #[must_use]
  467. pub fn title<S: Into<String>>(mut self, title: S) -> Self {
  468. self.window_builder = self.window_builder.title(title);
  469. self
  470. }
  471. /// Whether to start the window in fullscreen or not.
  472. #[must_use]
  473. pub fn fullscreen(mut self, fullscreen: bool) -> Self {
  474. self.window_builder = self.window_builder.fullscreen(fullscreen);
  475. self
  476. }
  477. /// Sets the window to be initially focused.
  478. #[must_use]
  479. #[deprecated(
  480. since = "1.2.0",
  481. note = "The window is automatically focused by default. This function Will be removed in 2.0.0. Use `focused` instead."
  482. )]
  483. pub fn focus(mut self) -> Self {
  484. self.window_builder = self.window_builder.focused(true);
  485. self
  486. }
  487. /// Whether the window will be initially focused or not.
  488. #[must_use]
  489. pub fn focused(mut self, focused: bool) -> Self {
  490. self.window_builder = self.window_builder.focused(focused);
  491. self
  492. }
  493. /// Whether the window should be maximized upon creation.
  494. #[must_use]
  495. pub fn maximized(mut self, maximized: bool) -> Self {
  496. self.window_builder = self.window_builder.maximized(maximized);
  497. self
  498. }
  499. /// Whether the window should be immediately visible upon creation.
  500. #[must_use]
  501. pub fn visible(mut self, visible: bool) -> Self {
  502. self.window_builder = self.window_builder.visible(visible);
  503. self
  504. }
  505. /// Forces a theme or uses the system settings if None was provided.
  506. ///
  507. /// ## Platform-specific
  508. ///
  509. /// - **macOS**: Only supported on macOS 10.14+.
  510. #[must_use]
  511. pub fn theme(mut self, theme: Option<Theme>) -> Self {
  512. self.window_builder = self.window_builder.theme(theme);
  513. self
  514. }
  515. /// Whether the window should be transparent. If this is true, writing colors
  516. /// with alpha values different than `1.0` will produce a transparent window.
  517. #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
  518. #[cfg_attr(
  519. docsrs,
  520. doc(cfg(any(not(target_os = "macos"), feature = "macos-private-api")))
  521. )]
  522. #[must_use]
  523. pub fn transparent(mut self, transparent: bool) -> Self {
  524. self.window_builder = self.window_builder.transparent(transparent);
  525. self
  526. }
  527. /// Whether the window should have borders and bars.
  528. #[must_use]
  529. pub fn decorations(mut self, decorations: bool) -> Self {
  530. self.window_builder = self.window_builder.decorations(decorations);
  531. self
  532. }
  533. /// Whether the window should always be below other windows.
  534. #[must_use]
  535. pub fn always_on_bottom(mut self, always_on_bottom: bool) -> Self {
  536. self.window_builder = self.window_builder.always_on_bottom(always_on_bottom);
  537. self
  538. }
  539. /// Whether the window should always be on top of other windows.
  540. #[must_use]
  541. pub fn always_on_top(mut self, always_on_top: bool) -> Self {
  542. self.window_builder = self.window_builder.always_on_top(always_on_top);
  543. self
  544. }
  545. /// Whether the window will be visible on all workspaces or virtual desktops.
  546. ///
  547. /// ## Platform-specific
  548. ///
  549. /// - **Windows / iOS / Android:** Unsupported.
  550. #[must_use]
  551. pub fn visible_on_all_workspaces(mut self, visible_on_all_workspaces: bool) -> Self {
  552. self.window_builder = self
  553. .window_builder
  554. .visible_on_all_workspaces(visible_on_all_workspaces);
  555. self
  556. }
  557. /// Prevents the window contents from being captured by other apps.
  558. #[must_use]
  559. pub fn content_protected(mut self, protected: bool) -> Self {
  560. self.window_builder = self.window_builder.content_protected(protected);
  561. self
  562. }
  563. /// Sets the window icon.
  564. pub fn icon(mut self, icon: Image<'a>) -> crate::Result<Self> {
  565. self.window_builder = self.window_builder.icon(icon.into())?;
  566. Ok(self)
  567. }
  568. /// Sets whether or not the window icon should be hidden from the taskbar.
  569. ///
  570. /// ## Platform-specific
  571. ///
  572. /// - **macOS**: Unsupported.
  573. #[must_use]
  574. pub fn skip_taskbar(mut self, skip: bool) -> Self {
  575. self.window_builder = self.window_builder.skip_taskbar(skip);
  576. self
  577. }
  578. /// Sets custom name for Windows' window class. **Windows only**.
  579. #[must_use]
  580. pub fn window_classname<S: Into<String>>(mut self, classname: S) -> Self {
  581. self.window_builder = self.window_builder.window_classname(classname);
  582. self
  583. }
  584. /// Sets whether or not the window has shadow.
  585. ///
  586. /// ## Platform-specific
  587. ///
  588. /// - **Windows:**
  589. /// - `false` has no effect on decorated window, shadows are always ON.
  590. /// - `true` will make undecorated window have a 1px white border,
  591. /// and on Windows 11, it will have a rounded corners.
  592. /// - **Linux:** Unsupported.
  593. #[must_use]
  594. pub fn shadow(mut self, enable: bool) -> Self {
  595. self.window_builder = self.window_builder.shadow(enable);
  596. self
  597. }
  598. /// Sets a parent to the window to be created.
  599. ///
  600. /// ## Platform-specific
  601. ///
  602. /// - **Windows**: This sets the passed parent as an owner window to the window to be created.
  603. /// From [MSDN owned windows docs](https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows):
  604. /// - An owned window is always above its owner in the z-order.
  605. /// - The system automatically destroys an owned window when its owner is destroyed.
  606. /// - An owned window is hidden when its owner is minimized.
  607. /// - **Linux**: This makes the new window transient for parent, see <https://docs.gtk.org/gtk3/method.Window.set_transient_for.html>
  608. /// - **macOS**: This adds the window as a child of parent, see <https://developer.apple.com/documentation/appkit/nswindow/1419152-addchildwindow?language=objc>
  609. pub fn parent(mut self, parent: &Window<R>) -> crate::Result<Self> {
  610. #[cfg(windows)]
  611. {
  612. self.window_builder = self.window_builder.owner(parent.hwnd()?);
  613. }
  614. #[cfg(any(
  615. target_os = "linux",
  616. target_os = "dragonfly",
  617. target_os = "freebsd",
  618. target_os = "netbsd",
  619. target_os = "openbsd"
  620. ))]
  621. {
  622. self.window_builder = self.window_builder.transient_for(&parent.gtk_window()?);
  623. }
  624. #[cfg(target_os = "macos")]
  625. {
  626. self.window_builder = self.window_builder.parent(parent.ns_window()?);
  627. }
  628. Ok(self)
  629. }
  630. /// Set an owner to the window to be created.
  631. ///
  632. /// From MSDN:
  633. /// - An owned window is always above its owner in the z-order.
  634. /// - The system automatically destroys an owned window when its owner is destroyed.
  635. /// - An owned window is hidden when its owner is minimized.
  636. ///
  637. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
  638. #[cfg(windows)]
  639. pub fn owner(mut self, owner: &Window<R>) -> crate::Result<Self> {
  640. self.window_builder = self.window_builder.owner(owner.hwnd()?);
  641. Ok(self)
  642. }
  643. /// Set an owner to the window to be created.
  644. ///
  645. /// From MSDN:
  646. /// - An owned window is always above its owner in the z-order.
  647. /// - The system automatically destroys an owned window when its owner is destroyed.
  648. /// - An owned window is hidden when its owner is minimized.
  649. ///
  650. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
  651. ///
  652. /// **Note:** This is a low level API. See [`Self::parent`] for a higher level wrapper for Tauri windows.
  653. #[cfg(windows)]
  654. #[must_use]
  655. pub fn owner_raw(mut self, owner: HWND) -> Self {
  656. self.window_builder = self.window_builder.owner(owner);
  657. self
  658. }
  659. /// Sets a parent to the window to be created.
  660. ///
  661. /// A child window has the WS_CHILD style and is confined to the client area of its parent window.
  662. ///
  663. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows>
  664. ///
  665. /// **Note:** This is a low level API. See [`Self::parent`] for a higher level wrapper for Tauri windows.
  666. #[cfg(windows)]
  667. #[must_use]
  668. pub fn parent_raw(mut self, parent: HWND) -> Self {
  669. self.window_builder = self.window_builder.parent(parent);
  670. self
  671. }
  672. /// Sets a parent to the window to be created.
  673. ///
  674. /// See <https://developer.apple.com/documentation/appkit/nswindow/1419152-addchildwindow?language=objc>
  675. ///
  676. /// **Note:** This is a low level API. See [`Self::parent`] for a higher level wrapper for Tauri windows.
  677. #[cfg(target_os = "macos")]
  678. #[must_use]
  679. pub fn parent_raw(mut self, parent: *mut std::ffi::c_void) -> Self {
  680. self.window_builder = self.window_builder.parent(parent);
  681. self
  682. }
  683. /// Sets the window to be created transient for parent.
  684. ///
  685. /// See <https://docs.gtk.org/gtk3/method.Window.set_transient_for.html>
  686. ///
  687. /// **Note:** This is a low level API. See [`Self::parent`] for a higher level wrapper for Tauri windows.
  688. #[cfg(any(
  689. target_os = "linux",
  690. target_os = "dragonfly",
  691. target_os = "freebsd",
  692. target_os = "netbsd",
  693. target_os = "openbsd"
  694. ))]
  695. pub fn transient_for(mut self, parent: &Window<R>) -> crate::Result<Self> {
  696. self.window_builder = self.window_builder.transient_for(&parent.gtk_window()?);
  697. Ok(self)
  698. }
  699. /// Sets the window to be created transient for parent.
  700. ///
  701. /// See <https://docs.gtk.org/gtk3/method.Window.set_transient_for.html>
  702. ///
  703. /// **Note:** This is a low level API. See [`Self::parent`] and [`Self::transient_for`] for higher level wrappers for Tauri windows.
  704. #[cfg(any(
  705. target_os = "linux",
  706. target_os = "dragonfly",
  707. target_os = "freebsd",
  708. target_os = "netbsd",
  709. target_os = "openbsd"
  710. ))]
  711. #[must_use]
  712. pub fn transient_for_raw(mut self, parent: &impl gtk::glib::IsA<gtk::Window>) -> Self {
  713. self.window_builder = self.window_builder.transient_for(parent);
  714. self
  715. }
  716. /// Enables or disables drag and drop support.
  717. #[cfg(windows)]
  718. #[must_use]
  719. pub fn drag_and_drop(mut self, enabled: bool) -> Self {
  720. self.window_builder = self.window_builder.drag_and_drop(enabled);
  721. self
  722. }
  723. /// Sets the [`crate::TitleBarStyle`].
  724. #[cfg(target_os = "macos")]
  725. #[must_use]
  726. pub fn title_bar_style(mut self, style: crate::TitleBarStyle) -> Self {
  727. self.window_builder = self.window_builder.title_bar_style(style);
  728. self
  729. }
  730. /// Hide the window title.
  731. #[cfg(target_os = "macos")]
  732. #[must_use]
  733. pub fn hidden_title(mut self, hidden: bool) -> Self {
  734. self.window_builder = self.window_builder.hidden_title(hidden);
  735. self
  736. }
  737. /// Defines the window [tabbing identifier] for macOS.
  738. ///
  739. /// Windows with matching tabbing identifiers will be grouped together.
  740. /// If the tabbing identifier is not set, automatic tabbing will be disabled.
  741. ///
  742. /// [tabbing identifier]: <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
  743. #[cfg(target_os = "macos")]
  744. #[must_use]
  745. pub fn tabbing_identifier(mut self, identifier: &str) -> Self {
  746. self.window_builder = self.window_builder.tabbing_identifier(identifier);
  747. self
  748. }
  749. /// Sets window effects.
  750. ///
  751. /// Requires the window to be transparent.
  752. ///
  753. /// ## Platform-specific:
  754. ///
  755. /// - **Windows**: If using decorations or shadows, you may want to try this workaround <https://github.com/tauri-apps/tao/issues/72#issuecomment-975607891>
  756. /// - **Linux**: Unsupported
  757. pub fn effects(mut self, effects: WindowEffectsConfig) -> Self {
  758. self.window_effects.replace(effects);
  759. self
  760. }
  761. }
  762. /// A wrapper struct to hold the window menu state
  763. /// and whether it is global per-app or specific to this window.
  764. #[cfg(desktop)]
  765. pub(crate) struct WindowMenu<R: Runtime> {
  766. pub(crate) is_app_wide: bool,
  767. pub(crate) menu: Menu<R>,
  768. }
  769. // TODO: expand these docs since this is a pretty important type
  770. /// A window managed by Tauri.
  771. ///
  772. /// This type also implements [`Manager`] which allows you to manage other windows attached to
  773. /// the same application.
  774. #[default_runtime(crate::Wry, wry)]
  775. pub struct Window<R: Runtime> {
  776. /// The window created by the runtime.
  777. pub(crate) window: DetachedWindow<EventLoopMessage, R>,
  778. /// The manager to associate this window with.
  779. pub(crate) manager: Arc<AppManager<R>>,
  780. pub(crate) app_handle: AppHandle<R>,
  781. // The menu set for this window
  782. #[cfg(desktop)]
  783. pub(crate) menu: Arc<Mutex<Option<WindowMenu<R>>>>,
  784. pub(crate) resources_table: Arc<Mutex<ResourceTable>>,
  785. }
  786. impl<R: Runtime> std::fmt::Debug for Window<R> {
  787. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  788. f.debug_struct("Window")
  789. .field("window", &self.window)
  790. .field("manager", &self.manager)
  791. .field("app_handle", &self.app_handle)
  792. .finish()
  793. }
  794. }
  795. impl<R: Runtime> raw_window_handle::HasWindowHandle for Window<R> {
  796. fn window_handle(
  797. &self,
  798. ) -> std::result::Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
  799. self.window.dispatcher.window_handle()
  800. }
  801. }
  802. impl<R: Runtime> raw_window_handle::HasDisplayHandle for Window<R> {
  803. fn display_handle(
  804. &self,
  805. ) -> std::result::Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
  806. self.app_handle.display_handle()
  807. }
  808. }
  809. impl<R: Runtime> Clone for Window<R> {
  810. fn clone(&self) -> Self {
  811. Self {
  812. window: self.window.clone(),
  813. manager: self.manager.clone(),
  814. app_handle: self.app_handle.clone(),
  815. #[cfg(desktop)]
  816. menu: self.menu.clone(),
  817. resources_table: self.resources_table.clone(),
  818. }
  819. }
  820. }
  821. impl<R: Runtime> Hash for Window<R> {
  822. /// Only use the [`Window`]'s label to represent its hash.
  823. fn hash<H: Hasher>(&self, state: &mut H) {
  824. self.window.label.hash(state)
  825. }
  826. }
  827. impl<R: Runtime> Eq for Window<R> {}
  828. impl<R: Runtime> PartialEq for Window<R> {
  829. /// Only use the [`Window`]'s label to compare equality.
  830. fn eq(&self, other: &Self) -> bool {
  831. self.window.label.eq(&other.window.label)
  832. }
  833. }
  834. impl<R: Runtime> Manager<R> for Window<R> {
  835. fn resources_table(&self) -> MutexGuard<'_, ResourceTable> {
  836. self
  837. .resources_table
  838. .lock()
  839. .expect("poisoned window resources table")
  840. }
  841. }
  842. impl<R: Runtime> ManagerBase<R> for Window<R> {
  843. fn manager(&self) -> &AppManager<R> {
  844. &self.manager
  845. }
  846. fn manager_owned(&self) -> Arc<AppManager<R>> {
  847. self.manager.clone()
  848. }
  849. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  850. RuntimeOrDispatch::Dispatch(self.window.dispatcher.clone())
  851. }
  852. fn managed_app_handle(&self) -> &AppHandle<R> {
  853. &self.app_handle
  854. }
  855. }
  856. impl<'de, R: Runtime> CommandArg<'de, R> for Window<R> {
  857. /// Grabs the [`Window`] from the [`CommandItem`]. This will never fail.
  858. fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
  859. Ok(command.message.webview().window())
  860. }
  861. }
  862. /// Base window functions.
  863. impl<R: Runtime> Window<R> {
  864. /// Create a new window that is attached to the manager.
  865. pub(crate) fn new(
  866. manager: Arc<AppManager<R>>,
  867. window: DetachedWindow<EventLoopMessage, R>,
  868. app_handle: AppHandle<R>,
  869. #[cfg(desktop)] menu: Option<WindowMenu<R>>,
  870. ) -> Self {
  871. Self {
  872. window,
  873. manager,
  874. app_handle,
  875. #[cfg(desktop)]
  876. menu: Arc::new(std::sync::Mutex::new(menu)),
  877. resources_table: Default::default(),
  878. }
  879. }
  880. /// Initializes a window builder with the given window label.
  881. ///
  882. /// Data URLs are only supported with the `webview-data-url` feature flag.
  883. #[cfg(feature = "unstable")]
  884. #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
  885. pub fn builder<M: Manager<R>, L: Into<String>>(manager: &M, label: L) -> WindowBuilder<'_, R, M> {
  886. WindowBuilder::new(manager, label.into())
  887. }
  888. /// Adds a new webview as a child of this window.
  889. #[cfg(any(test, all(desktop, feature = "unstable")))]
  890. #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "unstable"))))]
  891. pub fn add_child<P: Into<Position>, S: Into<Size>>(
  892. &self,
  893. webview_builder: WebviewBuilder<R>,
  894. position: P,
  895. size: S,
  896. ) -> crate::Result<Webview<R>> {
  897. use std::sync::mpsc::channel;
  898. let (tx, rx) = channel();
  899. let position = position.into();
  900. let size = size.into();
  901. let window_ = self.clone();
  902. self.run_on_main_thread(move || {
  903. let res = webview_builder.build(window_, position, size);
  904. tx.send(res.map_err(Into::into)).unwrap();
  905. })?;
  906. rx.recv().unwrap()
  907. }
  908. /// List of webviews associated with this window.
  909. pub fn webviews(&self) -> Vec<Webview<R>> {
  910. self
  911. .manager
  912. .webview
  913. .webviews_lock()
  914. .values()
  915. .filter(|w| w.window_label() == self.label())
  916. .cloned()
  917. .collect()
  918. }
  919. pub(crate) fn is_webview_window(&self) -> bool {
  920. self.webviews().iter().all(|w| w.label() == self.label())
  921. }
  922. /// Runs the given closure on the main thread.
  923. pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
  924. self
  925. .window
  926. .dispatcher
  927. .run_on_main_thread(f)
  928. .map_err(Into::into)
  929. }
  930. /// The label of this window.
  931. pub fn label(&self) -> &str {
  932. &self.window.label
  933. }
  934. /// Registers a window event listener.
  935. pub fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) {
  936. self
  937. .window
  938. .dispatcher
  939. .on_window_event(move |event| f(&event.clone().into()));
  940. }
  941. }
  942. /// Menu APIs
  943. #[cfg(desktop)]
  944. impl<R: Runtime> Window<R> {
  945. /// Registers a global menu event listener.
  946. ///
  947. /// Note that this handler is called for any menu event,
  948. /// whether it is coming from this window, another window or from the tray icon menu.
  949. ///
  950. /// Also note that this handler will not be called if
  951. /// the window used to register it was closed.
  952. ///
  953. /// # Examples
  954. #[cfg_attr(
  955. feature = "unstable",
  956. doc = r####"
  957. ```
  958. use tauri::menu::{Menu, Submenu, MenuItem};
  959. tauri::Builder::default()
  960. .setup(|app| {
  961. let handle = app.handle();
  962. let save_menu_item = MenuItem::new(handle, "Save", true, None::<&str>)?;
  963. let menu = Menu::with_items(handle, &[
  964. &Submenu::with_items(handle, "File", true, &[
  965. &save_menu_item,
  966. ])?,
  967. ])?;
  968. let window = tauri::window::WindowBuilder::new(app, "editor")
  969. .menu(menu)
  970. .build()
  971. .unwrap();
  972. window.on_menu_event(move |window, event| {
  973. if event.id == save_menu_item.id() {
  974. // save menu item
  975. }
  976. });
  977. Ok(())
  978. });
  979. ```
  980. "####
  981. )]
  982. pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
  983. &self,
  984. f: F,
  985. ) {
  986. self
  987. .manager
  988. .menu
  989. .event_listeners
  990. .lock()
  991. .unwrap()
  992. .insert(self.label().to_string(), Box::new(f));
  993. }
  994. pub(crate) fn menu_lock(&self) -> std::sync::MutexGuard<'_, Option<WindowMenu<R>>> {
  995. self.menu.lock().expect("poisoned window")
  996. }
  997. #[cfg_attr(target_os = "macos", allow(dead_code))]
  998. pub(crate) fn has_app_wide_menu(&self) -> bool {
  999. self
  1000. .menu_lock()
  1001. .as_ref()
  1002. .map(|m| m.is_app_wide)
  1003. .unwrap_or(false)
  1004. }
  1005. #[cfg_attr(target_os = "macos", allow(dead_code))]
  1006. pub(crate) fn is_menu_in_use<I: PartialEq<MenuId>>(&self, id: &I) -> bool {
  1007. self
  1008. .menu_lock()
  1009. .as_ref()
  1010. .map(|m| id.eq(m.menu.id()))
  1011. .unwrap_or(false)
  1012. }
  1013. /// Returns this window menu .
  1014. pub fn menu(&self) -> Option<Menu<R>> {
  1015. self.menu_lock().as_ref().map(|m| m.menu.clone())
  1016. }
  1017. /// Sets the window menu and returns the previous one.
  1018. ///
  1019. /// ## Platform-specific:
  1020. ///
  1021. /// - **macOS:** Unsupported. The menu on macOS is app-wide and not specific to one
  1022. /// window, if you need to set it, use [`AppHandle::set_menu`] instead.
  1023. #[cfg_attr(target_os = "macos", allow(unused_variables))]
  1024. pub fn set_menu(&self, menu: Menu<R>) -> crate::Result<Option<Menu<R>>> {
  1025. let prev_menu = self.remove_menu()?;
  1026. self.manager.menu.insert_menu_into_stash(&menu);
  1027. let window = self.clone();
  1028. let menu_ = menu.clone();
  1029. self.run_on_main_thread(move || {
  1030. #[cfg(windows)]
  1031. if let Ok(hwnd) = window.hwnd() {
  1032. let theme = window
  1033. .theme()
  1034. .map(crate::menu::map_to_menu_theme)
  1035. .unwrap_or(muda::MenuTheme::Auto);
  1036. let _ = unsafe { menu_.inner().init_for_hwnd_with_theme(hwnd.0 as _, theme) };
  1037. }
  1038. #[cfg(any(
  1039. target_os = "linux",
  1040. target_os = "dragonfly",
  1041. target_os = "freebsd",
  1042. target_os = "netbsd",
  1043. target_os = "openbsd"
  1044. ))]
  1045. if let (Ok(gtk_window), Ok(gtk_box)) = (window.gtk_window(), window.default_vbox()) {
  1046. let _ = menu_
  1047. .inner()
  1048. .init_for_gtk_window(&gtk_window, Some(&gtk_box));
  1049. }
  1050. })?;
  1051. self.menu_lock().replace(WindowMenu {
  1052. is_app_wide: false,
  1053. menu,
  1054. });
  1055. Ok(prev_menu)
  1056. }
  1057. /// Removes the window menu and returns it.
  1058. ///
  1059. /// ## Platform-specific:
  1060. ///
  1061. /// - **macOS:** Unsupported. The menu on macOS is app-wide and not specific to one
  1062. /// window, if you need to remove it, use [`AppHandle::remove_menu`] instead.
  1063. pub fn remove_menu(&self) -> crate::Result<Option<Menu<R>>> {
  1064. let prev_menu = self.menu_lock().take().map(|m| m.menu);
  1065. // remove from the window
  1066. #[cfg_attr(target_os = "macos", allow(unused_variables))]
  1067. if let Some(menu) = &prev_menu {
  1068. let window = self.clone();
  1069. let menu = menu.clone();
  1070. self.run_on_main_thread(move || {
  1071. #[cfg(windows)]
  1072. if let Ok(hwnd) = window.hwnd() {
  1073. let _ = unsafe { menu.inner().remove_for_hwnd(hwnd.0 as _) };
  1074. }
  1075. #[cfg(any(
  1076. target_os = "linux",
  1077. target_os = "dragonfly",
  1078. target_os = "freebsd",
  1079. target_os = "netbsd",
  1080. target_os = "openbsd"
  1081. ))]
  1082. if let Ok(gtk_window) = window.gtk_window() {
  1083. let _ = menu.inner().remove_for_gtk_window(&gtk_window);
  1084. }
  1085. })?;
  1086. }
  1087. self
  1088. .manager
  1089. .remove_menu_from_stash_by_id(prev_menu.as_ref().map(|m| m.id()));
  1090. Ok(prev_menu)
  1091. }
  1092. /// Hides the window menu.
  1093. pub fn hide_menu(&self) -> crate::Result<()> {
  1094. // remove from the window
  1095. #[cfg_attr(target_os = "macos", allow(unused_variables))]
  1096. if let Some(window_menu) = &*self.menu_lock() {
  1097. let window = self.clone();
  1098. let menu_ = window_menu.menu.clone();
  1099. self.run_on_main_thread(move || {
  1100. #[cfg(windows)]
  1101. if let Ok(hwnd) = window.hwnd() {
  1102. let _ = unsafe { menu_.inner().hide_for_hwnd(hwnd.0 as _) };
  1103. }
  1104. #[cfg(any(
  1105. target_os = "linux",
  1106. target_os = "dragonfly",
  1107. target_os = "freebsd",
  1108. target_os = "netbsd",
  1109. target_os = "openbsd"
  1110. ))]
  1111. if let Ok(gtk_window) = window.gtk_window() {
  1112. let _ = menu_.inner().hide_for_gtk_window(&gtk_window);
  1113. }
  1114. })?;
  1115. }
  1116. Ok(())
  1117. }
  1118. /// Shows the window menu.
  1119. pub fn show_menu(&self) -> crate::Result<()> {
  1120. // remove from the window
  1121. #[cfg_attr(target_os = "macos", allow(unused_variables))]
  1122. if let Some(window_menu) = &*self.menu_lock() {
  1123. let window = self.clone();
  1124. let menu_ = window_menu.menu.clone();
  1125. self.run_on_main_thread(move || {
  1126. #[cfg(windows)]
  1127. if let Ok(hwnd) = window.hwnd() {
  1128. let _ = unsafe { menu_.inner().show_for_hwnd(hwnd.0 as _) };
  1129. }
  1130. #[cfg(any(
  1131. target_os = "linux",
  1132. target_os = "dragonfly",
  1133. target_os = "freebsd",
  1134. target_os = "netbsd",
  1135. target_os = "openbsd"
  1136. ))]
  1137. if let Ok(gtk_window) = window.gtk_window() {
  1138. let _ = menu_.inner().show_for_gtk_window(&gtk_window);
  1139. }
  1140. })?;
  1141. }
  1142. Ok(())
  1143. }
  1144. /// Shows the window menu.
  1145. pub fn is_menu_visible(&self) -> crate::Result<bool> {
  1146. // remove from the window
  1147. #[cfg_attr(target_os = "macos", allow(unused_variables))]
  1148. if let Some(window_menu) = &*self.menu_lock() {
  1149. let (tx, rx) = std::sync::mpsc::channel();
  1150. let window = self.clone();
  1151. let menu_ = window_menu.menu.clone();
  1152. self.run_on_main_thread(move || {
  1153. #[cfg(windows)]
  1154. if let Ok(hwnd) = window.hwnd() {
  1155. let _ = tx.send(unsafe { menu_.inner().is_visible_on_hwnd(hwnd.0 as _) });
  1156. }
  1157. #[cfg(any(
  1158. target_os = "linux",
  1159. target_os = "dragonfly",
  1160. target_os = "freebsd",
  1161. target_os = "netbsd",
  1162. target_os = "openbsd"
  1163. ))]
  1164. if let Ok(gtk_window) = window.gtk_window() {
  1165. let _ = tx.send(menu_.inner().is_visible_on_gtk_window(&gtk_window));
  1166. }
  1167. })?;
  1168. return Ok(rx.recv().unwrap_or(false));
  1169. }
  1170. Ok(false)
  1171. }
  1172. /// Shows the specified menu as a context menu at the cursor position.
  1173. pub fn popup_menu<M: ContextMenu>(&self, menu: &M) -> crate::Result<()> {
  1174. menu.popup(self.clone())
  1175. }
  1176. /// Shows the specified menu as a context menu at the specified position.
  1177. ///
  1178. /// The position is relative to the window's top-left corner.
  1179. pub fn popup_menu_at<M: ContextMenu, P: Into<Position>>(
  1180. &self,
  1181. menu: &M,
  1182. position: P,
  1183. ) -> crate::Result<()> {
  1184. menu.popup_at(self.clone(), position)
  1185. }
  1186. }
  1187. /// Window getters.
  1188. impl<R: Runtime> Window<R> {
  1189. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  1190. pub fn scale_factor(&self) -> crate::Result<f64> {
  1191. self.window.dispatcher.scale_factor().map_err(Into::into)
  1192. }
  1193. /// 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.
  1194. pub fn inner_position(&self) -> crate::Result<PhysicalPosition<i32>> {
  1195. self.window.dispatcher.inner_position().map_err(Into::into)
  1196. }
  1197. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  1198. pub fn outer_position(&self) -> crate::Result<PhysicalPosition<i32>> {
  1199. self.window.dispatcher.outer_position().map_err(Into::into)
  1200. }
  1201. /// Returns the physical size of the window's client area.
  1202. ///
  1203. /// The client area is the content of the window, excluding the title bar and borders.
  1204. pub fn inner_size(&self) -> crate::Result<PhysicalSize<u32>> {
  1205. self.window.dispatcher.inner_size().map_err(Into::into)
  1206. }
  1207. /// Returns the physical size of the entire window.
  1208. ///
  1209. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  1210. pub fn outer_size(&self) -> crate::Result<PhysicalSize<u32>> {
  1211. self.window.dispatcher.outer_size().map_err(Into::into)
  1212. }
  1213. /// Gets the window's current fullscreen state.
  1214. pub fn is_fullscreen(&self) -> crate::Result<bool> {
  1215. self.window.dispatcher.is_fullscreen().map_err(Into::into)
  1216. }
  1217. /// Gets the window's current minimized state.
  1218. pub fn is_minimized(&self) -> crate::Result<bool> {
  1219. self.window.dispatcher.is_minimized().map_err(Into::into)
  1220. }
  1221. /// Gets the window's current maximized state.
  1222. pub fn is_maximized(&self) -> crate::Result<bool> {
  1223. self.window.dispatcher.is_maximized().map_err(Into::into)
  1224. }
  1225. /// Gets the window's current focus state.
  1226. pub fn is_focused(&self) -> crate::Result<bool> {
  1227. self.window.dispatcher.is_focused().map_err(Into::into)
  1228. }
  1229. /// Gets the window's current decoration state.
  1230. pub fn is_decorated(&self) -> crate::Result<bool> {
  1231. self.window.dispatcher.is_decorated().map_err(Into::into)
  1232. }
  1233. /// Gets the window's current resizable state.
  1234. pub fn is_resizable(&self) -> crate::Result<bool> {
  1235. self.window.dispatcher.is_resizable().map_err(Into::into)
  1236. }
  1237. /// Whether the window is enabled or disabled.
  1238. pub fn is_enabled(&self) -> crate::Result<bool> {
  1239. self.window.dispatcher.is_enabled().map_err(Into::into)
  1240. }
  1241. /// Gets the window's native maximize button state
  1242. ///
  1243. /// ## Platform-specific
  1244. ///
  1245. /// - **Linux / iOS / Android:** Unsupported.
  1246. pub fn is_maximizable(&self) -> crate::Result<bool> {
  1247. self.window.dispatcher.is_maximizable().map_err(Into::into)
  1248. }
  1249. /// Gets the window's native minimize button state
  1250. ///
  1251. /// ## Platform-specific
  1252. ///
  1253. /// - **Linux / iOS / Android:** Unsupported.
  1254. pub fn is_minimizable(&self) -> crate::Result<bool> {
  1255. self.window.dispatcher.is_minimizable().map_err(Into::into)
  1256. }
  1257. /// Gets the window's native close button state
  1258. ///
  1259. /// ## Platform-specific
  1260. ///
  1261. /// - **Linux / iOS / Android:** Unsupported.
  1262. pub fn is_closable(&self) -> crate::Result<bool> {
  1263. self.window.dispatcher.is_closable().map_err(Into::into)
  1264. }
  1265. /// Gets the window's current visibility state.
  1266. pub fn is_visible(&self) -> crate::Result<bool> {
  1267. self.window.dispatcher.is_visible().map_err(Into::into)
  1268. }
  1269. /// Gets the window's current title.
  1270. pub fn title(&self) -> crate::Result<String> {
  1271. self.window.dispatcher.title().map_err(Into::into)
  1272. }
  1273. /// Returns the monitor on which the window currently resides.
  1274. ///
  1275. /// Returns None if current monitor can't be detected.
  1276. pub fn current_monitor(&self) -> crate::Result<Option<Monitor>> {
  1277. self
  1278. .window
  1279. .dispatcher
  1280. .current_monitor()
  1281. .map(|m| m.map(Into::into))
  1282. .map_err(Into::into)
  1283. }
  1284. /// Returns the monitor that contains the given point.
  1285. pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
  1286. self
  1287. .window
  1288. .dispatcher
  1289. .monitor_from_point(x, y)
  1290. .map(|m| m.map(Into::into))
  1291. .map_err(Into::into)
  1292. }
  1293. /// Returns the primary monitor of the system.
  1294. ///
  1295. /// Returns None if it can't identify any monitor as a primary one.
  1296. pub fn primary_monitor(&self) -> crate::Result<Option<Monitor>> {
  1297. self
  1298. .window
  1299. .dispatcher
  1300. .primary_monitor()
  1301. .map(|m| m.map(Into::into))
  1302. .map_err(Into::into)
  1303. }
  1304. /// Returns the list of all the monitors available on the system.
  1305. pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
  1306. self
  1307. .window
  1308. .dispatcher
  1309. .available_monitors()
  1310. .map(|m| m.into_iter().map(Into::into).collect())
  1311. .map_err(Into::into)
  1312. }
  1313. /// Returns the native handle that is used by this window.
  1314. #[cfg(target_os = "macos")]
  1315. pub fn ns_window(&self) -> crate::Result<*mut std::ffi::c_void> {
  1316. self
  1317. .window
  1318. .dispatcher
  1319. .window_handle()
  1320. .map_err(Into::into)
  1321. .and_then(|handle| {
  1322. if let raw_window_handle::RawWindowHandle::AppKit(h) = handle.as_raw() {
  1323. let view: &objc2_app_kit::NSView = unsafe { h.ns_view.cast().as_ref() };
  1324. let ns_window = view.window().expect("view to be installed in window");
  1325. Ok(objc2::rc::Retained::autorelease_ptr(ns_window).cast())
  1326. } else {
  1327. Err(crate::Error::InvalidWindowHandle)
  1328. }
  1329. })
  1330. }
  1331. /// Returns the pointer to the content view of this window.
  1332. #[cfg(target_os = "macos")]
  1333. pub fn ns_view(&self) -> crate::Result<*mut std::ffi::c_void> {
  1334. self
  1335. .window
  1336. .dispatcher
  1337. .window_handle()
  1338. .map_err(Into::into)
  1339. .and_then(|handle| {
  1340. if let raw_window_handle::RawWindowHandle::AppKit(h) = handle.as_raw() {
  1341. Ok(h.ns_view.as_ptr())
  1342. } else {
  1343. Err(crate::Error::InvalidWindowHandle)
  1344. }
  1345. })
  1346. }
  1347. /// Returns the native handle that is used by this window.
  1348. #[cfg(windows)]
  1349. pub fn hwnd(&self) -> crate::Result<HWND> {
  1350. self
  1351. .window
  1352. .dispatcher
  1353. .window_handle()
  1354. .map_err(Into::into)
  1355. .and_then(|handle| {
  1356. if let raw_window_handle::RawWindowHandle::Win32(h) = handle.as_raw() {
  1357. Ok(HWND(h.hwnd.get() as _))
  1358. } else {
  1359. Err(crate::Error::InvalidWindowHandle)
  1360. }
  1361. })
  1362. }
  1363. /// Returns the `ApplicationWindow` from gtk crate that is used by this window.
  1364. ///
  1365. /// Note that this type can only be used on the main thread.
  1366. #[cfg(any(
  1367. target_os = "linux",
  1368. target_os = "dragonfly",
  1369. target_os = "freebsd",
  1370. target_os = "netbsd",
  1371. target_os = "openbsd"
  1372. ))]
  1373. pub fn gtk_window(&self) -> crate::Result<gtk::ApplicationWindow> {
  1374. self.window.dispatcher.gtk_window().map_err(Into::into)
  1375. }
  1376. /// Returns the vertical [`gtk::Box`] that is added by default as the sole child of this window.
  1377. ///
  1378. /// Note that this type can only be used on the main thread.
  1379. #[cfg(any(
  1380. target_os = "linux",
  1381. target_os = "dragonfly",
  1382. target_os = "freebsd",
  1383. target_os = "netbsd",
  1384. target_os = "openbsd"
  1385. ))]
  1386. pub fn default_vbox(&self) -> crate::Result<gtk::Box> {
  1387. self.window.dispatcher.default_vbox().map_err(Into::into)
  1388. }
  1389. /// Returns the current window theme.
  1390. ///
  1391. /// ## Platform-specific
  1392. ///
  1393. /// - **macOS**: Only supported on macOS 10.14+.
  1394. pub fn theme(&self) -> crate::Result<Theme> {
  1395. self.window.dispatcher.theme().map_err(Into::into)
  1396. }
  1397. }
  1398. /// Desktop window getters.
  1399. #[cfg(desktop)]
  1400. impl<R: Runtime> Window<R> {
  1401. /// Get the cursor position relative to the top-left hand corner of the desktop.
  1402. ///
  1403. /// Note that the top-left hand corner of the desktop is not necessarily the same as the screen.
  1404. /// If the user uses a desktop with multiple monitors,
  1405. /// the top-left hand corner of the desktop is the top-left hand corner of the main monitor on Windows and macOS
  1406. /// or the top-left of the leftmost monitor on X11.
  1407. ///
  1408. /// The coordinates can be negative if the top-left hand corner of the window is outside of the visible screen region.
  1409. pub fn cursor_position(&self) -> crate::Result<PhysicalPosition<f64>> {
  1410. self.app_handle.cursor_position()
  1411. }
  1412. }
  1413. /// Desktop window setters and actions.
  1414. #[cfg(desktop)]
  1415. impl<R: Runtime> Window<R> {
  1416. /// Centers the window.
  1417. pub fn center(&self) -> crate::Result<()> {
  1418. self.window.dispatcher.center().map_err(Into::into)
  1419. }
  1420. /// Requests user attention to the window, this has no effect if the application
  1421. /// is already focused. How requesting for user attention manifests is platform dependent,
  1422. /// see `UserAttentionType` for details.
  1423. ///
  1424. /// Providing `None` will unset the request for user attention. Unsetting the request for
  1425. /// user attention might not be done automatically by the WM when the window receives input.
  1426. ///
  1427. /// ## Platform-specific
  1428. ///
  1429. /// - **macOS:** `None` has no effect.
  1430. /// - **Linux:** Urgency levels have the same effect.
  1431. pub fn request_user_attention(
  1432. &self,
  1433. request_type: Option<UserAttentionType>,
  1434. ) -> crate::Result<()> {
  1435. self
  1436. .window
  1437. .dispatcher
  1438. .request_user_attention(request_type)
  1439. .map_err(Into::into)
  1440. }
  1441. /// Determines if this window should be resizable.
  1442. /// When resizable is set to false, native window's maximize button is automatically disabled.
  1443. pub fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
  1444. self
  1445. .window
  1446. .dispatcher
  1447. .set_resizable(resizable)
  1448. .map_err(Into::into)
  1449. }
  1450. /// Determines if this window's native maximize button should be enabled.
  1451. /// If resizable is set to false, this setting is ignored.
  1452. ///
  1453. /// ## Platform-specific
  1454. ///
  1455. /// - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  1456. /// - **Linux / iOS / Android:** Unsupported.
  1457. pub fn set_maximizable(&self, maximizable: bool) -> crate::Result<()> {
  1458. self
  1459. .window
  1460. .dispatcher
  1461. .set_maximizable(maximizable)
  1462. .map_err(Into::into)
  1463. }
  1464. /// Determines if this window's native minimize button should be enabled.
  1465. ///
  1466. /// ## Platform-specific
  1467. ///
  1468. /// - **Linux / iOS / Android:** Unsupported.
  1469. pub fn set_minimizable(&self, minimizable: bool) -> crate::Result<()> {
  1470. self
  1471. .window
  1472. .dispatcher
  1473. .set_minimizable(minimizable)
  1474. .map_err(Into::into)
  1475. }
  1476. /// Determines if this window's native close button should be enabled.
  1477. ///
  1478. /// ## Platform-specific
  1479. ///
  1480. /// - **Linux:** "GTK+ will do its best to convince the window manager not to show a close button.
  1481. /// Depending on the system, this function may not have any effect when called on a window that is already visible"
  1482. /// - **iOS / Android:** Unsupported.
  1483. pub fn set_closable(&self, closable: bool) -> crate::Result<()> {
  1484. self
  1485. .window
  1486. .dispatcher
  1487. .set_closable(closable)
  1488. .map_err(Into::into)
  1489. }
  1490. /// Set this window's title.
  1491. pub fn set_title(&self, title: &str) -> crate::Result<()> {
  1492. self
  1493. .window
  1494. .dispatcher
  1495. .set_title(title.to_string())
  1496. .map_err(Into::into)
  1497. }
  1498. /// Enable or disable the window.
  1499. pub fn set_enabled(&self, enabled: bool) -> crate::Result<()> {
  1500. self
  1501. .window
  1502. .dispatcher
  1503. .set_enabled(enabled)
  1504. .map_err(Into::into)
  1505. }
  1506. /// Maximizes this window.
  1507. pub fn maximize(&self) -> crate::Result<()> {
  1508. self.window.dispatcher.maximize().map_err(Into::into)
  1509. }
  1510. /// Un-maximizes this window.
  1511. pub fn unmaximize(&self) -> crate::Result<()> {
  1512. self.window.dispatcher.unmaximize().map_err(Into::into)
  1513. }
  1514. /// Minimizes this window.
  1515. pub fn minimize(&self) -> crate::Result<()> {
  1516. self.window.dispatcher.minimize().map_err(Into::into)
  1517. }
  1518. /// Un-minimizes this window.
  1519. pub fn unminimize(&self) -> crate::Result<()> {
  1520. self.window.dispatcher.unminimize().map_err(Into::into)
  1521. }
  1522. /// Show this window.
  1523. pub fn show(&self) -> crate::Result<()> {
  1524. self.window.dispatcher.show().map_err(Into::into)
  1525. }
  1526. /// Hide this window.
  1527. pub fn hide(&self) -> crate::Result<()> {
  1528. self.window.dispatcher.hide().map_err(Into::into)
  1529. }
  1530. /// Closes this window. It emits [`crate::RunEvent::CloseRequested`] first like a user-initiated close request so you can intercept it.
  1531. pub fn close(&self) -> crate::Result<()> {
  1532. self.window.dispatcher.close().map_err(Into::into)
  1533. }
  1534. /// Destroys this window. Similar to [`Self::close`] but does not emit any events and force close the window instead.
  1535. pub fn destroy(&self) -> crate::Result<()> {
  1536. self.window.dispatcher.destroy().map_err(Into::into)
  1537. }
  1538. /// Determines if this window should be [decorated].
  1539. ///
  1540. /// [decorated]: https://en.wikipedia.org/wiki/Window_(computing)#Window_decoration
  1541. pub fn set_decorations(&self, decorations: bool) -> crate::Result<()> {
  1542. self
  1543. .window
  1544. .dispatcher
  1545. .set_decorations(decorations)
  1546. .map_err(Into::into)
  1547. }
  1548. /// Determines if this window should have shadow.
  1549. ///
  1550. /// ## Platform-specific
  1551. ///
  1552. /// - **Windows:**
  1553. /// - `false` has no effect on decorated window, shadow are always ON.
  1554. /// - `true` will make undecorated window have a 1px white border,
  1555. /// and on Windows 11, it will have a rounded corners.
  1556. /// - **Linux:** Unsupported.
  1557. pub fn set_shadow(&self, enable: bool) -> crate::Result<()> {
  1558. self
  1559. .window
  1560. .dispatcher
  1561. .set_shadow(enable)
  1562. .map_err(Into::into)
  1563. }
  1564. /// Sets window effects, pass [`None`] to clear any effects applied if possible.
  1565. ///
  1566. /// Requires the window to be transparent.
  1567. ///
  1568. /// See [`EffectsBuilder`] for a convenient builder for [`WindowEffectsConfig`].
  1569. ///
  1570. #[cfg_attr(
  1571. feature = "unstable",
  1572. doc = r####"
  1573. ```rust,no_run
  1574. use tauri::{Manager, window::{Color, Effect, EffectState, EffectsBuilder}};
  1575. tauri::Builder::default()
  1576. .setup(|app| {
  1577. let window = app.get_window("main").unwrap();
  1578. window.set_effects(
  1579. EffectsBuilder::new()
  1580. .effect(Effect::Popover)
  1581. .state(EffectState::Active)
  1582. .radius(5.)
  1583. .color(Color(0, 0, 0, 255))
  1584. .build(),
  1585. )?;
  1586. Ok(())
  1587. });
  1588. ```
  1589. "####
  1590. )]
  1591. ///
  1592. /// ## Platform-specific:
  1593. ///
  1594. /// - **Windows**: If using decorations or shadows, you may want to try this workaround <https://github.com/tauri-apps/tao/issues/72#issuecomment-975607891>
  1595. /// - **Linux**: Unsupported
  1596. pub fn set_effects<E: Into<Option<WindowEffectsConfig>>>(&self, effects: E) -> crate::Result<()> {
  1597. let effects = effects.into();
  1598. let window = self.clone();
  1599. self.run_on_main_thread(move || {
  1600. let _ = crate::vibrancy::set_window_effects(&window, effects);
  1601. })
  1602. }
  1603. /// Determines if this window should always be below other windows.
  1604. pub fn set_always_on_bottom(&self, always_on_bottom: bool) -> crate::Result<()> {
  1605. self
  1606. .window
  1607. .dispatcher
  1608. .set_always_on_bottom(always_on_bottom)
  1609. .map_err(Into::into)
  1610. }
  1611. /// Determines if this window should always be on top of other windows.
  1612. pub fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()> {
  1613. self
  1614. .window
  1615. .dispatcher
  1616. .set_always_on_top(always_on_top)
  1617. .map_err(Into::into)
  1618. }
  1619. /// Sets whether the window should be visible on all workspaces or virtual desktops.
  1620. ///
  1621. /// ## Platform-specific
  1622. ///
  1623. /// - **Windows / iOS / Android:** Unsupported.
  1624. pub fn set_visible_on_all_workspaces(
  1625. &self,
  1626. visible_on_all_workspaces: bool,
  1627. ) -> crate::Result<()> {
  1628. self
  1629. .window
  1630. .dispatcher
  1631. .set_visible_on_all_workspaces(visible_on_all_workspaces)
  1632. .map_err(Into::into)
  1633. }
  1634. /// Prevents the window contents from being captured by other apps.
  1635. pub fn set_content_protected(&self, protected: bool) -> crate::Result<()> {
  1636. self
  1637. .window
  1638. .dispatcher
  1639. .set_content_protected(protected)
  1640. .map_err(Into::into)
  1641. }
  1642. /// Resizes this window.
  1643. pub fn set_size<S: Into<Size>>(&self, size: S) -> crate::Result<()> {
  1644. self
  1645. .window
  1646. .dispatcher
  1647. .set_size(size.into())
  1648. .map_err(Into::into)
  1649. }
  1650. /// Sets this window's minimum inner size.
  1651. pub fn set_min_size<S: Into<Size>>(&self, size: Option<S>) -> crate::Result<()> {
  1652. self
  1653. .window
  1654. .dispatcher
  1655. .set_min_size(size.map(|s| s.into()))
  1656. .map_err(Into::into)
  1657. }
  1658. /// Sets this window's maximum inner size.
  1659. pub fn set_max_size<S: Into<Size>>(&self, size: Option<S>) -> crate::Result<()> {
  1660. self
  1661. .window
  1662. .dispatcher
  1663. .set_max_size(size.map(|s| s.into()))
  1664. .map_err(Into::into)
  1665. }
  1666. /// Sets this window's minimum inner width.
  1667. pub fn set_size_constraints(
  1668. &self,
  1669. constriants: tauri_runtime::window::WindowSizeConstraints,
  1670. ) -> crate::Result<()> {
  1671. self
  1672. .window
  1673. .dispatcher
  1674. .set_size_constraints(constriants)
  1675. .map_err(Into::into)
  1676. }
  1677. /// Sets this window's position.
  1678. pub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> crate::Result<()> {
  1679. self
  1680. .window
  1681. .dispatcher
  1682. .set_position(position.into())
  1683. .map_err(Into::into)
  1684. }
  1685. /// Determines if this window should be fullscreen.
  1686. pub fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()> {
  1687. self
  1688. .window
  1689. .dispatcher
  1690. .set_fullscreen(fullscreen)
  1691. .map_err(Into::into)
  1692. }
  1693. /// Bring the window to front and focus.
  1694. pub fn set_focus(&self) -> crate::Result<()> {
  1695. self.window.dispatcher.set_focus().map_err(Into::into)
  1696. }
  1697. /// Sets this window' icon.
  1698. pub fn set_icon(&self, icon: Image<'_>) -> crate::Result<()> {
  1699. self
  1700. .window
  1701. .dispatcher
  1702. .set_icon(icon.into())
  1703. .map_err(Into::into)
  1704. }
  1705. /// Whether to hide the window icon from the taskbar or not.
  1706. ///
  1707. /// ## Platform-specific
  1708. ///
  1709. /// - **macOS:** Unsupported.
  1710. pub fn set_skip_taskbar(&self, skip: bool) -> crate::Result<()> {
  1711. self
  1712. .window
  1713. .dispatcher
  1714. .set_skip_taskbar(skip)
  1715. .map_err(Into::into)
  1716. }
  1717. /// Grabs the cursor, preventing it from leaving the window.
  1718. ///
  1719. /// There's no guarantee that the cursor will be hidden. You should
  1720. /// hide it by yourself if you want so.
  1721. ///
  1722. /// ## Platform-specific
  1723. ///
  1724. /// - **Linux:** Unsupported.
  1725. /// - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
  1726. pub fn set_cursor_grab(&self, grab: bool) -> crate::Result<()> {
  1727. self
  1728. .window
  1729. .dispatcher
  1730. .set_cursor_grab(grab)
  1731. .map_err(Into::into)
  1732. }
  1733. /// Modifies the cursor's visibility.
  1734. ///
  1735. /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
  1736. ///
  1737. /// ## Platform-specific
  1738. ///
  1739. /// - **Windows:** The cursor is only hidden within the confines of the window.
  1740. /// - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor is
  1741. /// outside of the window.
  1742. pub fn set_cursor_visible(&self, visible: bool) -> crate::Result<()> {
  1743. self
  1744. .window
  1745. .dispatcher
  1746. .set_cursor_visible(visible)
  1747. .map_err(Into::into)
  1748. }
  1749. /// Modifies the cursor icon of the window.
  1750. pub fn set_cursor_icon(&self, icon: CursorIcon) -> crate::Result<()> {
  1751. self
  1752. .window
  1753. .dispatcher
  1754. .set_cursor_icon(icon)
  1755. .map_err(Into::into)
  1756. }
  1757. /// Changes the position of the cursor in window coordinates.
  1758. pub fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> crate::Result<()> {
  1759. self
  1760. .window
  1761. .dispatcher
  1762. .set_cursor_position(position)
  1763. .map_err(Into::into)
  1764. }
  1765. /// Ignores the window cursor events.
  1766. pub fn set_ignore_cursor_events(&self, ignore: bool) -> crate::Result<()> {
  1767. self
  1768. .window
  1769. .dispatcher
  1770. .set_ignore_cursor_events(ignore)
  1771. .map_err(Into::into)
  1772. }
  1773. /// Starts dragging the window.
  1774. pub fn start_dragging(&self) -> crate::Result<()> {
  1775. self.window.dispatcher.start_dragging().map_err(Into::into)
  1776. }
  1777. /// Starts resize-dragging the window.
  1778. pub fn start_resize_dragging(
  1779. &self,
  1780. direction: tauri_runtime::ResizeDirection,
  1781. ) -> crate::Result<()> {
  1782. self
  1783. .window
  1784. .dispatcher
  1785. .start_resize_dragging(direction)
  1786. .map_err(Into::into)
  1787. }
  1788. /// Sets the taskbar progress state.
  1789. ///
  1790. /// ## Platform-specific
  1791. ///
  1792. /// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
  1793. /// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
  1794. /// - **iOS / Android:** Unsupported.
  1795. pub fn set_progress_bar(&self, progress_state: ProgressBarState) -> crate::Result<()> {
  1796. self
  1797. .window
  1798. .dispatcher
  1799. .set_progress_bar(crate::runtime::ProgressBarState {
  1800. status: progress_state.status,
  1801. progress: progress_state.progress,
  1802. desktop_filename: Some(format!("{}.desktop", self.package_info().name)),
  1803. })
  1804. .map_err(Into::into)
  1805. }
  1806. /// Sets the title bar style. **macOS only**.
  1807. pub fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> crate::Result<()> {
  1808. self
  1809. .window
  1810. .dispatcher
  1811. .set_title_bar_style(style)
  1812. .map_err(Into::into)
  1813. }
  1814. /// Sets the theme for this window.
  1815. ///
  1816. /// ## Platform-specific
  1817. ///
  1818. /// - **Linux / macOS**: Theme is app-wide and not specific to this window.
  1819. /// - **iOS / Android:** Unsupported.
  1820. pub fn set_theme(&self, theme: Option<Theme>) -> crate::Result<()> {
  1821. self
  1822. .window
  1823. .dispatcher
  1824. .set_theme(theme)
  1825. .map_err(Into::<crate::Error>::into)?;
  1826. #[cfg(windows)]
  1827. if let (Some(menu), Ok(hwnd)) = (self.menu(), self.hwnd()) {
  1828. let raw_hwnd = hwnd.0 as isize;
  1829. self.run_on_main_thread(move || {
  1830. let _ = unsafe {
  1831. menu.inner().set_theme_for_hwnd(
  1832. raw_hwnd,
  1833. theme
  1834. .map(crate::menu::map_to_menu_theme)
  1835. .unwrap_or(muda::MenuTheme::Auto),
  1836. )
  1837. };
  1838. })?;
  1839. };
  1840. Ok(())
  1841. }
  1842. }
  1843. /// Progress bar state.
  1844. #[cfg(desktop)]
  1845. #[cfg_attr(
  1846. docsrs,
  1847. doc(cfg(any(target_os = "macos", target_os = "linux", windows)))
  1848. )]
  1849. #[derive(serde::Deserialize)]
  1850. pub struct ProgressBarState {
  1851. /// The progress bar status.
  1852. pub status: Option<ProgressBarStatus>,
  1853. /// The progress bar progress. This can be a value ranging from `0` to `100`
  1854. pub progress: Option<u64>,
  1855. }
  1856. impl<R: Runtime> Listener<R> for Window<R> {
  1857. /// Listen to an event on this window.
  1858. ///
  1859. /// # Examples
  1860. #[cfg_attr(
  1861. feature = "unstable",
  1862. doc = r####"
  1863. ```
  1864. use tauri::{Manager, Listener};
  1865. tauri::Builder::default()
  1866. .setup(|app| {
  1867. let window = app.get_window("main").unwrap();
  1868. window.listen("component-loaded", move |event| {
  1869. println!("window just loaded a component");
  1870. });
  1871. Ok(())
  1872. });
  1873. ```
  1874. "####
  1875. )]
  1876. fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
  1877. where
  1878. F: Fn(Event) + Send + 'static,
  1879. {
  1880. self.manager.listen(
  1881. event.into(),
  1882. EventTarget::Window {
  1883. label: self.label().to_string(),
  1884. },
  1885. handler,
  1886. )
  1887. }
  1888. /// Listen to an event on this window only once.
  1889. ///
  1890. /// See [`Self::listen`] for more information.
  1891. fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
  1892. where
  1893. F: FnOnce(Event) + Send + 'static,
  1894. {
  1895. self.manager.once(
  1896. event.into(),
  1897. EventTarget::Window {
  1898. label: self.label().to_string(),
  1899. },
  1900. handler,
  1901. )
  1902. }
  1903. /// Unlisten to an event on this window.
  1904. ///
  1905. /// # Examples
  1906. #[cfg_attr(
  1907. feature = "unstable",
  1908. doc = r####"
  1909. ```
  1910. use tauri::{Manager, Listener};
  1911. tauri::Builder::default()
  1912. .setup(|app| {
  1913. let window = app.get_window("main").unwrap();
  1914. let window_ = window.clone();
  1915. let handler = window.listen("component-loaded", move |event| {
  1916. println!("window just loaded a component");
  1917. // we no longer need to listen to the event
  1918. // we also could have used `window.once` instead
  1919. window_.unlisten(event.id());
  1920. });
  1921. // stop listening to the event when you do not need it anymore
  1922. window.unlisten(handler);
  1923. Ok(())
  1924. });
  1925. ```
  1926. "####
  1927. )]
  1928. fn unlisten(&self, id: EventId) {
  1929. self.manager.unlisten(id)
  1930. }
  1931. }
  1932. impl<R: Runtime> Emitter<R> for Window<R> {
  1933. /// Emits an event to all [targets](EventTarget).
  1934. ///
  1935. /// # Examples
  1936. #[cfg_attr(
  1937. feature = "unstable",
  1938. doc = r####"
  1939. ```
  1940. use tauri::Emitter;
  1941. #[tauri::command]
  1942. fn synchronize(window: tauri::Window) {
  1943. // emits the synchronized event to all webviews
  1944. window.emit("synchronized", ());
  1945. }
  1946. ```
  1947. "####
  1948. )]
  1949. fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
  1950. self.manager.emit(event, payload)
  1951. }
  1952. /// Emits an event to all [targets](EventTarget) matching the given target.
  1953. ///
  1954. /// # Examples
  1955. #[cfg_attr(
  1956. feature = "unstable",
  1957. doc = r####"
  1958. ```
  1959. use tauri::{Emitter, EventTarget};
  1960. #[tauri::command]
  1961. fn download(window: tauri::Window) {
  1962. for i in 1..100 {
  1963. std::thread::sleep(std::time::Duration::from_millis(150));
  1964. // emit a download progress event to all listeners
  1965. window.emit_to(EventTarget::any(), "download-progress", i);
  1966. // emit an event to listeners that used App::listen or AppHandle::listen
  1967. window.emit_to(EventTarget::app(), "download-progress", i);
  1968. // emit an event to any webview/window/webviewWindow matching the given label
  1969. window.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled
  1970. window.emit_to(EventTarget::labeled("updater"), "download-progress", i);
  1971. // emit an event to listeners that used WebviewWindow::listen
  1972. window.emit_to(EventTarget::webview_window("updater"), "download-progress", i);
  1973. }
  1974. }
  1975. ```
  1976. "####
  1977. )]
  1978. fn emit_to<I, S>(&self, target: I, event: &str, payload: S) -> crate::Result<()>
  1979. where
  1980. I: Into<EventTarget>,
  1981. S: Serialize + Clone,
  1982. {
  1983. self.manager.emit_to(target, event, payload)
  1984. }
  1985. /// Emits an event to all [targets](EventTarget) based on the given filter.
  1986. ///
  1987. /// # Examples
  1988. #[cfg_attr(
  1989. feature = "unstable",
  1990. doc = r####"
  1991. ```
  1992. use tauri::{Emitter, EventTarget};
  1993. #[tauri::command]
  1994. fn download(window: tauri::Window) {
  1995. for i in 1..100 {
  1996. std::thread::sleep(std::time::Duration::from_millis(150));
  1997. // emit a download progress event to the updater window
  1998. window.emit_filter("download-progress", i, |t| match t {
  1999. EventTarget::WebviewWindow { label } => label == "main",
  2000. _ => false,
  2001. });
  2002. }
  2003. }
  2004. ```
  2005. "####
  2006. )]
  2007. fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> crate::Result<()>
  2008. where
  2009. S: Serialize + Clone,
  2010. F: Fn(&EventTarget) -> bool,
  2011. {
  2012. self.manager.emit_filter(event, payload, filter)
  2013. }
  2014. }
  2015. /// The [`WindowEffectsConfig`] object builder
  2016. #[derive(Default)]
  2017. pub struct EffectsBuilder(WindowEffectsConfig);
  2018. impl EffectsBuilder {
  2019. /// Create a new [`WindowEffectsConfig`] builder
  2020. pub fn new() -> Self {
  2021. Self(WindowEffectsConfig::default())
  2022. }
  2023. /// Adds effect to the [`WindowEffectsConfig`] `effects` field
  2024. pub fn effect(mut self, effect: Effect) -> Self {
  2025. self.0.effects.push(effect);
  2026. self
  2027. }
  2028. /// Adds effects to the [`WindowEffectsConfig`] `effects` field
  2029. pub fn effects<I: IntoIterator<Item = Effect>>(mut self, effects: I) -> Self {
  2030. self.0.effects.extend(effects);
  2031. self
  2032. }
  2033. /// Clears the [`WindowEffectsConfig`] `effects` field
  2034. pub fn clear_effects(mut self) -> Self {
  2035. self.0.effects.clear();
  2036. self
  2037. }
  2038. /// Sets `state` field for the [`WindowEffectsConfig`] **macOS Only**
  2039. pub fn state(mut self, state: EffectState) -> Self {
  2040. self.0.state = Some(state);
  2041. self
  2042. }
  2043. /// Sets `radius` field fo the [`WindowEffectsConfig`] **macOS Only**
  2044. pub fn radius(mut self, radius: f64) -> Self {
  2045. self.0.radius = Some(radius);
  2046. self
  2047. }
  2048. /// Sets `color` field fo the [`WindowEffectsConfig`] **Windows Only**
  2049. pub fn color(mut self, color: Color) -> Self {
  2050. self.0.color = Some(color);
  2051. self
  2052. }
  2053. /// Builds a [`WindowEffectsConfig`]
  2054. pub fn build(self) -> WindowEffectsConfig {
  2055. self.0
  2056. }
  2057. }
  2058. impl From<WindowEffectsConfig> for EffectsBuilder {
  2059. fn from(value: WindowEffectsConfig) -> Self {
  2060. Self(value)
  2061. }
  2062. }
  2063. #[cfg(test)]
  2064. mod tests {
  2065. #[test]
  2066. fn window_is_send_sync() {
  2067. crate::test_utils::assert_send::<super::Window>();
  2068. crate::test_utils::assert_sync::<super::Window>();
  2069. }
  2070. }