123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- /**
- * Provides APIs to create windows, communicate with other windows and manipulate the current window.
- *
- * This package is also accessible with `window.__TAURI__.window` when `tauri.conf.json > build > withGlobalTauri` is set to true.
- *
- * The APIs must be allowlisted on `tauri.conf.json`:
- * ```json
- * {
- * "tauri": {
- * "allowlist": {
- * "window": {
- * "all": true, // enable all window APIs
- * "create": true // enable window creation
- * }
- * }
- * }
- * }
- * ```
- * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
- * @packageDocumentation
- */
- import { invokeTauriCommand } from './helpers/tauri'
- import { EventCallback, UnlistenFn, listen, once } from './event'
- import { emit } from './helpers/event'
- /** Allows you to retrieve information about a given monitor. */
- interface Monitor {
- /** Human-readable name of the monitor */
- name: string | null
- /** The monitor's resolution. */
- size: PhysicalSize
- /** the Top-left corner position of the monitor relative to the larger full screen area. */
- position: PhysicalPosition
- /** The scale factor that can be used to map physical pixels to logical pixels. */
- scaleFactor: number
- }
- /** A size represented in logical pixels. */
- class LogicalSize {
- type = 'Logical'
- width: number
- height: number
- constructor(width: number, height: number) {
- this.width = width
- this.height = height
- }
- }
- /** A size represented in physical pixels. */
- class PhysicalSize {
- type = 'Physical'
- width: number
- height: number
- constructor(width: number, height: number) {
- this.width = width
- this.height = height
- }
- /** Converts the physical size to a logical one. */
- toLogical(scaleFactor: number): LogicalSize {
- return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
- }
- }
- /** A position represented in logical pixels. */
- class LogicalPosition {
- type = 'Logical'
- x: number
- y: number
- constructor(x: number, y: number) {
- this.x = x
- this.y = y
- }
- }
- /** A position represented in physical pixels. */
- class PhysicalPosition {
- type = 'Physical'
- x: number
- y: number
- constructor(x: number, y: number) {
- this.x = x
- this.y = y
- }
- /** Converts the physical position to a logical one. */
- toLogical(scaleFactor: number): LogicalPosition {
- return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
- }
- }
- /** @ignore */
- interface WindowDef {
- label: string
- }
- /** @ignore */
- declare global {
- interface Window {
- __TAURI__: {
- __windows: WindowDef[]
- __currentWindow: WindowDef
- }
- }
- }
- /**
- * Get a handle to the current webview window. Allows emitting and listening to events from the backend that are tied to the window.
- *
- * @return The current window handle.
- */
- function getCurrent(): WebviewWindowHandle {
- return new WebviewWindowHandle(window.__TAURI__.__currentWindow.label)
- }
- /**
- * Gets metadata for all available webview windows.
- *
- * @return The list of webview handles.
- */
- function getAll(): WindowDef[] {
- return window.__TAURI__.__windows
- }
- /** @ignore */
- // events that are emitted right here instead of by the created webview
- const localTauriEvents = ['tauri://created', 'tauri://error']
- /**
- * A webview window handle allows emitting and listening to events from the backend that are tied to the window.
- */
- class WebviewWindowHandle {
- /** Window label. */
- label: string
- /** Local event listeners. */
- listeners: { [key: string]: Array<EventCallback<any>> }
- constructor(label: string) {
- this.label = label
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
- this.listeners = Object.create(null)
- }
- /**
- * Listen to an event emitted by the backend that is tied to the webview window.
- *
- * @param event Event name.
- * @param handler Event handler.
- * @returns A promise resolving to a function to unlisten to the event.
- */
- async listen<T>(
- event: string,
- handler: EventCallback<T>
- ): Promise<UnlistenFn> {
- if (this._handleTauriEvent(event, handler)) {
- return Promise.resolve(() => {
- // eslint-disable-next-line security/detect-object-injection
- const listeners = this.listeners[event]
- listeners.splice(listeners.indexOf(handler), 1)
- })
- }
- return listen(event, handler)
- }
- /**
- * Listen to an one-off event emitted by the backend that is tied to the webview window.
- *
- * @param event Event name.
- * @param handler Event handler.
- * @returns A promise resolving to a function to unlisten to the event.
- */
- async once<T>(event: string, handler: EventCallback<T>): Promise<UnlistenFn> {
- if (this._handleTauriEvent(event, handler)) {
- return Promise.resolve(() => {
- // eslint-disable-next-line security/detect-object-injection
- const listeners = this.listeners[event]
- listeners.splice(listeners.indexOf(handler), 1)
- })
- }
- return once(event, handler)
- }
- /**
- * Emits an event to the backend, tied to the webview window.
- *
- * @param event Event name.
- * @param payload Event payload.
- */
- async emit(event: string, payload?: string): Promise<void> {
- if (localTauriEvents.includes(event)) {
- // eslint-disable-next-line
- for (const handler of this.listeners[event] || []) {
- handler({ event, id: -1, payload })
- }
- return Promise.resolve()
- }
- return emit(event, this.label, payload)
- }
- _handleTauriEvent<T>(event: string, handler: EventCallback<T>): boolean {
- if (localTauriEvents.includes(event)) {
- if (!(event in this.listeners)) {
- // eslint-disable-next-line
- this.listeners[event] = [handler]
- } else {
- // eslint-disable-next-line
- this.listeners[event].push(handler)
- }
- return true
- }
- return false
- }
- }
- /**
- * Create new webview windows and get a handle to existing ones.
- * @example
- * ```typescript
- * // loading embedded asset:
- * const webview = new WebviewWindow('theUniqueLabel', {
- * url: 'path/to/page.html'
- * })
- * // alternatively, load a remote URL:
- * const webview = new WebviewWindow('theUniqueLabel', {
- * url: 'https://github.com/tauri-apps/tauri'
- * })
- *
- * webview.once('tauri://created', function () {
- * // webview window successfully created
- * })
- * webview.once('tauri://error', function (e) {
- * // an error happened creating the webview window
- * })
- *
- * // emit an event to the backend
- * await webview.emit("some event", "data")
- * // listen to an event from the backend
- * const unlisten = await webview.listen("event name", e => {})
- * unlisten()
- * ```
- */
- class WebviewWindow extends WebviewWindowHandle {
- constructor(label: string, options: WindowOptions = {}) {
- super(label)
- invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'createWebview',
- data: {
- options: {
- label,
- ...options
- }
- }
- }
- })
- .then(async () => this.emit('tauri://created'))
- .catch(async (e) => this.emit('tauri://error', e))
- }
- /**
- * Gets the WebviewWindow handle for the webview associated with the given label.
- *
- * @param label The webview window label.
- * @returns The handle to communicate with the webview or null if the webview doesn't exist.
- */
- static getByLabel(label: string): WebviewWindowHandle | null {
- if (getAll().some((w) => w.label === label)) {
- return new WebviewWindowHandle(label)
- }
- return null
- }
- }
- /**
- * Manage the current window object.
- */
- class WindowManager {
- // Getters
- /** The scale factor that can be used to map physical pixels to logical pixels. */
- async scaleFactor(): Promise<number> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'scaleFactor'
- }
- })
- }
- /** The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop. */
- async innerPosition(): Promise<PhysicalPosition> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'innerPosition'
- }
- })
- }
- /** The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop. */
- async outerPosition(): Promise<PhysicalPosition> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'outerPosition'
- }
- })
- }
- /**
- * The physical size of the window's client area.
- * The client area is the content of the window, excluding the title bar and borders.
- */
- async innerSize(): Promise<PhysicalSize> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'innerSize'
- }
- })
- }
- /**
- * The physical size of the entire window.
- * These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
- */
- async outerSize(): Promise<PhysicalSize> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'outerSize'
- }
- })
- }
- /** Gets the window's current fullscreen state. */
- async isFullscreen(): Promise<boolean> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'isFullscreen'
- }
- })
- }
- /** Gets the window's current maximized state. */
- async isMaximized(): Promise<boolean> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'isMaximized'
- }
- })
- }
- /** Gets the window's current decorated state. */
- async isDecorated(): Promise<boolean> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'isDecorated'
- }
- })
- }
- /** Gets the window's current resizable state. */
- async isResizable(): Promise<boolean> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'isResizable'
- }
- })
- }
- /** Gets the window's current visible state. */
- async isVisible(): Promise<boolean> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'isVisible'
- }
- })
- }
- // Setters
- /**
- * Updates the window resizable flag.
- *
- * @param resizable
- * @returns A promise indicating the success or failure of the operation.
- */
- async setResizable(resizable: boolean): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setResizable',
- data: resizable
- }
- })
- }
- /**
- * Sets the window title.
- *
- * @param title The new title
- * @returns A promise indicating the success or failure of the operation.
- */
- async setTitle(title: string): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setTitle',
- data: title
- }
- })
- }
- /**
- * Maximizes the window.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async maximize(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'maximize'
- }
- })
- }
- /**
- * Unmaximizes the window.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async unmaximize(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'unmaximize'
- }
- })
- }
- /**
- * Minimizes the window.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async minimize(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'minimize'
- }
- })
- }
- /**
- * Unminimizes the window.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async unminimize(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'unminimize'
- }
- })
- }
- /**
- * Sets the window visibility to true.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async show(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'show'
- }
- })
- }
- /**
- * Sets the window visibility to false.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async hide(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'hide'
- }
- })
- }
- /**
- * Closes the window.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async close(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'close'
- }
- })
- }
- /**
- * Whether the window should have borders and bars.
- *
- * @param decorations Whether the window should have borders and bars.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setDecorations(decorations: boolean): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setDecorations',
- data: decorations
- }
- })
- }
- /**
- * Whether the window should always be on top of other windows.
- *
- * @param alwaysOnTop Whether the window should always be on top of other windows or not.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setAlwaysOnTop(alwaysOnTop: boolean): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setAlwaysOnTop',
- data: alwaysOnTop
- }
- })
- }
- /**
- * Resizes the window.
- * @example
- * ```typescript
- * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
- * await appWindow.setSize(new LogicalSize(600, 500))
- * ```
- *
- * @param size The logical or physical size.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
- if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
- throw new Error(
- 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
- )
- }
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setSize',
- data: {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- }
- })
- }
- /**
- * Sets the window min size. If the `size` argument is not provided, the min size is unset.
- * @example
- * ```typescript
- * import { appWindow, PhysicalSize } from '@tauri-apps/api/window'
- * await appWindow.setMinSize(new PhysicalSize(600, 500))
- * ```
- *
- * @param size The logical or physical size.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMinSize(
- size: LogicalSize | PhysicalSize | undefined
- ): Promise<void> {
- if (size && size.type !== 'Logical' && size.type !== 'Physical') {
- throw new Error(
- 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
- )
- }
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setMinSize',
- data: size
- ? {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- : null
- }
- })
- }
- /**
- * Sets the window max size. If the `size` argument is undefined, the max size is unset.
- * @example
- * ```typescript
- * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
- * await appWindow.setMaxSize(new LogicalSize(600, 500))
- * ```
- *
- * @param size The logical or physical size.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMaxSize(
- size: LogicalSize | PhysicalSize | undefined
- ): Promise<void> {
- if (size && size.type !== 'Logical' && size.type !== 'Physical') {
- throw new Error(
- 'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
- )
- }
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setMaxSize',
- data: size
- ? {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- : null
- }
- })
- }
- /**
- * Sets the window position.
- * @example
- * ```typescript
- * import { appWindow, LogicalPosition } from '@tauri-apps/api/window'
- * await appWindow.setPosition(new LogicalPosition(600, 500))
- * ```
- *
- * @param position The new position, in logical or physical pixels.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setPosition(
- position: LogicalPosition | PhysicalPosition
- ): Promise<void> {
- if (
- !position ||
- (position.type !== 'Logical' && position.type !== 'Physical')
- ) {
- throw new Error(
- 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
- )
- }
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setPosition',
- data: {
- type: position.type,
- data: {
- x: position.x,
- y: position.y
- }
- }
- }
- })
- }
- /**
- * Sets the window fullscreen state.
- *
- * @param fullscreen Whether the window should go to fullscreen or not.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setFullscreen(fullscreen: boolean): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setFullscreen',
- data: fullscreen
- }
- })
- }
- /**
- * Bring the window to front and focus.
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async setFocus(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setFocus'
- }
- })
- }
- /**
- * Sets the window icon.
- *
- * @param icon Icon bytes or path to the icon file.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setIcon(icon: string | number[]): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setIcon',
- data: {
- icon
- }
- }
- })
- }
- /**
- * Whether to show the window icon in the task bar or not.
- *
- * @param skip true to hide window icon, false to show it.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setSkipTaskbar(skip: boolean): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'setSkipTaskbar',
- data: skip
- }
- })
- }
- /**
- * Starts dragging the window.
- *
- * @return A promise indicating the success or failure of the operation.
- */
- async startDragging(): Promise<void> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'startDragging'
- }
- })
- }
- }
- /** The manager for the current window. Allows you to manipulate the window object. */
- const appWindow = new WindowManager()
- /** Configuration for the window to create. */
- interface WindowOptions {
- /**
- * Remote URL or local file path to open, e.g. `https://github.com/tauri-apps` or `path/to/page.html`.
- */
- url?: string
- /** The initial vertical position. Only applies if `y` is also set. */
- x?: number
- /** The initial horizontal position. Only applies if `x` is also set. */
- y?: number
- /** The initial width. */
- width?: number
- /** The initial height. */
- height?: number
- /** The minimum width. Only applies if `minHeight` is also set. */
- minWidth?: number
- /** The minimum height. Only applies if `minWidth` is also set. */
- minHeight?: number
- /** The maximum width. Only applies if `maxHeight` is also set. */
- maxWidth?: number
- /** The maximum height. Only applies if `maxWidth` is also set. */
- maxHeight?: number
- /** Whether the window is resizable or not. */
- resizable?: boolean
- /** Window title. */
- title?: string
- /** Whether the window is in fullscreen mode or not. */
- fullscreen?: boolean
- /** Whether the window will be initially hidden or focused. */
- focus?: boolean
- /** Whether the window is transparent or not. */
- transparent?: boolean
- /** Whether the window should be maximized upon creation or not. */
- maximized?: boolean
- /** Whether the window should be immediately visible upon creation or not. */
- visible?: boolean
- /** Whether the window should have borders and bars or not. */
- decorations?: boolean
- /** Whether the window should always be on top of other windows or not. */
- alwaysOnTop?: boolean
- /** Whether or not the window icon should be added to the taskbar. */
- skipTaskbar?: boolean
- }
- /**
- * Returns the monitor on which the window currently resides.
- * Returns `null` if current monitor can't be detected.
- */
- async function currentMonitor(): Promise<Monitor | null> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'currentMonitor'
- }
- })
- }
- /**
- * Returns the primary monitor of the system.
- * Returns `null` if it can't identify any monitor as a primary one.
- */
- async function primaryMonitor(): Promise<Monitor | null> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'primaryMonitor'
- }
- })
- }
- /** Returns the list of all the monitors available on the system. */
- async function availableMonitors(): Promise<Monitor[]> {
- return invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'availableMonitors'
- }
- })
- }
- export {
- WebviewWindow,
- WebviewWindowHandle,
- WindowManager,
- getCurrent,
- getAll,
- appWindow,
- LogicalSize,
- PhysicalSize,
- LogicalPosition,
- PhysicalPosition,
- currentMonitor,
- primaryMonitor,
- availableMonitors
- }
- export type { Monitor, WindowOptions }
|