window.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. // Copyright 2019-2021 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 `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. *
  9. * The APIs must be allowlisted on `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. * "setSize": true,
  31. * "setMinSize": true,
  32. * "setMaxSize": true,
  33. * "setPosition": true,
  34. * "setFullscreen": true,
  35. * "setFocus": true,
  36. * "setIcon": true,
  37. * "setSkipTaskbar": true,
  38. * "setCursorGrab": true,
  39. * "setCursorVisible": true,
  40. * "setCursorIcon": true,
  41. * "setCursorPosition": true,
  42. * "startDragging": true,
  43. * "print": true
  44. * }
  45. * }
  46. * }
  47. * }
  48. * ```
  49. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  50. *
  51. * # Window events
  52. *
  53. * Events can be listened using `appWindow.listen`:
  54. * ```typescript
  55. * import { appWindow } from '@tauri-apps/api/window'
  56. * appWindow.listen('tauri://move', ({ event, payload }) => {
  57. * const { x, y } = payload // payload here is a `PhysicalPosition`
  58. * })
  59. * ```
  60. *
  61. * Window-specific events emitted by the backend:
  62. *
  63. * #### 'tauri://resize'
  64. * Emitted when the size of the window has changed.
  65. * *EventPayload*:
  66. * ```typescript
  67. * type ResizePayload = PhysicalSize
  68. * ```
  69. *
  70. * #### 'tauri://move'
  71. * Emitted when the position of the window has changed.
  72. * *EventPayload*:
  73. * ```typescript
  74. * type MovePayload = PhysicalPosition
  75. * ```
  76. *
  77. * #### 'tauri://close-requested'
  78. * Emitted when the user requests the window to be closed.
  79. * If a listener is registered for this event, Tauri won't close the window so you must call `appWindow.close()` manually.
  80. *
  81. * #### 'tauri://focus'
  82. * Emitted when the window gains focus.
  83. *
  84. * #### 'tauri://blur'
  85. * Emitted when the window loses focus.
  86. *
  87. * #### 'tauri://scale-change'
  88. * Emitted when the window's scale factor has changed.
  89. * The following user actions can cause DPI changes:
  90. * - Changing the display's resolution.
  91. * - Changing the display's scale factor (e.g. in Control Panel on Windows).
  92. * - Moving the window to a display with a different scale factor.
  93. * *Event payload*:
  94. * ```typescript
  95. * interface ScaleFactorChanged {
  96. * scaleFactor: number
  97. * size: PhysicalSize
  98. * }
  99. * ```
  100. *
  101. * #### 'tauri://menu'
  102. * Emitted when a menu item is clicked.
  103. * *EventPayload*:
  104. * ```typescript
  105. * type MenuClicked = string
  106. * ```
  107. *
  108. * @module
  109. */
  110. import { invokeTauriCommand } from './helpers/tauri'
  111. import type { EventName, EventCallback, UnlistenFn } from './event'
  112. import { emit, listen, once } from './helpers/event'
  113. type Theme = 'light' | 'dark'
  114. /** Allows you to retrieve information about a given monitor. */
  115. interface Monitor {
  116. /** Human-readable name of the monitor */
  117. name: string | null
  118. /** The monitor's resolution. */
  119. size: PhysicalSize
  120. /** the Top-left corner position of the monitor relative to the larger full screen area. */
  121. position: PhysicalPosition
  122. /** The scale factor that can be used to map physical pixels to logical pixels. */
  123. scaleFactor: number
  124. }
  125. /** A size represented in logical pixels. */
  126. class LogicalSize {
  127. type = 'Logical'
  128. width: number
  129. height: number
  130. constructor(width: number, height: number) {
  131. this.width = width
  132. this.height = height
  133. }
  134. }
  135. /** A size represented in physical pixels. */
  136. class PhysicalSize {
  137. type = 'Physical'
  138. width: number
  139. height: number
  140. constructor(width: number, height: number) {
  141. this.width = width
  142. this.height = height
  143. }
  144. /**
  145. * Converts the physical size to a logical one.
  146. * @example
  147. * ```typescript
  148. * import { appWindow } from '@tauri-apps/api/window';
  149. * const factor = await appWindow.scaleFactor();
  150. * const size = await appWindow.innerSize();
  151. * const logical = size.toLogical(factor);
  152. * ```
  153. * */
  154. toLogical(scaleFactor: number): LogicalSize {
  155. return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
  156. }
  157. }
  158. /** A position represented in logical pixels. */
  159. class LogicalPosition {
  160. type = 'Logical'
  161. x: number
  162. y: number
  163. constructor(x: number, y: number) {
  164. this.x = x
  165. this.y = y
  166. }
  167. }
  168. /** A position represented in physical pixels. */
  169. class PhysicalPosition {
  170. type = 'Physical'
  171. x: number
  172. y: number
  173. constructor(x: number, y: number) {
  174. this.x = x
  175. this.y = y
  176. }
  177. /**
  178. * Converts the physical position to a logical one.
  179. * @example
  180. * ```typescript
  181. * import { appWindow } from '@tauri-apps/api/window';
  182. * const factor = await appWindow.scaleFactor();
  183. * const position = await appWindow.innerPosition();
  184. * const logical = position.toLogical(factor);
  185. * ```
  186. * */
  187. toLogical(scaleFactor: number): LogicalPosition {
  188. return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
  189. }
  190. }
  191. /** @ignore */
  192. interface WindowDef {
  193. label: string
  194. }
  195. /** @ignore */
  196. declare global {
  197. interface Window {
  198. __TAURI_METADATA__: {
  199. __windows: WindowDef[]
  200. __currentWindow: WindowDef
  201. }
  202. }
  203. }
  204. /** Attention type to request on a window. */
  205. enum UserAttentionType {
  206. /**
  207. * #### Platform-specific
  208. * - **macOS:** Bounces the dock icon until the application is in focus.
  209. * - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
  210. */
  211. Critical = 1,
  212. /**
  213. * #### Platform-specific
  214. * - **macOS:** Bounces the dock icon once.
  215. * - **Windows:** Flashes the taskbar button until the application is in focus.
  216. */
  217. Informational
  218. }
  219. export type CursorIcon =
  220. | 'default'
  221. | 'crosshair'
  222. | 'hand'
  223. | 'arrow'
  224. | 'move'
  225. | 'text'
  226. | 'wait'
  227. | 'help'
  228. | 'progress'
  229. // something cannot be done
  230. | 'notAllowed'
  231. | 'contextMenu'
  232. | 'cell'
  233. | 'verticalText'
  234. | 'alias'
  235. | 'copy'
  236. | 'noDrop'
  237. // something can be grabbed
  238. | 'grab'
  239. /// something is grabbed
  240. | 'grabbing'
  241. | 'allScroll'
  242. | 'zoomIn'
  243. | 'zoomOut'
  244. // edge is to be moved
  245. | 'eResize'
  246. | 'nResize'
  247. | 'neResize'
  248. | 'nwResize'
  249. | 'sResize'
  250. | 'seResize'
  251. | 'swResize'
  252. | 'wResize'
  253. | 'ewResize'
  254. | 'nsResize'
  255. | 'neswResize'
  256. | 'nwseResize'
  257. | 'colResize'
  258. | 'rowResize'
  259. /**
  260. * Get an instance of `WebviewWindow` for the current webview window.
  261. *
  262. * @return The current WebviewWindow.
  263. */
  264. function getCurrent(): WebviewWindow {
  265. return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
  266. // @ts-expect-error
  267. skip: true
  268. })
  269. }
  270. /**
  271. * Gets an instance of `WebviewWindow` for all available webview windows.
  272. *
  273. * @return The list of WebviewWindow.
  274. */
  275. function getAll(): WebviewWindow[] {
  276. return window.__TAURI_METADATA__.__windows.map(
  277. (w) =>
  278. new WebviewWindow(w.label, {
  279. // @ts-expect-error
  280. skip: true
  281. })
  282. )
  283. }
  284. /** @ignore */
  285. // events that are emitted right here instead of by the created webview
  286. const localTauriEvents = ['tauri://created', 'tauri://error']
  287. /** @ignore */
  288. export type WindowLabel = string
  289. /**
  290. * A webview window handle allows emitting and listening to events from the backend that are tied to the window.
  291. */
  292. class WebviewWindowHandle {
  293. /** The window label. It is a unique identifier for the window, can be used to reference it later. */
  294. label: WindowLabel
  295. /** Local event listeners. */
  296. listeners: { [key: string]: Array<EventCallback<any>> }
  297. constructor(label: WindowLabel) {
  298. this.label = label
  299. // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  300. this.listeners = Object.create(null)
  301. }
  302. /**
  303. * Listen to an event emitted by the backend that is tied to the webview window.
  304. *
  305. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  306. * @param handler Event handler.
  307. * @returns A promise resolving to a function to unlisten to the event.
  308. */
  309. async listen<T>(
  310. event: EventName,
  311. handler: EventCallback<T>
  312. ): Promise<UnlistenFn> {
  313. if (this._handleTauriEvent(event, handler)) {
  314. return Promise.resolve(() => {
  315. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
  316. const listeners = this.listeners[event]
  317. listeners.splice(listeners.indexOf(handler), 1)
  318. })
  319. }
  320. return listen(event, this.label, handler)
  321. }
  322. /**
  323. * Listen to an one-off event emitted by the backend that is tied to the webview window.
  324. *
  325. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  326. * @param handler Event handler.
  327. * @returns A promise resolving to a function to unlisten to the event.
  328. */
  329. async once<T>(event: string, handler: EventCallback<T>): Promise<UnlistenFn> {
  330. if (this._handleTauriEvent(event, handler)) {
  331. return Promise.resolve(() => {
  332. // eslint-disable-next-line security/detect-object-injection
  333. const listeners = this.listeners[event]
  334. listeners.splice(listeners.indexOf(handler), 1)
  335. })
  336. }
  337. return once(event, this.label, handler)
  338. }
  339. /**
  340. * Emits an event to the backend, tied to the webview window.
  341. *
  342. * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
  343. * @param payload Event payload.
  344. */
  345. async emit(event: string, payload?: unknown): Promise<void> {
  346. if (localTauriEvents.includes(event)) {
  347. // eslint-disable-next-line
  348. for (const handler of this.listeners[event] || []) {
  349. handler({ event, id: -1, windowLabel: this.label, payload })
  350. }
  351. return Promise.resolve()
  352. }
  353. return emit(event, this.label, payload)
  354. }
  355. _handleTauriEvent<T>(event: string, handler: EventCallback<T>): boolean {
  356. if (localTauriEvents.includes(event)) {
  357. if (!(event in this.listeners)) {
  358. // eslint-disable-next-line
  359. this.listeners[event] = [handler]
  360. } else {
  361. // eslint-disable-next-line
  362. this.listeners[event].push(handler)
  363. }
  364. return true
  365. }
  366. return false
  367. }
  368. }
  369. /**
  370. * Manage the current window object.
  371. */
  372. class WindowManager extends WebviewWindowHandle {
  373. // Getters
  374. /**
  375. * The scale factor that can be used to map physical pixels to logical pixels.
  376. * @example
  377. * ```typescript
  378. * import { appWindow } from '@tauri-apps/api/window';
  379. * const factor = await appWindow.scaleFactor();
  380. * ```
  381. *
  382. * @returns The window's monitor scale factor.
  383. * */
  384. async scaleFactor(): Promise<number> {
  385. return invokeTauriCommand({
  386. __tauriModule: 'Window',
  387. message: {
  388. cmd: 'manage',
  389. data: {
  390. label: this.label,
  391. cmd: {
  392. type: 'scaleFactor'
  393. }
  394. }
  395. }
  396. })
  397. }
  398. /**
  399. * The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop.
  400. * @example
  401. * ```typescript
  402. * import { appWindow } from '@tauri-apps/api/window';
  403. * const position = await appWindow.innerPosition();
  404. * ```
  405. *
  406. * @returns The window's inner position.
  407. * */
  408. async innerPosition(): Promise<PhysicalPosition> {
  409. return invokeTauriCommand<{ x: number; y: number }>({
  410. __tauriModule: 'Window',
  411. message: {
  412. cmd: 'manage',
  413. data: {
  414. label: this.label,
  415. cmd: {
  416. type: 'innerPosition'
  417. }
  418. }
  419. }
  420. }).then(({ x, y }) => new PhysicalPosition(x, y))
  421. }
  422. /**
  423. * The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  424. * @example
  425. * ```typescript
  426. * import { appWindow } from '@tauri-apps/api/window';
  427. * const position = await appWindow.outerPosition();
  428. * ```
  429. *
  430. * @returns The window's outer position.
  431. * */
  432. async outerPosition(): Promise<PhysicalPosition> {
  433. return invokeTauriCommand<{ x: number; y: number }>({
  434. __tauriModule: 'Window',
  435. message: {
  436. cmd: 'manage',
  437. data: {
  438. label: this.label,
  439. cmd: {
  440. type: 'outerPosition'
  441. }
  442. }
  443. }
  444. }).then(({ x, y }) => new PhysicalPosition(x, y))
  445. }
  446. /**
  447. * The physical size of the window's client area.
  448. * The client area is the content of the window, excluding the title bar and borders.
  449. * @example
  450. * ```typescript
  451. * import { appWindow } from '@tauri-apps/api/window';
  452. * const size = await appWindow.innerSize();
  453. * ```
  454. *
  455. * @returns The window's inner size.
  456. */
  457. async innerSize(): Promise<PhysicalSize> {
  458. return invokeTauriCommand<{ width: number; height: number }>({
  459. __tauriModule: 'Window',
  460. message: {
  461. cmd: 'manage',
  462. data: {
  463. label: this.label,
  464. cmd: {
  465. type: 'innerSize'
  466. }
  467. }
  468. }
  469. }).then(({ width, height }) => new PhysicalSize(width, height))
  470. }
  471. /**
  472. * The physical size of the entire window.
  473. * These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  474. * @example
  475. * ```typescript
  476. * import { appWindow } from '@tauri-apps/api/window';
  477. * const size = await appWindow.outerSize();
  478. * ```
  479. *
  480. * @returns The window's outer size.
  481. */
  482. async outerSize(): Promise<PhysicalSize> {
  483. return invokeTauriCommand<{ width: number; height: number }>({
  484. __tauriModule: 'Window',
  485. message: {
  486. cmd: 'manage',
  487. data: {
  488. label: this.label,
  489. cmd: {
  490. type: 'outerSize'
  491. }
  492. }
  493. }
  494. }).then(({ width, height }) => new PhysicalSize(width, height))
  495. }
  496. /**
  497. * Gets the window's current fullscreen state.
  498. * @example
  499. * ```typescript
  500. * import { appWindow } from '@tauri-apps/api/window';
  501. * const fullscreen = await appWindow.isFullscreen();
  502. * ```
  503. *
  504. * @returns Whether the window is in fullscreen mode or not.
  505. * */
  506. async isFullscreen(): Promise<boolean> {
  507. return invokeTauriCommand({
  508. __tauriModule: 'Window',
  509. message: {
  510. cmd: 'manage',
  511. data: {
  512. label: this.label,
  513. cmd: {
  514. type: 'isFullscreen'
  515. }
  516. }
  517. }
  518. })
  519. }
  520. /**
  521. * Gets the window's current maximized state.
  522. * @example
  523. * ```typescript
  524. * import { appWindow } from '@tauri-apps/api/window';
  525. * const maximized = await appWindow.isMaximized();
  526. * ```
  527. *
  528. * @returns Whether the window is maximized or not.
  529. * */
  530. async isMaximized(): Promise<boolean> {
  531. return invokeTauriCommand({
  532. __tauriModule: 'Window',
  533. message: {
  534. cmd: 'manage',
  535. data: {
  536. label: this.label,
  537. cmd: {
  538. type: 'isMaximized'
  539. }
  540. }
  541. }
  542. })
  543. }
  544. /**
  545. * Gets the window's current decorated state.
  546. * @example
  547. * ```typescript
  548. * import { appWindow } from '@tauri-apps/api/window';
  549. * const decorated = await appWindow.isDecorated();
  550. * ```
  551. *
  552. * @returns Whether the window is decorated or not.
  553. * */
  554. async isDecorated(): Promise<boolean> {
  555. return invokeTauriCommand({
  556. __tauriModule: 'Window',
  557. message: {
  558. cmd: 'manage',
  559. data: {
  560. label: this.label,
  561. cmd: {
  562. type: 'isDecorated'
  563. }
  564. }
  565. }
  566. })
  567. }
  568. /**
  569. * Gets the window's current resizable state.
  570. * @example
  571. * ```typescript
  572. * import { appWindow } from '@tauri-apps/api/window';
  573. * const resizable = await appWindow.isResizable();
  574. * ```
  575. *
  576. * @returns Whether the window is resizable or not.
  577. * */
  578. async isResizable(): Promise<boolean> {
  579. return invokeTauriCommand({
  580. __tauriModule: 'Window',
  581. message: {
  582. cmd: 'manage',
  583. data: {
  584. label: this.label,
  585. cmd: {
  586. type: 'isResizable'
  587. }
  588. }
  589. }
  590. })
  591. }
  592. /**
  593. * Gets the window's current visible state.
  594. * @example
  595. * ```typescript
  596. * import { appWindow } from '@tauri-apps/api/window';
  597. * const visible = await appWindow.isVisible();
  598. * ```
  599. *
  600. * @returns Whether the window is visible or not.
  601. * */
  602. async isVisible(): Promise<boolean> {
  603. return invokeTauriCommand({
  604. __tauriModule: 'Window',
  605. message: {
  606. cmd: 'manage',
  607. data: {
  608. label: this.label,
  609. cmd: {
  610. type: 'isVisible'
  611. }
  612. }
  613. }
  614. })
  615. }
  616. /**
  617. * Gets the window's current visible state.
  618. * @example
  619. * ```typescript
  620. * import { appWindow } from '@tauri-apps/api/window';
  621. * const theme = await appWindow.theme();
  622. * ```
  623. *
  624. * @returns The system theme.
  625. * */
  626. async theme(): Promise<Theme | null> {
  627. return invokeTauriCommand({
  628. __tauriModule: 'Window',
  629. message: {
  630. cmd: 'manage',
  631. data: {
  632. label: this.label,
  633. cmd: {
  634. type: 'theme'
  635. }
  636. }
  637. }
  638. })
  639. }
  640. // Setters
  641. /**
  642. * Centers the window.
  643. * @example
  644. * ```typescript
  645. * import { appWindow } from '@tauri-apps/api/window';
  646. * await appWindow.center();
  647. * ```
  648. *
  649. * @param resizable
  650. * @returns A promise indicating the success or failure of the operation.
  651. */
  652. async center(): Promise<void> {
  653. return invokeTauriCommand({
  654. __tauriModule: 'Window',
  655. message: {
  656. cmd: 'manage',
  657. data: {
  658. label: this.label,
  659. cmd: {
  660. type: 'center'
  661. }
  662. }
  663. }
  664. })
  665. }
  666. /**
  667. * Requests user attention to the window, this has no effect if the application
  668. * is already focused. How requesting for user attention manifests is platform dependent,
  669. * see `UserAttentionType` for details.
  670. *
  671. * Providing `null` will unset the request for user attention. Unsetting the request for
  672. * user attention might not be done automatically by the WM when the window receives input.
  673. *
  674. * #### Platform-specific
  675. *
  676. * - **macOS:** `null` has no effect.
  677. * - **Linux:** Urgency levels have the same effect.
  678. * @example
  679. * ```typescript
  680. * import { appWindow } from '@tauri-apps/api/window';
  681. * await appWindow.requestUserAttention();
  682. * ```
  683. *
  684. * @param resizable
  685. * @returns A promise indicating the success or failure of the operation.
  686. */
  687. async requestUserAttention(
  688. requestType: UserAttentionType | null
  689. ): Promise<void> {
  690. let requestType_ = null
  691. if (requestType) {
  692. if (requestType === UserAttentionType.Critical) {
  693. requestType_ = { type: 'Critical' }
  694. } else {
  695. requestType_ = { type: 'Informational' }
  696. }
  697. }
  698. return invokeTauriCommand({
  699. __tauriModule: 'Window',
  700. message: {
  701. cmd: 'manage',
  702. data: {
  703. label: this.label,
  704. cmd: {
  705. type: 'requestUserAttention',
  706. payload: requestType_
  707. }
  708. }
  709. }
  710. })
  711. }
  712. /**
  713. * Updates the window resizable flag.
  714. * @example
  715. * ```typescript
  716. * import { appWindow } from '@tauri-apps/api/window';
  717. * await appWindow.setResizable(false);
  718. * ```
  719. *
  720. * @param resizable
  721. * @returns A promise indicating the success or failure of the operation.
  722. */
  723. async setResizable(resizable: boolean): Promise<void> {
  724. return invokeTauriCommand({
  725. __tauriModule: 'Window',
  726. message: {
  727. cmd: 'manage',
  728. data: {
  729. label: this.label,
  730. cmd: {
  731. type: 'setResizable',
  732. payload: resizable
  733. }
  734. }
  735. }
  736. })
  737. }
  738. /**
  739. * Sets the window title.
  740. * @example
  741. * ```typescript
  742. * import { appWindow } from '@tauri-apps/api/window';
  743. * await appWindow.setTitle('Tauri');
  744. * ```
  745. *
  746. * @param title The new title
  747. * @returns A promise indicating the success or failure of the operation.
  748. */
  749. async setTitle(title: string): Promise<void> {
  750. return invokeTauriCommand({
  751. __tauriModule: 'Window',
  752. message: {
  753. cmd: 'manage',
  754. data: {
  755. label: this.label,
  756. cmd: {
  757. type: 'setTitle',
  758. payload: title
  759. }
  760. }
  761. }
  762. })
  763. }
  764. /**
  765. * Maximizes the window.
  766. * @example
  767. * ```typescript
  768. * import { appWindow } from '@tauri-apps/api/window';
  769. * await appWindow.maximize();
  770. * ```
  771. *
  772. * @returns A promise indicating the success or failure of the operation.
  773. */
  774. async maximize(): Promise<void> {
  775. return invokeTauriCommand({
  776. __tauriModule: 'Window',
  777. message: {
  778. cmd: 'manage',
  779. data: {
  780. label: this.label,
  781. cmd: {
  782. type: 'maximize'
  783. }
  784. }
  785. }
  786. })
  787. }
  788. /**
  789. * Unmaximizes the window.
  790. * @example
  791. * ```typescript
  792. * import { appWindow } from '@tauri-apps/api/window';
  793. * await appWindow.unmaximize();
  794. * ```
  795. *
  796. * @returns A promise indicating the success or failure of the operation.
  797. */
  798. async unmaximize(): Promise<void> {
  799. return invokeTauriCommand({
  800. __tauriModule: 'Window',
  801. message: {
  802. cmd: 'manage',
  803. data: {
  804. label: this.label,
  805. cmd: {
  806. type: 'unmaximize'
  807. }
  808. }
  809. }
  810. })
  811. }
  812. /**
  813. * Toggles the window maximized state.
  814. * @example
  815. * ```typescript
  816. * import { appWindow } from '@tauri-apps/api/window';
  817. * await appWindow.toggleMaximize();
  818. * ```
  819. *
  820. * @returns A promise indicating the success or failure of the operation.
  821. */
  822. async toggleMaximize(): Promise<void> {
  823. return invokeTauriCommand({
  824. __tauriModule: 'Window',
  825. message: {
  826. cmd: 'manage',
  827. data: {
  828. label: this.label,
  829. cmd: {
  830. type: 'toggleMaximize'
  831. }
  832. }
  833. }
  834. })
  835. }
  836. /**
  837. * Minimizes the window.
  838. * @example
  839. * ```typescript
  840. * import { appWindow } from '@tauri-apps/api/window';
  841. * await appWindow.minimize();
  842. * ```
  843. *
  844. * @returns A promise indicating the success or failure of the operation.
  845. */
  846. async minimize(): Promise<void> {
  847. return invokeTauriCommand({
  848. __tauriModule: 'Window',
  849. message: {
  850. cmd: 'manage',
  851. data: {
  852. label: this.label,
  853. cmd: {
  854. type: 'minimize'
  855. }
  856. }
  857. }
  858. })
  859. }
  860. /**
  861. * Unminimizes the window.
  862. * @example
  863. * ```typescript
  864. * import { appWindow } from '@tauri-apps/api/window';
  865. * await appWindow.unminimize();
  866. * ```
  867. *
  868. * @returns A promise indicating the success or failure of the operation.
  869. */
  870. async unminimize(): Promise<void> {
  871. return invokeTauriCommand({
  872. __tauriModule: 'Window',
  873. message: {
  874. cmd: 'manage',
  875. data: {
  876. label: this.label,
  877. cmd: {
  878. type: 'unminimize'
  879. }
  880. }
  881. }
  882. })
  883. }
  884. /**
  885. * Sets the window visibility to true.
  886. * @example
  887. * ```typescript
  888. * import { appWindow } from '@tauri-apps/api/window';
  889. * await appWindow.show();
  890. * ```
  891. *
  892. * @returns A promise indicating the success or failure of the operation.
  893. */
  894. async show(): Promise<void> {
  895. return invokeTauriCommand({
  896. __tauriModule: 'Window',
  897. message: {
  898. cmd: 'manage',
  899. data: {
  900. label: this.label,
  901. cmd: {
  902. type: 'show'
  903. }
  904. }
  905. }
  906. })
  907. }
  908. /**
  909. * Sets the window visibility to false.
  910. * @example
  911. * ```typescript
  912. * import { appWindow } from '@tauri-apps/api/window';
  913. * await appWindow.hide();
  914. * ```
  915. *
  916. * @returns A promise indicating the success or failure of the operation.
  917. */
  918. async hide(): Promise<void> {
  919. return invokeTauriCommand({
  920. __tauriModule: 'Window',
  921. message: {
  922. cmd: 'manage',
  923. data: {
  924. label: this.label,
  925. cmd: {
  926. type: 'hide'
  927. }
  928. }
  929. }
  930. })
  931. }
  932. /**
  933. * Closes the window.
  934. * @example
  935. * ```typescript
  936. * import { appWindow } from '@tauri-apps/api/window';
  937. * await appWindow.close();
  938. * ```
  939. *
  940. * @returns A promise indicating the success or failure of the operation.
  941. */
  942. async close(): Promise<void> {
  943. return invokeTauriCommand({
  944. __tauriModule: 'Window',
  945. message: {
  946. cmd: 'manage',
  947. data: {
  948. label: this.label,
  949. cmd: {
  950. type: 'close'
  951. }
  952. }
  953. }
  954. })
  955. }
  956. /**
  957. * Whether the window should have borders and bars.
  958. * @example
  959. * ```typescript
  960. * import { appWindow } from '@tauri-apps/api/window';
  961. * await appWindow.setDecorations(false);
  962. * ```
  963. *
  964. * @param decorations Whether the window should have borders and bars.
  965. * @returns A promise indicating the success or failure of the operation.
  966. */
  967. async setDecorations(decorations: boolean): Promise<void> {
  968. return invokeTauriCommand({
  969. __tauriModule: 'Window',
  970. message: {
  971. cmd: 'manage',
  972. data: {
  973. label: this.label,
  974. cmd: {
  975. type: 'setDecorations',
  976. payload: decorations
  977. }
  978. }
  979. }
  980. })
  981. }
  982. /**
  983. * Whether the window should always be on top of other windows.
  984. * @example
  985. * ```typescript
  986. * import { appWindow } from '@tauri-apps/api/window';
  987. * await appWindow.setAlwaysOnTop(true);
  988. * ```
  989. *
  990. * @param alwaysOnTop Whether the window should always be on top of other windows or not.
  991. * @returns A promise indicating the success or failure of the operation.
  992. */
  993. async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
  994. return invokeTauriCommand({
  995. __tauriModule: 'Window',
  996. message: {
  997. cmd: 'manage',
  998. data: {
  999. label: this.label,
  1000. cmd: {
  1001. type: 'setAlwaysOnTop',
  1002. payload: alwaysOnTop
  1003. }
  1004. }
  1005. }
  1006. })
  1007. }
  1008. /**
  1009. * Resizes the window with a new inner size.
  1010. * @example
  1011. * ```typescript
  1012. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1013. * await appWindow.setSize(new LogicalSize(600, 500));
  1014. * ```
  1015. *
  1016. * @param size The logical or physical inner size.
  1017. * @returns A promise indicating the success or failure of the operation.
  1018. */
  1019. async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
  1020. if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
  1021. throw new Error(
  1022. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1023. )
  1024. }
  1025. return invokeTauriCommand({
  1026. __tauriModule: 'Window',
  1027. message: {
  1028. cmd: 'manage',
  1029. data: {
  1030. label: this.label,
  1031. cmd: {
  1032. type: 'setSize',
  1033. payload: {
  1034. type: size.type,
  1035. data: {
  1036. width: size.width,
  1037. height: size.height
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. })
  1044. }
  1045. /**
  1046. * Sets the window minimum inner size. If the `size` argument is not provided, the constraint is unset.
  1047. * @example
  1048. * ```typescript
  1049. * import { appWindow, PhysicalSize } from '@tauri-apps/api/window';
  1050. * await appWindow.setMinSize(new PhysicalSize(600, 500));
  1051. * ```
  1052. *
  1053. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1054. * @returns A promise indicating the success or failure of the operation.
  1055. */
  1056. async setMinSize(
  1057. size: LogicalSize | PhysicalSize | null | undefined
  1058. ): Promise<void> {
  1059. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1060. throw new Error(
  1061. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1062. )
  1063. }
  1064. return invokeTauriCommand({
  1065. __tauriModule: 'Window',
  1066. message: {
  1067. cmd: 'manage',
  1068. data: {
  1069. label: this.label,
  1070. cmd: {
  1071. type: 'setMinSize',
  1072. payload: size
  1073. ? {
  1074. type: size.type,
  1075. data: {
  1076. width: size.width,
  1077. height: size.height
  1078. }
  1079. }
  1080. : null
  1081. }
  1082. }
  1083. }
  1084. })
  1085. }
  1086. /**
  1087. * Sets the window maximum inner size. If the `size` argument is undefined, the constraint is unset.
  1088. * @example
  1089. * ```typescript
  1090. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1091. * await appWindow.setMaxSize(new LogicalSize(600, 500));
  1092. * ```
  1093. *
  1094. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1095. * @returns A promise indicating the success or failure of the operation.
  1096. */
  1097. async setMaxSize(
  1098. size: LogicalSize | PhysicalSize | null | undefined
  1099. ): Promise<void> {
  1100. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1101. throw new Error(
  1102. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1103. )
  1104. }
  1105. return invokeTauriCommand({
  1106. __tauriModule: 'Window',
  1107. message: {
  1108. cmd: 'manage',
  1109. data: {
  1110. label: this.label,
  1111. cmd: {
  1112. type: 'setMaxSize',
  1113. payload: size
  1114. ? {
  1115. type: size.type,
  1116. data: {
  1117. width: size.width,
  1118. height: size.height
  1119. }
  1120. }
  1121. : null
  1122. }
  1123. }
  1124. }
  1125. })
  1126. }
  1127. /**
  1128. * Sets the window outer position.
  1129. * @example
  1130. * ```typescript
  1131. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1132. * await appWindow.setPosition(new LogicalPosition(600, 500));
  1133. * ```
  1134. *
  1135. * @param position The new position, in logical or physical pixels.
  1136. * @returns A promise indicating the success or failure of the operation.
  1137. */
  1138. async setPosition(
  1139. position: LogicalPosition | PhysicalPosition
  1140. ): Promise<void> {
  1141. if (
  1142. !position ||
  1143. (position.type !== 'Logical' && position.type !== 'Physical')
  1144. ) {
  1145. throw new Error(
  1146. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  1147. )
  1148. }
  1149. return invokeTauriCommand({
  1150. __tauriModule: 'Window',
  1151. message: {
  1152. cmd: 'manage',
  1153. data: {
  1154. label: this.label,
  1155. cmd: {
  1156. type: 'setPosition',
  1157. payload: {
  1158. type: position.type,
  1159. data: {
  1160. x: position.x,
  1161. y: position.y
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. })
  1168. }
  1169. /**
  1170. * Sets the window fullscreen state.
  1171. * @example
  1172. * ```typescript
  1173. * import { appWindow } from '@tauri-apps/api/window';
  1174. * await appWindow.setFullscreen(true);
  1175. * ```
  1176. *
  1177. * @param fullscreen Whether the window should go to fullscreen or not.
  1178. * @returns A promise indicating the success or failure of the operation.
  1179. */
  1180. async setFullscreen(fullscreen: boolean): Promise<void> {
  1181. return invokeTauriCommand({
  1182. __tauriModule: 'Window',
  1183. message: {
  1184. cmd: 'manage',
  1185. data: {
  1186. label: this.label,
  1187. cmd: {
  1188. type: 'setFullscreen',
  1189. payload: fullscreen
  1190. }
  1191. }
  1192. }
  1193. })
  1194. }
  1195. /**
  1196. * Bring the window to front and focus.
  1197. * @example
  1198. * ```typescript
  1199. * import { appWindow } from '@tauri-apps/api/window';
  1200. * await appWindow.setFocus();
  1201. * ```
  1202. *
  1203. * @returns A promise indicating the success or failure of the operation.
  1204. */
  1205. async setFocus(): Promise<void> {
  1206. return invokeTauriCommand({
  1207. __tauriModule: 'Window',
  1208. message: {
  1209. cmd: 'manage',
  1210. data: {
  1211. label: this.label,
  1212. cmd: {
  1213. type: 'setFocus'
  1214. }
  1215. }
  1216. }
  1217. })
  1218. }
  1219. /**
  1220. * Sets the window icon.
  1221. * @example
  1222. * ```typescript
  1223. * import { appWindow } from '@tauri-apps/api/window';
  1224. * await appWindow.setIcon('/tauri/awesome.png');
  1225. * ```
  1226. *
  1227. * Note that you need the `icon-ico` or `icon-png` Cargo features to use this API.
  1228. * To enable it, change your Cargo.toml file:
  1229. * ```toml
  1230. * [dependencies]
  1231. * tauri = { version = "...", features = ["...", "icon-png"] }
  1232. * ```
  1233. *
  1234. * @param icon Icon bytes or path to the icon file.
  1235. * @returns A promise indicating the success or failure of the operation.
  1236. */
  1237. async setIcon(icon: string | Uint8Array): Promise<void> {
  1238. return invokeTauriCommand({
  1239. __tauriModule: 'Window',
  1240. message: {
  1241. cmd: 'manage',
  1242. data: {
  1243. label: this.label,
  1244. cmd: {
  1245. type: 'setIcon',
  1246. payload: {
  1247. // correctly serialize Uint8Arrays
  1248. icon: typeof icon === 'string' ? icon : Array.from(icon)
  1249. }
  1250. }
  1251. }
  1252. }
  1253. })
  1254. }
  1255. /**
  1256. * Whether to show the window icon in the task bar or not.
  1257. * @example
  1258. * ```typescript
  1259. * import { appWindow } from '@tauri-apps/api/window';
  1260. * await appWindow.setSkipTaskbar(true);
  1261. * ```
  1262. *
  1263. * @param skip true to hide window icon, false to show it.
  1264. * @returns A promise indicating the success or failure of the operation.
  1265. */
  1266. async setSkipTaskbar(skip: boolean): Promise<void> {
  1267. return invokeTauriCommand({
  1268. __tauriModule: 'Window',
  1269. message: {
  1270. cmd: 'manage',
  1271. data: {
  1272. label: this.label,
  1273. cmd: {
  1274. type: 'setSkipTaskbar',
  1275. payload: skip
  1276. }
  1277. }
  1278. }
  1279. })
  1280. }
  1281. /**
  1282. * Grabs the cursor, preventing it from leaving the window.
  1283. *
  1284. * There's no guarantee that the cursor will be hidden. You should
  1285. * hide it by yourself if you want so.
  1286. *
  1287. * #### Platform-specific
  1288. *
  1289. * - **Linux:** Unsupported.
  1290. * - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
  1291. * @example
  1292. * ```typescript
  1293. * import { appWindow } from '@tauri-apps/api/window';
  1294. * await appWindow.setCursorGrab(true);
  1295. * ```
  1296. *
  1297. * @param grab `true` to grab the cursor icon, `false` to release it.
  1298. * @returns A promise indicating the success or failure of the operation.
  1299. */
  1300. async setCursorGrab(grab: boolean): Promise<void> {
  1301. return invokeTauriCommand({
  1302. __tauriModule: 'Window',
  1303. message: {
  1304. cmd: 'manage',
  1305. data: {
  1306. label: this.label,
  1307. cmd: {
  1308. type: 'setCursorGrab',
  1309. payload: grab
  1310. }
  1311. }
  1312. }
  1313. })
  1314. }
  1315. /**
  1316. * Modifies the cursor's visibility.
  1317. *
  1318. * #### Platform-specific
  1319. *
  1320. * - **Windows:** The cursor is only hidden within the confines of the window.
  1321. * - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor is
  1322. * outside of the window.
  1323. * @example
  1324. * ```typescript
  1325. * import { appWindow } from '@tauri-apps/api/window';
  1326. * await appWindow.setCursorVisible(false);
  1327. * ```
  1328. *
  1329. * @param visible If `false`, this will hide the cursor. If `true`, this will show the cursor.
  1330. * @returns A promise indicating the success or failure of the operation.
  1331. */
  1332. async setCursorVisible(visible: boolean): Promise<void> {
  1333. return invokeTauriCommand({
  1334. __tauriModule: 'Window',
  1335. message: {
  1336. cmd: 'manage',
  1337. data: {
  1338. label: this.label,
  1339. cmd: {
  1340. type: 'setCursorVisible',
  1341. payload: visible
  1342. }
  1343. }
  1344. }
  1345. })
  1346. }
  1347. /**
  1348. * Modifies the cursor icon of the window.
  1349. * @example
  1350. * ```typescript
  1351. * import { appWindow } from '@tauri-apps/api/window';
  1352. * await appWindow.setCursorIcon('help');
  1353. * ```
  1354. *
  1355. * @param icon The new cursor icon.
  1356. * @returns A promise indicating the success or failure of the operation.
  1357. */
  1358. async setCursorIcon(icon: CursorIcon): Promise<void> {
  1359. return invokeTauriCommand({
  1360. __tauriModule: 'Window',
  1361. message: {
  1362. cmd: 'manage',
  1363. data: {
  1364. label: this.label,
  1365. cmd: {
  1366. type: 'setCursorIcon',
  1367. payload: icon
  1368. }
  1369. }
  1370. }
  1371. })
  1372. }
  1373. /**
  1374. * Changes the position of the cursor in window coordinates.
  1375. * @example
  1376. * ```typescript
  1377. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1378. * await appWindow.setCursorPosition(new LogicalPosition(600, 300));
  1379. * ```
  1380. *
  1381. * @param position The new cursor position.
  1382. * @returns A promise indicating the success or failure of the operation.
  1383. */
  1384. async setCursorPosition(
  1385. position: LogicalPosition | PhysicalPosition
  1386. ): Promise<void> {
  1387. if (
  1388. !position ||
  1389. (position.type !== 'Logical' && position.type !== 'Physical')
  1390. ) {
  1391. throw new Error(
  1392. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  1393. )
  1394. }
  1395. return invokeTauriCommand({
  1396. __tauriModule: 'Window',
  1397. message: {
  1398. cmd: 'manage',
  1399. data: {
  1400. label: this.label,
  1401. cmd: {
  1402. type: 'setCursorPosition',
  1403. payload: {
  1404. type: position.type,
  1405. data: {
  1406. x: position.x,
  1407. y: position.y
  1408. }
  1409. }
  1410. }
  1411. }
  1412. }
  1413. })
  1414. }
  1415. /**
  1416. * Starts dragging the window.
  1417. * @example
  1418. * ```typescript
  1419. * import { appWindow } from '@tauri-apps/api/window';
  1420. * await appWindow.startDragging();
  1421. * ```
  1422. *
  1423. * @return A promise indicating the success or failure of the operation.
  1424. */
  1425. async startDragging(): Promise<void> {
  1426. return invokeTauriCommand({
  1427. __tauriModule: 'Window',
  1428. message: {
  1429. cmd: 'manage',
  1430. data: {
  1431. label: this.label,
  1432. cmd: {
  1433. type: 'startDragging'
  1434. }
  1435. }
  1436. }
  1437. })
  1438. }
  1439. }
  1440. /**
  1441. * Create new webview windows and get a handle to existing ones.
  1442. *
  1443. * Windows are identified by a *label* a unique identifier that can be used to reference it later.
  1444. * It may only contain alphanumeric characters `a-zA-Z` plus the following special characters `-`, `/`, `:` and `_`.
  1445. *
  1446. * @example
  1447. * ```typescript
  1448. * // loading embedded asset:
  1449. * const webview = new WebviewWindow('theUniqueLabel', {
  1450. * url: 'path/to/page.html'
  1451. * });
  1452. * // alternatively, load a remote URL:
  1453. * const webview = new WebviewWindow('theUniqueLabel', {
  1454. * url: 'https://github.com/tauri-apps/tauri'
  1455. * });
  1456. *
  1457. * webview.once('tauri://created', function () {
  1458. * // webview window successfully created
  1459. * });
  1460. * webview.once('tauri://error', function (e) {
  1461. * // an error happened creating the webview window
  1462. * });
  1463. *
  1464. * // emit an event to the backend
  1465. * await webview.emit("some event", "data");
  1466. * // listen to an event from the backend
  1467. * const unlisten = await webview.listen("event name", e => {});
  1468. * unlisten();
  1469. * ```
  1470. */
  1471. class WebviewWindow extends WindowManager {
  1472. /**
  1473. * Creates a new WebviewWindow.
  1474. * @example
  1475. * ```typescript
  1476. * import { WebviewWindow } from '@tauri-apps/api/window';
  1477. * const webview = new WebviewWindow('my-label', {
  1478. * url: 'https://github.com/tauri-apps/tauri'
  1479. * });
  1480. * webview.once('tauri://created', function () {
  1481. * // webview window successfully created
  1482. * });
  1483. * webview.once('tauri://error', function (e) {
  1484. * // an error happened creating the webview window
  1485. * });
  1486. * ```
  1487. *
  1488. * * @param label The unique webview window label. Must be alphanumeric: `a-zA-Z-/:_`.
  1489. * @returns The WebviewWindow instance to communicate with the webview.
  1490. */
  1491. constructor(label: WindowLabel, options: WindowOptions = {}) {
  1492. super(label)
  1493. // @ts-expect-error
  1494. if (!options?.skip) {
  1495. invokeTauriCommand({
  1496. __tauriModule: 'Window',
  1497. message: {
  1498. cmd: 'createWebview',
  1499. data: {
  1500. options: {
  1501. label,
  1502. ...options
  1503. }
  1504. }
  1505. }
  1506. })
  1507. .then(async () => this.emit('tauri://created'))
  1508. .catch(async (e: string) => this.emit('tauri://error', e))
  1509. }
  1510. }
  1511. /**
  1512. * Gets the WebviewWindow for the webview associated with the given label.
  1513. * @example
  1514. * ```typescript
  1515. * import { WebviewWindow } from '@tauri-apps/api/window';
  1516. * const mainWindow = WebviewWindow.getByLabel('main');
  1517. * ```
  1518. *
  1519. * @param label The webview window label.
  1520. * @returns The WebviewWindow instance to communicate with the webview or null if the webview doesn't exist.
  1521. */
  1522. static getByLabel(label: string): WebviewWindow | null {
  1523. if (getAll().some((w) => w.label === label)) {
  1524. // @ts-expect-error
  1525. return new WebviewWindow(label, { skip: true })
  1526. }
  1527. return null
  1528. }
  1529. }
  1530. /** The WebviewWindow for the current window. */
  1531. let appWindow: WebviewWindow
  1532. if ('__TAURI_METADATA__' in window) {
  1533. appWindow = new WebviewWindow(
  1534. window.__TAURI_METADATA__.__currentWindow.label,
  1535. {
  1536. // @ts-expect-error
  1537. skip: true
  1538. }
  1539. )
  1540. } else {
  1541. console.warn(
  1542. `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.`
  1543. )
  1544. appWindow = new WebviewWindow('main', {
  1545. // @ts-expect-error
  1546. skip: true
  1547. })
  1548. }
  1549. /** Configuration for the window to create. */
  1550. interface WindowOptions {
  1551. /**
  1552. * Remote URL or local file path to open.
  1553. *
  1554. * - URL such as `https://github.com/tauri-apps` is opened directly on a Tauri window.
  1555. * - data: URL such as `data:text/html,<html>...` is only supported with the `window-data-url` Cargo feature for the `tauri` dependency.
  1556. * - 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).
  1557. */
  1558. url?: string
  1559. /** Show window in the center of the screen.. */
  1560. center?: boolean
  1561. /** The initial vertical position. Only applies if `y` is also set. */
  1562. x?: number
  1563. /** The initial horizontal position. Only applies if `x` is also set. */
  1564. y?: number
  1565. /** The initial width. */
  1566. width?: number
  1567. /** The initial height. */
  1568. height?: number
  1569. /** The minimum width. Only applies if `minHeight` is also set. */
  1570. minWidth?: number
  1571. /** The minimum height. Only applies if `minWidth` is also set. */
  1572. minHeight?: number
  1573. /** The maximum width. Only applies if `maxHeight` is also set. */
  1574. maxWidth?: number
  1575. /** The maximum height. Only applies if `maxWidth` is also set. */
  1576. maxHeight?: number
  1577. /** Whether the window is resizable or not. */
  1578. resizable?: boolean
  1579. /** Window title. */
  1580. title?: string
  1581. /** Whether the window is in fullscreen mode or not. */
  1582. fullscreen?: boolean
  1583. /** Whether the window will be initially hidden or focused. */
  1584. focus?: boolean
  1585. /**
  1586. * Whether the window is transparent or not.
  1587. * Note that on `macOS` this requires the `macos-private-api` feature flag, enabled under `tauri.conf.json > tauri > macOSPrivateApi`.
  1588. * WARNING: Using private APIs on `macOS` prevents your application from being accepted to the `App Store`.
  1589. */
  1590. transparent?: boolean
  1591. /** Whether the window should be maximized upon creation or not. */
  1592. maximized?: boolean
  1593. /** Whether the window should be immediately visible upon creation or not. */
  1594. visible?: boolean
  1595. /** Whether the window should have borders and bars or not. */
  1596. decorations?: boolean
  1597. /** Whether the window should always be on top of other windows or not. */
  1598. alwaysOnTop?: boolean
  1599. /** Whether or not the window icon should be added to the taskbar. */
  1600. skipTaskbar?: boolean
  1601. /**
  1602. * Whether the file drop is enabled or not on the webview. By default it is enabled.
  1603. *
  1604. * Disabling it is required to use drag and drop on the frontend on Windows.
  1605. */
  1606. fileDropEnabled?: boolean
  1607. /**
  1608. * The initial window theme. Defaults to the system theme.
  1609. *
  1610. * Only implemented on Windows.
  1611. */
  1612. theme?: Theme
  1613. }
  1614. /**
  1615. * Returns the monitor on which the window currently resides.
  1616. * Returns `null` if current monitor can't be detected.
  1617. * @example
  1618. * ```typescript
  1619. * import { currentMonitor } from '@tauri-apps/api/window';
  1620. * const monitor = currentMonitor();
  1621. * ```
  1622. */
  1623. async function currentMonitor(): Promise<Monitor | null> {
  1624. return invokeTauriCommand({
  1625. __tauriModule: 'Window',
  1626. message: {
  1627. cmd: 'manage',
  1628. data: {
  1629. cmd: {
  1630. type: 'currentMonitor'
  1631. }
  1632. }
  1633. }
  1634. })
  1635. }
  1636. /**
  1637. * Returns the primary monitor of the system.
  1638. * Returns `null` if it can't identify any monitor as a primary one.
  1639. * @example
  1640. * ```typescript
  1641. * import { primaryMonitor } from '@tauri-apps/api/window';
  1642. * const monitor = primaryMonitor();
  1643. * ```
  1644. */
  1645. async function primaryMonitor(): Promise<Monitor | null> {
  1646. return invokeTauriCommand({
  1647. __tauriModule: 'Window',
  1648. message: {
  1649. cmd: 'manage',
  1650. data: {
  1651. cmd: {
  1652. type: 'primaryMonitor'
  1653. }
  1654. }
  1655. }
  1656. })
  1657. }
  1658. /**
  1659. * Returns the list of all the monitors available on the system.
  1660. * @example
  1661. * ```typescript
  1662. * import { availableMonitors } from '@tauri-apps/api/window';
  1663. * const monitors = availableMonitors();
  1664. * ```
  1665. * */
  1666. async function availableMonitors(): Promise<Monitor[]> {
  1667. return invokeTauriCommand({
  1668. __tauriModule: 'Window',
  1669. message: {
  1670. cmd: 'manage',
  1671. data: {
  1672. cmd: {
  1673. type: 'availableMonitors'
  1674. }
  1675. }
  1676. }
  1677. })
  1678. }
  1679. export {
  1680. WebviewWindow,
  1681. WebviewWindowHandle,
  1682. WindowManager,
  1683. getCurrent,
  1684. getAll,
  1685. appWindow,
  1686. LogicalSize,
  1687. PhysicalSize,
  1688. LogicalPosition,
  1689. PhysicalPosition,
  1690. UserAttentionType,
  1691. currentMonitor,
  1692. primaryMonitor,
  1693. availableMonitors
  1694. }
  1695. export type { Theme, Monitor, WindowOptions }