|
@@ -9,86 +9,32 @@ import { PhysicalPosition, PhysicalSize } from './dpi'
|
|
|
|
|
|
export type MouseButtonState = 'Up' | 'Down'
|
|
|
export type MouseButton = 'Left' | 'Right' | 'Middle'
|
|
|
+export type TrayIconEventType =
|
|
|
+ | 'Click'
|
|
|
+ | 'DoubleClick'
|
|
|
+ | 'Enter'
|
|
|
+ | 'Move'
|
|
|
+ | 'Leave'
|
|
|
|
|
|
-/** A click happened on the tray icon. */
|
|
|
-export interface TrayIconClickEvent {
|
|
|
+export type TrayIconEventBase<T extends TrayIconEventType> = {
|
|
|
+ /** The tray icon event type */
|
|
|
+ type: T
|
|
|
/** Id of the tray icon which triggered this event. */
|
|
|
id: string
|
|
|
- /** Physical X Position of the click the triggered this event. */
|
|
|
- x: number
|
|
|
- /** Physical Y Position of the click the triggered this event. */
|
|
|
- y: number
|
|
|
+ /** Physical position of the click the triggered this event. */
|
|
|
+ position: PhysicalPosition
|
|
|
/** Position and size of the tray icon. */
|
|
|
rect: {
|
|
|
position: PhysicalPosition
|
|
|
size: PhysicalSize
|
|
|
}
|
|
|
- /** Mouse button that triggered this event. */
|
|
|
- button: MouseButton
|
|
|
- /** Mouse button state when this event was triggered. */
|
|
|
- buttonState: MouseButtonState
|
|
|
}
|
|
|
|
|
|
-/** A double click happened on the tray icon. **Windows Only** */
|
|
|
-export interface TrayIconDoubleClickEvent {
|
|
|
- /** Id of the tray icon which triggered this event. */
|
|
|
- id: string
|
|
|
- /** Physical X Position of the click the triggered this event. */
|
|
|
- x: number
|
|
|
- /** Physical Y Position of the click the triggered this event. */
|
|
|
- y: number
|
|
|
- /** Position and size of the tray icon. */
|
|
|
- rect: {
|
|
|
- position: PhysicalPosition
|
|
|
- size: PhysicalSize
|
|
|
- }
|
|
|
+export type TrayIconClickEvent = {
|
|
|
/** Mouse button that triggered this event. */
|
|
|
button: MouseButton
|
|
|
-}
|
|
|
-
|
|
|
-/** The mouse entered the tray icon region. */
|
|
|
-export interface TrayIconEnterEvent {
|
|
|
- /** Id of the tray icon which triggered this event. */
|
|
|
- id: string
|
|
|
- /** Physical X Position of the click the triggered this event. */
|
|
|
- x: number
|
|
|
- /** Physical Y Position of the click the triggered this event. */
|
|
|
- y: number
|
|
|
- /** Position and size of the tray icon. */
|
|
|
- rect: {
|
|
|
- position: PhysicalPosition
|
|
|
- size: PhysicalSize
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/** The mouse moved over the tray icon region. */
|
|
|
-export interface TrayIconMoveEvent {
|
|
|
- /** Id of the tray icon which triggered this event. */
|
|
|
- id: string
|
|
|
- /** Physical X Position of the click the triggered this event. */
|
|
|
- x: number
|
|
|
- /** Physical Y Position of the click the triggered this event. */
|
|
|
- y: number
|
|
|
- /** Position and size of the tray icon. */
|
|
|
- rect: {
|
|
|
- position: PhysicalPosition
|
|
|
- size: PhysicalSize
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/** The mouse left the tray icon region. */
|
|
|
-export interface TrayIconLeaveEvent {
|
|
|
- /** Id of the tray icon which triggered this event. */
|
|
|
- id: string
|
|
|
- /** Physical X Position of the click the triggered this event. */
|
|
|
- x: number
|
|
|
- /** Physical Y Position of the click the triggered this event. */
|
|
|
- y: number
|
|
|
- /** Position and size of the tray icon. */
|
|
|
- rect: {
|
|
|
- position: PhysicalPosition
|
|
|
- size: PhysicalSize
|
|
|
- }
|
|
|
+ /** Mouse button state when this event was triggered. */
|
|
|
+ buttonState: MouseButtonState
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -100,11 +46,22 @@ export interface TrayIconLeaveEvent {
|
|
|
* the icon will still show a context menu on right click.
|
|
|
*/
|
|
|
export type TrayIconEvent =
|
|
|
- | { click: TrayIconClickEvent }
|
|
|
- | { doubleClick: TrayIconDoubleClickEvent }
|
|
|
- | { enter: TrayIconEnterEvent }
|
|
|
- | { move: TrayIconMoveEvent }
|
|
|
- | { leave: TrayIconLeaveEvent }
|
|
|
+ | (TrayIconEventBase<'Click'> & TrayIconClickEvent)
|
|
|
+ | (TrayIconEventBase<'DoubleClick'> & Omit<TrayIconClickEvent, 'buttonState'>)
|
|
|
+ | TrayIconEventBase<'Enter'>
|
|
|
+ | TrayIconEventBase<'Move'>
|
|
|
+ | TrayIconEventBase<'Leave'>
|
|
|
+
|
|
|
+type RustTrayIconEvent = Omit<TrayIconEvent, 'rect'> & {
|
|
|
+ rect: {
|
|
|
+ position: {
|
|
|
+ Physical: { x: number; y: number }
|
|
|
+ }
|
|
|
+ size: {
|
|
|
+ Physical: { width: number; height: number }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* Tray icon types and utilities.
|
|
@@ -223,38 +180,10 @@ export class TrayIcon extends Resource {
|
|
|
options.icon = transformImage(options.icon)
|
|
|
}
|
|
|
|
|
|
- const handler = new Channel<TrayIconEvent>()
|
|
|
+ const handler = new Channel<RustTrayIconEvent>()
|
|
|
if (options?.action) {
|
|
|
const action = options.action
|
|
|
- handler.onmessage = (e) => {
|
|
|
- if ('click' in e) {
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.click.rect.position = mapPosition(e.click.rect.position)
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.click.rect.size = mapSize(e.click.rect.size)
|
|
|
- } else if ('doubleClick' in e) {
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.doubleClick.rect.position = mapPosition(e.doubleClick.rect.position)
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.doubleClick.rect.size = mapSize(e.doubleClick.rect.size)
|
|
|
- } else if ('enter' in e) {
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.enter.rect.position = mapPosition(e.enter.rect.position)
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.enter.rect.size = mapSize(e.enter.rect.size)
|
|
|
- } else if ('move' in e) {
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.move.rect.position = mapPosition(e.move.rect.position)
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.move.rect.size = mapSize(e.move.rect.size)
|
|
|
- } else if ('leave' in e) {
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.leave.rect.position = mapPosition(e.leave.rect.position)
|
|
|
- // @ts-expect-error `TrayIconEvent` doesn't quite match the value yet so we reconstruct the incorrect fields
|
|
|
- e.leave.rect.size = mapSize(e.leave.rect.size)
|
|
|
- }
|
|
|
- action(e)
|
|
|
- }
|
|
|
+ handler.onmessage = (e) => action(mapEvent(e))
|
|
|
delete options.action
|
|
|
}
|
|
|
|
|
@@ -358,13 +287,19 @@ export class TrayIcon extends Resource {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function mapPosition(pos: {
|
|
|
- Physical: { x: number; y: number }
|
|
|
-}): PhysicalPosition {
|
|
|
- return new PhysicalPosition(pos.Physical.x, pos.Physical.y)
|
|
|
-}
|
|
|
-function mapSize(pos: {
|
|
|
- Physical: { width: number; height: number }
|
|
|
-}): PhysicalSize {
|
|
|
- return new PhysicalSize(pos.Physical.width, pos.Physical.height)
|
|
|
+function mapEvent(e: RustTrayIconEvent): TrayIconEvent {
|
|
|
+ const out = e as unknown as TrayIconEvent
|
|
|
+
|
|
|
+ out.position = new PhysicalPosition(e.position.x, e.position.y)
|
|
|
+
|
|
|
+ out.rect.position = new PhysicalPosition(
|
|
|
+ e.rect.position.Physical.x,
|
|
|
+ e.rect.position.Physical.y
|
|
|
+ )
|
|
|
+ out.rect.size = new PhysicalSize(
|
|
|
+ e.rect.size.Physical.width,
|
|
|
+ e.rect.size.Physical.height
|
|
|
+ )
|
|
|
+
|
|
|
+ return out
|
|
|
}
|