Skip to content

Commit cbe3bd8

Browse files
fix(api): convert Position to PhsyicalPosition for TrayIconEvent (#11104)
* fix(api): convert `Position` to `PhsyicalPosition` for `TrayIconEvent` ref: tauri-apps/plugins-workspace#1822 (comment) * fix lint * Add missing `doubleClick` event type * change file * update lockfile * Update .changes/api-tray-icon-event-value-mismatch-type.md [skip ci] --------- Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
1 parent ae12f3c commit cbe3bd8

File tree

6 files changed

+174
-97
lines changed

6 files changed

+174
-97
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Add missing `TrayIconDoubleClickEvent` type and `doubleClick` variant in `TrayIconEvent` type.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Fix invalid value passed to the callback `action` function that doesn't match the `TrayIconEvent` type.
6+

crates/tauri/scripts/bundle.global.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.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@
2525
"devDependencies": {
2626
"prettier": "^3.3.3"
2727
},
28-
"packageManager": "[email protected]"
28+
"packageManager": "[email protected]",
29+
"pnpm": {
30+
"overrides": {
31+
"rollup@>=4.0.0 <4.22.4": ">=4.22.4"
32+
}
33+
}
2934
}

packages/api/src/tray.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ export interface TrayIconClickEvent {
2929
button_state: MouseButtonState
3030
}
3131

32+
/** A double click happened on the tray icon. **Windows Only** */
33+
export interface TrayIconDoubleClickEvent {
34+
/** Id of the tray icon which triggered this event. */
35+
id: string
36+
/** Physical X Position of the click the triggered this event. */
37+
x: number
38+
/** Physical Y Position of the click the triggered this event. */
39+
y: number
40+
/** Position and size of the tray icon. */
41+
rect: {
42+
position: PhysicalPosition
43+
size: PhysicalSize
44+
}
45+
/** Mouse button that triggered this event. */
46+
button: MouseButton
47+
}
48+
3249
/** The mouse entered the tray icon region. */
3350
export interface TrayIconEnterEvent {
3451
/** Id of the tray icon which triggered this event. */
@@ -84,6 +101,7 @@ export interface TrayIconLeaveEvent {
84101
*/
85102
export type TrayIconEvent =
86103
| { click: TrayIconClickEvent }
104+
| { doubleClick: TrayIconDoubleClickEvent }
87105
| { enter: TrayIconEnterEvent }
88106
| { move: TrayIconMoveEvent }
89107
| { leave: TrayIconLeaveEvent }
@@ -207,7 +225,36 @@ export class TrayIcon extends Resource {
207225

208226
const handler = new Channel<TrayIconEvent>()
209227
if (options?.action) {
210-
handler.onmessage = options.action
228+
const action = options.action
229+
handler.onmessage = (e) => {
230+
if ('click' in e) {
231+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
232+
e.click.rect.position = mapPosition(e.click.rect.position)
233+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
234+
e.click.rect.size = mapSize(e.click.rect.size)
235+
} else if ('doubleClick' in e) {
236+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
237+
e.doubleClick.rect.position = mapPosition(e.doubleClick.rect.position)
238+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
239+
e.doubleClick.rect.size = mapSize(e.doubleClick.rect.size)
240+
} else if ('enter' in e) {
241+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
242+
e.enter.rect.position = mapPosition(e.enter.rect.position)
243+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
244+
e.enter.rect.size = mapSize(e.enter.rect.size)
245+
} else if ('move' in e) {
246+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
247+
e.move.rect.position = mapPosition(e.move.rect.position)
248+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
249+
e.move.rect.size = mapSize(e.move.rect.size)
250+
} else if ('leave' in e) {
251+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
252+
e.leave.rect.position = mapPosition(e.leave.rect.position)
253+
// @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
254+
e.leave.rect.size = mapSize(e.leave.rect.size)
255+
}
256+
action(e)
257+
}
211258
delete options.action
212259
}
213260

@@ -310,3 +357,14 @@ export class TrayIcon extends Resource {
310357
})
311358
}
312359
}
360+
361+
function mapPosition(pos: {
362+
Physical: { x: number; y: number }
363+
}): PhysicalPosition {
364+
return new PhysicalPosition(pos.Physical.x, pos.Physical.y)
365+
}
366+
function mapSize(pos: {
367+
Physical: { width: number; height: number }
368+
}): PhysicalSize {
369+
return new PhysicalSize(pos.Physical.width, pos.Physical.height)
370+
}

0 commit comments

Comments
 (0)