Skip to content

Commit 5d7626f

Browse files
authored
feat(api): WindowManager extends WebviewWindowHandle, add events docs (#2146)
1 parent acb8892 commit 5d7626f

5 files changed

Lines changed: 93 additions & 10 deletions

File tree

.changes/appwindow-events.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
You can now use `emit`, `listen` and `once` using the `appWindow` exported by the window module.

core/tauri-runtime/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub enum WindowEvent {
3232
///
3333
/// The parameter is true if the window has gained focus, and false if it has lost focus.
3434
Focused(bool),
35-
///The window's scale factor has changed.
35+
/// The window's scale factor has changed.
3636
///
3737
/// The following user actions can cause DPI changes:
3838
///

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/event.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ interface Event<T> {
2222
payload: T
2323
}
2424

25+
type EventName =
26+
| 'tauri://update'
27+
| 'tauri://update-available'
28+
| 'tauri://update-install'
29+
| 'tauri://update-status'
30+
| 'tauri://resize'
31+
| 'tauri://move'
32+
| 'tauri://close-requested'
33+
| 'tauri://destroyed'
34+
| 'tauri://focus'
35+
| 'tauri://blur'
36+
| 'tauri://scale-change'
37+
| 'tauri://menu'
38+
| 'tauri://file-drop'
39+
| 'tauri://file-drop-hover'
40+
| 'tauri://file-drop-cancelled'
41+
| string
42+
2543
type EventCallback<T> = (event: Event<T>) => void
2644

2745
type UnlistenFn = () => void
@@ -51,7 +69,7 @@ async function _unlisten(eventId: number): Promise<void> {
5169
* @return A promise resolving to a function to unlisten to the event.
5270
*/
5371
async function listen<T>(
54-
event: string,
72+
event: EventName,
5573
handler: EventCallback<T>
5674
): Promise<UnlistenFn> {
5775
return invokeTauriCommand<number>({
@@ -74,7 +92,7 @@ async function listen<T>(
7492
* @returns A promise resolving to a function to unlisten to the event.
7593
*/
7694
async function once<T>(
77-
event: string,
95+
event: EventName,
7896
handler: EventCallback<T>
7997
): Promise<UnlistenFn> {
8098
return listen<T>(event, (eventData) => {
@@ -94,6 +112,6 @@ async function emit(event: string, payload?: string): Promise<void> {
94112
return emitEvent(event, undefined, payload)
95113
}
96114

97-
export type { Event, EventCallback, UnlistenFn }
115+
export type { Event, EventName, EventCallback, UnlistenFn }
98116

99117
export { listen, once, emit }

tooling/api/src/window.ts

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,71 @@
2121
* }
2222
* ```
2323
* 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+
*
2484
* @packageDocumentation
2585
*/
2686

2787
import { invokeTauriCommand } from './helpers/tauri'
28-
import { EventCallback, UnlistenFn, listen, once } from './event'
88+
import { EventName, EventCallback, UnlistenFn, listen, once } from './event'
2989
import { emit } from './helpers/event'
3090

3191
/** Allows you to retrieve information about a given monitor. */
@@ -174,7 +234,7 @@ class WebviewWindowHandle {
174234
* @returns A promise resolving to a function to unlisten to the event.
175235
*/
176236
async listen<T>(
177-
event: string,
237+
event: EventName,
178238
handler: EventCallback<T>
179239
): Promise<UnlistenFn> {
180240
if (this._handleTauriEvent(event, handler)) {
@@ -300,7 +360,7 @@ class WebviewWindow extends WebviewWindowHandle {
300360
/**
301361
* Manage the current window object.
302362
*/
303-
class WindowManager {
363+
class WindowManager extends WebviewWindowHandle {
304364
// Getters
305365
/** The scale factor that can be used to map physical pixels to logical pixels. */
306366
async scaleFactor(): Promise<number> {
@@ -842,8 +902,8 @@ class WindowManager {
842902
}
843903
}
844904

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)
847907

848908
/** Configuration for the window to create. */
849909
interface WindowOptions {

0 commit comments

Comments
 (0)