window.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. import { invokeTauriCommand } from './helpers/tauri.js';
  2. import { listen, once, emit } from './helpers/event.js';
  3. import { TauriEvent } from './event.js';
  4. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  5. // SPDX-License-Identifier: Apache-2.0
  6. // SPDX-License-Identifier: MIT
  7. /**
  8. * Provides APIs to create windows, communicate with other windows and manipulate the current window.
  9. *
  10. * This package is also accessible with `window.__TAURI__.window` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  11. *
  12. * The APIs must be added to [`tauri.allowlist.window`](https://tauri.app/v1/api/config/#allowlistconfig.window) in `tauri.conf.json`:
  13. * ```json
  14. * {
  15. * "tauri": {
  16. * "allowlist": {
  17. * "window": {
  18. * "all": true, // enable all window APIs
  19. * "create": true, // enable window creation
  20. * "center": true,
  21. * "requestUserAttention": true,
  22. * "setResizable": true,
  23. * "setMaximizable": true,
  24. * "setMinimizable": true,
  25. * "setClosable": true,
  26. * "setTitle": true,
  27. * "maximize": true,
  28. * "unmaximize": true,
  29. * "minimize": true,
  30. * "unminimize": true,
  31. * "show": true,
  32. * "hide": true,
  33. * "close": true,
  34. * "setDecorations": true,
  35. * "setAlwaysOnTop": true,
  36. * "setContentProtected": true,
  37. * "setSize": true,
  38. * "setMinSize": true,
  39. * "setMaxSize": true,
  40. * "setPosition": true,
  41. * "setFullscreen": true,
  42. * "setFocus": true,
  43. * "setIcon": true,
  44. * "setSkipTaskbar": true,
  45. * "setCursorGrab": true,
  46. * "setCursorVisible": true,
  47. * "setCursorIcon": true,
  48. * "setCursorPosition": true,
  49. * "setIgnoreCursorEvents": true,
  50. * "startDragging": true,
  51. * "print": true
  52. * }
  53. * }
  54. * }
  55. * }
  56. * ```
  57. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  58. *
  59. * ## Window events
  60. *
  61. * Events can be listened to using `appWindow.listen`:
  62. * ```typescript
  63. * import { appWindow } from "@tauri-apps/api/window";
  64. * appWindow.listen("my-window-event", ({ event, payload }) => { });
  65. * ```
  66. *
  67. * @module
  68. */
  69. /**
  70. * A size represented in logical pixels.
  71. *
  72. * @since 1.0.0
  73. */
  74. class LogicalSize {
  75. constructor(width, height) {
  76. this.type = 'Logical';
  77. this.width = width;
  78. this.height = height;
  79. }
  80. }
  81. /**
  82. * A size represented in physical pixels.
  83. *
  84. * @since 1.0.0
  85. */
  86. class PhysicalSize {
  87. constructor(width, height) {
  88. this.type = 'Physical';
  89. this.width = width;
  90. this.height = height;
  91. }
  92. /**
  93. * Converts the physical size to a logical one.
  94. * @example
  95. * ```typescript
  96. * import { appWindow } from '@tauri-apps/api/window';
  97. * const factor = await appWindow.scaleFactor();
  98. * const size = await appWindow.innerSize();
  99. * const logical = size.toLogical(factor);
  100. * ```
  101. * */
  102. toLogical(scaleFactor) {
  103. return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor);
  104. }
  105. }
  106. /**
  107. * A position represented in logical pixels.
  108. *
  109. * @since 1.0.0
  110. */
  111. class LogicalPosition {
  112. constructor(x, y) {
  113. this.type = 'Logical';
  114. this.x = x;
  115. this.y = y;
  116. }
  117. }
  118. /**
  119. * A position represented in physical pixels.
  120. *
  121. * @since 1.0.0
  122. */
  123. class PhysicalPosition {
  124. constructor(x, y) {
  125. this.type = 'Physical';
  126. this.x = x;
  127. this.y = y;
  128. }
  129. /**
  130. * Converts the physical position to a logical one.
  131. * @example
  132. * ```typescript
  133. * import { appWindow } from '@tauri-apps/api/window';
  134. * const factor = await appWindow.scaleFactor();
  135. * const position = await appWindow.innerPosition();
  136. * const logical = position.toLogical(factor);
  137. * ```
  138. * */
  139. toLogical(scaleFactor) {
  140. return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor);
  141. }
  142. }
  143. /**
  144. * Attention type to request on a window.
  145. *
  146. * @since 1.0.0
  147. */
  148. var UserAttentionType;
  149. (function (UserAttentionType) {
  150. /**
  151. * #### Platform-specific
  152. * - **macOS:** Bounces the dock icon until the application is in focus.
  153. * - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
  154. */
  155. UserAttentionType[UserAttentionType["Critical"] = 1] = "Critical";
  156. /**
  157. * #### Platform-specific
  158. * - **macOS:** Bounces the dock icon once.
  159. * - **Windows:** Flashes the taskbar button until the application is in focus.
  160. */
  161. UserAttentionType[UserAttentionType["Informational"] = 2] = "Informational";
  162. })(UserAttentionType || (UserAttentionType = {}));
  163. /**
  164. * Get an instance of `WebviewWindow` for the current webview window.
  165. *
  166. * @since 1.0.0
  167. */
  168. function getCurrent() {
  169. return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
  170. // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
  171. skip: true
  172. });
  173. }
  174. /**
  175. * Gets a list of instances of `WebviewWindow` for all available webview windows.
  176. *
  177. * @since 1.0.0
  178. */
  179. function getAll() {
  180. return window.__TAURI_METADATA__.__windows.map((w) => new WebviewWindow(w.label, {
  181. // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
  182. skip: true
  183. }));
  184. }
  185. /** @ignore */
  186. // events that are emitted right here instead of by the created webview
  187. const localTauriEvents = ['tauri://created', 'tauri://error'];
  188. /**
  189. * A webview window handle allows emitting and listening to events from the backend that are tied to the window.
  190. *
  191. * @ignore
  192. * @since 1.0.0
  193. */
  194. class WebviewWindowHandle {
  195. constructor(label) {
  196. this.label = label;
  197. // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  198. this.listeners = Object.create(null);
  199. }
  200. /**
  201. * Listen to an event emitted by the backend or webview.
  202. * The event must either be a global event or an event targetting this window.
  203. *
  204. * See {@link WebviewWindow.emit | `emit`} for more information.
  205. *
  206. * @example
  207. * ```typescript
  208. * import { appWindow } from '@tauri-apps/api/window';
  209. * const unlisten = await appWindow.listen<string>('state-changed', (event) => {
  210. * console.log(`Got error: ${payload}`);
  211. * });
  212. *
  213. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  214. * unlisten();
  215. * ```
  216. *
  217. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  218. *
  219. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  220. * @param handler Event handler.
  221. * @returns A promise resolving to a function to unlisten to the event.
  222. */
  223. async listen(event, handler) {
  224. if (this._handleTauriEvent(event, handler)) {
  225. return Promise.resolve(() => {
  226. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
  227. const listeners = this.listeners[event];
  228. listeners.splice(listeners.indexOf(handler), 1);
  229. });
  230. }
  231. return listen(event, this.label, handler);
  232. }
  233. /**
  234. * Listen to an one-off event.
  235. * See {@link WebviewWindow.listen | `listen`} for more information.
  236. *
  237. * @example
  238. * ```typescript
  239. * import { appWindow } from '@tauri-apps/api/window';
  240. * const unlisten = await appWindow.once<null>('initialized', (event) => {
  241. * console.log(`Window initialized!`);
  242. * });
  243. *
  244. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  245. * unlisten();
  246. * ```
  247. *
  248. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  249. *
  250. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  251. * @param handler Event handler.
  252. * @returns A promise resolving to a function to unlisten to the event.
  253. */
  254. async once(event, handler) {
  255. if (this._handleTauriEvent(event, handler)) {
  256. return Promise.resolve(() => {
  257. // eslint-disable-next-line security/detect-object-injection
  258. const listeners = this.listeners[event];
  259. listeners.splice(listeners.indexOf(handler), 1);
  260. });
  261. }
  262. return once(event, this.label, handler);
  263. }
  264. /**
  265. * Emits an event to the backend and all Tauri windows.
  266. * The event will have this window's {@link WebviewWindow.label | label} as {@link Event.windowLabel | source window label}.
  267. *
  268. * @example
  269. * ```typescript
  270. * import { appWindow } from '@tauri-apps/api/window';
  271. * await appWindow.emit('window-loaded', { loggedIn: true, token: 'authToken' });
  272. * ```
  273. *
  274. * This function can also be used to communicate between windows:
  275. * ```typescript
  276. * import { appWindow } from '@tauri-apps/api/window';
  277. * await appWindow.listen('sync-data', (event) => { });
  278. *
  279. * // on another window...
  280. * import { WebviewWindow } from '@tauri-apps/api/window';
  281. * const otherWindow = WebviewWindow.getByLabel('other')
  282. * await otherWindow.emit('sync-data');
  283. * ```
  284. *
  285. * Global listeners are also triggered:
  286. * ```typescript
  287. * import { appWindow } from '@tauri-apps/api/window';
  288. * import { listen } from '@tauri-apps/api/event';
  289. * await listen('ping', (event) => { });
  290. *
  291. * await appWindow.emit('ping');
  292. * ```
  293. *
  294. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  295. * @param payload Event payload.
  296. */
  297. async emit(event, payload) {
  298. if (localTauriEvents.includes(event)) {
  299. // eslint-disable-next-line
  300. for (const handler of this.listeners[event] || []) {
  301. handler({ event, id: -1, windowLabel: this.label, payload });
  302. }
  303. return Promise.resolve();
  304. }
  305. return emit(event, this.label, payload);
  306. }
  307. /** @ignore */
  308. _handleTauriEvent(event, handler) {
  309. if (localTauriEvents.includes(event)) {
  310. if (!(event in this.listeners)) {
  311. // eslint-disable-next-line
  312. this.listeners[event] = [handler];
  313. }
  314. else {
  315. // eslint-disable-next-line
  316. this.listeners[event].push(handler);
  317. }
  318. return true;
  319. }
  320. return false;
  321. }
  322. }
  323. /**
  324. * Manage the current window object.
  325. *
  326. * @ignore
  327. * @since 1.0.0
  328. */
  329. class WindowManager extends WebviewWindowHandle {
  330. // Getters
  331. /**
  332. * The scale factor that can be used to map physical pixels to logical pixels.
  333. * @example
  334. * ```typescript
  335. * import { appWindow } from '@tauri-apps/api/window';
  336. * const factor = await appWindow.scaleFactor();
  337. * ```
  338. *
  339. * @returns The window's monitor scale factor.
  340. * */
  341. async scaleFactor() {
  342. return invokeTauriCommand({
  343. __tauriModule: 'Window',
  344. message: {
  345. cmd: 'manage',
  346. data: {
  347. label: this.label,
  348. cmd: {
  349. type: 'scaleFactor'
  350. }
  351. }
  352. }
  353. });
  354. }
  355. /**
  356. * The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop.
  357. * @example
  358. * ```typescript
  359. * import { appWindow } from '@tauri-apps/api/window';
  360. * const position = await appWindow.innerPosition();
  361. * ```
  362. *
  363. * @returns The window's inner position.
  364. * */
  365. async innerPosition() {
  366. return invokeTauriCommand({
  367. __tauriModule: 'Window',
  368. message: {
  369. cmd: 'manage',
  370. data: {
  371. label: this.label,
  372. cmd: {
  373. type: 'innerPosition'
  374. }
  375. }
  376. }
  377. }).then(({ x, y }) => new PhysicalPosition(x, y));
  378. }
  379. /**
  380. * The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  381. * @example
  382. * ```typescript
  383. * import { appWindow } from '@tauri-apps/api/window';
  384. * const position = await appWindow.outerPosition();
  385. * ```
  386. *
  387. * @returns The window's outer position.
  388. * */
  389. async outerPosition() {
  390. return invokeTauriCommand({
  391. __tauriModule: 'Window',
  392. message: {
  393. cmd: 'manage',
  394. data: {
  395. label: this.label,
  396. cmd: {
  397. type: 'outerPosition'
  398. }
  399. }
  400. }
  401. }).then(({ x, y }) => new PhysicalPosition(x, y));
  402. }
  403. /**
  404. * The physical size of the window's client area.
  405. * The client area is the content of the window, excluding the title bar and borders.
  406. * @example
  407. * ```typescript
  408. * import { appWindow } from '@tauri-apps/api/window';
  409. * const size = await appWindow.innerSize();
  410. * ```
  411. *
  412. * @returns The window's inner size.
  413. */
  414. async innerSize() {
  415. return invokeTauriCommand({
  416. __tauriModule: 'Window',
  417. message: {
  418. cmd: 'manage',
  419. data: {
  420. label: this.label,
  421. cmd: {
  422. type: 'innerSize'
  423. }
  424. }
  425. }
  426. }).then(({ width, height }) => new PhysicalSize(width, height));
  427. }
  428. /**
  429. * The physical size of the entire window.
  430. * These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  431. * @example
  432. * ```typescript
  433. * import { appWindow } from '@tauri-apps/api/window';
  434. * const size = await appWindow.outerSize();
  435. * ```
  436. *
  437. * @returns The window's outer size.
  438. */
  439. async outerSize() {
  440. return invokeTauriCommand({
  441. __tauriModule: 'Window',
  442. message: {
  443. cmd: 'manage',
  444. data: {
  445. label: this.label,
  446. cmd: {
  447. type: 'outerSize'
  448. }
  449. }
  450. }
  451. }).then(({ width, height }) => new PhysicalSize(width, height));
  452. }
  453. /**
  454. * Gets the window's current fullscreen state.
  455. * @example
  456. * ```typescript
  457. * import { appWindow } from '@tauri-apps/api/window';
  458. * const fullscreen = await appWindow.isFullscreen();
  459. * ```
  460. *
  461. * @returns Whether the window is in fullscreen mode or not.
  462. * */
  463. async isFullscreen() {
  464. return invokeTauriCommand({
  465. __tauriModule: 'Window',
  466. message: {
  467. cmd: 'manage',
  468. data: {
  469. label: this.label,
  470. cmd: {
  471. type: 'isFullscreen'
  472. }
  473. }
  474. }
  475. });
  476. }
  477. /**
  478. * Gets the window's current minimized state.
  479. * @example
  480. * ```typescript
  481. * import { appWindow } from '@tauri-apps/api/window';
  482. * const minimized = await appWindow.isMinimized();
  483. * ```
  484. *
  485. * @since 1.3.0
  486. * */
  487. async isMinimized() {
  488. return invokeTauriCommand({
  489. __tauriModule: 'Window',
  490. message: {
  491. cmd: 'manage',
  492. data: {
  493. label: this.label,
  494. cmd: {
  495. type: 'isMinimized'
  496. }
  497. }
  498. }
  499. });
  500. }
  501. /**
  502. * Gets the window's current maximized state.
  503. * @example
  504. * ```typescript
  505. * import { appWindow } from '@tauri-apps/api/window';
  506. * const maximized = await appWindow.isMaximized();
  507. * ```
  508. *
  509. * @returns Whether the window is maximized or not.
  510. * */
  511. async isMaximized() {
  512. return invokeTauriCommand({
  513. __tauriModule: 'Window',
  514. message: {
  515. cmd: 'manage',
  516. data: {
  517. label: this.label,
  518. cmd: {
  519. type: 'isMaximized'
  520. }
  521. }
  522. }
  523. });
  524. }
  525. /**
  526. * Gets the window's current focus state.
  527. * @example
  528. * ```typescript
  529. * import { appWindow } from '@tauri-apps/api/window';
  530. * const focused = await appWindow.isFocused();
  531. * ```
  532. *
  533. * @returns Whether the window is focused or not.
  534. *
  535. * @since 1.4
  536. * */
  537. async isFocused() {
  538. return invokeTauriCommand({
  539. __tauriModule: 'Window',
  540. message: {
  541. cmd: 'manage',
  542. data: {
  543. label: this.label,
  544. cmd: {
  545. type: 'isFocused'
  546. }
  547. }
  548. }
  549. });
  550. }
  551. /**
  552. * Gets the window's current decorated state.
  553. * @example
  554. * ```typescript
  555. * import { appWindow } from '@tauri-apps/api/window';
  556. * const decorated = await appWindow.isDecorated();
  557. * ```
  558. *
  559. * @returns Whether the window is decorated or not.
  560. * */
  561. async isDecorated() {
  562. return invokeTauriCommand({
  563. __tauriModule: 'Window',
  564. message: {
  565. cmd: 'manage',
  566. data: {
  567. label: this.label,
  568. cmd: {
  569. type: 'isDecorated'
  570. }
  571. }
  572. }
  573. });
  574. }
  575. /**
  576. * Gets the window's current resizable state.
  577. * @example
  578. * ```typescript
  579. * import { appWindow } from '@tauri-apps/api/window';
  580. * const resizable = await appWindow.isResizable();
  581. * ```
  582. *
  583. * @returns Whether the window is resizable or not.
  584. * */
  585. async isResizable() {
  586. return invokeTauriCommand({
  587. __tauriModule: 'Window',
  588. message: {
  589. cmd: 'manage',
  590. data: {
  591. label: this.label,
  592. cmd: {
  593. type: 'isResizable'
  594. }
  595. }
  596. }
  597. });
  598. }
  599. /**
  600. * Gets the window’s native maximize button state.
  601. *
  602. * #### Platform-specific
  603. *
  604. * - **Linux / iOS / Android:** Unsupported.
  605. *
  606. * @example
  607. * ```typescript
  608. * import { appWindow } from '@tauri-apps/api/window';
  609. * const maximizable = await appWindow.isMaximizable();
  610. * ```
  611. *
  612. * @returns Whether the window's native maximize button is enabled or not.
  613. * */
  614. async isMaximizable() {
  615. return invokeTauriCommand({
  616. __tauriModule: 'Window',
  617. message: {
  618. cmd: 'manage',
  619. data: {
  620. label: this.label,
  621. cmd: {
  622. type: 'isMaximizable'
  623. }
  624. }
  625. }
  626. });
  627. }
  628. /**
  629. * Gets the window’s native minimize button state.
  630. *
  631. * #### Platform-specific
  632. *
  633. * - **Linux / iOS / Android:** Unsupported.
  634. *
  635. * @example
  636. * ```typescript
  637. * import { appWindow } from '@tauri-apps/api/window';
  638. * const minimizable = await appWindow.isMinimizable();
  639. * ```
  640. *
  641. * @returns Whether the window's native minimize button is enabled or not.
  642. * */
  643. async isMinimizable() {
  644. return invokeTauriCommand({
  645. __tauriModule: 'Window',
  646. message: {
  647. cmd: 'manage',
  648. data: {
  649. label: this.label,
  650. cmd: {
  651. type: 'isMinimizable'
  652. }
  653. }
  654. }
  655. });
  656. }
  657. /**
  658. * Gets the window’s native close button state.
  659. *
  660. * #### Platform-specific
  661. *
  662. * - **Linux / iOS / Android:** Unsupported.
  663. *
  664. * @example
  665. * ```typescript
  666. * import { appWindow } from '@tauri-apps/api/window';
  667. * const closable = await appWindow.isClosable();
  668. * ```
  669. *
  670. * @returns Whether the window's native close button is enabled or not.
  671. * */
  672. async isClosable() {
  673. return invokeTauriCommand({
  674. __tauriModule: 'Window',
  675. message: {
  676. cmd: 'manage',
  677. data: {
  678. label: this.label,
  679. cmd: {
  680. type: 'isClosable'
  681. }
  682. }
  683. }
  684. });
  685. }
  686. /**
  687. * Gets the window's current visible state.
  688. * @example
  689. * ```typescript
  690. * import { appWindow } from '@tauri-apps/api/window';
  691. * const visible = await appWindow.isVisible();
  692. * ```
  693. *
  694. * @returns Whether the window is visible or not.
  695. * */
  696. async isVisible() {
  697. return invokeTauriCommand({
  698. __tauriModule: 'Window',
  699. message: {
  700. cmd: 'manage',
  701. data: {
  702. label: this.label,
  703. cmd: {
  704. type: 'isVisible'
  705. }
  706. }
  707. }
  708. });
  709. }
  710. /**
  711. * Gets the window's current title.
  712. * @example
  713. * ```typescript
  714. * import { appWindow } from '@tauri-apps/api/window';
  715. * const title = await appWindow.title();
  716. * ```
  717. *
  718. * @since 1.3.0
  719. * */
  720. async title() {
  721. return invokeTauriCommand({
  722. __tauriModule: 'Window',
  723. message: {
  724. cmd: 'manage',
  725. data: {
  726. label: this.label,
  727. cmd: {
  728. type: 'title'
  729. }
  730. }
  731. }
  732. });
  733. }
  734. /**
  735. * Gets the window's current theme.
  736. *
  737. * #### Platform-specific
  738. *
  739. * - **macOS:** Theme was introduced on macOS 10.14. Returns `light` on macOS 10.13 and below.
  740. *
  741. * @example
  742. * ```typescript
  743. * import { appWindow } from '@tauri-apps/api/window';
  744. * const theme = await appWindow.theme();
  745. * ```
  746. *
  747. * @returns The window theme.
  748. * */
  749. async theme() {
  750. return invokeTauriCommand({
  751. __tauriModule: 'Window',
  752. message: {
  753. cmd: 'manage',
  754. data: {
  755. label: this.label,
  756. cmd: {
  757. type: 'theme'
  758. }
  759. }
  760. }
  761. });
  762. }
  763. // Setters
  764. /**
  765. * Centers the window.
  766. * @example
  767. * ```typescript
  768. * import { appWindow } from '@tauri-apps/api/window';
  769. * await appWindow.center();
  770. * ```
  771. *
  772. * @returns A promise indicating the success or failure of the operation.
  773. */
  774. async center() {
  775. return invokeTauriCommand({
  776. __tauriModule: 'Window',
  777. message: {
  778. cmd: 'manage',
  779. data: {
  780. label: this.label,
  781. cmd: {
  782. type: 'center'
  783. }
  784. }
  785. }
  786. });
  787. }
  788. /**
  789. * Requests user attention to the window, this has no effect if the application
  790. * is already focused. How requesting for user attention manifests is platform dependent,
  791. * see `UserAttentionType` for details.
  792. *
  793. * Providing `null` will unset the request for user attention. Unsetting the request for
  794. * user attention might not be done automatically by the WM when the window receives input.
  795. *
  796. * #### Platform-specific
  797. *
  798. * - **macOS:** `null` has no effect.
  799. * - **Linux:** Urgency levels have the same effect.
  800. * @example
  801. * ```typescript
  802. * import { appWindow } from '@tauri-apps/api/window';
  803. * await appWindow.requestUserAttention();
  804. * ```
  805. *
  806. * @param requestType
  807. * @returns A promise indicating the success or failure of the operation.
  808. */
  809. async requestUserAttention(requestType) {
  810. let requestType_ = null;
  811. if (requestType) {
  812. if (requestType === UserAttentionType.Critical) {
  813. requestType_ = { type: 'Critical' };
  814. }
  815. else {
  816. requestType_ = { type: 'Informational' };
  817. }
  818. }
  819. return invokeTauriCommand({
  820. __tauriModule: 'Window',
  821. message: {
  822. cmd: 'manage',
  823. data: {
  824. label: this.label,
  825. cmd: {
  826. type: 'requestUserAttention',
  827. payload: requestType_
  828. }
  829. }
  830. }
  831. });
  832. }
  833. /**
  834. * Updates the window resizable flag.
  835. * @example
  836. * ```typescript
  837. * import { appWindow } from '@tauri-apps/api/window';
  838. * await appWindow.setResizable(false);
  839. * ```
  840. *
  841. * @param resizable
  842. * @returns A promise indicating the success or failure of the operation.
  843. */
  844. async setResizable(resizable) {
  845. return invokeTauriCommand({
  846. __tauriModule: 'Window',
  847. message: {
  848. cmd: 'manage',
  849. data: {
  850. label: this.label,
  851. cmd: {
  852. type: 'setResizable',
  853. payload: resizable
  854. }
  855. }
  856. }
  857. });
  858. }
  859. /**
  860. * Sets whether the window's native maximize button is enabled or not.
  861. * If resizable is set to false, this setting is ignored.
  862. *
  863. * #### Platform-specific
  864. *
  865. * - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  866. * - **Linux / iOS / Android:** Unsupported.
  867. *
  868. * @example
  869. * ```typescript
  870. * import { appWindow } from '@tauri-apps/api/window';
  871. * await appWindow.setMaximizable(false);
  872. * ```
  873. *
  874. * @param maximizable
  875. * @returns A promise indicating the success or failure of the operation.
  876. */
  877. async setMaximizable(maximizable) {
  878. return invokeTauriCommand({
  879. __tauriModule: 'Window',
  880. message: {
  881. cmd: 'manage',
  882. data: {
  883. label: this.label,
  884. cmd: {
  885. type: 'setMaximizable',
  886. payload: maximizable
  887. }
  888. }
  889. }
  890. });
  891. }
  892. /**
  893. * Sets whether the window's native minimize button is enabled or not.
  894. *
  895. * #### Platform-specific
  896. *
  897. * - **Linux / iOS / Android:** Unsupported.
  898. *
  899. * @example
  900. * ```typescript
  901. * import { appWindow } from '@tauri-apps/api/window';
  902. * await appWindow.setMinimizable(false);
  903. * ```
  904. *
  905. * @param minimizable
  906. * @returns A promise indicating the success or failure of the operation.
  907. */
  908. async setMinimizable(minimizable) {
  909. return invokeTauriCommand({
  910. __tauriModule: 'Window',
  911. message: {
  912. cmd: 'manage',
  913. data: {
  914. label: this.label,
  915. cmd: {
  916. type: 'setMinimizable',
  917. payload: minimizable
  918. }
  919. }
  920. }
  921. });
  922. }
  923. /**
  924. * Sets whether the window's native close button is enabled or not.
  925. *
  926. * #### Platform-specific
  927. *
  928. * - **Linux:** GTK+ will do its best to convince the window manager not to show a close button. Depending on the system, this function may not have any effect when called on a window that is already visible
  929. * - **iOS / Android:** Unsupported.
  930. *
  931. * @example
  932. * ```typescript
  933. * import { appWindow } from '@tauri-apps/api/window';
  934. * await appWindow.setClosable(false);
  935. * ```
  936. *
  937. * @param closable
  938. * @returns A promise indicating the success or failure of the operation.
  939. */
  940. async setClosable(closable) {
  941. return invokeTauriCommand({
  942. __tauriModule: 'Window',
  943. message: {
  944. cmd: 'manage',
  945. data: {
  946. label: this.label,
  947. cmd: {
  948. type: 'setClosable',
  949. payload: closable
  950. }
  951. }
  952. }
  953. });
  954. }
  955. /**
  956. * Sets the window title.
  957. * @example
  958. * ```typescript
  959. * import { appWindow } from '@tauri-apps/api/window';
  960. * await appWindow.setTitle('Tauri');
  961. * ```
  962. *
  963. * @param title The new title
  964. * @returns A promise indicating the success or failure of the operation.
  965. */
  966. async setTitle(title) {
  967. return invokeTauriCommand({
  968. __tauriModule: 'Window',
  969. message: {
  970. cmd: 'manage',
  971. data: {
  972. label: this.label,
  973. cmd: {
  974. type: 'setTitle',
  975. payload: title
  976. }
  977. }
  978. }
  979. });
  980. }
  981. /**
  982. * Maximizes the window.
  983. * @example
  984. * ```typescript
  985. * import { appWindow } from '@tauri-apps/api/window';
  986. * await appWindow.maximize();
  987. * ```
  988. *
  989. * @returns A promise indicating the success or failure of the operation.
  990. */
  991. async maximize() {
  992. return invokeTauriCommand({
  993. __tauriModule: 'Window',
  994. message: {
  995. cmd: 'manage',
  996. data: {
  997. label: this.label,
  998. cmd: {
  999. type: 'maximize'
  1000. }
  1001. }
  1002. }
  1003. });
  1004. }
  1005. /**
  1006. * Unmaximizes the window.
  1007. * @example
  1008. * ```typescript
  1009. * import { appWindow } from '@tauri-apps/api/window';
  1010. * await appWindow.unmaximize();
  1011. * ```
  1012. *
  1013. * @returns A promise indicating the success or failure of the operation.
  1014. */
  1015. async unmaximize() {
  1016. return invokeTauriCommand({
  1017. __tauriModule: 'Window',
  1018. message: {
  1019. cmd: 'manage',
  1020. data: {
  1021. label: this.label,
  1022. cmd: {
  1023. type: 'unmaximize'
  1024. }
  1025. }
  1026. }
  1027. });
  1028. }
  1029. /**
  1030. * Toggles the window maximized state.
  1031. * @example
  1032. * ```typescript
  1033. * import { appWindow } from '@tauri-apps/api/window';
  1034. * await appWindow.toggleMaximize();
  1035. * ```
  1036. *
  1037. * @returns A promise indicating the success or failure of the operation.
  1038. */
  1039. async toggleMaximize() {
  1040. return invokeTauriCommand({
  1041. __tauriModule: 'Window',
  1042. message: {
  1043. cmd: 'manage',
  1044. data: {
  1045. label: this.label,
  1046. cmd: {
  1047. type: 'toggleMaximize'
  1048. }
  1049. }
  1050. }
  1051. });
  1052. }
  1053. /**
  1054. * Minimizes the window.
  1055. * @example
  1056. * ```typescript
  1057. * import { appWindow } from '@tauri-apps/api/window';
  1058. * await appWindow.minimize();
  1059. * ```
  1060. *
  1061. * @returns A promise indicating the success or failure of the operation.
  1062. */
  1063. async minimize() {
  1064. return invokeTauriCommand({
  1065. __tauriModule: 'Window',
  1066. message: {
  1067. cmd: 'manage',
  1068. data: {
  1069. label: this.label,
  1070. cmd: {
  1071. type: 'minimize'
  1072. }
  1073. }
  1074. }
  1075. });
  1076. }
  1077. /**
  1078. * Unminimizes the window.
  1079. * @example
  1080. * ```typescript
  1081. * import { appWindow } from '@tauri-apps/api/window';
  1082. * await appWindow.unminimize();
  1083. * ```
  1084. *
  1085. * @returns A promise indicating the success or failure of the operation.
  1086. */
  1087. async unminimize() {
  1088. return invokeTauriCommand({
  1089. __tauriModule: 'Window',
  1090. message: {
  1091. cmd: 'manage',
  1092. data: {
  1093. label: this.label,
  1094. cmd: {
  1095. type: 'unminimize'
  1096. }
  1097. }
  1098. }
  1099. });
  1100. }
  1101. /**
  1102. * Sets the window visibility to true.
  1103. * @example
  1104. * ```typescript
  1105. * import { appWindow } from '@tauri-apps/api/window';
  1106. * await appWindow.show();
  1107. * ```
  1108. *
  1109. * @returns A promise indicating the success or failure of the operation.
  1110. */
  1111. async show() {
  1112. return invokeTauriCommand({
  1113. __tauriModule: 'Window',
  1114. message: {
  1115. cmd: 'manage',
  1116. data: {
  1117. label: this.label,
  1118. cmd: {
  1119. type: 'show'
  1120. }
  1121. }
  1122. }
  1123. });
  1124. }
  1125. /**
  1126. * Sets the window visibility to false.
  1127. * @example
  1128. * ```typescript
  1129. * import { appWindow } from '@tauri-apps/api/window';
  1130. * await appWindow.hide();
  1131. * ```
  1132. *
  1133. * @returns A promise indicating the success or failure of the operation.
  1134. */
  1135. async hide() {
  1136. return invokeTauriCommand({
  1137. __tauriModule: 'Window',
  1138. message: {
  1139. cmd: 'manage',
  1140. data: {
  1141. label: this.label,
  1142. cmd: {
  1143. type: 'hide'
  1144. }
  1145. }
  1146. }
  1147. });
  1148. }
  1149. /**
  1150. * Closes the window.
  1151. * @example
  1152. * ```typescript
  1153. * import { appWindow } from '@tauri-apps/api/window';
  1154. * await appWindow.close();
  1155. * ```
  1156. *
  1157. * @returns A promise indicating the success or failure of the operation.
  1158. */
  1159. async close() {
  1160. return invokeTauriCommand({
  1161. __tauriModule: 'Window',
  1162. message: {
  1163. cmd: 'manage',
  1164. data: {
  1165. label: this.label,
  1166. cmd: {
  1167. type: 'close'
  1168. }
  1169. }
  1170. }
  1171. });
  1172. }
  1173. /**
  1174. * Whether the window should have borders and bars.
  1175. * @example
  1176. * ```typescript
  1177. * import { appWindow } from '@tauri-apps/api/window';
  1178. * await appWindow.setDecorations(false);
  1179. * ```
  1180. *
  1181. * @param decorations Whether the window should have borders and bars.
  1182. * @returns A promise indicating the success or failure of the operation.
  1183. */
  1184. async setDecorations(decorations) {
  1185. return invokeTauriCommand({
  1186. __tauriModule: 'Window',
  1187. message: {
  1188. cmd: 'manage',
  1189. data: {
  1190. label: this.label,
  1191. cmd: {
  1192. type: 'setDecorations',
  1193. payload: decorations
  1194. }
  1195. }
  1196. }
  1197. });
  1198. }
  1199. /**
  1200. * Whether the window should always be on top of other windows.
  1201. * @example
  1202. * ```typescript
  1203. * import { appWindow } from '@tauri-apps/api/window';
  1204. * await appWindow.setAlwaysOnTop(true);
  1205. * ```
  1206. *
  1207. * @param alwaysOnTop Whether the window should always be on top of other windows or not.
  1208. * @returns A promise indicating the success or failure of the operation.
  1209. */
  1210. async setAlwaysOnTop(alwaysOnTop) {
  1211. return invokeTauriCommand({
  1212. __tauriModule: 'Window',
  1213. message: {
  1214. cmd: 'manage',
  1215. data: {
  1216. label: this.label,
  1217. cmd: {
  1218. type: 'setAlwaysOnTop',
  1219. payload: alwaysOnTop
  1220. }
  1221. }
  1222. }
  1223. });
  1224. }
  1225. /**
  1226. * Prevents the window contents from being captured by other apps.
  1227. * @example
  1228. * ```typescript
  1229. * import { appWindow } from '@tauri-apps/api/window';
  1230. * await appWindow.setContentProtected(true);
  1231. * ```
  1232. *
  1233. * @returns A promise indicating the success or failure of the operation.
  1234. *
  1235. * @since 1.2.0
  1236. */
  1237. async setContentProtected(protected_) {
  1238. return invokeTauriCommand({
  1239. __tauriModule: 'Window',
  1240. message: {
  1241. cmd: 'manage',
  1242. data: {
  1243. label: this.label,
  1244. cmd: {
  1245. type: 'setContentProtected',
  1246. payload: protected_
  1247. }
  1248. }
  1249. }
  1250. });
  1251. }
  1252. /**
  1253. * Resizes the window with a new inner size.
  1254. * @example
  1255. * ```typescript
  1256. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1257. * await appWindow.setSize(new LogicalSize(600, 500));
  1258. * ```
  1259. *
  1260. * @param size The logical or physical inner size.
  1261. * @returns A promise indicating the success or failure of the operation.
  1262. */
  1263. async setSize(size) {
  1264. if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
  1265. throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
  1266. }
  1267. return invokeTauriCommand({
  1268. __tauriModule: 'Window',
  1269. message: {
  1270. cmd: 'manage',
  1271. data: {
  1272. label: this.label,
  1273. cmd: {
  1274. type: 'setSize',
  1275. payload: {
  1276. type: size.type,
  1277. data: {
  1278. width: size.width,
  1279. height: size.height
  1280. }
  1281. }
  1282. }
  1283. }
  1284. }
  1285. });
  1286. }
  1287. /**
  1288. * Sets the window minimum inner size. If the `size` argument is not provided, the constraint is unset.
  1289. * @example
  1290. * ```typescript
  1291. * import { appWindow, PhysicalSize } from '@tauri-apps/api/window';
  1292. * await appWindow.setMinSize(new PhysicalSize(600, 500));
  1293. * ```
  1294. *
  1295. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1296. * @returns A promise indicating the success or failure of the operation.
  1297. */
  1298. async setMinSize(size) {
  1299. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1300. throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
  1301. }
  1302. return invokeTauriCommand({
  1303. __tauriModule: 'Window',
  1304. message: {
  1305. cmd: 'manage',
  1306. data: {
  1307. label: this.label,
  1308. cmd: {
  1309. type: 'setMinSize',
  1310. payload: size
  1311. ? {
  1312. type: size.type,
  1313. data: {
  1314. width: size.width,
  1315. height: size.height
  1316. }
  1317. }
  1318. : null
  1319. }
  1320. }
  1321. }
  1322. });
  1323. }
  1324. /**
  1325. * Sets the window maximum inner size. If the `size` argument is undefined, the constraint is unset.
  1326. * @example
  1327. * ```typescript
  1328. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1329. * await appWindow.setMaxSize(new LogicalSize(600, 500));
  1330. * ```
  1331. *
  1332. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1333. * @returns A promise indicating the success or failure of the operation.
  1334. */
  1335. async setMaxSize(size) {
  1336. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1337. throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
  1338. }
  1339. return invokeTauriCommand({
  1340. __tauriModule: 'Window',
  1341. message: {
  1342. cmd: 'manage',
  1343. data: {
  1344. label: this.label,
  1345. cmd: {
  1346. type: 'setMaxSize',
  1347. payload: size
  1348. ? {
  1349. type: size.type,
  1350. data: {
  1351. width: size.width,
  1352. height: size.height
  1353. }
  1354. }
  1355. : null
  1356. }
  1357. }
  1358. }
  1359. });
  1360. }
  1361. /**
  1362. * Sets the window outer position.
  1363. * @example
  1364. * ```typescript
  1365. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1366. * await appWindow.setPosition(new LogicalPosition(600, 500));
  1367. * ```
  1368. *
  1369. * @param position The new position, in logical or physical pixels.
  1370. * @returns A promise indicating the success or failure of the operation.
  1371. */
  1372. async setPosition(position) {
  1373. if (!position ||
  1374. (position.type !== 'Logical' && position.type !== 'Physical')) {
  1375. throw new Error('the `position` argument must be either a LogicalPosition or a PhysicalPosition instance');
  1376. }
  1377. return invokeTauriCommand({
  1378. __tauriModule: 'Window',
  1379. message: {
  1380. cmd: 'manage',
  1381. data: {
  1382. label: this.label,
  1383. cmd: {
  1384. type: 'setPosition',
  1385. payload: {
  1386. type: position.type,
  1387. data: {
  1388. x: position.x,
  1389. y: position.y
  1390. }
  1391. }
  1392. }
  1393. }
  1394. }
  1395. });
  1396. }
  1397. /**
  1398. * Sets the window fullscreen state.
  1399. * @example
  1400. * ```typescript
  1401. * import { appWindow } from '@tauri-apps/api/window';
  1402. * await appWindow.setFullscreen(true);
  1403. * ```
  1404. *
  1405. * @param fullscreen Whether the window should go to fullscreen or not.
  1406. * @returns A promise indicating the success or failure of the operation.
  1407. */
  1408. async setFullscreen(fullscreen) {
  1409. return invokeTauriCommand({
  1410. __tauriModule: 'Window',
  1411. message: {
  1412. cmd: 'manage',
  1413. data: {
  1414. label: this.label,
  1415. cmd: {
  1416. type: 'setFullscreen',
  1417. payload: fullscreen
  1418. }
  1419. }
  1420. }
  1421. });
  1422. }
  1423. /**
  1424. * Bring the window to front and focus.
  1425. * @example
  1426. * ```typescript
  1427. * import { appWindow } from '@tauri-apps/api/window';
  1428. * await appWindow.setFocus();
  1429. * ```
  1430. *
  1431. * @returns A promise indicating the success or failure of the operation.
  1432. */
  1433. async setFocus() {
  1434. return invokeTauriCommand({
  1435. __tauriModule: 'Window',
  1436. message: {
  1437. cmd: 'manage',
  1438. data: {
  1439. label: this.label,
  1440. cmd: {
  1441. type: 'setFocus'
  1442. }
  1443. }
  1444. }
  1445. });
  1446. }
  1447. /**
  1448. * Sets the window icon.
  1449. * @example
  1450. * ```typescript
  1451. * import { appWindow } from '@tauri-apps/api/window';
  1452. * await appWindow.setIcon('/tauri/awesome.png');
  1453. * ```
  1454. *
  1455. * Note that you need the `icon-ico` or `icon-png` Cargo features to use this API.
  1456. * To enable it, change your Cargo.toml file:
  1457. * ```toml
  1458. * [dependencies]
  1459. * tauri = { version = "...", features = ["...", "icon-png"] }
  1460. * ```
  1461. *
  1462. * @param icon Icon bytes or path to the icon file.
  1463. * @returns A promise indicating the success or failure of the operation.
  1464. */
  1465. async setIcon(icon) {
  1466. return invokeTauriCommand({
  1467. __tauriModule: 'Window',
  1468. message: {
  1469. cmd: 'manage',
  1470. data: {
  1471. label: this.label,
  1472. cmd: {
  1473. type: 'setIcon',
  1474. payload: {
  1475. // correctly serialize Uint8Arrays
  1476. icon: typeof icon === 'string' ? icon : Array.from(icon)
  1477. }
  1478. }
  1479. }
  1480. }
  1481. });
  1482. }
  1483. /**
  1484. * Whether the window icon should be hidden from the taskbar or not.
  1485. *
  1486. * #### Platform-specific
  1487. *
  1488. * - **macOS:** Unsupported.
  1489. * @example
  1490. * ```typescript
  1491. * import { appWindow } from '@tauri-apps/api/window';
  1492. * await appWindow.setSkipTaskbar(true);
  1493. * ```
  1494. *
  1495. * @param skip true to hide window icon, false to show it.
  1496. * @returns A promise indicating the success or failure of the operation.
  1497. */
  1498. async setSkipTaskbar(skip) {
  1499. return invokeTauriCommand({
  1500. __tauriModule: 'Window',
  1501. message: {
  1502. cmd: 'manage',
  1503. data: {
  1504. label: this.label,
  1505. cmd: {
  1506. type: 'setSkipTaskbar',
  1507. payload: skip
  1508. }
  1509. }
  1510. }
  1511. });
  1512. }
  1513. /**
  1514. * Grabs the cursor, preventing it from leaving the window.
  1515. *
  1516. * There's no guarantee that the cursor will be hidden. You should
  1517. * hide it by yourself if you want so.
  1518. *
  1519. * #### Platform-specific
  1520. *
  1521. * - **Linux:** Unsupported.
  1522. * - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
  1523. * @example
  1524. * ```typescript
  1525. * import { appWindow } from '@tauri-apps/api/window';
  1526. * await appWindow.setCursorGrab(true);
  1527. * ```
  1528. *
  1529. * @param grab `true` to grab the cursor icon, `false` to release it.
  1530. * @returns A promise indicating the success or failure of the operation.
  1531. */
  1532. async setCursorGrab(grab) {
  1533. return invokeTauriCommand({
  1534. __tauriModule: 'Window',
  1535. message: {
  1536. cmd: 'manage',
  1537. data: {
  1538. label: this.label,
  1539. cmd: {
  1540. type: 'setCursorGrab',
  1541. payload: grab
  1542. }
  1543. }
  1544. }
  1545. });
  1546. }
  1547. /**
  1548. * Modifies the cursor's visibility.
  1549. *
  1550. * #### Platform-specific
  1551. *
  1552. * - **Windows:** The cursor is only hidden within the confines of the window.
  1553. * - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor is
  1554. * outside of the window.
  1555. * @example
  1556. * ```typescript
  1557. * import { appWindow } from '@tauri-apps/api/window';
  1558. * await appWindow.setCursorVisible(false);
  1559. * ```
  1560. *
  1561. * @param visible If `false`, this will hide the cursor. If `true`, this will show the cursor.
  1562. * @returns A promise indicating the success or failure of the operation.
  1563. */
  1564. async setCursorVisible(visible) {
  1565. return invokeTauriCommand({
  1566. __tauriModule: 'Window',
  1567. message: {
  1568. cmd: 'manage',
  1569. data: {
  1570. label: this.label,
  1571. cmd: {
  1572. type: 'setCursorVisible',
  1573. payload: visible
  1574. }
  1575. }
  1576. }
  1577. });
  1578. }
  1579. /**
  1580. * Modifies the cursor icon of the window.
  1581. * @example
  1582. * ```typescript
  1583. * import { appWindow } from '@tauri-apps/api/window';
  1584. * await appWindow.setCursorIcon('help');
  1585. * ```
  1586. *
  1587. * @param icon The new cursor icon.
  1588. * @returns A promise indicating the success or failure of the operation.
  1589. */
  1590. async setCursorIcon(icon) {
  1591. return invokeTauriCommand({
  1592. __tauriModule: 'Window',
  1593. message: {
  1594. cmd: 'manage',
  1595. data: {
  1596. label: this.label,
  1597. cmd: {
  1598. type: 'setCursorIcon',
  1599. payload: icon
  1600. }
  1601. }
  1602. }
  1603. });
  1604. }
  1605. /**
  1606. * Changes the position of the cursor in window coordinates.
  1607. * @example
  1608. * ```typescript
  1609. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1610. * await appWindow.setCursorPosition(new LogicalPosition(600, 300));
  1611. * ```
  1612. *
  1613. * @param position The new cursor position.
  1614. * @returns A promise indicating the success or failure of the operation.
  1615. */
  1616. async setCursorPosition(position) {
  1617. if (!position ||
  1618. (position.type !== 'Logical' && position.type !== 'Physical')) {
  1619. throw new Error('the `position` argument must be either a LogicalPosition or a PhysicalPosition instance');
  1620. }
  1621. return invokeTauriCommand({
  1622. __tauriModule: 'Window',
  1623. message: {
  1624. cmd: 'manage',
  1625. data: {
  1626. label: this.label,
  1627. cmd: {
  1628. type: 'setCursorPosition',
  1629. payload: {
  1630. type: position.type,
  1631. data: {
  1632. x: position.x,
  1633. y: position.y
  1634. }
  1635. }
  1636. }
  1637. }
  1638. }
  1639. });
  1640. }
  1641. /**
  1642. * Changes the cursor events behavior.
  1643. *
  1644. * @example
  1645. * ```typescript
  1646. * import { appWindow } from '@tauri-apps/api/window';
  1647. * await appWindow.setIgnoreCursorEvents(true);
  1648. * ```
  1649. *
  1650. * @param ignore `true` to ignore the cursor events; `false` to process them as usual.
  1651. * @returns A promise indicating the success or failure of the operation.
  1652. */
  1653. async setIgnoreCursorEvents(ignore) {
  1654. return invokeTauriCommand({
  1655. __tauriModule: 'Window',
  1656. message: {
  1657. cmd: 'manage',
  1658. data: {
  1659. label: this.label,
  1660. cmd: {
  1661. type: 'setIgnoreCursorEvents',
  1662. payload: ignore
  1663. }
  1664. }
  1665. }
  1666. });
  1667. }
  1668. /**
  1669. * Starts dragging the window.
  1670. * @example
  1671. * ```typescript
  1672. * import { appWindow } from '@tauri-apps/api/window';
  1673. * await appWindow.startDragging();
  1674. * ```
  1675. *
  1676. * @return A promise indicating the success or failure of the operation.
  1677. */
  1678. async startDragging() {
  1679. return invokeTauriCommand({
  1680. __tauriModule: 'Window',
  1681. message: {
  1682. cmd: 'manage',
  1683. data: {
  1684. label: this.label,
  1685. cmd: {
  1686. type: 'startDragging'
  1687. }
  1688. }
  1689. }
  1690. });
  1691. }
  1692. // Listeners
  1693. /**
  1694. * Listen to window resize.
  1695. *
  1696. * @example
  1697. * ```typescript
  1698. * import { appWindow } from "@tauri-apps/api/window";
  1699. * const unlisten = await appWindow.onResized(({ payload: size }) => {
  1700. * console.log('Window resized', size);
  1701. * });
  1702. *
  1703. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1704. * unlisten();
  1705. * ```
  1706. *
  1707. * @returns A promise resolving to a function to unlisten to the event.
  1708. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1709. *
  1710. * @since 1.0.2
  1711. */
  1712. async onResized(handler) {
  1713. return this.listen(TauriEvent.WINDOW_RESIZED, (e) => {
  1714. e.payload = mapPhysicalSize(e.payload);
  1715. handler(e);
  1716. });
  1717. }
  1718. /**
  1719. * Listen to window move.
  1720. *
  1721. * @example
  1722. * ```typescript
  1723. * import { appWindow } from "@tauri-apps/api/window";
  1724. * const unlisten = await appWindow.onMoved(({ payload: position }) => {
  1725. * console.log('Window moved', position);
  1726. * });
  1727. *
  1728. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1729. * unlisten();
  1730. * ```
  1731. *
  1732. * @returns A promise resolving to a function to unlisten to the event.
  1733. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1734. *
  1735. * @since 1.0.2
  1736. */
  1737. async onMoved(handler) {
  1738. return this.listen(TauriEvent.WINDOW_MOVED, (e) => {
  1739. e.payload = mapPhysicalPosition(e.payload);
  1740. handler(e);
  1741. });
  1742. }
  1743. /**
  1744. * Listen to window close requested. Emitted when the user requests to closes the window.
  1745. *
  1746. * @example
  1747. * ```typescript
  1748. * import { appWindow } from "@tauri-apps/api/window";
  1749. * import { confirm } from '@tauri-apps/api/dialog';
  1750. * const unlisten = await appWindow.onCloseRequested(async (event) => {
  1751. * const confirmed = await confirm('Are you sure?');
  1752. * if (!confirmed) {
  1753. * // user did not confirm closing the window; let's prevent it
  1754. * event.preventDefault();
  1755. * }
  1756. * });
  1757. *
  1758. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1759. * unlisten();
  1760. * ```
  1761. *
  1762. * @returns A promise resolving to a function to unlisten to the event.
  1763. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1764. *
  1765. * @since 1.0.2
  1766. */
  1767. /* eslint-disable @typescript-eslint/promise-function-async */
  1768. async onCloseRequested(handler) {
  1769. return this.listen(TauriEvent.WINDOW_CLOSE_REQUESTED, (event) => {
  1770. const evt = new CloseRequestedEvent(event);
  1771. void Promise.resolve(handler(evt)).then(() => {
  1772. if (!evt.isPreventDefault()) {
  1773. return this.close();
  1774. }
  1775. });
  1776. });
  1777. }
  1778. /* eslint-enable */
  1779. /**
  1780. * Listen to window focus change.
  1781. *
  1782. * @example
  1783. * ```typescript
  1784. * import { appWindow } from "@tauri-apps/api/window";
  1785. * const unlisten = await appWindow.onFocusChanged(({ payload: focused }) => {
  1786. * console.log('Focus changed, window is focused? ' + focused);
  1787. * });
  1788. *
  1789. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1790. * unlisten();
  1791. * ```
  1792. *
  1793. * @returns A promise resolving to a function to unlisten to the event.
  1794. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1795. *
  1796. * @since 1.0.2
  1797. */
  1798. async onFocusChanged(handler) {
  1799. const unlistenFocus = await this.listen(TauriEvent.WINDOW_FOCUS, (event) => {
  1800. handler({ ...event, payload: true });
  1801. });
  1802. const unlistenBlur = await this.listen(TauriEvent.WINDOW_BLUR, (event) => {
  1803. handler({ ...event, payload: false });
  1804. });
  1805. return () => {
  1806. unlistenFocus();
  1807. unlistenBlur();
  1808. };
  1809. }
  1810. /**
  1811. * Listen to window scale change. Emitted when the window's scale factor has changed.
  1812. * The following user actions can cause DPI changes:
  1813. * - Changing the display's resolution.
  1814. * - Changing the display's scale factor (e.g. in Control Panel on Windows).
  1815. * - Moving the window to a display with a different scale factor.
  1816. *
  1817. * @example
  1818. * ```typescript
  1819. * import { appWindow } from "@tauri-apps/api/window";
  1820. * const unlisten = await appWindow.onScaleChanged(({ payload }) => {
  1821. * console.log('Scale changed', payload.scaleFactor, payload.size);
  1822. * });
  1823. *
  1824. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1825. * unlisten();
  1826. * ```
  1827. *
  1828. * @returns A promise resolving to a function to unlisten to the event.
  1829. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1830. *
  1831. * @since 1.0.2
  1832. */
  1833. async onScaleChanged(handler) {
  1834. return this.listen(TauriEvent.WINDOW_SCALE_FACTOR_CHANGED, handler);
  1835. }
  1836. /**
  1837. * Listen to the window menu item click. The payload is the item id.
  1838. *
  1839. * @example
  1840. * ```typescript
  1841. * import { appWindow } from "@tauri-apps/api/window";
  1842. * const unlisten = await appWindow.onMenuClicked(({ payload: menuId }) => {
  1843. * console.log('Menu clicked: ' + menuId);
  1844. * });
  1845. *
  1846. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1847. * unlisten();
  1848. * ```
  1849. *
  1850. * @returns A promise resolving to a function to unlisten to the event.
  1851. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1852. *
  1853. * @since 1.0.2
  1854. */
  1855. async onMenuClicked(handler) {
  1856. return this.listen(TauriEvent.MENU, handler);
  1857. }
  1858. /**
  1859. * Listen to a file drop event.
  1860. * The listener is triggered when the user hovers the selected files on the window,
  1861. * drops the files or cancels the operation.
  1862. *
  1863. * @example
  1864. * ```typescript
  1865. * import { appWindow } from "@tauri-apps/api/window";
  1866. * const unlisten = await appWindow.onFileDropEvent((event) => {
  1867. * if (event.payload.type === 'hover') {
  1868. * console.log('User hovering', event.payload.paths);
  1869. * } else if (event.payload.type === 'drop') {
  1870. * console.log('User dropped', event.payload.paths);
  1871. * } else {
  1872. * console.log('File drop cancelled');
  1873. * }
  1874. * });
  1875. *
  1876. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1877. * unlisten();
  1878. * ```
  1879. *
  1880. * @returns A promise resolving to a function to unlisten to the event.
  1881. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1882. *
  1883. * @since 1.0.2
  1884. */
  1885. async onFileDropEvent(handler) {
  1886. const unlistenFileDrop = await this.listen(TauriEvent.WINDOW_FILE_DROP, (event) => {
  1887. handler({ ...event, payload: { type: 'drop', paths: event.payload } });
  1888. });
  1889. const unlistenFileHover = await this.listen(TauriEvent.WINDOW_FILE_DROP_HOVER, (event) => {
  1890. handler({ ...event, payload: { type: 'hover', paths: event.payload } });
  1891. });
  1892. const unlistenCancel = await this.listen(TauriEvent.WINDOW_FILE_DROP_CANCELLED, (event) => {
  1893. handler({ ...event, payload: { type: 'cancel' } });
  1894. });
  1895. return () => {
  1896. unlistenFileDrop();
  1897. unlistenFileHover();
  1898. unlistenCancel();
  1899. };
  1900. }
  1901. /**
  1902. * Listen to the system theme change.
  1903. *
  1904. * @example
  1905. * ```typescript
  1906. * import { appWindow } from "@tauri-apps/api/window";
  1907. * const unlisten = await appWindow.onThemeChanged(({ payload: theme }) => {
  1908. * console.log('New theme: ' + theme);
  1909. * });
  1910. *
  1911. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  1912. * unlisten();
  1913. * ```
  1914. *
  1915. * @returns A promise resolving to a function to unlisten to the event.
  1916. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  1917. *
  1918. * @since 1.0.2
  1919. */
  1920. async onThemeChanged(handler) {
  1921. return this.listen(TauriEvent.WINDOW_THEME_CHANGED, handler);
  1922. }
  1923. }
  1924. /**
  1925. * @since 1.0.2
  1926. */
  1927. class CloseRequestedEvent {
  1928. constructor(event) {
  1929. this._preventDefault = false;
  1930. this.event = event.event;
  1931. this.windowLabel = event.windowLabel;
  1932. this.id = event.id;
  1933. }
  1934. preventDefault() {
  1935. this._preventDefault = true;
  1936. }
  1937. isPreventDefault() {
  1938. return this._preventDefault;
  1939. }
  1940. }
  1941. /**
  1942. * Create new webview windows and get a handle to existing ones.
  1943. *
  1944. * Windows are identified by a *label* a unique identifier that can be used to reference it later.
  1945. * It may only contain alphanumeric characters `a-zA-Z` plus the following special characters `-`, `/`, `:` and `_`.
  1946. *
  1947. * @example
  1948. * ```typescript
  1949. * // loading embedded asset:
  1950. * const webview = new WebviewWindow('theUniqueLabel', {
  1951. * url: 'path/to/page.html'
  1952. * });
  1953. * // alternatively, load a remote URL:
  1954. * const webview = new WebviewWindow('theUniqueLabel', {
  1955. * url: 'https://github.com/tauri-apps/tauri'
  1956. * });
  1957. *
  1958. * webview.once('tauri://created', function () {
  1959. * // webview window successfully created
  1960. * });
  1961. * webview.once('tauri://error', function (e) {
  1962. * // an error happened creating the webview window
  1963. * });
  1964. *
  1965. * // emit an event to the backend
  1966. * await webview.emit("some event", "data");
  1967. * // listen to an event from the backend
  1968. * const unlisten = await webview.listen("event name", e => {});
  1969. * unlisten();
  1970. * ```
  1971. *
  1972. * @since 1.0.2
  1973. */
  1974. class WebviewWindow extends WindowManager {
  1975. /**
  1976. * Creates a new WebviewWindow.
  1977. * @example
  1978. * ```typescript
  1979. * import { WebviewWindow } from '@tauri-apps/api/window';
  1980. * const webview = new WebviewWindow('my-label', {
  1981. * url: 'https://github.com/tauri-apps/tauri'
  1982. * });
  1983. * webview.once('tauri://created', function () {
  1984. * // webview window successfully created
  1985. * });
  1986. * webview.once('tauri://error', function (e) {
  1987. * // an error happened creating the webview window
  1988. * });
  1989. * ```
  1990. *
  1991. * * @param label The unique webview window label. Must be alphanumeric: `a-zA-Z-/:_`.
  1992. * @returns The WebviewWindow instance to communicate with the webview.
  1993. */
  1994. constructor(label, options = {}) {
  1995. super(label);
  1996. // @ts-expect-error `skip` is not a public API so it is not defined in WindowOptions
  1997. if (!(options === null || options === void 0 ? void 0 : options.skip)) {
  1998. invokeTauriCommand({
  1999. __tauriModule: 'Window',
  2000. message: {
  2001. cmd: 'createWebview',
  2002. data: {
  2003. options: {
  2004. label,
  2005. ...options
  2006. }
  2007. }
  2008. }
  2009. })
  2010. .then(async () => this.emit('tauri://created'))
  2011. .catch(async (e) => this.emit('tauri://error', e));
  2012. }
  2013. }
  2014. /**
  2015. * Gets the WebviewWindow for the webview associated with the given label.
  2016. * @example
  2017. * ```typescript
  2018. * import { WebviewWindow } from '@tauri-apps/api/window';
  2019. * const mainWindow = WebviewWindow.getByLabel('main');
  2020. * ```
  2021. *
  2022. * @param label The webview window label.
  2023. * @returns The WebviewWindow instance to communicate with the webview or null if the webview doesn't exist.
  2024. */
  2025. static getByLabel(label) {
  2026. if (getAll().some((w) => w.label === label)) {
  2027. // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
  2028. return new WebviewWindow(label, { skip: true });
  2029. }
  2030. return null;
  2031. }
  2032. /**
  2033. * Gets the focused window.
  2034. * @example
  2035. * ```typescript
  2036. * import { WebviewWindow } from '@tauri-apps/api/window';
  2037. * const focusedWindow = WebviewWindow.getFocusedWindow();
  2038. * ```
  2039. *
  2040. * @returns The WebviewWindow instance to communicate with the webview or `undefined` if there is not any focused window.
  2041. *
  2042. * @since 1.4
  2043. */
  2044. static async getFocusedWindow() {
  2045. for (const w of getAll()) {
  2046. if (await w.isFocused()) {
  2047. return w;
  2048. }
  2049. }
  2050. return null;
  2051. }
  2052. }
  2053. /** The WebviewWindow for the current window. */
  2054. let appWindow;
  2055. if ('__TAURI_METADATA__' in window) {
  2056. appWindow = new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
  2057. // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
  2058. skip: true
  2059. });
  2060. }
  2061. else {
  2062. console.warn(`Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.\nNote that this is not an issue if running this frontend on a browser instead of a Tauri window.`);
  2063. appWindow = new WebviewWindow('main', {
  2064. // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
  2065. skip: true
  2066. });
  2067. }
  2068. function mapMonitor(m) {
  2069. return m === null
  2070. ? null
  2071. : {
  2072. name: m.name,
  2073. scaleFactor: m.scaleFactor,
  2074. position: mapPhysicalPosition(m.position),
  2075. size: mapPhysicalSize(m.size)
  2076. };
  2077. }
  2078. function mapPhysicalPosition(m) {
  2079. return new PhysicalPosition(m.x, m.y);
  2080. }
  2081. function mapPhysicalSize(m) {
  2082. return new PhysicalSize(m.width, m.height);
  2083. }
  2084. /**
  2085. * Returns the monitor on which the window currently resides.
  2086. * Returns `null` if current monitor can't be detected.
  2087. * @example
  2088. * ```typescript
  2089. * import { currentMonitor } from '@tauri-apps/api/window';
  2090. * const monitor = currentMonitor();
  2091. * ```
  2092. *
  2093. * @since 1.0.0
  2094. */
  2095. async function currentMonitor() {
  2096. return invokeTauriCommand({
  2097. __tauriModule: 'Window',
  2098. message: {
  2099. cmd: 'manage',
  2100. data: {
  2101. cmd: {
  2102. type: 'currentMonitor'
  2103. }
  2104. }
  2105. }
  2106. }).then(mapMonitor);
  2107. }
  2108. /**
  2109. * Returns the primary monitor of the system.
  2110. * Returns `null` if it can't identify any monitor as a primary one.
  2111. * @example
  2112. * ```typescript
  2113. * import { primaryMonitor } from '@tauri-apps/api/window';
  2114. * const monitor = primaryMonitor();
  2115. * ```
  2116. *
  2117. * @since 1.0.0
  2118. */
  2119. async function primaryMonitor() {
  2120. return invokeTauriCommand({
  2121. __tauriModule: 'Window',
  2122. message: {
  2123. cmd: 'manage',
  2124. data: {
  2125. cmd: {
  2126. type: 'primaryMonitor'
  2127. }
  2128. }
  2129. }
  2130. }).then(mapMonitor);
  2131. }
  2132. /**
  2133. * Returns the list of all the monitors available on the system.
  2134. * @example
  2135. * ```typescript
  2136. * import { availableMonitors } from '@tauri-apps/api/window';
  2137. * const monitors = availableMonitors();
  2138. * ```
  2139. *
  2140. * @since 1.0.0
  2141. */
  2142. async function availableMonitors() {
  2143. return invokeTauriCommand({
  2144. __tauriModule: 'Window',
  2145. message: {
  2146. cmd: 'manage',
  2147. data: {
  2148. cmd: {
  2149. type: 'availableMonitors'
  2150. }
  2151. }
  2152. }
  2153. }).then((ms) => ms.map(mapMonitor));
  2154. }
  2155. export { CloseRequestedEvent, LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, UserAttentionType, WebviewWindow, WebviewWindowHandle, WindowManager, appWindow, availableMonitors, currentMonitor, getAll, getCurrent, primaryMonitor };