window.ts 63 KB

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