|
21 | 21 | * } |
22 | 22 | * ``` |
23 | 23 | * It is recommended to allowlist only the APIs you use for optimal bundle size and security. |
| 24 | + * |
| 25 | + * # Window events |
| 26 | + * |
| 27 | + * Events can be listened using `appWindow.listen`: |
| 28 | + * ```typescript |
| 29 | + * import { appWindow } from '@tauri-apps/api/window' |
| 30 | + * appWindow.listen('tauri://move', ({ event, payload }) => { |
| 31 | + * const { x, y } = payload // payload here is a `PhysicalPosition` |
| 32 | + * }) |
| 33 | + * ``` |
| 34 | + * |
| 35 | + * Window-specific events emitted by the backend: |
| 36 | + * |
| 37 | + * #### 'tauri://resize' |
| 38 | + * Emitted when the size of the window has changed. |
| 39 | + * *EventPayload*: |
| 40 | + * ```typescript |
| 41 | + * type ResizePayload = PhysicalSize |
| 42 | + * ``` |
| 43 | + * |
| 44 | + * #### 'tauri://move' |
| 45 | + * Emitted when the position of the window has changed. |
| 46 | + * *EventPayload*: |
| 47 | + * ```typescript |
| 48 | + * type MovePayload = PhysicalPosition |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * #### 'tauri://close-requested' |
| 52 | + * Emitted when the user requests the window to be closed. |
| 53 | + * |
| 54 | + * #### 'tauri://destroyed' |
| 55 | + * Emitted after the window is closed. |
| 56 | + * |
| 57 | + * #### 'tauri://focus' |
| 58 | + * Emitted when the window gains focus. |
| 59 | + * |
| 60 | + * #### 'tauri://blur' |
| 61 | + * Emitted when the window loses focus. |
| 62 | + * |
| 63 | + * #### 'tauri://scale-change' |
| 64 | + * Emitted when the window's scale factor has changed. |
| 65 | + * The following user actions can cause DPI changes: |
| 66 | + * - Changing the display's resolution. |
| 67 | + * - Changing the display's scale factor (e.g. in Control Panel on Windows). |
| 68 | + * - Moving the window to a display with a different scale factor. |
| 69 | + * *Event payload*: |
| 70 | + * ```typescript |
| 71 | + * interface ScaleFactorChanged { |
| 72 | + * scaleFactor: number |
| 73 | + * size: PhysicalSize |
| 74 | + * } |
| 75 | + * ``` |
| 76 | + * |
| 77 | + * #### 'tauri://menu' |
| 78 | + * Emitted when a menu item is clicked. |
| 79 | + * *EventPayload*: |
| 80 | + * ```typescript |
| 81 | + * type MenuClicked = string |
| 82 | + * ``` |
| 83 | + * |
24 | 84 | * @packageDocumentation |
25 | 85 | */ |
26 | 86 |
|
27 | 87 | import { invokeTauriCommand } from './helpers/tauri' |
28 | | -import { EventCallback, UnlistenFn, listen, once } from './event' |
| 88 | +import { EventName, EventCallback, UnlistenFn, listen, once } from './event' |
29 | 89 | import { emit } from './helpers/event' |
30 | 90 |
|
31 | 91 | /** Allows you to retrieve information about a given monitor. */ |
@@ -174,7 +234,7 @@ class WebviewWindowHandle { |
174 | 234 | * @returns A promise resolving to a function to unlisten to the event. |
175 | 235 | */ |
176 | 236 | async listen<T>( |
177 | | - event: string, |
| 237 | + event: EventName, |
178 | 238 | handler: EventCallback<T> |
179 | 239 | ): Promise<UnlistenFn> { |
180 | 240 | if (this._handleTauriEvent(event, handler)) { |
@@ -300,7 +360,7 @@ class WebviewWindow extends WebviewWindowHandle { |
300 | 360 | /** |
301 | 361 | * Manage the current window object. |
302 | 362 | */ |
303 | | -class WindowManager { |
| 363 | +class WindowManager extends WebviewWindowHandle { |
304 | 364 | // Getters |
305 | 365 | /** The scale factor that can be used to map physical pixels to logical pixels. */ |
306 | 366 | async scaleFactor(): Promise<number> { |
@@ -842,8 +902,8 @@ class WindowManager { |
842 | 902 | } |
843 | 903 | } |
844 | 904 |
|
845 | | -/** The manager for the current window. Allows you to manipulate the window object. */ |
846 | | -const appWindow = new WindowManager() |
| 905 | +/** The manager for the current window. Allows you to manipulate the window object, listen and emit events. */ |
| 906 | +const appWindow = new WindowManager(window.__TAURI__.__currentWindow.label) |
847 | 907 |
|
848 | 908 | /** Configuration for the window to create. */ |
849 | 909 | interface WindowOptions { |
|
0 commit comments