| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171 |
- 'use strict';
- var tauri = require('./helpers/tauri.cjs');
- var event = require('./helpers/event.cjs');
- var event$1 = require('./event.cjs');
- // Copyright 2019-2023 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 [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
- *
- * The APIs must be added to [`tauri.allowlist.window`](https://tauri.app/v1/api/config/#allowlistconfig.window) in `tauri.conf.json`:
- * ```json
- * {
- * "tauri": {
- * "allowlist": {
- * "window": {
- * "all": true, // enable all window APIs
- * "create": true, // enable window creation
- * "center": true,
- * "requestUserAttention": true,
- * "setResizable": true,
- * "setMaximizable": true,
- * "setMinimizable": true,
- * "setClosable": true,
- * "setTitle": true,
- * "maximize": true,
- * "unmaximize": true,
- * "minimize": true,
- * "unminimize": true,
- * "show": true,
- * "hide": true,
- * "close": true,
- * "setDecorations": true,
- * "setAlwaysOnTop": true,
- * "setContentProtected": true,
- * "setSize": true,
- * "setMinSize": true,
- * "setMaxSize": true,
- * "setPosition": true,
- * "setFullscreen": true,
- * "setFocus": true,
- * "setIcon": true,
- * "setSkipTaskbar": true,
- * "setCursorGrab": true,
- * "setCursorVisible": true,
- * "setCursorIcon": true,
- * "setCursorPosition": true,
- * "setIgnoreCursorEvents": true,
- * "startDragging": true,
- * "print": true
- * }
- * }
- * }
- * }
- * ```
- * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
- *
- * ## Window events
- *
- * Events can be listened to using `appWindow.listen`:
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * appWindow.listen("my-window-event", ({ event, payload }) => { });
- * ```
- *
- * @module
- */
- /**
- * A size represented in logical pixels.
- *
- * @since 1.0.0
- */
- class LogicalSize {
- constructor(width, height) {
- this.type = 'Logical';
- this.width = width;
- this.height = height;
- }
- }
- /**
- * A size represented in physical pixels.
- *
- * @since 1.0.0
- */
- class PhysicalSize {
- constructor(width, height) {
- this.type = 'Physical';
- this.width = width;
- this.height = height;
- }
- /**
- * Converts the physical size to a logical one.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const factor = await appWindow.scaleFactor();
- * const size = await appWindow.innerSize();
- * const logical = size.toLogical(factor);
- * ```
- * */
- toLogical(scaleFactor) {
- return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor);
- }
- }
- /**
- * A position represented in logical pixels.
- *
- * @since 1.0.0
- */
- class LogicalPosition {
- constructor(x, y) {
- this.type = 'Logical';
- this.x = x;
- this.y = y;
- }
- }
- /**
- * A position represented in physical pixels.
- *
- * @since 1.0.0
- */
- class PhysicalPosition {
- constructor(x, y) {
- this.type = 'Physical';
- this.x = x;
- this.y = y;
- }
- /**
- * Converts the physical position to a logical one.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const factor = await appWindow.scaleFactor();
- * const position = await appWindow.innerPosition();
- * const logical = position.toLogical(factor);
- * ```
- * */
- toLogical(scaleFactor) {
- return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor);
- }
- }
- /**
- * Attention type to request on a window.
- *
- * @since 1.0.0
- */
- exports.UserAttentionType = void 0;
- (function (UserAttentionType) {
- /**
- * #### Platform-specific
- * - **macOS:** Bounces the dock icon until the application is in focus.
- * - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
- */
- UserAttentionType[UserAttentionType["Critical"] = 1] = "Critical";
- /**
- * #### Platform-specific
- * - **macOS:** Bounces the dock icon once.
- * - **Windows:** Flashes the taskbar button until the application is in focus.
- */
- UserAttentionType[UserAttentionType["Informational"] = 2] = "Informational";
- })(exports.UserAttentionType || (exports.UserAttentionType = {}));
- /**
- * Get an instance of `WebviewWindow` for the current webview window.
- *
- * @since 1.0.0
- */
- function getCurrent() {
- return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
- // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
- skip: true
- });
- }
- /**
- * Gets a list of instances of `WebviewWindow` for all available webview windows.
- *
- * @since 1.0.0
- */
- function getAll() {
- return window.__TAURI_METADATA__.__windows.map((w) => new WebviewWindow(w.label, {
- // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
- skip: true
- }));
- }
- /** @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.
- *
- * @ignore
- * @since 1.0.0
- */
- class WebviewWindowHandle {
- constructor(label) {
- 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 or webview.
- * The event must either be a global event or an event targetting this window.
- *
- * See {@link WebviewWindow.emit | `emit`} for more information.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const unlisten = await appWindow.listen<string>('state-changed', (event) => {
- * console.log(`Got error: ${payload}`);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param handler Event handler.
- * @returns A promise resolving to a function to unlisten to the event.
- */
- async listen(event$1, handler) {
- if (this._handleTauriEvent(event$1, handler)) {
- return Promise.resolve(() => {
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
- const listeners = this.listeners[event$1];
- listeners.splice(listeners.indexOf(handler), 1);
- });
- }
- return event.listen(event$1, this.label, handler);
- }
- /**
- * Listen to an one-off event.
- * See {@link WebviewWindow.listen | `listen`} for more information.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const unlisten = await appWindow.once<null>('initialized', (event) => {
- * console.log(`Window initialized!`);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param handler Event handler.
- * @returns A promise resolving to a function to unlisten to the event.
- */
- async once(event$1, handler) {
- if (this._handleTauriEvent(event$1, handler)) {
- return Promise.resolve(() => {
- // eslint-disable-next-line security/detect-object-injection
- const listeners = this.listeners[event$1];
- listeners.splice(listeners.indexOf(handler), 1);
- });
- }
- return event.once(event$1, this.label, handler);
- }
- /**
- * Emits an event to the backend and all Tauri windows.
- * The event will have this window's {@link WebviewWindow.label | label} as {@link Event.windowLabel | source window label}.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.emit('window-loaded', { loggedIn: true, token: 'authToken' });
- * ```
- *
- * This function can also be used to communicate between windows:
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.listen('sync-data', (event) => { });
- *
- * // on another window...
- * import { WebviewWindow } from '@tauri-apps/api/window';
- * const otherWindow = WebviewWindow.getByLabel('other')
- * await otherWindow.emit('sync-data');
- * ```
- *
- * Global listeners are also triggered:
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * import { listen } from '@tauri-apps/api/event';
- * await listen('ping', (event) => { });
- *
- * await appWindow.emit('ping');
- * ```
- *
- * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
- * @param payload Event payload.
- */
- async emit(event$1, payload) {
- if (localTauriEvents.includes(event$1)) {
- // eslint-disable-next-line
- for (const handler of this.listeners[event$1] || []) {
- handler({ event: event$1, id: -1, windowLabel: this.label, payload });
- }
- return Promise.resolve();
- }
- return event.emit(event$1, this.label, payload);
- }
- /** @ignore */
- _handleTauriEvent(event, handler) {
- 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;
- }
- }
- /**
- * Manage the current window object.
- *
- * @ignore
- * @since 1.0.0
- */
- class WindowManager extends WebviewWindowHandle {
- // Getters
- /**
- * The scale factor that can be used to map physical pixels to logical pixels.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const factor = await appWindow.scaleFactor();
- * ```
- *
- * @returns The window's monitor scale factor.
- * */
- async scaleFactor() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: '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.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const position = await appWindow.innerPosition();
- * ```
- *
- * @returns The window's inner position.
- * */
- async innerPosition() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'innerPosition'
- }
- }
- }
- }).then(({ x, y }) => new PhysicalPosition(x, y));
- }
- /**
- * The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const position = await appWindow.outerPosition();
- * ```
- *
- * @returns The window's outer position.
- * */
- async outerPosition() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'outerPosition'
- }
- }
- }
- }).then(({ x, y }) => new PhysicalPosition(x, y));
- }
- /**
- * The physical size of the window's client area.
- * The client area is the content of the window, excluding the title bar and borders.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const size = await appWindow.innerSize();
- * ```
- *
- * @returns The window's inner size.
- */
- async innerSize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'innerSize'
- }
- }
- }
- }).then(({ width, height }) => new PhysicalSize(width, height));
- }
- /**
- * 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.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const size = await appWindow.outerSize();
- * ```
- *
- * @returns The window's outer size.
- */
- async outerSize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'outerSize'
- }
- }
- }
- }).then(({ width, height }) => new PhysicalSize(width, height));
- }
- /**
- * Gets the window's current fullscreen state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const fullscreen = await appWindow.isFullscreen();
- * ```
- *
- * @returns Whether the window is in fullscreen mode or not.
- * */
- async isFullscreen() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isFullscreen'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current minimized state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const minimized = await appWindow.isMinimized();
- * ```
- *
- * @since 1.3.0
- * */
- async isMinimized() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isMinimized'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current maximized state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const maximized = await appWindow.isMaximized();
- * ```
- *
- * @returns Whether the window is maximized or not.
- * */
- async isMaximized() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isMaximized'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current focus state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const focused = await appWindow.isFocused();
- * ```
- *
- * @returns Whether the window is focused or not.
- *
- * @since 1.4
- * */
- async isFocused() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isFocused'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current decorated state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const decorated = await appWindow.isDecorated();
- * ```
- *
- * @returns Whether the window is decorated or not.
- * */
- async isDecorated() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isDecorated'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current resizable state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const resizable = await appWindow.isResizable();
- * ```
- *
- * @returns Whether the window is resizable or not.
- * */
- async isResizable() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isResizable'
- }
- }
- }
- });
- }
- /**
- * Gets the window’s native maximize button state.
- *
- * #### Platform-specific
- *
- * - **Linux / iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const maximizable = await appWindow.isMaximizable();
- * ```
- *
- * @returns Whether the window's native maximize button is enabled or not.
- * */
- async isMaximizable() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isMaximizable'
- }
- }
- }
- });
- }
- /**
- * Gets the window’s native minimize button state.
- *
- * #### Platform-specific
- *
- * - **Linux / iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const minimizable = await appWindow.isMinimizable();
- * ```
- *
- * @returns Whether the window's native minimize button is enabled or not.
- * */
- async isMinimizable() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isMinimizable'
- }
- }
- }
- });
- }
- /**
- * Gets the window’s native close button state.
- *
- * #### Platform-specific
- *
- * - **Linux / iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const closable = await appWindow.isClosable();
- * ```
- *
- * @returns Whether the window's native close button is enabled or not.
- * */
- async isClosable() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isClosable'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current visible state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const visible = await appWindow.isVisible();
- * ```
- *
- * @returns Whether the window is visible or not.
- * */
- async isVisible() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'isVisible'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current title.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const title = await appWindow.title();
- * ```
- *
- * @since 1.3.0
- * */
- async title() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'title'
- }
- }
- }
- });
- }
- /**
- * Gets the window's current theme.
- *
- * #### Platform-specific
- *
- * - **macOS:** Theme was introduced on macOS 10.14. Returns `light` on macOS 10.13 and below.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * const theme = await appWindow.theme();
- * ```
- *
- * @returns The window theme.
- * */
- async theme() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'theme'
- }
- }
- }
- });
- }
- // Setters
- /**
- * Centers the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.center();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async center() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'center'
- }
- }
- }
- });
- }
- /**
- * Requests user attention to the window, this has no effect if the application
- * is already focused. How requesting for user attention manifests is platform dependent,
- * see `UserAttentionType` for details.
- *
- * Providing `null` will unset the request for user attention. Unsetting the request for
- * user attention might not be done automatically by the WM when the window receives input.
- *
- * #### Platform-specific
- *
- * - **macOS:** `null` has no effect.
- * - **Linux:** Urgency levels have the same effect.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.requestUserAttention();
- * ```
- *
- * @param requestType
- * @returns A promise indicating the success or failure of the operation.
- */
- async requestUserAttention(requestType) {
- let requestType_ = null;
- if (requestType) {
- if (requestType === exports.UserAttentionType.Critical) {
- requestType_ = { type: 'Critical' };
- }
- else {
- requestType_ = { type: 'Informational' };
- }
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'requestUserAttention',
- payload: requestType_
- }
- }
- }
- });
- }
- /**
- * Updates the window resizable flag.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setResizable(false);
- * ```
- *
- * @param resizable
- * @returns A promise indicating the success or failure of the operation.
- */
- async setResizable(resizable) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setResizable',
- payload: resizable
- }
- }
- }
- });
- }
- /**
- * Sets whether the window's native maximize button is enabled or not.
- * If resizable is set to false, this setting is ignored.
- *
- * #### Platform-specific
- *
- * - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
- * - **Linux / iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setMaximizable(false);
- * ```
- *
- * @param maximizable
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMaximizable(maximizable) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setMaximizable',
- payload: maximizable
- }
- }
- }
- });
- }
- /**
- * Sets whether the window's native minimize button is enabled or not.
- *
- * #### Platform-specific
- *
- * - **Linux / iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setMinimizable(false);
- * ```
- *
- * @param minimizable
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMinimizable(minimizable) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setMinimizable',
- payload: minimizable
- }
- }
- }
- });
- }
- /**
- * Sets whether the window's native close button is enabled or not.
- *
- * #### Platform-specific
- *
- * - **Linux:** GTK+ will do its best to convince the window manager not to show a close button. Depending on the system, this function may not have any effect when called on a window that is already visible
- * - **iOS / Android:** Unsupported.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setClosable(false);
- * ```
- *
- * @param closable
- * @returns A promise indicating the success or failure of the operation.
- */
- async setClosable(closable) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setClosable',
- payload: closable
- }
- }
- }
- });
- }
- /**
- * Sets the window title.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setTitle('Tauri');
- * ```
- *
- * @param title The new title
- * @returns A promise indicating the success or failure of the operation.
- */
- async setTitle(title) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setTitle',
- payload: title
- }
- }
- }
- });
- }
- /**
- * Maximizes the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.maximize();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async maximize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'maximize'
- }
- }
- }
- });
- }
- /**
- * Unmaximizes the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.unmaximize();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async unmaximize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'unmaximize'
- }
- }
- }
- });
- }
- /**
- * Toggles the window maximized state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.toggleMaximize();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async toggleMaximize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'toggleMaximize'
- }
- }
- }
- });
- }
- /**
- * Minimizes the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.minimize();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async minimize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'minimize'
- }
- }
- }
- });
- }
- /**
- * Unminimizes the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.unminimize();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async unminimize() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'unminimize'
- }
- }
- }
- });
- }
- /**
- * Sets the window visibility to true.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.show();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async show() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'show'
- }
- }
- }
- });
- }
- /**
- * Sets the window visibility to false.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.hide();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async hide() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'hide'
- }
- }
- }
- });
- }
- /**
- * Closes the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.close();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async close() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'close'
- }
- }
- }
- });
- }
- /**
- * Whether the window should have borders and bars.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setDecorations(false);
- * ```
- *
- * @param decorations Whether the window should have borders and bars.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setDecorations(decorations) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setDecorations',
- payload: decorations
- }
- }
- }
- });
- }
- /**
- * Whether the window should always be on top of other windows.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setAlwaysOnTop(true);
- * ```
- *
- * @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) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setAlwaysOnTop',
- payload: alwaysOnTop
- }
- }
- }
- });
- }
- /**
- * Prevents the window contents from being captured by other apps.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setContentProtected(true);
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- *
- * @since 1.2.0
- */
- async setContentProtected(protected_) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setContentProtected',
- payload: protected_
- }
- }
- }
- });
- }
- /**
- * Resizes the window with a new inner size.
- * @example
- * ```typescript
- * import { appWindow, LogicalSize } from '@tauri-apps/api/window';
- * await appWindow.setSize(new LogicalSize(600, 500));
- * ```
- *
- * @param size The logical or physical inner size.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setSize(size) {
- if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
- throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setSize',
- payload: {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- }
- }
- }
- });
- }
- /**
- * Sets the window minimum inner size. If the `size` argument is not provided, the constraint 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 inner size, or `null` to unset the constraint.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMinSize(size) {
- if (size && size.type !== 'Logical' && size.type !== 'Physical') {
- throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setMinSize',
- payload: size
- ? {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- : null
- }
- }
- }
- });
- }
- /**
- * Sets the window maximum inner size. If the `size` argument is undefined, the constraint 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 inner size, or `null` to unset the constraint.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setMaxSize(size) {
- if (size && size.type !== 'Logical' && size.type !== 'Physical') {
- throw new Error('the `size` argument must be either a LogicalSize or a PhysicalSize instance');
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setMaxSize',
- payload: size
- ? {
- type: size.type,
- data: {
- width: size.width,
- height: size.height
- }
- }
- : null
- }
- }
- }
- });
- }
- /**
- * Sets the window outer 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) {
- if (!position ||
- (position.type !== 'Logical' && position.type !== 'Physical')) {
- throw new Error('the `position` argument must be either a LogicalPosition or a PhysicalPosition instance');
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setPosition',
- payload: {
- type: position.type,
- data: {
- x: position.x,
- y: position.y
- }
- }
- }
- }
- }
- });
- }
- /**
- * Sets the window fullscreen state.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setFullscreen(true);
- * ```
- *
- * @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) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setFullscreen',
- payload: fullscreen
- }
- }
- }
- });
- }
- /**
- * Bring the window to front and focus.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setFocus();
- * ```
- *
- * @returns A promise indicating the success or failure of the operation.
- */
- async setFocus() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setFocus'
- }
- }
- }
- });
- }
- /**
- * Sets the window icon.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setIcon('/tauri/awesome.png');
- * ```
- *
- * Note that you need the `icon-ico` or `icon-png` Cargo features to use this API.
- * To enable it, change your Cargo.toml file:
- * ```toml
- * [dependencies]
- * tauri = { version = "...", features = ["...", "icon-png"] }
- * ```
- *
- * @param icon Icon bytes or path to the icon file.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setIcon(icon) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setIcon',
- payload: {
- // correctly serialize Uint8Arrays
- icon: typeof icon === 'string' ? icon : Array.from(icon)
- }
- }
- }
- }
- });
- }
- /**
- * Whether the window icon should be hidden from the taskbar or not.
- *
- * #### Platform-specific
- *
- * - **macOS:** Unsupported.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setSkipTaskbar(true);
- * ```
- *
- * @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) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setSkipTaskbar',
- payload: skip
- }
- }
- }
- });
- }
- /**
- * Grabs the cursor, preventing it from leaving the window.
- *
- * There's no guarantee that the cursor will be hidden. You should
- * hide it by yourself if you want so.
- *
- * #### Platform-specific
- *
- * - **Linux:** Unsupported.
- * - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setCursorGrab(true);
- * ```
- *
- * @param grab `true` to grab the cursor icon, `false` to release it.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setCursorGrab(grab) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setCursorGrab',
- payload: grab
- }
- }
- }
- });
- }
- /**
- * Modifies the cursor's visibility.
- *
- * #### Platform-specific
- *
- * - **Windows:** The cursor is only hidden within the confines of the window.
- * - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor is
- * outside of the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setCursorVisible(false);
- * ```
- *
- * @param visible If `false`, this will hide the cursor. If `true`, this will show the cursor.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setCursorVisible(visible) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setCursorVisible',
- payload: visible
- }
- }
- }
- });
- }
- /**
- * Modifies the cursor icon of the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setCursorIcon('help');
- * ```
- *
- * @param icon The new cursor icon.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setCursorIcon(icon) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setCursorIcon',
- payload: icon
- }
- }
- }
- });
- }
- /**
- * Changes the position of the cursor in window coordinates.
- * @example
- * ```typescript
- * import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
- * await appWindow.setCursorPosition(new LogicalPosition(600, 300));
- * ```
- *
- * @param position The new cursor position.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setCursorPosition(position) {
- if (!position ||
- (position.type !== 'Logical' && position.type !== 'Physical')) {
- throw new Error('the `position` argument must be either a LogicalPosition or a PhysicalPosition instance');
- }
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setCursorPosition',
- payload: {
- type: position.type,
- data: {
- x: position.x,
- y: position.y
- }
- }
- }
- }
- }
- });
- }
- /**
- * Changes the cursor events behavior.
- *
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.setIgnoreCursorEvents(true);
- * ```
- *
- * @param ignore `true` to ignore the cursor events; `false` to process them as usual.
- * @returns A promise indicating the success or failure of the operation.
- */
- async setIgnoreCursorEvents(ignore) {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'setIgnoreCursorEvents',
- payload: ignore
- }
- }
- }
- });
- }
- /**
- * Starts dragging the window.
- * @example
- * ```typescript
- * import { appWindow } from '@tauri-apps/api/window';
- * await appWindow.startDragging();
- * ```
- *
- * @return A promise indicating the success or failure of the operation.
- */
- async startDragging() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- label: this.label,
- cmd: {
- type: 'startDragging'
- }
- }
- }
- });
- }
- // Listeners
- /**
- * Listen to window resize.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onResized(({ payload: size }) => {
- * console.log('Window resized', size);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onResized(handler) {
- return this.listen(event$1.TauriEvent.WINDOW_RESIZED, (e) => {
- e.payload = mapPhysicalSize(e.payload);
- handler(e);
- });
- }
- /**
- * Listen to window move.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onMoved(({ payload: position }) => {
- * console.log('Window moved', position);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onMoved(handler) {
- return this.listen(event$1.TauriEvent.WINDOW_MOVED, (e) => {
- e.payload = mapPhysicalPosition(e.payload);
- handler(e);
- });
- }
- /**
- * Listen to window close requested. Emitted when the user requests to closes the window.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * import { confirm } from '@tauri-apps/api/dialog';
- * const unlisten = await appWindow.onCloseRequested(async (event) => {
- * const confirmed = await confirm('Are you sure?');
- * if (!confirmed) {
- * // user did not confirm closing the window; let's prevent it
- * event.preventDefault();
- * }
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- /* eslint-disable @typescript-eslint/promise-function-async */
- async onCloseRequested(handler) {
- return this.listen(event$1.TauriEvent.WINDOW_CLOSE_REQUESTED, (event) => {
- const evt = new CloseRequestedEvent(event);
- void Promise.resolve(handler(evt)).then(() => {
- if (!evt.isPreventDefault()) {
- return this.close();
- }
- });
- });
- }
- /* eslint-enable */
- /**
- * Listen to window focus change.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onFocusChanged(({ payload: focused }) => {
- * console.log('Focus changed, window is focused? ' + focused);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onFocusChanged(handler) {
- const unlistenFocus = await this.listen(event$1.TauriEvent.WINDOW_FOCUS, (event) => {
- handler({ ...event, payload: true });
- });
- const unlistenBlur = await this.listen(event$1.TauriEvent.WINDOW_BLUR, (event) => {
- handler({ ...event, payload: false });
- });
- return () => {
- unlistenFocus();
- unlistenBlur();
- };
- }
- /**
- * Listen to window scale change. Emitted when the window's scale factor has changed.
- * The following user actions can cause DPI changes:
- * - Changing the display's resolution.
- * - Changing the display's scale factor (e.g. in Control Panel on Windows).
- * - Moving the window to a display with a different scale factor.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onScaleChanged(({ payload }) => {
- * console.log('Scale changed', payload.scaleFactor, payload.size);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onScaleChanged(handler) {
- return this.listen(event$1.TauriEvent.WINDOW_SCALE_FACTOR_CHANGED, handler);
- }
- /**
- * Listen to the window menu item click. The payload is the item id.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onMenuClicked(({ payload: menuId }) => {
- * console.log('Menu clicked: ' + menuId);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onMenuClicked(handler) {
- return this.listen(event$1.TauriEvent.MENU, handler);
- }
- /**
- * Listen to a file drop event.
- * The listener is triggered when the user hovers the selected files on the window,
- * drops the files or cancels the operation.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onFileDropEvent((event) => {
- * if (event.payload.type === 'hover') {
- * console.log('User hovering', event.payload.paths);
- * } else if (event.payload.type === 'drop') {
- * console.log('User dropped', event.payload.paths);
- * } else {
- * console.log('File drop cancelled');
- * }
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onFileDropEvent(handler) {
- const unlistenFileDrop = await this.listen(event$1.TauriEvent.WINDOW_FILE_DROP, (event) => {
- handler({ ...event, payload: { type: 'drop', paths: event.payload } });
- });
- const unlistenFileHover = await this.listen(event$1.TauriEvent.WINDOW_FILE_DROP_HOVER, (event) => {
- handler({ ...event, payload: { type: 'hover', paths: event.payload } });
- });
- const unlistenCancel = await this.listen(event$1.TauriEvent.WINDOW_FILE_DROP_CANCELLED, (event) => {
- handler({ ...event, payload: { type: 'cancel' } });
- });
- return () => {
- unlistenFileDrop();
- unlistenFileHover();
- unlistenCancel();
- };
- }
- /**
- * Listen to the system theme change.
- *
- * @example
- * ```typescript
- * import { appWindow } from "@tauri-apps/api/window";
- * const unlisten = await appWindow.onThemeChanged(({ payload: theme }) => {
- * console.log('New theme: ' + theme);
- * });
- *
- * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
- * unlisten();
- * ```
- *
- * @returns A promise resolving to a function to unlisten to the event.
- * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
- *
- * @since 1.0.2
- */
- async onThemeChanged(handler) {
- return this.listen(event$1.TauriEvent.WINDOW_THEME_CHANGED, handler);
- }
- }
- /**
- * @since 1.0.2
- */
- class CloseRequestedEvent {
- constructor(event) {
- this._preventDefault = false;
- this.event = event.event;
- this.windowLabel = event.windowLabel;
- this.id = event.id;
- }
- preventDefault() {
- this._preventDefault = true;
- }
- isPreventDefault() {
- return this._preventDefault;
- }
- }
- /**
- * Create new webview windows and get a handle to existing ones.
- *
- * Windows are identified by a *label* a unique identifier that can be used to reference it later.
- * It may only contain alphanumeric characters `a-zA-Z` plus the following special characters `-`, `/`, `:` and `_`.
- *
- * @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();
- * ```
- *
- * @since 1.0.2
- */
- class WebviewWindow extends WindowManager {
- /**
- * Creates a new WebviewWindow.
- * @example
- * ```typescript
- * import { WebviewWindow } from '@tauri-apps/api/window';
- * const webview = new WebviewWindow('my-label', {
- * 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
- * });
- * ```
- *
- * * @param label The unique webview window label. Must be alphanumeric: `a-zA-Z-/:_`.
- * @returns The WebviewWindow instance to communicate with the webview.
- */
- constructor(label, options = {}) {
- super(label);
- // @ts-expect-error `skip` is not a public API so it is not defined in WindowOptions
- if (!(options === null || options === void 0 ? void 0 : options.skip)) {
- tauri.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 for the webview associated with the given label.
- * @example
- * ```typescript
- * import { WebviewWindow } from '@tauri-apps/api/window';
- * const mainWindow = WebviewWindow.getByLabel('main');
- * ```
- *
- * @param label The webview window label.
- * @returns The WebviewWindow instance to communicate with the webview or null if the webview doesn't exist.
- */
- static getByLabel(label) {
- if (getAll().some((w) => w.label === label)) {
- // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
- return new WebviewWindow(label, { skip: true });
- }
- return null;
- }
- /**
- * Gets the focused window.
- * @example
- * ```typescript
- * import { WebviewWindow } from '@tauri-apps/api/window';
- * const focusedWindow = WebviewWindow.getFocusedWindow();
- * ```
- *
- * @returns The WebviewWindow instance to communicate with the webview or `undefined` if there is not any focused window.
- *
- * @since 1.4
- */
- static async getFocusedWindow() {
- for (const w of getAll()) {
- if (await w.isFocused()) {
- return w;
- }
- }
- return null;
- }
- }
- /** The WebviewWindow for the current window. */
- exports.appWindow = void 0;
- if ('__TAURI_METADATA__' in window) {
- exports.appWindow = new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
- // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
- skip: true
- });
- }
- else {
- console.warn(`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.`);
- exports.appWindow = new WebviewWindow('main', {
- // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
- skip: true
- });
- }
- function mapMonitor(m) {
- return m === null
- ? null
- : {
- name: m.name,
- scaleFactor: m.scaleFactor,
- position: mapPhysicalPosition(m.position),
- size: mapPhysicalSize(m.size)
- };
- }
- function mapPhysicalPosition(m) {
- return new PhysicalPosition(m.x, m.y);
- }
- function mapPhysicalSize(m) {
- return new PhysicalSize(m.width, m.height);
- }
- /**
- * Returns the monitor on which the window currently resides.
- * Returns `null` if current monitor can't be detected.
- * @example
- * ```typescript
- * import { currentMonitor } from '@tauri-apps/api/window';
- * const monitor = currentMonitor();
- * ```
- *
- * @since 1.0.0
- */
- async function currentMonitor() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- cmd: {
- type: 'currentMonitor'
- }
- }
- }
- }).then(mapMonitor);
- }
- /**
- * Returns the primary monitor of the system.
- * Returns `null` if it can't identify any monitor as a primary one.
- * @example
- * ```typescript
- * import { primaryMonitor } from '@tauri-apps/api/window';
- * const monitor = primaryMonitor();
- * ```
- *
- * @since 1.0.0
- */
- async function primaryMonitor() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- cmd: {
- type: 'primaryMonitor'
- }
- }
- }
- }).then(mapMonitor);
- }
- /**
- * Returns the list of all the monitors available on the system.
- * @example
- * ```typescript
- * import { availableMonitors } from '@tauri-apps/api/window';
- * const monitors = availableMonitors();
- * ```
- *
- * @since 1.0.0
- */
- async function availableMonitors() {
- return tauri.invokeTauriCommand({
- __tauriModule: 'Window',
- message: {
- cmd: 'manage',
- data: {
- cmd: {
- type: 'availableMonitors'
- }
- }
- }
- }).then((ms) => ms.map(mapMonitor));
- }
- exports.CloseRequestedEvent = CloseRequestedEvent;
- exports.LogicalPosition = LogicalPosition;
- exports.LogicalSize = LogicalSize;
- exports.PhysicalPosition = PhysicalPosition;
- exports.PhysicalSize = PhysicalSize;
- exports.WebviewWindow = WebviewWindow;
- exports.WebviewWindowHandle = WebviewWindowHandle;
- exports.WindowManager = WindowManager;
- exports.availableMonitors = availableMonitors;
- exports.currentMonitor = currentMonitor;
- exports.getAll = getAll;
- exports.getCurrent = getCurrent;
- exports.primaryMonitor = primaryMonitor;
|