window.ts 59 KB

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