window.ts 60 KB

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