window.ts 59 KB

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