window.ts 66 KB

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