window.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. /** Gets the window's current resizable state. */
  341. async isResizable(): Promise<boolean> {
  342. return invokeTauriCommand({
  343. __tauriModule: 'Window',
  344. message: {
  345. cmd: 'isResizable'
  346. }
  347. })
  348. }
  349. /** Gets the window's current visible state. */
  350. async isVisible(): Promise<boolean> {
  351. return invokeTauriCommand({
  352. __tauriModule: 'Window',
  353. message: {
  354. cmd: 'isVisible'
  355. }
  356. })
  357. }
  358. // Setters
  359. /**
  360. * Updates the window resizable flag.
  361. *
  362. * @param resizable
  363. * @returns A promise indicating the success or failure of the operation.
  364. */
  365. async setResizable(resizable: boolean): Promise<void> {
  366. return invokeTauriCommand({
  367. __tauriModule: 'Window',
  368. message: {
  369. cmd: 'setResizable',
  370. data: resizable
  371. }
  372. })
  373. }
  374. /**
  375. * Sets the window title.
  376. *
  377. * @param title The new title
  378. * @returns A promise indicating the success or failure of the operation.
  379. */
  380. async setTitle(title: string): Promise<void> {
  381. return invokeTauriCommand({
  382. __tauriModule: 'Window',
  383. message: {
  384. cmd: 'setTitle',
  385. data: title
  386. }
  387. })
  388. }
  389. /**
  390. * Maximizes the window.
  391. *
  392. * @returns A promise indicating the success or failure of the operation.
  393. */
  394. async maximize(): Promise<void> {
  395. return invokeTauriCommand({
  396. __tauriModule: 'Window',
  397. message: {
  398. cmd: 'maximize'
  399. }
  400. })
  401. }
  402. /**
  403. * Unmaximizes the window.
  404. *
  405. * @returns A promise indicating the success or failure of the operation.
  406. */
  407. async unmaximize(): Promise<void> {
  408. return invokeTauriCommand({
  409. __tauriModule: 'Window',
  410. message: {
  411. cmd: 'unmaximize'
  412. }
  413. })
  414. }
  415. /**
  416. * Minimizes the window.
  417. *
  418. * @returns A promise indicating the success or failure of the operation.
  419. */
  420. async minimize(): Promise<void> {
  421. return invokeTauriCommand({
  422. __tauriModule: 'Window',
  423. message: {
  424. cmd: 'minimize'
  425. }
  426. })
  427. }
  428. /**
  429. * Unminimizes the window.
  430. *
  431. * @returns A promise indicating the success or failure of the operation.
  432. */
  433. async unminimize(): Promise<void> {
  434. return invokeTauriCommand({
  435. __tauriModule: 'Window',
  436. message: {
  437. cmd: 'unminimize'
  438. }
  439. })
  440. }
  441. /**
  442. * Sets the window visibility to true.
  443. *
  444. * @returns A promise indicating the success or failure of the operation.
  445. */
  446. async show(): Promise<void> {
  447. return invokeTauriCommand({
  448. __tauriModule: 'Window',
  449. message: {
  450. cmd: 'show'
  451. }
  452. })
  453. }
  454. /**
  455. * Sets the window visibility to false.
  456. *
  457. * @returns A promise indicating the success or failure of the operation.
  458. */
  459. async hide(): Promise<void> {
  460. return invokeTauriCommand({
  461. __tauriModule: 'Window',
  462. message: {
  463. cmd: 'hide'
  464. }
  465. })
  466. }
  467. /**
  468. * Closes the window.
  469. *
  470. * @returns A promise indicating the success or failure of the operation.
  471. */
  472. async close(): Promise<void> {
  473. return invokeTauriCommand({
  474. __tauriModule: 'Window',
  475. message: {
  476. cmd: 'close'
  477. }
  478. })
  479. }
  480. /**
  481. * Whether the window should have borders and bars.
  482. *
  483. * @param decorations Whether the window should have borders and bars.
  484. * @returns A promise indicating the success or failure of the operation.
  485. */
  486. async setDecorations(decorations: boolean): Promise<void> {
  487. return invokeTauriCommand({
  488. __tauriModule: 'Window',
  489. message: {
  490. cmd: 'setDecorations',
  491. data: decorations
  492. }
  493. })
  494. }
  495. /**
  496. * Whether the window should always be on top of other windows.
  497. *
  498. * @param alwaysOnTop Whether the window should always be on top of other windows or not.
  499. * @returns A promise indicating the success or failure of the operation.
  500. */
  501. async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
  502. return invokeTauriCommand({
  503. __tauriModule: 'Window',
  504. message: {
  505. cmd: 'setAlwaysOnTop',
  506. data: alwaysOnTop
  507. }
  508. })
  509. }
  510. /**
  511. * Resizes the window.
  512. * @example
  513. * ```typescript
  514. * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
  515. * await appWindow.setSize(new LogicalSize(600, 500))
  516. * ```
  517. *
  518. * @param size The logical or physical size.
  519. * @returns A promise indicating the success or failure of the operation.
  520. */
  521. async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
  522. if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
  523. throw new Error(
  524. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  525. )
  526. }
  527. return invokeTauriCommand({
  528. __tauriModule: 'Window',
  529. message: {
  530. cmd: 'setSize',
  531. data: {
  532. type: size.type,
  533. data: {
  534. width: size.width,
  535. height: size.height
  536. }
  537. }
  538. }
  539. })
  540. }
  541. /**
  542. * Sets the window min size. If the `size` argument is not provided, the min size is unset.
  543. * @example
  544. * ```typescript
  545. * import { appWindow, PhysicalSize } from '@tauri-apps/api/window'
  546. * await appWindow.setMinSize(new PhysicalSize(600, 500))
  547. * ```
  548. *
  549. * @param size The logical or physical size.
  550. * @returns A promise indicating the success or failure of the operation.
  551. */
  552. async setMinSize(
  553. size: LogicalSize | PhysicalSize | undefined
  554. ): Promise<void> {
  555. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  556. throw new Error(
  557. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  558. )
  559. }
  560. return invokeTauriCommand({
  561. __tauriModule: 'Window',
  562. message: {
  563. cmd: 'setMinSize',
  564. data: size
  565. ? {
  566. type: size.type,
  567. data: {
  568. width: size.width,
  569. height: size.height
  570. }
  571. }
  572. : null
  573. }
  574. })
  575. }
  576. /**
  577. * Sets the window max size. If the `size` argument is undefined, the max size is unset.
  578. * @example
  579. * ```typescript
  580. * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
  581. * await appWindow.setMaxSize(new LogicalSize(600, 500))
  582. * ```
  583. *
  584. * @param size The logical or physical size.
  585. * @returns A promise indicating the success or failure of the operation.
  586. */
  587. async setMaxSize(
  588. size: LogicalSize | PhysicalSize | undefined
  589. ): Promise<void> {
  590. if (size && size.type !== 'Logical' && size.type !== 'Physical') {
  591. throw new Error(
  592. 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
  593. )
  594. }
  595. return invokeTauriCommand({
  596. __tauriModule: 'Window',
  597. message: {
  598. cmd: 'setMaxSize',
  599. data: size
  600. ? {
  601. type: size.type,
  602. data: {
  603. width: size.width,
  604. height: size.height
  605. }
  606. }
  607. : null
  608. }
  609. })
  610. }
  611. /**
  612. * Sets the window position.
  613. * @example
  614. * ```typescript
  615. * import { appWindow, LogicalPosition } from '@tauri-apps/api/window'
  616. * await appWindow.setPosition(new LogicalPosition(600, 500))
  617. * ```
  618. *
  619. * @param position The new position, in logical or physical pixels.
  620. * @returns A promise indicating the success or failure of the operation.
  621. */
  622. async setPosition(
  623. position: LogicalPosition | PhysicalPosition
  624. ): Promise<void> {
  625. if (
  626. !position ||
  627. (position.type !== 'Logical' && position.type !== 'Physical')
  628. ) {
  629. throw new Error(
  630. 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
  631. )
  632. }
  633. return invokeTauriCommand({
  634. __tauriModule: 'Window',
  635. message: {
  636. cmd: 'setPosition',
  637. data: {
  638. type: position.type,
  639. data: {
  640. x: position.x,
  641. y: position.y
  642. }
  643. }
  644. }
  645. })
  646. }
  647. /**
  648. * Sets the window fullscreen state.
  649. *
  650. * @param fullscreen Whether the window should go to fullscreen or not.
  651. * @returns A promise indicating the success or failure of the operation.
  652. */
  653. async setFullscreen(fullscreen: boolean): Promise<void> {
  654. return invokeTauriCommand({
  655. __tauriModule: 'Window',
  656. message: {
  657. cmd: 'setFullscreen',
  658. data: fullscreen
  659. }
  660. })
  661. }
  662. /**
  663. * Bring the window to front and focus.
  664. *
  665. * @returns A promise indicating the success or failure of the operation.
  666. */
  667. async setFocus(): Promise<void> {
  668. return invokeTauriCommand({
  669. __tauriModule: 'Window',
  670. message: {
  671. cmd: 'setFocus'
  672. }
  673. })
  674. }
  675. /**
  676. * Sets the window icon.
  677. *
  678. * @param icon Icon bytes or path to the icon file.
  679. * @returns A promise indicating the success or failure of the operation.
  680. */
  681. async setIcon(icon: string | number[]): Promise<void> {
  682. return invokeTauriCommand({
  683. __tauriModule: 'Window',
  684. message: {
  685. cmd: 'setIcon',
  686. data: {
  687. icon
  688. }
  689. }
  690. })
  691. }
  692. /**
  693. * Whether to show the window icon in the task bar or not.
  694. *
  695. * @param skip true to hide window icon, false to show it.
  696. * @returns A promise indicating the success or failure of the operation.
  697. */
  698. async setSkipTaskbar(skip: boolean): Promise<void> {
  699. return invokeTauriCommand({
  700. __tauriModule: 'Window',
  701. message: {
  702. cmd: 'setSkipTaskbar',
  703. data: skip
  704. }
  705. })
  706. }
  707. /**
  708. * Starts dragging the window.
  709. *
  710. * @return A promise indicating the success or failure of the operation.
  711. */
  712. async startDragging(): Promise<void> {
  713. return invokeTauriCommand({
  714. __tauriModule: 'Window',
  715. message: {
  716. cmd: 'startDragging'
  717. }
  718. })
  719. }
  720. }
  721. /** The manager for the current window. Allows you to manipulate the window object. */
  722. const appWindow = new WindowManager()
  723. /** Configuration for the window to create. */
  724. interface WindowOptions {
  725. /**
  726. * Remote URL or local file path to open, e.g. `https://github.com/tauri-apps` or `path/to/page.html`.
  727. */
  728. url?: string
  729. /** The initial vertical position. Only applies if `y` is also set. */
  730. x?: number
  731. /** The initial horizontal position. Only applies if `x` is also set. */
  732. y?: number
  733. /** The initial width. */
  734. width?: number
  735. /** The initial height. */
  736. height?: number
  737. /** The minimum width. Only applies if `minHeight` is also set. */
  738. minWidth?: number
  739. /** The minimum height. Only applies if `minWidth` is also set. */
  740. minHeight?: number
  741. /** The maximum width. Only applies if `maxHeight` is also set. */
  742. maxWidth?: number
  743. /** The maximum height. Only applies if `maxWidth` is also set. */
  744. maxHeight?: number
  745. /** Whether the window is resizable or not. */
  746. resizable?: boolean
  747. /** Window title. */
  748. title?: string
  749. /** Whether the window is in fullscreen mode or not. */
  750. fullscreen?: boolean
  751. /** Whether the window will be initially hidden or focused. */
  752. focus?: boolean
  753. /** Whether the window is transparent or not. */
  754. transparent?: boolean
  755. /** Whether the window should be maximized upon creation or not. */
  756. maximized?: boolean
  757. /** Whether the window should be immediately visible upon creation or not. */
  758. visible?: boolean
  759. /** Whether the window should have borders and bars or not. */
  760. decorations?: boolean
  761. /** Whether the window should always be on top of other windows or not. */
  762. alwaysOnTop?: boolean
  763. /** Whether or not the window icon should be added to the taskbar. */
  764. skipTaskbar?: boolean
  765. }
  766. /**
  767. * Returns the monitor on which the window currently resides.
  768. * Returns `null` if current monitor can't be detected.
  769. */
  770. async function currentMonitor(): Promise<Monitor | null> {
  771. return invokeTauriCommand({
  772. __tauriModule: 'Window',
  773. message: {
  774. cmd: 'currentMonitor'
  775. }
  776. })
  777. }
  778. /**
  779. * Returns the primary monitor of the system.
  780. * Returns `null` if it can't identify any monitor as a primary one.
  781. */
  782. async function primaryMonitor(): Promise<Monitor | null> {
  783. return invokeTauriCommand({
  784. __tauriModule: 'Window',
  785. message: {
  786. cmd: 'primaryMonitor'
  787. }
  788. })
  789. }
  790. /** Returns the list of all the monitors available on the system. */
  791. async function availableMonitors(): Promise<Monitor[]> {
  792. return invokeTauriCommand({
  793. __tauriModule: 'Window',
  794. message: {
  795. cmd: 'availableMonitors'
  796. }
  797. })
  798. }
  799. export {
  800. WebviewWindow,
  801. WebviewWindowHandle,
  802. WindowManager,
  803. getCurrent,
  804. getAll,
  805. appWindow,
  806. LogicalSize,
  807. PhysicalSize,
  808. LogicalPosition,
  809. PhysicalPosition,
  810. currentMonitor,
  811. primaryMonitor,
  812. availableMonitors
  813. }
  814. export type { Monitor, WindowOptions }