window.ts 65 KB

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