@@ -9,86 +9,32 @@ import { PhysicalPosition, PhysicalSize } from './dpi'
99
1010export type MouseButtonState = 'Up' | 'Down'
1111export type MouseButton = 'Left' | 'Right' | 'Middle'
12+ export type TrayIconEventType =
13+ | 'Click'
14+ | 'DoubleClick'
15+ | 'Enter'
16+ | 'Move'
17+ | 'Leave'
1218
13- /** A click happened on the tray icon. */
14- export interface TrayIconClickEvent {
19+ export type TrayIconEventBase < T extends TrayIconEventType > = {
20+ /** The tray icon event type */
21+ type : T
1522 /** Id of the tray icon which triggered this event. */
1623 id : string
17- /** Physical X Position of the click the triggered this event. */
18- x : number
19- /** Physical Y Position of the click the triggered this event. */
20- y : number
24+ /** Physical position of the click the triggered this event. */
25+ position : PhysicalPosition
2126 /** Position and size of the tray icon. */
2227 rect : {
2328 position : PhysicalPosition
2429 size : PhysicalSize
2530 }
26- /** Mouse button that triggered this event. */
27- button : MouseButton
28- /** Mouse button state when this event was triggered. */
29- buttonState : MouseButtonState
3031}
3132
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- }
33+ export type TrayIconClickEvent = {
4534 /** Mouse button that triggered this event. */
4635 button : MouseButton
47- }
48-
49- /** The mouse entered the tray icon region. */
50- export interface TrayIconEnterEvent {
51- /** Id of the tray icon which triggered this event. */
52- id : string
53- /** Physical X Position of the click the triggered this event. */
54- x : number
55- /** Physical Y Position of the click the triggered this event. */
56- y : number
57- /** Position and size of the tray icon. */
58- rect : {
59- position : PhysicalPosition
60- size : PhysicalSize
61- }
62- }
63-
64- /** The mouse moved over the tray icon region. */
65- export interface TrayIconMoveEvent {
66- /** Id of the tray icon which triggered this event. */
67- id : string
68- /** Physical X Position of the click the triggered this event. */
69- x : number
70- /** Physical Y Position of the click the triggered this event. */
71- y : number
72- /** Position and size of the tray icon. */
73- rect : {
74- position : PhysicalPosition
75- size : PhysicalSize
76- }
77- }
78-
79- /** The mouse left the tray icon region. */
80- export interface TrayIconLeaveEvent {
81- /** Id of the tray icon which triggered this event. */
82- id : string
83- /** Physical X Position of the click the triggered this event. */
84- x : number
85- /** Physical Y Position of the click the triggered this event. */
86- y : number
87- /** Position and size of the tray icon. */
88- rect : {
89- position : PhysicalPosition
90- size : PhysicalSize
91- }
36+ /** Mouse button state when this event was triggered. */
37+ buttonState : MouseButtonState
9238}
9339
9440/**
@@ -100,11 +46,22 @@ export interface TrayIconLeaveEvent {
10046 * the icon will still show a context menu on right click.
10147 */
10248export type TrayIconEvent =
103- | { click : TrayIconClickEvent }
104- | { doubleClick : TrayIconDoubleClickEvent }
105- | { enter : TrayIconEnterEvent }
106- | { move : TrayIconMoveEvent }
107- | { leave : TrayIconLeaveEvent }
49+ | ( TrayIconEventBase < 'Click' > & TrayIconClickEvent )
50+ | ( TrayIconEventBase < 'DoubleClick' > & Omit < TrayIconClickEvent , 'buttonState' > )
51+ | TrayIconEventBase < 'Enter' >
52+ | TrayIconEventBase < 'Move' >
53+ | TrayIconEventBase < 'Leave' >
54+
55+ type RustTrayIconEvent = Omit < TrayIconEvent , 'rect' > & {
56+ rect : {
57+ position : {
58+ Physical : { x : number ; y : number }
59+ }
60+ size : {
61+ Physical : { width : number ; height : number }
62+ }
63+ }
64+ }
10865
10966/**
11067 * Tray icon types and utilities.
@@ -223,38 +180,10 @@ export class TrayIcon extends Resource {
223180 options . icon = transformImage ( options . icon )
224181 }
225182
226- const handler = new Channel < TrayIconEvent > ( )
183+ const handler = new Channel < RustTrayIconEvent > ( )
227184 if ( options ?. action ) {
228185 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- }
186+ handler . onmessage = ( e ) => action ( mapEvent ( e ) )
258187 delete options . action
259188 }
260189
@@ -358,13 +287,19 @@ export class TrayIcon extends Resource {
358287 }
359288}
360289
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 )
290+ function mapEvent ( e : RustTrayIconEvent ) : TrayIconEvent {
291+ const out = e as unknown as TrayIconEvent
292+
293+ out . position = new PhysicalPosition ( e . position . x , e . position . y )
294+
295+ out . rect . position = new PhysicalPosition (
296+ e . rect . position . Physical . x ,
297+ e . rect . position . Physical . y
298+ )
299+ out . rect . size = new PhysicalSize (
300+ e . rect . size . Physical . width ,
301+ e . rect . size . Physical . height
302+ )
303+
304+ return out
370305}
0 commit comments