diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index d62be73b..74926819 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -3604,7 +3604,6 @@ checksum = "fd1ba337640d60c3e96bc6f0638a939b9c9a7f2c316a1598c279828b3d1dc8c5" [[package]] name = "tauri" version = "2.0.0-alpha.8" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "anyhow", "bytes", @@ -3660,7 +3659,6 @@ dependencies = [ [[package]] name = "tauri-build" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "anyhow", "cargo_toml", @@ -3681,7 +3679,6 @@ dependencies = [ [[package]] name = "tauri-codegen" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "base64 0.21.0", "brotli", @@ -3706,7 +3703,6 @@ dependencies = [ [[package]] name = "tauri-macros" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "heck 0.4.1", "proc-macro2", @@ -3907,7 +3903,6 @@ dependencies = [ [[package]] name = "tauri-runtime" version = "0.13.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "gtk", "http", @@ -3928,7 +3923,6 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "cocoa", "gtk", @@ -3948,7 +3942,6 @@ dependencies = [ [[package]] name = "tauri-utils" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=next#9122e27ed863a5cb2bf13dc0dd0433c7387e141b" dependencies = [ "aes-gcm", "brotli", diff --git a/examples/api/src-tauri/Cargo.toml b/examples/api/src-tauri/Cargo.toml index fe8c70fe..1cfd4e57 100644 --- a/examples/api/src-tauri/Cargo.toml +++ b/examples/api/src-tauri/Cargo.toml @@ -31,8 +31,14 @@ tauri-plugin-updater = { path = "../../../plugins/updater" } tauri-plugin-window = { path = "../../../plugins/window" } [patch.crates-io] -tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" } -tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "next" } +#tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" } +#tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "next" } +tauri = { path = "../../../../tauri/core/tauri" } +tauri-build = { path = "../../../../tauri/core/tauri-build" } + +[patch.'https://github.com/tauri-apps/tauri'] +tauri = { path = "../../../../tauri/core/tauri" } +tauri-build = { path = "../../../../tauri/core/tauri-build" } [dependencies.tauri] version = "2.0.0-alpha.8" diff --git a/examples/api/src/App.svelte b/examples/api/src/App.svelte index e7662e0f..3bd71a0d 100644 --- a/examples/api/src/App.svelte +++ b/examples/api/src/App.svelte @@ -20,7 +20,6 @@ import App from "./views/App.svelte"; import { onMount } from "svelte"; - import { listen } from "@tauri-apps/api/event"; import { ask } from "tauri-plugin-dialog-api"; if (appWindow.label !== "main") { @@ -124,7 +123,7 @@ onMount(async () => { const window = getCurrent(); isWindowMaximized = await window.isMaximized(); - listen("tauri://resize", async () => { + window.onResized(async () => { isWindowMaximized = await window.isMaximized(); }); }); diff --git a/examples/api/src/views/Communication.svelte b/examples/api/src/views/Communication.svelte index e5b47f01..76a823f5 100644 --- a/examples/api/src/views/Communication.svelte +++ b/examples/api/src/views/Communication.svelte @@ -1,41 +1,41 @@ diff --git a/plugins/fs-watch/guest-js/index.ts b/plugins/fs-watch/guest-js/index.ts index 634b964f..9d25096f 100644 --- a/plugins/fs-watch/guest-js/index.ts +++ b/plugins/fs-watch/guest-js/index.ts @@ -21,17 +21,17 @@ export type RawEvent = { type RawEventKind = | "any " | { - access?: unknown; - } + access?: unknown; + } | { - create?: unknown; - } + create?: unknown; + } | { - modify?: unknown; - } + modify?: unknown; + } | { - remove?: unknown; - } + remove?: unknown; + } | "other"; export type DebouncedEvent = diff --git a/plugins/window/guest-js/event.ts b/plugins/window/guest-js/event.ts new file mode 100644 index 00000000..798b4fb4 --- /dev/null +++ b/plugins/window/guest-js/event.ts @@ -0,0 +1,98 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +import { invoke, transformCallback } from "@tauri-apps/api/tauri"; + +export interface Event { + /** Event name */ + event: string; + /** The label of the window that emitted this event. */ + windowLabel: string; + /** Event identifier used to unlisten */ + id: number; + /** Event payload */ + payload: T; +} + +export type EventCallback = (event: Event) => void; + +export type UnlistenFn = () => void; + +/** + * Unregister the event listener associated with the given name and id. + * + * @ignore + * @param event The event name + * @param eventId Event identifier + * @returns + */ +async function _unlisten(event: string, eventId: number): Promise { + await invoke("plugin:event|unlisten", { + event, + eventId, + }); +} + +/** + * Emits an event to the backend. + * + * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`. + * @param [windowLabel] The label of the window to which the event is sent, if null/undefined the event will be sent to all windows + * @param [payload] Event payload + * @returns + */ +async function emit( + event: string, + windowLabel?: string, + payload?: unknown +): Promise { + await invoke("plugin:event|emit", { + event, + windowLabel, + payload, + }); +} + +/** + * Listen to an event from the backend. + * + * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`. + * @param handler Event handler callback. + * @return A promise resolving to a function to unlisten to the event. + */ +async function listen( + event: string, + windowLabel: string | null, + handler: EventCallback +): Promise { + return invoke("plugin:event|listen", { + event, + windowLabel, + handler: transformCallback(handler), + }).then((eventId) => { + return async () => _unlisten(event, eventId); + }); +} + +/** + * Listen to an one-off event from the backend. + * + * @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`. + * @param handler Event handler callback. + * @returns A promise resolving to a function to unlisten to the event. + */ +async function once( + event: string, + windowLabel: string | null, + handler: EventCallback +): Promise { + return listen(event, windowLabel, (eventData) => { + handler(eventData); + _unlisten(event, eventData.id).catch(() => { + // do nothing + }); + }); +} + +export { emit, listen, once }; diff --git a/plugins/window/guest-js/index.ts b/plugins/window/guest-js/index.ts index 597b508e..94beb524 100644 --- a/plugins/window/guest-js/index.ts +++ b/plugins/window/guest-js/index.ts @@ -61,12 +61,18 @@ * @module */ -import { invoke } from '@tauri-apps/api/tauri' -import type { Event, EventName, EventCallback, UnlistenFn } from '@tauri-apps/api/event' -import { emit, listen, once, TauriEvent } from '@tauri-apps/api/event' - -type Theme = 'light' | 'dark' -type TitleBarStyle = 'visible' | 'transparent' | 'overlay' +import { invoke } from "@tauri-apps/api/tauri"; +import type { + Event, + EventName, + EventCallback, + UnlistenFn, +} from "@tauri-apps/api/event"; +import { TauriEvent } from "@tauri-apps/api/event"; +import { emit, listen, once } from "./event"; + +type Theme = "light" | "dark"; +type TitleBarStyle = "visible" | "transparent" | "overlay"; /** * Allows you to retrieve information about a given monitor. @@ -75,13 +81,13 @@ type TitleBarStyle = 'visible' | 'transparent' | 'overlay' */ interface Monitor { /** Human-readable name of the monitor */ - name: string | null + name: string | null; /** The monitor's resolution. */ - size: PhysicalSize + size: PhysicalSize; /** the Top-left corner position of the monitor relative to the larger full screen area. */ - position: PhysicalPosition + position: PhysicalPosition; /** The scale factor that can be used to map physical pixels to logical pixels. */ - scaleFactor: number + scaleFactor: number; } /** @@ -91,16 +97,16 @@ interface Monitor { */ interface ScaleFactorChanged { /** The new window scale factor. */ - scaleFactor: number + scaleFactor: number; /** The new window size */ - size: PhysicalSize + size: PhysicalSize; } /** The file drop event types. */ type FileDropEvent = - | { type: 'hover'; paths: string[] } - | { type: 'drop'; paths: string[] } - | { type: 'cancel' } + | { type: "hover"; paths: string[] } + | { type: "drop"; paths: string[] } + | { type: "cancel" }; /** * A size represented in logical pixels. @@ -108,13 +114,13 @@ type FileDropEvent = * @since 1.0.0 */ class LogicalSize { - type = 'Logical' - width: number - height: number + type = "Logical"; + width: number; + height: number; constructor(width: number, height: number) { - this.width = width - this.height = height + this.width = width; + this.height = height; } } @@ -124,13 +130,13 @@ class LogicalSize { * @since 1.0.0 */ class PhysicalSize { - type = 'Physical' - width: number - height: number + type = "Physical"; + width: number; + height: number; constructor(width: number, height: number) { - this.width = width - this.height = height + this.width = width; + this.height = height; } /** @@ -144,7 +150,7 @@ class PhysicalSize { * ``` * */ toLogical(scaleFactor: number): LogicalSize { - return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor) + return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor); } } @@ -154,13 +160,13 @@ class PhysicalSize { * @since 1.0.0 */ class LogicalPosition { - type = 'Logical' - x: number - y: number + type = "Logical"; + x: number; + y: number; constructor(x: number, y: number) { - this.x = x - this.y = y + this.x = x; + this.y = y; } } @@ -170,13 +176,13 @@ class LogicalPosition { * @since 1.0.0 */ class PhysicalPosition { - type = 'Physical' - x: number - y: number + type = "Physical"; + x: number; + y: number; constructor(x: number, y: number) { - this.x = x - this.y = y + this.x = x; + this.y = y; } /** @@ -190,22 +196,22 @@ class PhysicalPosition { * ``` * */ toLogical(scaleFactor: number): LogicalPosition { - return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor) + return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor); } } /** @ignore */ interface WindowDef { - label: string + label: string; } /** @ignore */ declare global { interface Window { __TAURI_METADATA__: { - __windows: WindowDef[] - __currentWindow: WindowDef - } + __windows: WindowDef[]; + __currentWindow: WindowDef; + }; } } @@ -226,49 +232,49 @@ enum UserAttentionType { * - **macOS:** Bounces the dock icon once. * - **Windows:** Flashes the taskbar button until the application is in focus. */ - Informational + Informational, } export type CursorIcon = - | 'default' - | 'crosshair' - | 'hand' - | 'arrow' - | 'move' - | 'text' - | 'wait' - | 'help' - | 'progress' + | "default" + | "crosshair" + | "hand" + | "arrow" + | "move" + | "text" + | "wait" + | "help" + | "progress" // something cannot be done - | 'notAllowed' - | 'contextMenu' - | 'cell' - | 'verticalText' - | 'alias' - | 'copy' - | 'noDrop' + | "notAllowed" + | "contextMenu" + | "cell" + | "verticalText" + | "alias" + | "copy" + | "noDrop" // something can be grabbed - | 'grab' + | "grab" /// something is grabbed - | 'grabbing' - | 'allScroll' - | 'zoomIn' - | 'zoomOut' + | "grabbing" + | "allScroll" + | "zoomIn" + | "zoomOut" // edge is to be moved - | 'eResize' - | 'nResize' - | 'neResize' - | 'nwResize' - | 'sResize' - | 'seResize' - | 'swResize' - | 'wResize' - | 'ewResize' - | 'nsResize' - | 'neswResize' - | 'nwseResize' - | 'colResize' - | 'rowResize' + | "eResize" + | "nResize" + | "neResize" + | "nwResize" + | "sResize" + | "seResize" + | "swResize" + | "wResize" + | "ewResize" + | "nsResize" + | "neswResize" + | "nwseResize" + | "colResize" + | "rowResize"; /** * Get an instance of `WebviewWindow` for the current webview window. @@ -278,8 +284,8 @@ export type CursorIcon = function getCurrent(): WebviewWindow { 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 - }) + skip: true, + }); } /** @@ -292,16 +298,16 @@ function getAll(): WebviewWindow[] { (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 + skip: true, }) - ) + ); } /** @ignore */ // events that are emitted right here instead of by the created webview -const localTauriEvents = ['tauri://created', 'tauri://error'] +const localTauriEvents = ["tauri://created", "tauri://error"]; /** @ignore */ -export type WindowLabel = string +export type WindowLabel = string; /** * A webview window handle allows emitting and listening to events from the backend that are tied to the window. * @@ -310,14 +316,14 @@ export type WindowLabel = string */ class WebviewWindowHandle { /** The window label. It is a unique identifier for the window, can be used to reference it later. */ - label: WindowLabel + label: WindowLabel; /** Local event listeners. */ - listeners: Record>> + listeners: Record>>; constructor(label: WindowLabel) { - this.label = label + this.label = label; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - this.listeners = Object.create(null) + this.listeners = Object.create(null); } /** @@ -346,12 +352,11 @@ class WebviewWindowHandle { if (this._handleTauriEvent(event, handler)) { return Promise.resolve(() => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection - const listeners = this.listeners[event] - listeners.splice(listeners.indexOf(handler), 1) - }) + const listeners = this.listeners[event]; + listeners.splice(listeners.indexOf(handler), 1); + }); } - // @ts-expect-error event will be added later // TODO - return listen(event, this.label, handler) + return listen(event, this.label, handler); } /** @@ -377,12 +382,11 @@ class WebviewWindowHandle { 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) - }) + const listeners = this.listeners[event]; + listeners.splice(listeners.indexOf(handler), 1); + }); } - // @ts-expect-error event will be added later // TODO - return once(event, this.label, handler) + return once(event, this.label, handler); } /** @@ -400,12 +404,11 @@ class WebviewWindowHandle { if (localTauriEvents.includes(event)) { // eslint-disable-next-line for (const handler of this.listeners[event] || []) { - handler({ event, id: -1, windowLabel: this.label, payload }) + handler({ event, id: -1, windowLabel: this.label, payload }); } - return Promise.resolve() + return Promise.resolve(); } - // @ts-expect-error event will be added later // TODO - return emit(event, this.label, payload) + return emit(event, this.label, payload); } /** @ignore */ @@ -413,14 +416,14 @@ class WebviewWindowHandle { if (localTauriEvents.includes(event)) { if (!(event in this.listeners)) { // eslint-disable-next-line - this.listeners[event] = [handler] + this.listeners[event] = [handler]; } else { // eslint-disable-next-line - this.listeners[event].push(handler) + this.listeners[event].push(handler); } - return true + return true; } - return false + return false; } } @@ -443,9 +446,9 @@ class WindowManager extends WebviewWindowHandle { * @returns The window's monitor scale factor. * */ async scaleFactor(): Promise { - return invoke('plugin:window|scale_factor', { - label: this.label - }) + return invoke("plugin:window|scale_factor", { + label: this.label, + }); } /** @@ -459,9 +462,9 @@ class WindowManager extends WebviewWindowHandle { * @returns The window's inner position. * */ async innerPosition(): Promise { - return invoke<{ x: number; y: number }>('plugin:window|inner_position', { - label: this.label - }).then(({ x, y }) => new PhysicalPosition(x, y)) + return invoke<{ x: number; y: number }>("plugin:window|inner_position", { + label: this.label, + }).then(({ x, y }) => new PhysicalPosition(x, y)); } /** @@ -475,9 +478,9 @@ class WindowManager extends WebviewWindowHandle { * @returns The window's outer position. * */ async outerPosition(): Promise { - return invoke<{ x: number; y: number }>('plugin:window|outer_position', { + return invoke<{ x: number; y: number }>("plugin:window|outer_position", { label: this.label, - }).then(({ x, y }) => new PhysicalPosition(x, y)) + }).then(({ x, y }) => new PhysicalPosition(x, y)); } /** @@ -492,9 +495,12 @@ class WindowManager extends WebviewWindowHandle { * @returns The window's inner size. */ async innerSize(): Promise { - return invoke<{ width: number; height: number }>('plugin:window|inner_size', { - label: this.label, - }).then(({ width, height }) => new PhysicalSize(width, height)) + return invoke<{ width: number; height: number }>( + "plugin:window|inner_size", + { + label: this.label, + } + ).then(({ width, height }) => new PhysicalSize(width, height)); } /** @@ -509,9 +515,12 @@ class WindowManager extends WebviewWindowHandle { * @returns The window's outer size. */ async outerSize(): Promise { - return invoke<{ width: number; height: number }>('plugin:window|outer_size', { - label: this.label, - }).then(({ width, height }) => new PhysicalSize(width, height)) + return invoke<{ width: number; height: number }>( + "plugin:window|outer_size", + { + label: this.label, + } + ).then(({ width, height }) => new PhysicalSize(width, height)); } /** @@ -525,9 +534,9 @@ class WindowManager extends WebviewWindowHandle { * @returns Whether the window is in fullscreen mode or not. * */ async isFullscreen(): Promise { - return invoke('plugin:window|is_fullscreen', { - label: this.label - }) + return invoke("plugin:window|is_fullscreen", { + label: this.label, + }); } /** @@ -541,9 +550,9 @@ class WindowManager extends WebviewWindowHandle { * @since 1.3.0 * */ async isMinimized(): Promise { - return invoke('plugin:window|is_minimized', { - label: this.label - }) + return invoke("plugin:window|is_minimized", { + label: this.label, + }); } /** @@ -557,9 +566,9 @@ class WindowManager extends WebviewWindowHandle { * @returns Whether the window is maximized or not. * */ async isMaximized(): Promise { - return invoke('plugin:window|is_maximized', { - label: this.label - }) + return invoke("plugin:window|is_maximized", { + label: this.label, + }); } /** @@ -573,9 +582,9 @@ class WindowManager extends WebviewWindowHandle { * @returns Whether the window is decorated or not. * */ async isDecorated(): Promise { - return invoke('plugin:window|is_decorated', { - label: this.label - }) + return invoke("plugin:window|is_decorated", { + label: this.label, + }); } /** @@ -589,9 +598,9 @@ class WindowManager extends WebviewWindowHandle { * @returns Whether the window is resizable or not. * */ async isResizable(): Promise { - return invoke('plugin:window|is_resizable', { - label: this.label - }) + return invoke("plugin:window|is_resizable", { + label: this.label, + }); } /** @@ -605,9 +614,9 @@ class WindowManager extends WebviewWindowHandle { * @returns Whether the window is visible or not. * */ async isVisible(): Promise { - return invoke('plugin:window|is_visible', { - label: this.label - }) + return invoke("plugin:window|is_visible", { + label: this.label, + }); } /** @@ -621,9 +630,9 @@ class WindowManager extends WebviewWindowHandle { * @since 1.3.0 * */ async title(): Promise { - return invoke('plugin:window|title', { - label: this.label - }) + return invoke("plugin:window|title", { + label: this.label, + }); } /** @@ -642,9 +651,9 @@ class WindowManager extends WebviewWindowHandle { * @returns The window theme. * */ async theme(): Promise { - return invoke('plugin:window|theme', { - label: this.label - }) + return invoke("plugin:window|theme", { + label: this.label, + }); } // Setters @@ -661,9 +670,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async center(): Promise { - return invoke('plugin:window|center', { - label: this.label - }) + return invoke("plugin:window|center", { + label: this.label, + }); } /** @@ -690,19 +699,19 @@ class WindowManager extends WebviewWindowHandle { async requestUserAttention( requestType: UserAttentionType | null ): Promise { - let requestType_ = null + let requestType_ = null; if (requestType) { if (requestType === UserAttentionType.Critical) { - requestType_ = { type: 'Critical' } + requestType_ = { type: "Critical" }; } else { - requestType_ = { type: 'Informational' } + requestType_ = { type: "Informational" }; } } - return invoke('plugin:window|request_user_attention', { + return invoke("plugin:window|request_user_attention", { label: this.label, - value: requestType_ - }) + value: requestType_, + }); } /** @@ -717,10 +726,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setResizable(resizable: boolean): Promise { - return invoke('plugin:window|set_resizable', { + return invoke("plugin:window|set_resizable", { label: this.label, - value: resizable - }) + value: resizable, + }); } /** @@ -735,10 +744,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setTitle(title: string): Promise { - return invoke('plugin:window|set_title', { + return invoke("plugin:window|set_title", { label: this.label, - value: title - }) + value: title, + }); } /** @@ -752,9 +761,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async maximize(): Promise { - return invoke('plugin:window|maximize', { - label: this.label - }) + return invoke("plugin:window|maximize", { + label: this.label, + }); } /** @@ -768,9 +777,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async unmaximize(): Promise { - return invoke('plugin:window|unmaximize', { + return invoke("plugin:window|unmaximize", { label: this.label, - }) + }); } /** @@ -784,9 +793,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async toggleMaximize(): Promise { - return invoke('plugin:window|toggle_maximize', { - label: this.label - }) + return invoke("plugin:window|toggle_maximize", { + label: this.label, + }); } /** @@ -800,9 +809,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async minimize(): Promise { - return invoke('plugin:window|minimize', { - label: this.label - }) + return invoke("plugin:window|minimize", { + label: this.label, + }); } /** @@ -816,9 +825,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async unminimize(): Promise { - return invoke('plugin:window|unminimize', { - label: this.label - }) + return invoke("plugin:window|unminimize", { + label: this.label, + }); } /** @@ -832,9 +841,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async show(): Promise { - return invoke('plugin:window|show', { - label: this.label - }) + return invoke("plugin:window|show", { + label: this.label, + }); } /** @@ -848,9 +857,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async hide(): Promise { - return invoke('plugin:window|hide', { - label: this.label - }) + return invoke("plugin:window|hide", { + label: this.label, + }); } /** @@ -864,9 +873,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async close(): Promise { - return invoke('plugin:window|close', { - label: this.label - }) + return invoke("plugin:window|close", { + label: this.label, + }); } /** @@ -881,10 +890,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setDecorations(decorations: boolean): Promise { - return invoke('plugin:window|set_decorations', { + return invoke("plugin:window|set_decorations", { label: this.label, - value: decorations - }) + value: decorations, + }); } /** @@ -909,10 +918,10 @@ class WindowManager extends WebviewWindowHandle { * @since 2.0 */ async setShadow(enable: boolean): Promise { - return invoke('plugin:window|set_shadow', { + return invoke("plugin:window|set_shadow", { label: this.label, - value: enable - }) + value: enable, + }); } /** @@ -927,10 +936,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setAlwaysOnTop(alwaysOnTop: boolean): Promise { - return invoke('plugin:window|set_always_on_top', { + return invoke("plugin:window|set_always_on_top", { label: this.label, - value: alwaysOnTop - }) + value: alwaysOnTop, + }); } /** @@ -946,10 +955,10 @@ class WindowManager extends WebviewWindowHandle { * @since 1.2.0 */ async setContentProtected(protected_: boolean): Promise { - return invoke('plugin:window|set_content_protected', { + return invoke("plugin:window|set_content_protected", { label: this.label, - value: protected_ - }) + value: protected_, + }); } /** @@ -964,22 +973,22 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setSize(size: LogicalSize | PhysicalSize): Promise { - if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) { + if (!size || (size.type !== "Logical" && size.type !== "Physical")) { throw new Error( - 'the `size` argument must be either a LogicalSize or a PhysicalSize instance' - ) + "the `size` argument must be either a LogicalSize or a PhysicalSize instance" + ); } - return invoke('plugin:window|set_size', { + return invoke("plugin:window|set_size", { label: this.label, value: { type: size.type, data: { width: size.width, - height: size.height - } - } - }) + height: size.height, + }, + }, + }); } /** @@ -996,24 +1005,24 @@ class WindowManager extends WebviewWindowHandle { async setMinSize( size: LogicalSize | PhysicalSize | null | undefined ): Promise { - if (size && size.type !== 'Logical' && size.type !== 'Physical') { + if (size && size.type !== "Logical" && size.type !== "Physical") { throw new Error( - 'the `size` argument must be either a LogicalSize or a PhysicalSize instance' - ) + "the `size` argument must be either a LogicalSize or a PhysicalSize instance" + ); } - return invoke('plugin:window|set_min_size', { + return invoke("plugin:window|set_min_size", { label: this.label, value: size ? { - type: size.type, - data: { - width: size.width, - height: size.height + type: size.type, + data: { + width: size.width, + height: size.height, + }, } - } - : null - }) + : null, + }); } /** @@ -1030,24 +1039,24 @@ class WindowManager extends WebviewWindowHandle { async setMaxSize( size: LogicalSize | PhysicalSize | null | undefined ): Promise { - if (size && size.type !== 'Logical' && size.type !== 'Physical') { + if (size && size.type !== "Logical" && size.type !== "Physical") { throw new Error( - 'the `size` argument must be either a LogicalSize or a PhysicalSize instance' - ) + "the `size` argument must be either a LogicalSize or a PhysicalSize instance" + ); } - return invoke('plugin:window|set_max_size', { + return invoke("plugin:window|set_max_size", { label: this.label, value: size ? { - type: size.type, - data: { - width: size.width, - height: size.height + type: size.type, + data: { + width: size.width, + height: size.height, + }, } - } - : null - }) + : null, + }); } /** @@ -1066,23 +1075,23 @@ class WindowManager extends WebviewWindowHandle { ): Promise { if ( !position || - (position.type !== 'Logical' && position.type !== 'Physical') + (position.type !== "Logical" && position.type !== "Physical") ) { throw new Error( - 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance' - ) + "the `position` argument must be either a LogicalPosition or a PhysicalPosition instance" + ); } - return invoke('plugin:window|set_position', { + return invoke("plugin:window|set_position", { label: this.label, value: { type: position.type, data: { x: position.x, - y: position.y - } - } - }) + y: position.y, + }, + }, + }); } /** @@ -1097,10 +1106,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setFullscreen(fullscreen: boolean): Promise { - return invoke('plugin:window|set_fullscreen', { + return invoke("plugin:window|set_fullscreen", { label: this.label, - value: fullscreen - }) + value: fullscreen, + }); } /** @@ -1114,9 +1123,9 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setFocus(): Promise { - return invoke('plugin:window|set_focus', { + return invoke("plugin:window|set_focus", { label: this.label, - }) + }); } /** @@ -1138,10 +1147,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setIcon(icon: string | Uint8Array): Promise { - return invoke('plugin:window|set_icon', { + return invoke("plugin:window|set_icon", { label: this.label, - value: typeof icon === 'string' ? icon : Array.from(icon) - }) + value: typeof icon === "string" ? icon : Array.from(icon), + }); } /** @@ -1160,10 +1169,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setSkipTaskbar(skip: boolean): Promise { - return invoke('plugin:window|set_skip_taskbar', { + return invoke("plugin:window|set_skip_taskbar", { label: this.label, - value: skip - }) + value: skip, + }); } /** @@ -1186,10 +1195,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setCursorGrab(grab: boolean): Promise { - return invoke('plugin:window|set_cursor_grab', { + return invoke("plugin:window|set_cursor_grab", { label: this.label, - value: grab - }) + value: grab, + }); } /** @@ -1210,10 +1219,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setCursorVisible(visible: boolean): Promise { - return invoke('plugin:window|set_cursor_visible', { + return invoke("plugin:window|set_cursor_visible", { label: this.label, - value: visible - }) + value: visible, + }); } /** @@ -1228,10 +1237,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setCursorIcon(icon: CursorIcon): Promise { - return invoke('plugin:window|set_cursor_icon', { + return invoke("plugin:window|set_cursor_icon", { label: this.label, - value: icon - }) + value: icon, + }); } /** @@ -1250,23 +1259,23 @@ class WindowManager extends WebviewWindowHandle { ): Promise { if ( !position || - (position.type !== 'Logical' && position.type !== 'Physical') + (position.type !== "Logical" && position.type !== "Physical") ) { throw new Error( - 'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance' - ) + "the `position` argument must be either a LogicalPosition or a PhysicalPosition instance" + ); } - return invoke('plugin:window|set_cursor_position', { + return invoke("plugin:window|set_cursor_position", { label: this.label, value: { type: position.type, data: { x: position.x, - y: position.y - } - } - }) + y: position.y, + }, + }, + }); } /** @@ -1282,10 +1291,10 @@ class WindowManager extends WebviewWindowHandle { * @returns A promise indicating the success or failure of the operation. */ async setIgnoreCursorEvents(ignore: boolean): Promise { - return invoke('plugin:window|set_ignore_cursor_events', { + return invoke("plugin:window|set_ignore_cursor_events", { label: this.label, - value: ignore - }) + value: ignore, + }); } /** @@ -1299,9 +1308,9 @@ class WindowManager extends WebviewWindowHandle { * @return A promise indicating the success or failure of the operation. */ async startDragging(): Promise { - return invoke('plugin:window|start_dragging', { - label: this.label - }) + return invoke("plugin:window|start_dragging", { + label: this.label, + }); } // Listeners @@ -1327,9 +1336,9 @@ class WindowManager extends WebviewWindowHandle { */ async onResized(handler: EventCallback): Promise { return this.listen(TauriEvent.WINDOW_RESIZED, (e) => { - e.payload = mapPhysicalSize(e.payload) - handler(e) - }) + e.payload = mapPhysicalSize(e.payload); + handler(e); + }); } /** @@ -1353,9 +1362,9 @@ class WindowManager extends WebviewWindowHandle { */ async onMoved(handler: EventCallback): Promise { return this.listen(TauriEvent.WINDOW_MOVED, (e) => { - e.payload = mapPhysicalPosition(e.payload) - handler(e) - }) + e.payload = mapPhysicalPosition(e.payload); + handler(e); + }); } /** @@ -1387,13 +1396,13 @@ class WindowManager extends WebviewWindowHandle { handler: (event: CloseRequestedEvent) => void | Promise ): Promise { return this.listen(TauriEvent.WINDOW_CLOSE_REQUESTED, (event) => { - const evt = new CloseRequestedEvent(event) + const evt = new CloseRequestedEvent(event); void Promise.resolve(handler(evt)).then(() => { if (!evt.isPreventDefault()) { - return this.close() + return this.close(); } - }) - }) + }); + }); } /* eslint-enable */ @@ -1420,19 +1429,19 @@ class WindowManager extends WebviewWindowHandle { const unlistenFocus = await this.listen( TauriEvent.WINDOW_FOCUS, (event) => { - handler({ ...event, payload: true }) + handler({ ...event, payload: true }); } - ) + ); const unlistenBlur = await this.listen( TauriEvent.WINDOW_BLUR, (event) => { - handler({ ...event, payload: false }) + handler({ ...event, payload: false }); } - ) + ); return () => { - unlistenFocus() - unlistenBlur() - } + unlistenFocus(); + unlistenBlur(); + }; } /** @@ -1464,7 +1473,7 @@ class WindowManager extends WebviewWindowHandle { return this.listen( TauriEvent.WINDOW_SCALE_FACTOR_CHANGED, handler - ) + ); } /** @@ -1487,7 +1496,7 @@ class WindowManager extends WebviewWindowHandle { * @since 1.0.2 */ async onMenuClicked(handler: EventCallback): Promise { - return this.listen(TauriEvent.MENU, handler) + return this.listen(TauriEvent.MENU, handler); } /** @@ -1523,29 +1532,29 @@ class WindowManager extends WebviewWindowHandle { const unlistenFileDrop = await this.listen( TauriEvent.WINDOW_FILE_DROP, (event) => { - handler({ ...event, payload: { type: 'drop', paths: event.payload } }) + handler({ ...event, payload: { type: "drop", paths: event.payload } }); } - ) + ); const unlistenFileHover = await this.listen( TauriEvent.WINDOW_FILE_DROP_HOVER, (event) => { - handler({ ...event, payload: { type: 'hover', paths: event.payload } }) + handler({ ...event, payload: { type: "hover", paths: event.payload } }); } - ) + ); const unlistenCancel = await this.listen( TauriEvent.WINDOW_FILE_DROP_CANCELLED, (event) => { - handler({ ...event, payload: { type: 'cancel' } }) + handler({ ...event, payload: { type: "cancel" } }); } - ) + ); return () => { - unlistenFileDrop() - unlistenFileHover() - unlistenCancel() - } + unlistenFileDrop(); + unlistenFileHover(); + unlistenCancel(); + }; } /** @@ -1568,7 +1577,7 @@ class WindowManager extends WebviewWindowHandle { * @since 1.0.2 */ async onThemeChanged(handler: EventCallback): Promise { - return this.listen(TauriEvent.WINDOW_THEME_CHANGED, handler) + return this.listen(TauriEvent.WINDOW_THEME_CHANGED, handler); } } @@ -1577,25 +1586,25 @@ class WindowManager extends WebviewWindowHandle { */ class CloseRequestedEvent { /** Event name */ - event: EventName + event: EventName; /** The label of the window that emitted this event. */ - windowLabel: string + windowLabel: string; /** Event identifier used to unlisten */ - id: number - private _preventDefault = false + id: number; + private _preventDefault = false; constructor(event: Event) { - this.event = event.event - this.windowLabel = event.windowLabel - this.id = event.id + this.event = event.event; + this.windowLabel = event.windowLabel; + this.id = event.id; } preventDefault(): void { - this._preventDefault = true + this._preventDefault = true; } isPreventDefault(): boolean { - return this._preventDefault + return this._preventDefault; } } @@ -1653,17 +1662,17 @@ class WebviewWindow extends WindowManager { * @returns The WebviewWindow instance to communicate with the webview. */ constructor(label: WindowLabel, options: WindowOptions = {}) { - super(label) + super(label); // @ts-expect-error `skip` is not a public API so it is not defined in WindowOptions if (!options?.skip) { - invoke('plugin:window|create', { + invoke("plugin:window|create", { options: { ...options, label, - } + }, }) - .then(async () => this.emit('tauri://created')) - .catch(async (e: string) => this.emit('tauri://error', e)) + .then(async () => this.emit("tauri://created")) + .catch(async (e: string) => this.emit("tauri://error", e)); } } @@ -1681,30 +1690,30 @@ class WebviewWindow extends WindowManager { static getByLabel(label: string): WebviewWindow | null { 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 new WebviewWindow(label, { skip: true }); } - return null + return null; } } /** The WebviewWindow for the current window. */ -let appWindow: WebviewWindow -if ('__TAURI_METADATA__' in window) { +let appWindow: WebviewWindow; +if ("__TAURI_METADATA__" in window) { 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 + 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.` - ) - appWindow = new WebviewWindow('main', { + ); + appWindow = new WebviewWindow("main", { // @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor - skip: true - }) + skip: true, + }); } /** @@ -1720,51 +1729,51 @@ interface WindowOptions { * - data: URL such as `data:text/html,...` is only supported with the `window-data-url` Cargo feature for the `tauri` dependency. * - local file path or route such as `/path/to/page.html` or `/users` is appended to the application URL (the devServer URL on development, or `tauri://localhost/` and `https://tauri.localhost/` on production). */ - url?: string + url?: string; /** Show window in the center of the screen.. */ - center?: boolean + center?: boolean; /** The initial vertical position. Only applies if `y` is also set. */ - x?: number + x?: number; /** The initial horizontal position. Only applies if `x` is also set. */ - y?: number + y?: number; /** The initial width. */ - width?: number + width?: number; /** The initial height. */ - height?: number + height?: number; /** The minimum width. Only applies if `minHeight` is also set. */ - minWidth?: number + minWidth?: number; /** The minimum height. Only applies if `minWidth` is also set. */ - minHeight?: number + minHeight?: number; /** The maximum width. Only applies if `maxHeight` is also set. */ - maxWidth?: number + maxWidth?: number; /** The maximum height. Only applies if `maxWidth` is also set. */ - maxHeight?: number + maxHeight?: number; /** Whether the window is resizable or not. */ - resizable?: boolean + resizable?: boolean; /** Window title. */ - title?: string + title?: string; /** Whether the window is in fullscreen mode or not. */ - fullscreen?: boolean + fullscreen?: boolean; /** Whether the window will be initially focused or not. */ - focus?: boolean + focus?: boolean; /** * Whether the window is transparent or not. * Note that on `macOS` this requires the `macos-private-api` feature flag, enabled under `tauri.conf.json > tauri > macOSPrivateApi`. * WARNING: Using private APIs on `macOS` prevents your application from being accepted to the `App Store`. */ - transparent?: boolean + transparent?: boolean; /** Whether the window should be maximized upon creation or not. */ - maximized?: boolean + maximized?: boolean; /** Whether the window should be immediately visible upon creation or not. */ - visible?: boolean + visible?: boolean; /** Whether the window should have borders and bars or not. */ - decorations?: boolean + decorations?: boolean; /** Whether the window should always be on top of other windows or not. */ - alwaysOnTop?: boolean + alwaysOnTop?: boolean; /** Prevents the window contents from being captured by other apps. */ - contentProtected?: boolean + contentProtected?: boolean; /** Whether or not the window icon should be added to the taskbar. */ - skipTaskbar?: boolean + skipTaskbar?: boolean; /** * Whether or not the window has shadow. * @@ -1778,61 +1787,61 @@ interface WindowOptions { * * @since 2.0 */ - shadow?: boolean + shadow?: boolean; /** * Whether the file drop is enabled or not on the webview. By default it is enabled. * * Disabling it is required to use drag and drop on the frontend on Windows. */ - fileDropEnabled?: boolean + fileDropEnabled?: boolean; /** * The initial window theme. Defaults to the system theme. * * Only implemented on Windows and macOS 10.14+. */ - theme?: Theme + theme?: Theme; /** * The style of the macOS title bar. */ - titleBarStyle?: TitleBarStyle + titleBarStyle?: TitleBarStyle; /** * If `true`, sets the window title to be hidden on macOS. */ - hiddenTitle?: boolean + hiddenTitle?: boolean; /** * Whether clicking an inactive window also clicks through to the webview on macOS. */ - acceptFirstMouse?: boolean + acceptFirstMouse?: boolean; /** * Defines the window [tabbing identifier](https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier) on macOS. * * Windows with the same tabbing identifier will be grouped together. * If the tabbing identifier is not set, automatic tabbing will be disabled. */ - tabbingIdentifier?: string + tabbingIdentifier?: string; /** * The user agent for the webview. */ - userAgent?: string + userAgent?: string; } function mapMonitor(m: Monitor | null): Monitor | null { return m === null ? null : { - name: m.name, - scaleFactor: m.scaleFactor, - position: mapPhysicalPosition(m.position), - size: mapPhysicalSize(m.size) - } + name: m.name, + scaleFactor: m.scaleFactor, + position: mapPhysicalPosition(m.position), + size: mapPhysicalSize(m.size), + }; } function mapPhysicalPosition(m: PhysicalPosition): PhysicalPosition { - return new PhysicalPosition(m.x, m.y) + return new PhysicalPosition(m.x, m.y); } function mapPhysicalSize(m: PhysicalSize): PhysicalSize { - return new PhysicalSize(m.width, m.height) + return new PhysicalSize(m.width, m.height); } /** @@ -1847,7 +1856,9 @@ function mapPhysicalSize(m: PhysicalSize): PhysicalSize { * @since 1.0.0 */ async function currentMonitor(): Promise { - return invoke('plugin:window|current_monitor').then(mapMonitor) + return invoke("plugin:window|current_monitor").then( + mapMonitor + ); } /** @@ -1862,7 +1873,9 @@ async function currentMonitor(): Promise { * @since 1.0.0 */ async function primaryMonitor(): Promise { - return invoke('plugin:window|primary_monitor').then(mapMonitor) + return invoke("plugin:window|primary_monitor").then( + mapMonitor + ); } /** @@ -1876,7 +1889,9 @@ async function primaryMonitor(): Promise { * @since 1.0.0 */ async function availableMonitors(): Promise { - return invoke('plugin:window|available_monitors').then((ms) => ms.map(mapMonitor) as Monitor[]) + return invoke("plugin:window|available_monitors").then( + (ms) => ms.map(mapMonitor) as Monitor[] + ); } export { @@ -1894,8 +1909,8 @@ export { UserAttentionType, currentMonitor, primaryMonitor, - availableMonitors -} + availableMonitors, +}; export type { Theme, @@ -1903,5 +1918,5 @@ export type { Monitor, ScaleFactorChanged, FileDropEvent, - WindowOptions -} + WindowOptions, +}; diff --git a/plugins/window/package.json b/plugins/window/package.json index f8502cfa..6e564693 100644 --- a/plugins/window/package.json +++ b/plugins/window/package.json @@ -29,4 +29,4 @@ "dependencies": { "@tauri-apps/api": "^1.2.0" } -} \ No newline at end of file +}