window.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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 theme.
  618. *
  619. * Only implemented on Windows and macOS 10.14+.
  620. *
  621. * @example
  622. * ```typescript
  623. * import { appWindow } from '@tauri-apps/api/window';
  624. * const theme = await appWindow.theme();
  625. * ```
  626. *
  627. * @returns The window theme.
  628. * */
  629. async theme(): Promise<Theme | null> {
  630. return invokeTauriCommand({
  631. __tauriModule: 'Window',
  632. message: {
  633. cmd: 'manage',
  634. data: {
  635. label: this.label,
  636. cmd: {
  637. type: 'theme'
  638. }
  639. }
  640. }
  641. })
  642. }
  643. // Setters
  644. /**
  645. * Centers the window.
  646. * @example
  647. * ```typescript
  648. * import { appWindow } from '@tauri-apps/api/window';
  649. * await appWindow.center();
  650. * ```
  651. *
  652. * @param resizable
  653. * @returns A promise indicating the success or failure of the operation.
  654. */
  655. async center(): Promise<void> {
  656. return invokeTauriCommand({
  657. __tauriModule: 'Window',
  658. message: {
  659. cmd: 'manage',
  660. data: {
  661. label: this.label,
  662. cmd: {
  663. type: 'center'
  664. }
  665. }
  666. }
  667. })
  668. }
  669. /**
  670. * Requests user attention to the window, this has no effect if the application
  671. * is already focused. How requesting for user attention manifests is platform dependent,
  672. * see `UserAttentionType` for details.
  673. *
  674. * Providing `null` will unset the request for user attention. Unsetting the request for
  675. * user attention might not be done automatically by the WM when the window receives input.
  676. *
  677. * #### Platform-specific
  678. *
  679. * - **macOS:** `null` has no effect.
  680. * - **Linux:** Urgency levels have the same effect.
  681. * @example
  682. * ```typescript
  683. * import { appWindow } from '@tauri-apps/api/window';
  684. * await appWindow.requestUserAttention();
  685. * ```
  686. *
  687. * @param resizable
  688. * @returns A promise indicating the success or failure of the operation.
  689. */
  690. async requestUserAttention(
  691. requestType: UserAttentionType | null
  692. ): Promise<void> {
  693. let requestType_ = null
  694. if (requestType) {
  695. if (requestType === UserAttentionType.Critical) {
  696. requestType_ = { type: 'Critical' }
  697. } else {
  698. requestType_ = { type: 'Informational' }
  699. }
  700. }
  701. return invokeTauriCommand({
  702. __tauriModule: 'Window',
  703. message: {
  704. cmd: 'manage',
  705. data: {
  706. label: this.label,
  707. cmd: {
  708. type: 'requestUserAttention',
  709. payload: requestType_
  710. }
  711. }
  712. }
  713. })
  714. }
  715. /**
  716. * Updates the window resizable flag.
  717. * @example
  718. * ```typescript
  719. * import { appWindow } from '@tauri-apps/api/window';
  720. * await appWindow.setResizable(false);
  721. * ```
  722. *
  723. * @param resizable
  724. * @returns A promise indicating the success or failure of the operation.
  725. */
  726. async setResizable(resizable: boolean): Promise<void> {
  727. return invokeTauriCommand({
  728. __tauriModule: 'Window',
  729. message: {
  730. cmd: 'manage',
  731. data: {
  732. label: this.label,
  733. cmd: {
  734. type: 'setResizable',
  735. payload: resizable
  736. }
  737. }
  738. }
  739. })
  740. }
  741. /**
  742. * Sets the window title.
  743. * @example
  744. * ```typescript
  745. * import { appWindow } from '@tauri-apps/api/window';
  746. * await appWindow.setTitle('Tauri');
  747. * ```
  748. *
  749. * @param title The new title
  750. * @returns A promise indicating the success or failure of the operation.
  751. */
  752. async setTitle(title: string): Promise<void> {
  753. return invokeTauriCommand({
  754. __tauriModule: 'Window',
  755. message: {
  756. cmd: 'manage',
  757. data: {
  758. label: this.label,
  759. cmd: {
  760. type: 'setTitle',
  761. payload: title
  762. }
  763. }
  764. }
  765. })
  766. }
  767. /**
  768. * Maximizes the window.
  769. * @example
  770. * ```typescript
  771. * import { appWindow } from '@tauri-apps/api/window';
  772. * await appWindow.maximize();
  773. * ```
  774. *
  775. * @returns A promise indicating the success or failure of the operation.
  776. */
  777. async maximize(): Promise<void> {
  778. return invokeTauriCommand({
  779. __tauriModule: 'Window',
  780. message: {
  781. cmd: 'manage',
  782. data: {
  783. label: this.label,
  784. cmd: {
  785. type: 'maximize'
  786. }
  787. }
  788. }
  789. })
  790. }
  791. /**
  792. * Unmaximizes the window.
  793. * @example
  794. * ```typescript
  795. * import { appWindow } from '@tauri-apps/api/window';
  796. * await appWindow.unmaximize();
  797. * ```
  798. *
  799. * @returns A promise indicating the success or failure of the operation.
  800. */
  801. async unmaximize(): Promise<void> {
  802. return invokeTauriCommand({
  803. __tauriModule: 'Window',
  804. message: {
  805. cmd: 'manage',
  806. data: {
  807. label: this.label,
  808. cmd: {
  809. type: 'unmaximize'
  810. }
  811. }
  812. }
  813. })
  814. }
  815. /**
  816. * Toggles the window maximized state.
  817. * @example
  818. * ```typescript
  819. * import { appWindow } from '@tauri-apps/api/window';
  820. * await appWindow.toggleMaximize();
  821. * ```
  822. *
  823. * @returns A promise indicating the success or failure of the operation.
  824. */
  825. async toggleMaximize(): Promise<void> {
  826. return invokeTauriCommand({
  827. __tauriModule: 'Window',
  828. message: {
  829. cmd: 'manage',
  830. data: {
  831. label: this.label,
  832. cmd: {
  833. type: 'toggleMaximize'
  834. }
  835. }
  836. }
  837. })
  838. }
  839. /**
  840. * Minimizes the window.
  841. * @example
  842. * ```typescript
  843. * import { appWindow } from '@tauri-apps/api/window';
  844. * await appWindow.minimize();
  845. * ```
  846. *
  847. * @returns A promise indicating the success or failure of the operation.
  848. */
  849. async minimize(): Promise<void> {
  850. return invokeTauriCommand({
  851. __tauriModule: 'Window',
  852. message: {
  853. cmd: 'manage',
  854. data: {
  855. label: this.label,
  856. cmd: {
  857. type: 'minimize'
  858. }
  859. }
  860. }
  861. })
  862. }
  863. /**
  864. * Unminimizes the window.
  865. * @example
  866. * ```typescript
  867. * import { appWindow } from '@tauri-apps/api/window';
  868. * await appWindow.unminimize();
  869. * ```
  870. *
  871. * @returns A promise indicating the success or failure of the operation.
  872. */
  873. async unminimize(): Promise<void> {
  874. return invokeTauriCommand({
  875. __tauriModule: 'Window',
  876. message: {
  877. cmd: 'manage',
  878. data: {
  879. label: this.label,
  880. cmd: {
  881. type: 'unminimize'
  882. }
  883. }
  884. }
  885. })
  886. }
  887. /**
  888. * Sets the window visibility to true.
  889. * @example
  890. * ```typescript
  891. * import { appWindow } from '@tauri-apps/api/window';
  892. * await appWindow.show();
  893. * ```
  894. *
  895. * @returns A promise indicating the success or failure of the operation.
  896. */
  897. async show(): Promise<void> {
  898. return invokeTauriCommand({
  899. __tauriModule: 'Window',
  900. message: {
  901. cmd: 'manage',
  902. data: {
  903. label: this.label,
  904. cmd: {
  905. type: 'show'
  906. }
  907. }
  908. }
  909. })
  910. }
  911. /**
  912. * Sets the window visibility to false.
  913. * @example
  914. * ```typescript
  915. * import { appWindow } from '@tauri-apps/api/window';
  916. * await appWindow.hide();
  917. * ```
  918. *
  919. * @returns A promise indicating the success or failure of the operation.
  920. */
  921. async hide(): Promise<void> {
  922. return invokeTauriCommand({
  923. __tauriModule: 'Window',
  924. message: {
  925. cmd: 'manage',
  926. data: {
  927. label: this.label,
  928. cmd: {
  929. type: 'hide'
  930. }
  931. }
  932. }
  933. })
  934. }
  935. /**
  936. * Closes the window.
  937. * @example
  938. * ```typescript
  939. * import { appWindow } from '@tauri-apps/api/window';
  940. * await appWindow.close();
  941. * ```
  942. *
  943. * @returns A promise indicating the success or failure of the operation.
  944. */
  945. async close(): Promise<void> {
  946. return invokeTauriCommand({
  947. __tauriModule: 'Window',
  948. message: {
  949. cmd: 'manage',
  950. data: {
  951. label: this.label,
  952. cmd: {
  953. type: 'close'
  954. }
  955. }
  956. }
  957. })
  958. }
  959. /**
  960. * Whether the window should have borders and bars.
  961. * @example
  962. * ```typescript
  963. * import { appWindow } from '@tauri-apps/api/window';
  964. * await appWindow.setDecorations(false);
  965. * ```
  966. *
  967. * @param decorations Whether the window should have borders and bars.
  968. * @returns A promise indicating the success or failure of the operation.
  969. */
  970. async setDecorations(decorations: boolean): Promise<void> {
  971. return invokeTauriCommand({
  972. __tauriModule: 'Window',
  973. message: {
  974. cmd: 'manage',
  975. data: {
  976. label: this.label,
  977. cmd: {
  978. type: 'setDecorations',
  979. payload: decorations
  980. }
  981. }
  982. }
  983. })
  984. }
  985. /**
  986. * Whether the window should always be on top of other windows.
  987. * @example
  988. * ```typescript
  989. * import { appWindow } from '@tauri-apps/api/window';
  990. * await appWindow.setAlwaysOnTop(true);
  991. * ```
  992. *
  993. * @param alwaysOnTop Whether the window should always be on top of other windows or not.
  994. * @returns A promise indicating the success or failure of the operation.
  995. */
  996. async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
  997. return invokeTauriCommand({
  998. __tauriModule: 'Window',
  999. message: {
  1000. cmd: 'manage',
  1001. data: {
  1002. label: this.label,
  1003. cmd: {
  1004. type: 'setAlwaysOnTop',
  1005. payload: alwaysOnTop
  1006. }
  1007. }
  1008. }
  1009. })
  1010. }
  1011. /**
  1012. * Resizes the window with a new inner size.
  1013. * @example
  1014. * ```typescript
  1015. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1016. * await appWindow.setSize(new LogicalSize(600, 500));
  1017. * ```
  1018. *
  1019. * @param size The logical or physical inner size.
  1020. * @returns A promise indicating the success or failure of the operation.
  1021. */
  1022. async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
  1023. if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
  1024. throw new Error(
  1025. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1026. )
  1027. }
  1028. return invokeTauriCommand({
  1029. __tauriModule: 'Window',
  1030. message: {
  1031. cmd: 'manage',
  1032. data: {
  1033. label: this.label,
  1034. cmd: {
  1035. type: 'setSize',
  1036. payload: {
  1037. type: size.type,
  1038. data: {
  1039. width: size.width,
  1040. height: size.height
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. })
  1047. }
  1048. /**
  1049. * Sets the window minimum inner size. If the `size` argument is not provided, the constraint is unset.
  1050. * @example
  1051. * ```typescript
  1052. * import { appWindow, PhysicalSize } from '@tauri-apps/api/window';
  1053. * await appWindow.setMinSize(new PhysicalSize(600, 500));
  1054. * ```
  1055. *
  1056. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1057. * @returns A promise indicating the success or failure of the operation.
  1058. */
  1059. async setMinSize(
  1060. size: LogicalSize | PhysicalSize | null | undefined
  1061. ): Promise<void> {
  1062. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1063. throw new Error(
  1064. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1065. )
  1066. }
  1067. return invokeTauriCommand({
  1068. __tauriModule: 'Window',
  1069. message: {
  1070. cmd: 'manage',
  1071. data: {
  1072. label: this.label,
  1073. cmd: {
  1074. type: 'setMinSize',
  1075. payload: size
  1076. ? {
  1077. type: size.type,
  1078. data: {
  1079. width: size.width,
  1080. height: size.height
  1081. }
  1082. }
  1083. : null
  1084. }
  1085. }
  1086. }
  1087. })
  1088. }
  1089. /**
  1090. * Sets the window maximum inner size. If the `size` argument is undefined, the constraint is unset.
  1091. * @example
  1092. * ```typescript
  1093. * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
  1094. * await appWindow.setMaxSize(new LogicalSize(600, 500));
  1095. * ```
  1096. *
  1097. * @param size The logical or physical inner size, or `null` to unset the constraint.
  1098. * @returns A promise indicating the success or failure of the operation.
  1099. */
  1100. async setMaxSize(
  1101. size: LogicalSize | PhysicalSize | null | undefined
  1102. ): Promise<void> {
  1103. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  1104. throw new Error(
  1105. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  1106. )
  1107. }
  1108. return invokeTauriCommand({
  1109. __tauriModule: 'Window',
  1110. message: {
  1111. cmd: 'manage',
  1112. data: {
  1113. label: this.label,
  1114. cmd: {
  1115. type: 'setMaxSize',
  1116. payload: size
  1117. ? {
  1118. type: size.type,
  1119. data: {
  1120. width: size.width,
  1121. height: size.height
  1122. }
  1123. }
  1124. : null
  1125. }
  1126. }
  1127. }
  1128. })
  1129. }
  1130. /**
  1131. * Sets the window outer position.
  1132. * @example
  1133. * ```typescript
  1134. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1135. * await appWindow.setPosition(new LogicalPosition(600, 500));
  1136. * ```
  1137. *
  1138. * @param position The new position, in logical or physical pixels.
  1139. * @returns A promise indicating the success or failure of the operation.
  1140. */
  1141. async setPosition(
  1142. position: LogicalPosition | PhysicalPosition
  1143. ): Promise<void> {
  1144. if (
  1145. !position ||
  1146. (position.type !== 'Logical' && position.type !== 'Physical')
  1147. ) {
  1148. throw new Error(
  1149. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  1150. )
  1151. }
  1152. return invokeTauriCommand({
  1153. __tauriModule: 'Window',
  1154. message: {
  1155. cmd: 'manage',
  1156. data: {
  1157. label: this.label,
  1158. cmd: {
  1159. type: 'setPosition',
  1160. payload: {
  1161. type: position.type,
  1162. data: {
  1163. x: position.x,
  1164. y: position.y
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. })
  1171. }
  1172. /**
  1173. * Sets the window fullscreen state.
  1174. * @example
  1175. * ```typescript
  1176. * import { appWindow } from '@tauri-apps/api/window';
  1177. * await appWindow.setFullscreen(true);
  1178. * ```
  1179. *
  1180. * @param fullscreen Whether the window should go to fullscreen or not.
  1181. * @returns A promise indicating the success or failure of the operation.
  1182. */
  1183. async setFullscreen(fullscreen: boolean): Promise<void> {
  1184. return invokeTauriCommand({
  1185. __tauriModule: 'Window',
  1186. message: {
  1187. cmd: 'manage',
  1188. data: {
  1189. label: this.label,
  1190. cmd: {
  1191. type: 'setFullscreen',
  1192. payload: fullscreen
  1193. }
  1194. }
  1195. }
  1196. })
  1197. }
  1198. /**
  1199. * Bring the window to front and focus.
  1200. * @example
  1201. * ```typescript
  1202. * import { appWindow } from '@tauri-apps/api/window';
  1203. * await appWindow.setFocus();
  1204. * ```
  1205. *
  1206. * @returns A promise indicating the success or failure of the operation.
  1207. */
  1208. async setFocus(): Promise<void> {
  1209. return invokeTauriCommand({
  1210. __tauriModule: 'Window',
  1211. message: {
  1212. cmd: 'manage',
  1213. data: {
  1214. label: this.label,
  1215. cmd: {
  1216. type: 'setFocus'
  1217. }
  1218. }
  1219. }
  1220. })
  1221. }
  1222. /**
  1223. * Sets the window icon.
  1224. * @example
  1225. * ```typescript
  1226. * import { appWindow } from '@tauri-apps/api/window';
  1227. * await appWindow.setIcon('/tauri/awesome.png');
  1228. * ```
  1229. *
  1230. * Note that you need the `icon-ico` or `icon-png` Cargo features to use this API.
  1231. * To enable it, change your Cargo.toml file:
  1232. * ```toml
  1233. * [dependencies]
  1234. * tauri = { version = "...", features = ["...", "icon-png"] }
  1235. * ```
  1236. *
  1237. * @param icon Icon bytes or path to the icon file.
  1238. * @returns A promise indicating the success or failure of the operation.
  1239. */
  1240. async setIcon(icon: string | Uint8Array): Promise<void> {
  1241. return invokeTauriCommand({
  1242. __tauriModule: 'Window',
  1243. message: {
  1244. cmd: 'manage',
  1245. data: {
  1246. label: this.label,
  1247. cmd: {
  1248. type: 'setIcon',
  1249. payload: {
  1250. // correctly serialize Uint8Arrays
  1251. icon: typeof icon === 'string' ? icon : Array.from(icon)
  1252. }
  1253. }
  1254. }
  1255. }
  1256. })
  1257. }
  1258. /**
  1259. * Whether to show the window icon in the task bar or not.
  1260. * @example
  1261. * ```typescript
  1262. * import { appWindow } from '@tauri-apps/api/window';
  1263. * await appWindow.setSkipTaskbar(true);
  1264. * ```
  1265. *
  1266. * @param skip true to hide window icon, false to show it.
  1267. * @returns A promise indicating the success or failure of the operation.
  1268. */
  1269. async setSkipTaskbar(skip: boolean): Promise<void> {
  1270. return invokeTauriCommand({
  1271. __tauriModule: 'Window',
  1272. message: {
  1273. cmd: 'manage',
  1274. data: {
  1275. label: this.label,
  1276. cmd: {
  1277. type: 'setSkipTaskbar',
  1278. payload: skip
  1279. }
  1280. }
  1281. }
  1282. })
  1283. }
  1284. /**
  1285. * Grabs the cursor, preventing it from leaving the window.
  1286. *
  1287. * There's no guarantee that the cursor will be hidden. You should
  1288. * hide it by yourself if you want so.
  1289. *
  1290. * #### Platform-specific
  1291. *
  1292. * - **Linux:** Unsupported.
  1293. * - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
  1294. * @example
  1295. * ```typescript
  1296. * import { appWindow } from '@tauri-apps/api/window';
  1297. * await appWindow.setCursorGrab(true);
  1298. * ```
  1299. *
  1300. * @param grab `true` to grab the cursor icon, `false` to release it.
  1301. * @returns A promise indicating the success or failure of the operation.
  1302. */
  1303. async setCursorGrab(grab: boolean): Promise<void> {
  1304. return invokeTauriCommand({
  1305. __tauriModule: 'Window',
  1306. message: {
  1307. cmd: 'manage',
  1308. data: {
  1309. label: this.label,
  1310. cmd: {
  1311. type: 'setCursorGrab',
  1312. payload: grab
  1313. }
  1314. }
  1315. }
  1316. })
  1317. }
  1318. /**
  1319. * Modifies the cursor's visibility.
  1320. *
  1321. * #### Platform-specific
  1322. *
  1323. * - **Windows:** The cursor is only hidden within the confines of the window.
  1324. * - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor is
  1325. * outside of the window.
  1326. * @example
  1327. * ```typescript
  1328. * import { appWindow } from '@tauri-apps/api/window';
  1329. * await appWindow.setCursorVisible(false);
  1330. * ```
  1331. *
  1332. * @param visible If `false`, this will hide the cursor. If `true`, this will show the cursor.
  1333. * @returns A promise indicating the success or failure of the operation.
  1334. */
  1335. async setCursorVisible(visible: boolean): Promise<void> {
  1336. return invokeTauriCommand({
  1337. __tauriModule: 'Window',
  1338. message: {
  1339. cmd: 'manage',
  1340. data: {
  1341. label: this.label,
  1342. cmd: {
  1343. type: 'setCursorVisible',
  1344. payload: visible
  1345. }
  1346. }
  1347. }
  1348. })
  1349. }
  1350. /**
  1351. * Modifies the cursor icon of the window.
  1352. * @example
  1353. * ```typescript
  1354. * import { appWindow } from '@tauri-apps/api/window';
  1355. * await appWindow.setCursorIcon('help');
  1356. * ```
  1357. *
  1358. * @param icon The new cursor icon.
  1359. * @returns A promise indicating the success or failure of the operation.
  1360. */
  1361. async setCursorIcon(icon: CursorIcon): Promise<void> {
  1362. return invokeTauriCommand({
  1363. __tauriModule: 'Window',
  1364. message: {
  1365. cmd: 'manage',
  1366. data: {
  1367. label: this.label,
  1368. cmd: {
  1369. type: 'setCursorIcon',
  1370. payload: icon
  1371. }
  1372. }
  1373. }
  1374. })
  1375. }
  1376. /**
  1377. * Changes the position of the cursor in window coordinates.
  1378. * @example
  1379. * ```typescript
  1380. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
  1381. * await appWindow.setCursorPosition(new LogicalPosition(600, 300));
  1382. * ```
  1383. *
  1384. * @param position The new cursor position.
  1385. * @returns A promise indicating the success or failure of the operation.
  1386. */
  1387. async setCursorPosition(
  1388. position: LogicalPosition | PhysicalPosition
  1389. ): Promise<void> {
  1390. if (
  1391. !position ||
  1392. (position.type !== 'Logical' && position.type !== 'Physical')
  1393. ) {
  1394. throw new Error(
  1395. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  1396. )
  1397. }
  1398. return invokeTauriCommand({
  1399. __tauriModule: 'Window',
  1400. message: {
  1401. cmd: 'manage',
  1402. data: {
  1403. label: this.label,
  1404. cmd: {
  1405. type: 'setCursorPosition',
  1406. payload: {
  1407. type: position.type,
  1408. data: {
  1409. x: position.x,
  1410. y: position.y
  1411. }
  1412. }
  1413. }
  1414. }
  1415. }
  1416. })
  1417. }
  1418. /**
  1419. * Starts dragging the window.
  1420. * @example
  1421. * ```typescript
  1422. * import { appWindow } from '@tauri-apps/api/window';
  1423. * await appWindow.startDragging();
  1424. * ```
  1425. *
  1426. * @return A promise indicating the success or failure of the operation.
  1427. */
  1428. async startDragging(): Promise<void> {
  1429. return invokeTauriCommand({
  1430. __tauriModule: 'Window',
  1431. message: {
  1432. cmd: 'manage',
  1433. data: {
  1434. label: this.label,
  1435. cmd: {
  1436. type: 'startDragging'
  1437. }
  1438. }
  1439. }
  1440. })
  1441. }
  1442. }
  1443. /**
  1444. * Create new webview windows and get a handle to existing ones.
  1445. *
  1446. * Windows are identified by a *label* a unique identifier that can be used to reference it later.
  1447. * It may only contain alphanumeric characters `a-zA-Z` plus the following special characters `-`, `/`, `:` and `_`.
  1448. *
  1449. * @example
  1450. * ```typescript
  1451. * // loading embedded asset:
  1452. * const webview = new WebviewWindow('theUniqueLabel', {
  1453. * url: 'path/to/page.html'
  1454. * });
  1455. * // alternatively, load a remote URL:
  1456. * const webview = new WebviewWindow('theUniqueLabel', {
  1457. * url: 'https://github.com/tauri-apps/tauri'
  1458. * });
  1459. *
  1460. * webview.once('tauri://created', function () {
  1461. * // webview window successfully created
  1462. * });
  1463. * webview.once('tauri://error', function (e) {
  1464. * // an error happened creating the webview window
  1465. * });
  1466. *
  1467. * // emit an event to the backend
  1468. * await webview.emit("some event", "data");
  1469. * // listen to an event from the backend
  1470. * const unlisten = await webview.listen("event name", e => {});
  1471. * unlisten();
  1472. * ```
  1473. */
  1474. class WebviewWindow extends WindowManager {
  1475. /**
  1476. * Creates a new WebviewWindow.
  1477. * @example
  1478. * ```typescript
  1479. * import { WebviewWindow } from '@tauri-apps/api/window';
  1480. * const webview = new WebviewWindow('my-label', {
  1481. * url: 'https://github.com/tauri-apps/tauri'
  1482. * });
  1483. * webview.once('tauri://created', function () {
  1484. * // webview window successfully created
  1485. * });
  1486. * webview.once('tauri://error', function (e) {
  1487. * // an error happened creating the webview window
  1488. * });
  1489. * ```
  1490. *
  1491. * * @param label The unique webview window label. Must be alphanumeric: `a-zA-Z-/:_`.
  1492. * @returns The WebviewWindow instance to communicate with the webview.
  1493. */
  1494. constructor(label: WindowLabel, options: WindowOptions = {}) {
  1495. super(label)
  1496. // @ts-expect-error
  1497. if (!options?.skip) {
  1498. invokeTauriCommand({
  1499. __tauriModule: 'Window',
  1500. message: {
  1501. cmd: 'createWebview',
  1502. data: {
  1503. options: {
  1504. label,
  1505. ...options
  1506. }
  1507. }
  1508. }
  1509. })
  1510. .then(async () => this.emit('tauri://created'))
  1511. .catch(async (e: string) => this.emit('tauri://error', e))
  1512. }
  1513. }
  1514. /**
  1515. * Gets the WebviewWindow for the webview associated with the given label.
  1516. * @example
  1517. * ```typescript
  1518. * import { WebviewWindow } from '@tauri-apps/api/window';
  1519. * const mainWindow = WebviewWindow.getByLabel('main');
  1520. * ```
  1521. *
  1522. * @param label The webview window label.
  1523. * @returns The WebviewWindow instance to communicate with the webview or null if the webview doesn't exist.
  1524. */
  1525. static getByLabel(label: string): WebviewWindow | null {
  1526. if (getAll().some((w) => w.label === label)) {
  1527. // @ts-expect-error
  1528. return new WebviewWindow(label, { skip: true })
  1529. }
  1530. return null
  1531. }
  1532. }
  1533. /** The WebviewWindow for the current window. */
  1534. let appWindow: WebviewWindow
  1535. if ('__TAURI_METADATA__' in window) {
  1536. appWindow = new WebviewWindow(
  1537. window.__TAURI_METADATA__.__currentWindow.label,
  1538. {
  1539. // @ts-expect-error
  1540. skip: true
  1541. }
  1542. )
  1543. } else {
  1544. console.warn(
  1545. `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.`
  1546. )
  1547. appWindow = new WebviewWindow('main', {
  1548. // @ts-expect-error
  1549. skip: true
  1550. })
  1551. }
  1552. /** Configuration for the window to create. */
  1553. interface WindowOptions {
  1554. /**
  1555. * Remote URL or local file path to open.
  1556. *
  1557. * - URL such as `https://github.com/tauri-apps` is opened directly on a Tauri window.
  1558. * - data: URL such as `data:text/html,<html>...` is only supported with the `window-data-url` Cargo feature for the `tauri` dependency.
  1559. * - 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).
  1560. */
  1561. url?: string
  1562. /** Show window in the center of the screen.. */
  1563. center?: boolean
  1564. /** The initial vertical position. Only applies if `y` is also set. */
  1565. x?: number
  1566. /** The initial horizontal position. Only applies if `x` is also set. */
  1567. y?: number
  1568. /** The initial width. */
  1569. width?: number
  1570. /** The initial height. */
  1571. height?: number
  1572. /** The minimum width. Only applies if `minHeight` is also set. */
  1573. minWidth?: number
  1574. /** The minimum height. Only applies if `minWidth` is also set. */
  1575. minHeight?: number
  1576. /** The maximum width. Only applies if `maxHeight` is also set. */
  1577. maxWidth?: number
  1578. /** The maximum height. Only applies if `maxWidth` is also set. */
  1579. maxHeight?: number
  1580. /** Whether the window is resizable or not. */
  1581. resizable?: boolean
  1582. /** Window title. */
  1583. title?: string
  1584. /** Whether the window is in fullscreen mode or not. */
  1585. fullscreen?: boolean
  1586. /** Whether the window will be initially hidden or focused. */
  1587. focus?: boolean
  1588. /**
  1589. * Whether the window is transparent or not.
  1590. * Note that on `macOS` this requires the `macos-private-api` feature flag, enabled under `tauri.conf.json > tauri > macOSPrivateApi`.
  1591. * WARNING: Using private APIs on `macOS` prevents your application from being accepted to the `App Store`.
  1592. */
  1593. transparent?: boolean
  1594. /** Whether the window should be maximized upon creation or not. */
  1595. maximized?: boolean
  1596. /** Whether the window should be immediately visible upon creation or not. */
  1597. visible?: boolean
  1598. /** Whether the window should have borders and bars or not. */
  1599. decorations?: boolean
  1600. /** Whether the window should always be on top of other windows or not. */
  1601. alwaysOnTop?: boolean
  1602. /** Whether or not the window icon should be added to the taskbar. */
  1603. skipTaskbar?: boolean
  1604. /**
  1605. * Whether the file drop is enabled or not on the webview. By default it is enabled.
  1606. *
  1607. * Disabling it is required to use drag and drop on the frontend on Windows.
  1608. */
  1609. fileDropEnabled?: boolean
  1610. /**
  1611. * The initial window theme. Defaults to the system theme.
  1612. *
  1613. * Only implemented on Windows and macOS 10.14+.
  1614. */
  1615. theme?: Theme
  1616. }
  1617. /**
  1618. * Returns the monitor on which the window currently resides.
  1619. * Returns `null` if current monitor can't be detected.
  1620. * @example
  1621. * ```typescript
  1622. * import { currentMonitor } from '@tauri-apps/api/window';
  1623. * const monitor = currentMonitor();
  1624. * ```
  1625. */
  1626. async function currentMonitor(): Promise<Monitor | null> {
  1627. return invokeTauriCommand({
  1628. __tauriModule: 'Window',
  1629. message: {
  1630. cmd: 'manage',
  1631. data: {
  1632. cmd: {
  1633. type: 'currentMonitor'
  1634. }
  1635. }
  1636. }
  1637. })
  1638. }
  1639. /**
  1640. * Returns the primary monitor of the system.
  1641. * Returns `null` if it can't identify any monitor as a primary one.
  1642. * @example
  1643. * ```typescript
  1644. * import { primaryMonitor } from '@tauri-apps/api/window';
  1645. * const monitor = primaryMonitor();
  1646. * ```
  1647. */
  1648. async function primaryMonitor(): Promise<Monitor | null> {
  1649. return invokeTauriCommand({
  1650. __tauriModule: 'Window',
  1651. message: {
  1652. cmd: 'manage',
  1653. data: {
  1654. cmd: {
  1655. type: 'primaryMonitor'
  1656. }
  1657. }
  1658. }
  1659. })
  1660. }
  1661. /**
  1662. * Returns the list of all the monitors available on the system.
  1663. * @example
  1664. * ```typescript
  1665. * import { availableMonitors } from '@tauri-apps/api/window';
  1666. * const monitors = availableMonitors();
  1667. * ```
  1668. * */
  1669. async function availableMonitors(): Promise<Monitor[]> {
  1670. return invokeTauriCommand({
  1671. __tauriModule: 'Window',
  1672. message: {
  1673. cmd: 'manage',
  1674. data: {
  1675. cmd: {
  1676. type: 'availableMonitors'
  1677. }
  1678. }
  1679. }
  1680. })
  1681. }
  1682. export {
  1683. WebviewWindow,
  1684. WebviewWindowHandle,
  1685. WindowManager,
  1686. getCurrent,
  1687. getAll,
  1688. appWindow,
  1689. LogicalSize,
  1690. PhysicalSize,
  1691. LogicalPosition,
  1692. PhysicalPosition,
  1693. UserAttentionType,
  1694. currentMonitor,
  1695. primaryMonitor,
  1696. availableMonitors
  1697. }
  1698. export type { Theme, Monitor, WindowOptions }