window.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. * }
  18. * }
  19. * }
  20. * }
  21. * ```
  22. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  23. * @packageDocumentation
  24. */
  25. import { invokeTauriCommand } from './helpers/tauri'
  26. import { EventCallback, UnlistenFn, listen, once } from './event'
  27. import { emit } from './helpers/event'
  28. /** Allows you to retrieve information about a given monitor. */
  29. interface Monitor {
  30. /** Human-readable name of the monitor */
  31. name: string | null
  32. /** The monitor's resolution. */
  33. size: PhysicalSize
  34. /** the Top-left corner position of the monitor relative to the larger full screen area. */
  35. position: PhysicalPosition
  36. /** The scale factor that can be used to map physical pixels to logical pixels. */
  37. scaleFactor: number
  38. }
  39. /** A size represented in logical pixels. */
  40. class LogicalSize {
  41. type = 'Logical'
  42. width: number
  43. height: number
  44. constructor(width: number, height: number) {
  45. this.width = width
  46. this.height = height
  47. }
  48. }
  49. /** A size represented in physical pixels. */
  50. class PhysicalSize {
  51. type = 'Physical'
  52. width: number
  53. height: number
  54. constructor(width: number, height: number) {
  55. this.width = width
  56. this.height = height
  57. }
  58. /** Converts the physical size to a logical one. */
  59. toLogical(scaleFactor: number): LogicalSize {
  60. return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
  61. }
  62. }
  63. /** A position represented in logical pixels. */
  64. class LogicalPosition {
  65. type = 'Logical'
  66. x: number
  67. y: number
  68. constructor(x: number, y: number) {
  69. this.x = x
  70. this.y = y
  71. }
  72. }
  73. /** A position represented in physical pixels. */
  74. class PhysicalPosition {
  75. type = 'Physical'
  76. x: number
  77. y: number
  78. constructor(x: number, y: number) {
  79. this.x = x
  80. this.y = y
  81. }
  82. /** Converts the physical position to a logical one. */
  83. toLogical(scaleFactor: number): LogicalPosition {
  84. return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
  85. }
  86. }
  87. /** @ignore */
  88. interface WindowDef {
  89. label: string
  90. }
  91. /** @ignore */
  92. declare global {
  93. interface Window {
  94. __TAURI__: {
  95. __windows: WindowDef[]
  96. __currentWindow: WindowDef
  97. }
  98. }
  99. }
  100. /**
  101. * Get a handle to the current webview window. Allows emitting and listening to events from the backend that are tied to the window.
  102. *
  103. * @return The current window handle.
  104. */
  105. function getCurrent(): WebviewWindowHandle {
  106. return new WebviewWindowHandle(window.__TAURI__.__currentWindow.label)
  107. }
  108. /**
  109. * Gets metadata for all available webview windows.
  110. *
  111. * @return The list of webview handles.
  112. */
  113. function getAll(): WindowDef[] {
  114. return window.__TAURI__.__windows
  115. }
  116. /** @ignore */
  117. // events that are emitted right here instead of by the created webview
  118. const localTauriEvents = ['tauri://created', 'tauri://error']
  119. /**
  120. * A webview window handle allows emitting and listening to events from the backend that are tied to the window.
  121. */
  122. class WebviewWindowHandle {
  123. /** Window label. */
  124. label: string
  125. /** Local event listeners. */
  126. listeners: { [key: string]: Array<EventCallback<any>> }
  127. constructor(label: string) {
  128. this.label = label
  129. // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  130. this.listeners = Object.create(null)
  131. }
  132. /**
  133. * Listen to an event emitted by the backend that is tied to the webview window.
  134. *
  135. * @param event Event name.
  136. * @param handler Event handler.
  137. * @returns A promise resolving to a function to unlisten to the event.
  138. */
  139. async listen<T>(
  140. event: string,
  141. handler: EventCallback<T>
  142. ): Promise<UnlistenFn> {
  143. if (this._handleTauriEvent(event, handler)) {
  144. return Promise.resolve(() => {
  145. // eslint-disable-next-line security/detect-object-injection
  146. const listeners = this.listeners[event]
  147. listeners.splice(listeners.indexOf(handler), 1)
  148. })
  149. }
  150. return listen(event, handler)
  151. }
  152. /**
  153. * Listen to an one-off event emitted by the backend that is tied to the webview window.
  154. *
  155. * @param event Event name.
  156. * @param handler Event handler.
  157. * @returns A promise resolving to a function to unlisten to the event.
  158. */
  159. async once<T>(event: string, handler: EventCallback<T>): Promise<UnlistenFn> {
  160. if (this._handleTauriEvent(event, handler)) {
  161. return Promise.resolve(() => {
  162. // eslint-disable-next-line security/detect-object-injection
  163. const listeners = this.listeners[event]
  164. listeners.splice(listeners.indexOf(handler), 1)
  165. })
  166. }
  167. return once(event, handler)
  168. }
  169. /**
  170. * Emits an event to the backend, tied to the webview window.
  171. *
  172. * @param event Event name.
  173. * @param payload Event payload.
  174. */
  175. async emit(event: string, payload?: string): Promise<void> {
  176. if (localTauriEvents.includes(event)) {
  177. // eslint-disable-next-line
  178. for (const handler of this.listeners[event] || []) {
  179. handler({ event, id: -1, payload })
  180. }
  181. return Promise.resolve()
  182. }
  183. return emit(event, this.label, payload)
  184. }
  185. _handleTauriEvent<T>(event: string, handler: EventCallback<T>): boolean {
  186. if (localTauriEvents.includes(event)) {
  187. if (!(event in this.listeners)) {
  188. // eslint-disable-next-line
  189. this.listeners[event] = [handler]
  190. } else {
  191. // eslint-disable-next-line
  192. this.listeners[event].push(handler)
  193. }
  194. return true
  195. }
  196. return false
  197. }
  198. }
  199. /**
  200. * Create new webview windows and get a handle to existing ones.
  201. * @example
  202. * ```typescript
  203. * // loading embedded asset:
  204. * const webview = new WebviewWindow('theUniqueLabel', {
  205. * url: 'path/to/page.html'
  206. * })
  207. * // alternatively, load a remote URL:
  208. * const webview = new WebviewWindow('theUniqueLabel', {
  209. * url: 'https://github.com/tauri-apps/tauri'
  210. * })
  211. *
  212. * webview.once('tauri://created', function () {
  213. * // webview window successfully created
  214. * })
  215. * webview.once('tauri://error', function (e) {
  216. * // an error happened creating the webview window
  217. * })
  218. *
  219. * // emit an event to the backend
  220. * await webview.emit("some event", "data")
  221. * // listen to an event from the backend
  222. * const unlisten = await webview.listen("event name", e => {})
  223. * unlisten()
  224. * ```
  225. */
  226. class WebviewWindow extends WebviewWindowHandle {
  227. constructor(label: string, options: WindowOptions = {}) {
  228. super(label)
  229. invokeTauriCommand({
  230. __tauriModule: 'Window',
  231. message: {
  232. cmd: 'createWebview',
  233. data: {
  234. options: {
  235. label,
  236. ...options
  237. }
  238. }
  239. }
  240. })
  241. .then(async () => this.emit('tauri://created'))
  242. .catch(async (e) => this.emit('tauri://error', e))
  243. }
  244. /**
  245. * Gets the WebviewWindow handle for the webview associated with the given label.
  246. *
  247. * @param label The webview window label.
  248. * @returns The handle to communicate with the webview or null if the webview doesn't exist.
  249. */
  250. static getByLabel(label: string): WebviewWindowHandle | null {
  251. if (getAll().some((w) => w.label === label)) {
  252. return new WebviewWindowHandle(label)
  253. }
  254. return null
  255. }
  256. }
  257. /**
  258. * Manage the current window object.
  259. */
  260. class WindowManager {
  261. // Getters
  262. /** The scale factor that can be used to map physical pixels to logical pixels. */
  263. async scaleFactor(): Promise<number> {
  264. return invokeTauriCommand({
  265. __tauriModule: 'Window',
  266. message: {
  267. cmd: 'scaleFactor'
  268. }
  269. })
  270. }
  271. /** The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop. */
  272. async innerPosition(): Promise<PhysicalPosition> {
  273. return invokeTauriCommand({
  274. __tauriModule: 'Window',
  275. message: {
  276. cmd: 'innerPosition'
  277. }
  278. })
  279. }
  280. /** The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop. */
  281. async outerPosition(): Promise<PhysicalPosition> {
  282. return invokeTauriCommand({
  283. __tauriModule: 'Window',
  284. message: {
  285. cmd: 'outerPosition'
  286. }
  287. })
  288. }
  289. /**
  290. * The physical size of the window's client area.
  291. * The client area is the content of the window, excluding the title bar and borders.
  292. */
  293. async innerSize(): Promise<PhysicalSize> {
  294. return invokeTauriCommand({
  295. __tauriModule: 'Window',
  296. message: {
  297. cmd: 'innerSize'
  298. }
  299. })
  300. }
  301. /**
  302. * The physical size of the entire window.
  303. * These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  304. */
  305. async outerSize(): Promise<PhysicalSize> {
  306. return invokeTauriCommand({
  307. __tauriModule: 'Window',
  308. message: {
  309. cmd: 'outerSize'
  310. }
  311. })
  312. }
  313. /** Gets the window's current fullscreen state. */
  314. async isFullscreen(): Promise<boolean> {
  315. return invokeTauriCommand({
  316. __tauriModule: 'Window',
  317. message: {
  318. cmd: 'isFullscreen'
  319. }
  320. })
  321. }
  322. /** Gets the window's current maximized state. */
  323. async isMaximized(): Promise<boolean> {
  324. return invokeTauriCommand({
  325. __tauriModule: 'Window',
  326. message: {
  327. cmd: 'isMaximized'
  328. }
  329. })
  330. }
  331. /** Gets the window's current decorated state. */
  332. async isDecorated(): Promise<boolean> {
  333. return invokeTauriCommand({
  334. __tauriModule: 'Window',
  335. message: {
  336. cmd: 'isDecorated'
  337. }
  338. })
  339. }
  340. // Setters
  341. /**
  342. * Updates the window resizable flag.
  343. *
  344. * @param resizable
  345. * @returns A promise indicating the success or failure of the operation.
  346. */
  347. async setResizable(resizable: boolean): Promise<void> {
  348. return invokeTauriCommand({
  349. __tauriModule: 'Window',
  350. message: {
  351. cmd: 'setResizable',
  352. data: resizable
  353. }
  354. })
  355. }
  356. /**
  357. * Sets the window title.
  358. *
  359. * @param title The new title
  360. * @returns A promise indicating the success or failure of the operation.
  361. */
  362. async setTitle(title: string): Promise<void> {
  363. return invokeTauriCommand({
  364. __tauriModule: 'Window',
  365. message: {
  366. cmd: 'setTitle',
  367. data: title
  368. }
  369. })
  370. }
  371. /**
  372. * Maximizes the window.
  373. *
  374. * @returns A promise indicating the success or failure of the operation.
  375. */
  376. async maximize(): Promise<void> {
  377. return invokeTauriCommand({
  378. __tauriModule: 'Window',
  379. message: {
  380. cmd: 'maximize'
  381. }
  382. })
  383. }
  384. /**
  385. * Unmaximizes the window.
  386. *
  387. * @returns A promise indicating the success or failure of the operation.
  388. */
  389. async unmaximize(): Promise<void> {
  390. return invokeTauriCommand({
  391. __tauriModule: 'Window',
  392. message: {
  393. cmd: 'unmaximize'
  394. }
  395. })
  396. }
  397. /**
  398. * Minimizes the window.
  399. *
  400. * @returns A promise indicating the success or failure of the operation.
  401. */
  402. async minimize(): Promise<void> {
  403. return invokeTauriCommand({
  404. __tauriModule: 'Window',
  405. message: {
  406. cmd: 'minimize'
  407. }
  408. })
  409. }
  410. /**
  411. * Unminimizes the window.
  412. *
  413. * @returns A promise indicating the success or failure of the operation.
  414. */
  415. async unminimize(): Promise<void> {
  416. return invokeTauriCommand({
  417. __tauriModule: 'Window',
  418. message: {
  419. cmd: 'unminimize'
  420. }
  421. })
  422. }
  423. /**
  424. * Sets the window visibility to true.
  425. *
  426. * @returns A promise indicating the success or failure of the operation.
  427. */
  428. async show(): Promise<void> {
  429. return invokeTauriCommand({
  430. __tauriModule: 'Window',
  431. message: {
  432. cmd: 'show'
  433. }
  434. })
  435. }
  436. /**
  437. * Sets the window visibility to false.
  438. *
  439. * @returns A promise indicating the success or failure of the operation.
  440. */
  441. async hide(): Promise<void> {
  442. return invokeTauriCommand({
  443. __tauriModule: 'Window',
  444. message: {
  445. cmd: 'hide'
  446. }
  447. })
  448. }
  449. /**
  450. * Closes the window.
  451. *
  452. * @returns A promise indicating the success or failure of the operation.
  453. */
  454. async close(): Promise<void> {
  455. return invokeTauriCommand({
  456. __tauriModule: 'Window',
  457. message: {
  458. cmd: 'close'
  459. }
  460. })
  461. }
  462. /**
  463. * Whether the window should have borders and bars.
  464. *
  465. * @param decorations Whether the window should have borders and bars.
  466. * @returns A promise indicating the success or failure of the operation.
  467. */
  468. async setDecorations(decorations: boolean): Promise<void> {
  469. return invokeTauriCommand({
  470. __tauriModule: 'Window',
  471. message: {
  472. cmd: 'setDecorations',
  473. data: decorations
  474. }
  475. })
  476. }
  477. /**
  478. * Whether the window should always be on top of other windows.
  479. *
  480. * @param alwaysOnTop Whether the window should always be on top of other windows or not.
  481. * @returns A promise indicating the success or failure of the operation.
  482. */
  483. async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
  484. return invokeTauriCommand({
  485. __tauriModule: 'Window',
  486. message: {
  487. cmd: 'setAlwaysOnTop',
  488. data: alwaysOnTop
  489. }
  490. })
  491. }
  492. /**
  493. * Resizes the window.
  494. * @example
  495. * ```typescript
  496. * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
  497. * await appWindow.setSize(new LogicalSize(600, 500))
  498. * ```
  499. *
  500. * @param size The logical or physical size.
  501. * @returns A promise indicating the success or failure of the operation.
  502. */
  503. async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
  504. if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
  505. throw new Error(
  506. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  507. )
  508. }
  509. return invokeTauriCommand({
  510. __tauriModule: 'Window',
  511. message: {
  512. cmd: 'setSize',
  513. data: {
  514. type: size.type,
  515. data: {
  516. width: size.width,
  517. height: size.height
  518. }
  519. }
  520. }
  521. })
  522. }
  523. /**
  524. * Sets the window min size. If the `size` argument is not provided, the min size is unset.
  525. * @example
  526. * ```typescript
  527. * import { appWindow, PhysicalSize } from '@tauri-apps/api/window'
  528. * await appWindow.setMinSize(new PhysicalSize(600, 500))
  529. * ```
  530. *
  531. * @param size The logical or physical size.
  532. * @returns A promise indicating the success or failure of the operation.
  533. */
  534. async setMinSize(
  535. size: LogicalSize | PhysicalSize | undefined
  536. ): Promise<void> {
  537. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  538. throw new Error(
  539. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  540. )
  541. }
  542. return invokeTauriCommand({
  543. __tauriModule: 'Window',
  544. message: {
  545. cmd: 'setMinSize',
  546. data: size
  547. ? {
  548. type: size.type,
  549. data: {
  550. width: size.width,
  551. height: size.height
  552. }
  553. }
  554. : null
  555. }
  556. })
  557. }
  558. /**
  559. * Sets the window max size. If the `size` argument is undefined, the max size is unset.
  560. * @example
  561. * ```typescript
  562. * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
  563. * await appWindow.setMaxSize(new LogicalSize(600, 500))
  564. * ```
  565. *
  566. * @param size The logical or physical size.
  567. * @returns A promise indicating the success or failure of the operation.
  568. */
  569. async setMaxSize(
  570. size: LogicalSize | PhysicalSize | undefined
  571. ): Promise<void> {
  572. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  573. throw new Error(
  574. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  575. )
  576. }
  577. return invokeTauriCommand({
  578. __tauriModule: 'Window',
  579. message: {
  580. cmd: 'setMaxSize',
  581. data: size
  582. ? {
  583. type: size.type,
  584. data: {
  585. width: size.width,
  586. height: size.height
  587. }
  588. }
  589. : null
  590. }
  591. })
  592. }
  593. /**
  594. * Sets the window position.
  595. * @example
  596. * ```typescript
  597. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window'
  598. * await appWindow.setPosition(new LogicalPosition(600, 500))
  599. * ```
  600. *
  601. * @param position The new position, in logical or physical pixels.
  602. * @returns A promise indicating the success or failure of the operation.
  603. */
  604. async setPosition(
  605. position: LogicalPosition | PhysicalPosition
  606. ): Promise<void> {
  607. if (
  608. !position ||
  609. (position.type !== 'Logical' && position.type !== 'Physical')
  610. ) {
  611. throw new Error(
  612. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  613. )
  614. }
  615. return invokeTauriCommand({
  616. __tauriModule: 'Window',
  617. message: {
  618. cmd: 'setPosition',
  619. data: {
  620. type: position.type,
  621. data: {
  622. x: position.x,
  623. y: position.y
  624. }
  625. }
  626. }
  627. })
  628. }
  629. /**
  630. * Sets the window fullscreen state.
  631. *
  632. * @param fullscreen Whether the window should go to fullscreen or not.
  633. * @returns A promise indicating the success or failure of the operation.
  634. */
  635. async setFullscreen(fullscreen: boolean): Promise<void> {
  636. return invokeTauriCommand({
  637. __tauriModule: 'Window',
  638. message: {
  639. cmd: 'setFullscreen',
  640. data: fullscreen
  641. }
  642. })
  643. }
  644. /**
  645. * Bring the window to front and focus.
  646. *
  647. * @returns A promise indicating the success or failure of the operation.
  648. */
  649. async setFocus(): Promise<void> {
  650. return invokeTauriCommand({
  651. __tauriModule: 'Window',
  652. message: {
  653. cmd: 'setFocus'
  654. }
  655. })
  656. }
  657. /**
  658. * Sets the window icon.
  659. *
  660. * @param icon Icon bytes or path to the icon file.
  661. * @returns A promise indicating the success or failure of the operation.
  662. */
  663. async setIcon(icon: string | number[]): Promise<void> {
  664. return invokeTauriCommand({
  665. __tauriModule: 'Window',
  666. message: {
  667. cmd: 'setIcon',
  668. data: {
  669. icon
  670. }
  671. }
  672. })
  673. }
  674. /**
  675. * Starts dragging the window.
  676. *
  677. * @return A promise indicating the success or failure of the operation.
  678. */
  679. async startDragging(): Promise<void> {
  680. return invokeTauriCommand({
  681. __tauriModule: 'Window',
  682. message: {
  683. cmd: 'startDragging'
  684. }
  685. })
  686. }
  687. }
  688. /** The manager for the current window. Allows you to manipulate the window object. */
  689. const appWindow = new WindowManager()
  690. /** Configuration for the window to create. */
  691. interface WindowOptions {
  692. /**
  693. * Remote URL or local file path to open, e.g. `https://github.com/tauri-apps` or `path/to/page.html`.
  694. */
  695. url?: string
  696. /** The initial vertical position. Only applies if `y` is also set. */
  697. x?: number
  698. /** The initial horizontal position. Only applies if `x` is also set. */
  699. y?: number
  700. /** The initial width. */
  701. width?: number
  702. /** The initial height. */
  703. height?: number
  704. /** The minimum width. Only applies if `minHeight` is also set. */
  705. minWidth?: number
  706. /** The minimum height. Only applies if `minWidth` is also set. */
  707. minHeight?: number
  708. /** The maximum width. Only applies if `maxHeight` is also set. */
  709. maxWidth?: number
  710. /** The maximum height. Only applies if `maxWidth` is also set. */
  711. maxHeight?: number
  712. /** Whether the window is resizable or not. */
  713. resizable?: boolean
  714. /** Window title. */
  715. title?: string
  716. /** Whether the window is in fullscreen mode or not. */
  717. fullscreen?: boolean
  718. /** Whether the window is transparent or not. */
  719. transparent?: boolean
  720. /** Whether the window should be maximized upon creation or not. */
  721. maximized?: boolean
  722. /** Whether the window should be immediately visible upon creation or not. */
  723. visible?: boolean
  724. /** Whether the window should have borders and bars or not. */
  725. decorations?: boolean
  726. /** Whether the window should always be on top of other windows or not. */
  727. alwaysOnTop?: boolean
  728. }
  729. /**
  730. * Returns the monitor on which the window currently resides.
  731. * Returns `null` if current monitor can't be detected.
  732. */
  733. async function currentMonitor(): Promise<Monitor | null> {
  734. return invokeTauriCommand({
  735. __tauriModule: 'Window',
  736. message: {
  737. cmd: 'currentMonitor'
  738. }
  739. })
  740. }
  741. /**
  742. * Returns the primary monitor of the system.
  743. * Returns `null` if it can't identify any monitor as a primary one.
  744. */
  745. async function primaryMonitor(): Promise<Monitor | null> {
  746. return invokeTauriCommand({
  747. __tauriModule: 'Window',
  748. message: {
  749. cmd: 'primaryMonitor'
  750. }
  751. })
  752. }
  753. /** Returns the list of all the monitors available on the system. */
  754. async function availableMonitors(): Promise<Monitor[]> {
  755. return invokeTauriCommand({
  756. __tauriModule: 'Window',
  757. message: {
  758. cmd: 'availableMonitors'
  759. }
  760. })
  761. }
  762. export {
  763. WebviewWindow,
  764. WebviewWindowHandle,
  765. WindowManager,
  766. getCurrent,
  767. getAll,
  768. appWindow,
  769. LogicalSize,
  770. PhysicalSize,
  771. LogicalPosition,
  772. PhysicalPosition,
  773. currentMonitor,
  774. primaryMonitor,
  775. availableMonitors
  776. }
  777. export type { Monitor, WindowOptions }