feat(window): refactor and improvemnts

pull/426/head
amrbashir 2 years ago
parent 1091d6d6ac
commit 3b6604fd57
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

@ -0,0 +1,10 @@
---
"window": "patch"
"window-js": "patch"
---
The window plugin is recieving a few changes to improve consistency and add new features:
- Removed `appWindow` variable from JS module, use `getCurrent` or `Window.getCurrent`.
- Removed `WindowManager`, `WebviewWindow` and `WebviewHandle` types and merged them into one `Window` type that matches the name of the rust window type.
- Added `Window.getCurrent` and `Window.getAll` which is a convenient method for `getCurrent` and `getAll` functions.

@ -7,10 +7,10 @@
*
* ## Window events
*
* Events can be listened to using `appWindow.listen`:
* Events can be listened to using {@link Window.listen}:
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* appWindow.listen("my-window-event", ({ event, payload }) => { });
* import { getCurrent } from "@tauri-apps/plugin-window";
* getCurrent().listen("my-window-event", ({ event, payload }) => { });
* ```
*
* @module
@ -104,7 +104,8 @@ class PhysicalSize {
* Converts the physical size to a logical one.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* import { getCurrent } from '@tauri-apps/window';
* const appWindow = getCurrent();
* const factor = await appWindow.scaleFactor();
* const size = await appWindow.innerSize();
* const logical = size.toLogical(factor);
@ -150,7 +151,8 @@ class PhysicalPosition {
* Converts the physical position to a logical one.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* import { getCurrent } from '@tauri-apps/window';
* const appWindow = getCurrent();
* const factor = await appWindow.scaleFactor();
* const position = await appWindow.innerPosition();
* const logical = position.toLogical(factor);
@ -196,6 +198,30 @@ enum UserAttentionType {
Informational,
}
class CloseRequestedEvent {
/** Event name */
event: EventName;
/** The label of the window that emitted this event. */
windowLabel: string;
/** Event identifier used to unlisten */
id: number;
private _preventDefault = false;
constructor(event: Event<null>) {
this.event = event.event;
this.windowLabel = event.windowLabel;
this.id = event.id;
}
preventDefault(): void {
this._preventDefault = true;
}
isPreventDefault(): boolean {
return this._preventDefault;
}
}
export type CursorIcon =
| "default"
| "crosshair"
@ -238,26 +264,26 @@ export type CursorIcon =
| "rowResize";
/**
* Get an instance of `WebviewWindow` for the current webview window.
* Get an instance of `Window` for the current window.
*
* @since 2.0.0
*/
function getCurrent(): WebviewWindow {
return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
function getCurrent(): Window {
return new Window(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.
* Gets a list of instances of `Window` for all available windows.
*
* @since 2.0.0
*/
function getAll(): WebviewWindow[] {
function getAll(): Window[] {
return window.__TAURI_METADATA__.__windows.map(
(w) =>
new WebviewWindow(w.label, {
new Window(w.label, {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
skip: true,
})
@ -269,23 +295,123 @@ function getAll(): WebviewWindow[] {
const localTauriEvents = ["tauri://created", "tauri://error"];
/** @ignore */
export type WindowLabel = string;
/**
* A webview window handle allows emitting and listening to events from the backend that are tied to the window.
* Create new webview window or get a handle to an existing one.
*
* 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 appWindow = new Window('theUniqueLabel', {
* url: 'path/to/page.html'
* });
* // alternatively, load a remote URL:
* const appWindow = new Window('theUniqueLabel', {
* url: 'https://github.com/tauri-apps/tauri'
* });
*
* appWindow.once('tauri://created', function () {
* // window successfully created
* });
* appWindow.once('tauri://error', function (e) {
* // an error happened creating the window
* });
*
* // emit an event to the backend
* await appWindow.emit("some event", "data");
* // listen to an event from the backend
* const unlisten = await appWindow.listen("event name", e => {});
* unlisten();
* ```
*
* @ignore
* @since 2.0.0
*/
class WebviewWindowHandle {
class Window {
/** The window label. It is a unique identifier for the window, can be used to reference it later. */
label: WindowLabel;
/** Local event listeners. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listeners: Record<string, Array<EventCallback<any>>>;
constructor(label: WindowLabel) {
/**
* Creates a new WebviewWindow.
* @example
* ```typescript
* import { Window } from '@tauri-apps/window';
* const appWindow = new Window('my-label', {
* url: 'https://github.com/tauri-apps/tauri'
* });
* appWindow.once('tauri://created', function () {
* // window successfully created
* });
* appWindow.once('tauri://error', function (e) {
* // an error happened creating the window
* });
* ```
*
* @param label The unique webview window label. Must be alphanumeric: `a-zA-Z-/:_`.
* @returns The {@link Window} instance to communicate with the webview.
*
* @since 2.0.0
*/
constructor(label: WindowLabel, options: WindowOptions = {}) {
this.label = label;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.listeners = Object.create(null);
// @ts-expect-error `skip` is not a public API so it is not defined in WindowOptions
if (!options?.skip) {
window
.__TAURI_INVOKE__("plugin:window|create", {
options: {
...options,
label,
},
})
.then(async () => this.emit("tauri://created"))
.catch(async (e: string) => this.emit("tauri://error", e));
}
}
/**
* Gets the WebviewWindow for the webview associated with the given label.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/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.
*
* @since 2.0.0
*/
static getByLabel(label: string): Window | 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 null;
}
/**
* Get an instance of `Window` for the current window.
*
* @since 2.0.0
*/
static getCurrent(): Window {
return getCurrent();
}
/**
* Gets a list of instances of `Window` for all available windows.
*
* @since 2.0.0
*/
static getAll(): Window[] {
return getAll();
}
/**
@ -293,8 +419,8 @@ class WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const unlisten = await appWindow.listen<string>('state-changed', (event) => {
* import { getCurrent } from '@tauri-apps/window';
* const unlisten = await getCurrent().listen<string>('state-changed', (event) => {
* console.log(`Got error: ${payload}`);
* });
*
@ -328,8 +454,8 @@ class WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const unlisten = await appWindow.once<null>('initialized', (event) => {
* import { getCurrent } from '@tauri-apps/window';
* const unlisten = await getCurrent().once<null>('initialized', (event) => {
* console.log(`Window initialized!`);
* });
*
@ -359,8 +485,8 @@ class WebviewWindowHandle {
* Emits an event to the backend, tied to the webview window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.emit('window-loaded', { loggedIn: true, token: 'authToken' });
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().emit('window-loaded', { loggedIn: true, token: 'authToken' });
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
@ -391,22 +517,14 @@ class WebviewWindowHandle {
}
return false;
}
}
/**
* Manage the current window object.
*
* @ignore
* @since 2.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/window';
* const factor = await appWindow.scaleFactor();
* import { getCurrent } from '@tauri-apps/window';
* const factor = await getCurrent().scaleFactor();
* ```
*
* @returns The window's monitor scale factor.
@ -423,8 +541,8 @@ class WindowManager extends WebviewWindowHandle {
* 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/window';
* const position = await appWindow.innerPosition();
* import { getCurrent } from '@tauri-apps/window';
* const position = await getCurrent().innerPosition();
* ```
*
* @returns The window's inner position.
@ -446,8 +564,8 @@ class WindowManager extends WebviewWindowHandle {
* 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/window';
* const position = await appWindow.outerPosition();
* import { getCurrent } from '@tauri-apps/window';
* const position = await getCurrent().outerPosition();
* ```
*
* @returns The window's outer position.
@ -470,8 +588,8 @@ class WindowManager extends WebviewWindowHandle {
* The client area is the content of the window, excluding the title bar and borders.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const size = await appWindow.innerSize();
* import { getCurrent } from '@tauri-apps/window';
* const size = await getCurrent().innerSize();
* ```
*
* @returns The window's inner size.
@ -494,8 +612,8 @@ class WindowManager extends WebviewWindowHandle {
* 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/window';
* const size = await appWindow.outerSize();
* import { getCurrent } from '@tauri-apps/window';
* const size = await getCurrent().outerSize();
* ```
*
* @returns The window's outer size.
@ -517,8 +635,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current fullscreen state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const fullscreen = await appWindow.isFullscreen();
* import { getCurrent } from '@tauri-apps/window';
* const fullscreen = await getCurrent().isFullscreen();
* ```
*
* @returns Whether the window is in fullscreen mode or not.
@ -535,8 +653,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current minimized state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const minimized = await appWindow.isMinimized();
* import { getCurrent } from '@tauri-apps/window';
* const minimized = await getCurrent().isMinimized();
* ```
*
* @since 2.0.0
@ -551,8 +669,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current maximized state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const maximized = await appWindow.isMaximized();
* import { getCurrent } from '@tauri-apps/window';
* const maximized = await getCurrent().isMaximized();
* ```
*
* @returns Whether the window is maximized or not.
@ -569,8 +687,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current decorated state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const decorated = await appWindow.isDecorated();
* import { getCurrent } from '@tauri-apps/window';
* const decorated = await getCurrent().isDecorated();
* ```
*
* @returns Whether the window is decorated or not.
@ -587,8 +705,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current resizable state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const resizable = await appWindow.isResizable();
* import { getCurrent } from '@tauri-apps/window';
* const resizable = await getCurrent().isResizable();
* ```
*
* @returns Whether the window is resizable or not.
@ -605,8 +723,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current visible state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const visible = await appWindow.isVisible();
* import { getCurrent } from '@tauri-apps/window';
* const visible = await getCurrent().isVisible();
* ```
*
* @returns Whether the window is visible or not.
@ -623,8 +741,8 @@ class WindowManager extends WebviewWindowHandle {
* Gets the window's current title.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const title = await appWindow.title();
* import { getCurrent } from '@tauri-apps/window';
* const title = await getCurrent().title();
* ```
*
* @since 2.0.0
@ -644,8 +762,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* const theme = await appWindow.theme();
* import { getCurrent } from '@tauri-apps/window';
* const theme = await getCurrent().theme();
* ```
*
* @returns The window theme.
@ -664,8 +782,8 @@ class WindowManager extends WebviewWindowHandle {
* Centers the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.center();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().center();
* ```
*
* @param resizable
@ -693,8 +811,8 @@ class WindowManager extends WebviewWindowHandle {
* - **Linux:** Urgency levels have the same effect.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.requestUserAttention();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().requestUserAttention();
* ```
*
* @param resizable
@ -724,8 +842,8 @@ class WindowManager extends WebviewWindowHandle {
* Updates the window resizable flag.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setResizable(false);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setResizable(false);
* ```
*
* @param resizable
@ -744,8 +862,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window title.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setTitle('Tauri');
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setTitle('Tauri');
* ```
*
* @param title The new title
@ -764,8 +882,8 @@ class WindowManager extends WebviewWindowHandle {
* Maximizes the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.maximize();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().maximize();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -782,8 +900,8 @@ class WindowManager extends WebviewWindowHandle {
* Unmaximizes the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.unmaximize();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().unmaximize();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -800,8 +918,8 @@ class WindowManager extends WebviewWindowHandle {
* Toggles the window maximized state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.toggleMaximize();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().toggleMaximize();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -818,8 +936,8 @@ class WindowManager extends WebviewWindowHandle {
* Minimizes the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.minimize();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().minimize();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -836,8 +954,8 @@ class WindowManager extends WebviewWindowHandle {
* Unminimizes the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.unminimize();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().unminimize();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -854,8 +972,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window visibility to true.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.show();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().show();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -872,8 +990,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window visibility to false.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.hide();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().hide();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -890,8 +1008,8 @@ class WindowManager extends WebviewWindowHandle {
* Closes the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.close();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().close();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -908,8 +1026,8 @@ class WindowManager extends WebviewWindowHandle {
* Whether the window should have borders and bars.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setDecorations(false);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setDecorations(false);
* ```
*
* @param decorations Whether the window should have borders and bars.
@ -937,8 +1055,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setShadow(false);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setShadow(false);
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -956,8 +1074,8 @@ class WindowManager extends WebviewWindowHandle {
* Whether the window should always be on top of other windows.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setAlwaysOnTop(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setAlwaysOnTop(true);
* ```
*
* @param alwaysOnTop Whether the window should always be on top of other windows or not.
@ -976,8 +1094,8 @@ class WindowManager extends WebviewWindowHandle {
* Prevents the window contents from being captured by other apps.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setContentProtected(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setContentProtected(true);
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -995,8 +1113,8 @@ class WindowManager extends WebviewWindowHandle {
* Resizes the window with a new inner size.
* @example
* ```typescript
* import { appWindow, LogicalSize } from '@tauri-apps/window';
* await appWindow.setSize(new LogicalSize(600, 500));
* import { getCurrent, LogicalSize } from '@tauri-apps/window';
* await getCurrent().setSize(new LogicalSize(600, 500));
* ```
*
* @param size The logical or physical inner size.
@ -1027,8 +1145,8 @@ class WindowManager extends WebviewWindowHandle {
* 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/window';
* await appWindow.setMinSize(new PhysicalSize(600, 500));
* import { getCurrent, PhysicalSize } from '@tauri-apps/window';
* await getCurrent().setMinSize(new PhysicalSize(600, 500));
* ```
*
* @param size The logical or physical inner size, or `null` to unset the constraint.
@ -1063,8 +1181,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window maximum inner size. If the `size` argument is undefined, the constraint is unset.
* @example
* ```typescript
* import { appWindow, LogicalSize } from '@tauri-apps/window';
* await appWindow.setMaxSize(new LogicalSize(600, 500));
* import { getCurrent, LogicalSize } from '@tauri-apps/window';
* await getCurrent().setMaxSize(new LogicalSize(600, 500));
* ```
*
* @param size The logical or physical inner size, or `null` to unset the constraint.
@ -1099,8 +1217,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window outer position.
* @example
* ```typescript
* import { appWindow, LogicalPosition } from '@tauri-apps/window';
* await appWindow.setPosition(new LogicalPosition(600, 500));
* import { getCurrent, LogicalPosition } from '@tauri-apps/window';
* await getCurrent().setPosition(new LogicalPosition(600, 500));
* ```
*
* @param position The new position, in logical or physical pixels.
@ -1136,8 +1254,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window fullscreen state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setFullscreen(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setFullscreen(true);
* ```
*
* @param fullscreen Whether the window should go to fullscreen or not.
@ -1156,8 +1274,8 @@ class WindowManager extends WebviewWindowHandle {
* Bring the window to front and focus.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setFocus();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setFocus();
* ```
*
* @returns A promise indicating the success or failure of the operation.
@ -1174,8 +1292,8 @@ class WindowManager extends WebviewWindowHandle {
* Sets the window icon.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setIcon('/tauri/awesome.png');
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setIcon('/tauri/awesome.png');
* ```
*
* Note that you need the `icon-ico` or `icon-png` Cargo features to use this API.
@ -1205,8 +1323,8 @@ class WindowManager extends WebviewWindowHandle {
* - **macOS:** Unsupported.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setSkipTaskbar(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setSkipTaskbar(true);
* ```
*
* @param skip true to hide window icon, false to show it.
@ -1233,8 +1351,8 @@ class WindowManager extends WebviewWindowHandle {
* - **macOS:** This locks the cursor in a fixed location, which looks visually awkward.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setCursorGrab(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setCursorGrab(true);
* ```
*
* @param grab `true` to grab the cursor icon, `false` to release it.
@ -1259,8 +1377,8 @@ class WindowManager extends WebviewWindowHandle {
* outside of the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setCursorVisible(false);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setCursorVisible(false);
* ```
*
* @param visible If `false`, this will hide the cursor. If `true`, this will show the cursor.
@ -1279,8 +1397,8 @@ class WindowManager extends WebviewWindowHandle {
* Modifies the cursor icon of the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setCursorIcon('help');
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setCursorIcon('help');
* ```
*
* @param icon The new cursor icon.
@ -1299,8 +1417,8 @@ class WindowManager extends WebviewWindowHandle {
* Changes the position of the cursor in window coordinates.
* @example
* ```typescript
* import { appWindow, LogicalPosition } from '@tauri-apps/window';
* await appWindow.setCursorPosition(new LogicalPosition(600, 300));
* import { getCurrent, LogicalPosition } from '@tauri-apps/window';
* await getCurrent().setCursorPosition(new LogicalPosition(600, 300));
* ```
*
* @param position The new cursor position.
@ -1337,8 +1455,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.setIgnoreCursorEvents(true);
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().setIgnoreCursorEvents(true);
* ```
*
* @param ignore `true` to ignore the cursor events; `false` to process them as usual.
@ -1357,8 +1475,8 @@ class WindowManager extends WebviewWindowHandle {
* Starts dragging the window.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/window';
* await appWindow.startDragging();
* import { getCurrent } from '@tauri-apps/window';
* await getCurrent().startDragging();
* ```
*
* @return A promise indicating the success or failure of the operation.
@ -1378,8 +1496,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onResized(({ payload: size }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onResized(({ payload: size }) => {
* console.log('Window resized', size);
* });
*
@ -1404,8 +1522,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onMoved(({ payload: position }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onMoved(({ payload: position }) => {
* console.log('Window moved', position);
* });
*
@ -1430,9 +1548,9 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* import { getCurrent } from "@tauri-apps/plugin-window";
* import { confirm } from '@tauri-apps/api/dialog';
* const unlisten = await appWindow.onCloseRequested(async (event) => {
* const unlisten = await getCurrent().onCloseRequested(async (event) => {
* const confirmed = await confirm('Are you sure?');
* if (!confirmed) {
* // user did not confirm closing the window; let's prevent it
@ -1469,8 +1587,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onFocusChanged(({ payload: focused }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onFocusChanged(({ payload: focused }) => {
* console.log('Focus changed, window is focused? ' + focused);
* });
*
@ -1511,8 +1629,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onScaleChanged(({ payload }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onScaleChanged(({ payload }) => {
* console.log('Scale changed', payload.scaleFactor, payload.size);
* });
*
@ -1539,8 +1657,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onMenuClicked(({ payload: menuId }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onMenuClicked(({ payload: menuId }) => {
* console.log('Menu clicked: ' + menuId);
* });
*
@ -1564,8 +1682,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onFileDropEvent((event) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onFileDropEvent((event) => {
* if (event.payload.type === 'hover') {
* console.log('User hovering', event.payload.paths);
* } else if (event.payload.type === 'drop') {
@ -1620,8 +1738,8 @@ class WindowManager extends WebviewWindowHandle {
*
* @example
* ```typescript
* import { appWindow } from "@tauri-apps/plugin-window";
* const unlisten = await appWindow.onThemeChanged(({ payload: theme }) => {
* import { getCurrent } from "@tauri-apps/plugin-window";
* const unlisten = await getCurrent().onThemeChanged(({ payload: theme }) => {
* console.log('New theme: ' + theme);
* });
*
@ -1639,146 +1757,6 @@ class WindowManager extends WebviewWindowHandle {
}
}
/**
* @since 2.0.0
*/
class CloseRequestedEvent {
/** Event name */
event: EventName;
/** The label of the window that emitted this event. */
windowLabel: string;
/** Event identifier used to unlisten */
id: number;
private _preventDefault = false;
constructor(event: Event<null>) {
this.event = event.event;
this.windowLabel = event.windowLabel;
this.id = event.id;
}
preventDefault(): void {
this._preventDefault = true;
}
isPreventDefault(): boolean {
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 2.0.0
*/
class WebviewWindow extends WindowManager {
/**
* Creates a new WebviewWindow.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/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.
*
* @since 2.0.0
*/
constructor(label: WindowLabel, options: WindowOptions = {}) {
super(label);
// @ts-expect-error `skip` is not a public API so it is not defined in WindowOptions
if (!options?.skip) {
window
.__TAURI_INVOKE__("plugin:window|create", {
options: {
...options,
label,
},
})
.then(async () => this.emit("tauri://created"))
.catch(async (e: string) => this.emit("tauri://error", e));
}
}
/**
* Gets the WebviewWindow for the webview associated with the given label.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/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.
*
* @since 2.0.0
*/
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 null;
}
}
/** The WebviewWindow for the current 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,
}
);
} 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", {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
skip: true,
});
}
/**
* Configuration for the window to create.
*
@ -1958,13 +1936,9 @@ async function availableMonitors(): Promise<Monitor[]> {
}
export {
WebviewWindow,
WebviewWindowHandle,
WindowManager,
CloseRequestedEvent,
getCurrent,
getAll,
appWindow,
LogicalSize,
PhysicalSize,
LogicalPosition,

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save