|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | +import { UnsupportedPlatformError } from '../../common/errors'; |
| 3 | +import { currentPlatform } from '../../common/platform'; |
| 4 | +import { linuxNotificationBackend } from '../platform/linux/gtk-notification'; |
| 5 | +import { macosNotificationBackend } from '../platform/macos/cocoa-notification'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Native desktop notifications — the drop-in equivalent of Electron's |
| 9 | + * `Notification`. |
| 10 | + * |
| 11 | + * Extends Node's {@link EventEmitter} so the full listener API |
| 12 | + * (`on`/`once`/`addListener`/…) matches Electron's contract. Events: |
| 13 | + * - `show` — emitted synchronously from {@link Notification.show}. |
| 14 | + * - `close` — emitted when the OS reports the notification was dismissed/closed, |
| 15 | + * IF the platform backend can wire it (Linux libnotify exposes a `closed` |
| 16 | + * signal; macOS un-bundled cannot, so it is best-effort there). |
| 17 | + * |
| 18 | + * `click` (and other user-action events) are DEFERRED in v1: they require an OS |
| 19 | + * delegate/action wiring that is not yet implemented. They are intentionally not |
| 20 | + * advertised so consumers do not rely on events Sambar does not deliver. |
| 21 | + * |
| 22 | + * The native backend is injectable (mirrors `menu`/`dialog`/`shell`) so the |
| 23 | + * class's option-mapping, event wiring, and lifecycle are unit-testable with a |
| 24 | + * fake — no FFI required. |
| 25 | + */ |
| 26 | + |
| 27 | +export type NotificationOptions = { |
| 28 | + readonly title?: string; |
| 29 | + readonly body?: string; |
| 30 | + readonly subtitle?: string; |
| 31 | + readonly silent?: boolean; |
| 32 | +}; |
| 33 | + |
| 34 | +/** The fields a backend needs to present one notification. */ |
| 35 | +export type NotificationSpec = { |
| 36 | + readonly title: string; |
| 37 | + readonly body: string; |
| 38 | + readonly subtitle: string; |
| 39 | + readonly silent: boolean; |
| 40 | +}; |
| 41 | + |
| 42 | +/** A live, presented notification the API can close and observe. */ |
| 43 | +export type NotificationHandle = { |
| 44 | + /** Dismiss the notification. Safe to call more than once. */ |
| 45 | + close(): void; |
| 46 | + /** Register a callback fired when the OS closes/dismisses the notification. */ |
| 47 | + onClosed(callback: () => void): void; |
| 48 | +}; |
| 49 | + |
| 50 | +/** The native backend the public `Notification` API delegates to. */ |
| 51 | +export type NotificationBackend = { |
| 52 | + /** The HONEST per-platform answer to whether notifications can be delivered. */ |
| 53 | + isSupported(): boolean; |
| 54 | + /** Present a notification and return a handle to close/observe it. */ |
| 55 | + present(spec: NotificationSpec): NotificationHandle; |
| 56 | +}; |
| 57 | + |
| 58 | +const macosBackend: NotificationBackend = macosNotificationBackend; |
| 59 | +const linuxBackend: NotificationBackend = linuxNotificationBackend; |
| 60 | + |
| 61 | +let backend: NotificationBackend | undefined; |
| 62 | + |
| 63 | +const getBackend = (): NotificationBackend => { |
| 64 | + if (backend !== undefined) { |
| 65 | + return backend; |
| 66 | + } |
| 67 | + if (currentPlatform() === 'macos') { |
| 68 | + return macosBackend; |
| 69 | + } |
| 70 | + if (currentPlatform() === 'linux') { |
| 71 | + return linuxBackend; |
| 72 | + } |
| 73 | + throw new UnsupportedPlatformError(`Notification is not supported on ${currentPlatform()} yet`); |
| 74 | +}; |
| 75 | + |
| 76 | +/** Override the native notification backend. Test-only. */ |
| 77 | +export const setNotificationBackendForTesting = (fake: NotificationBackend | undefined): void => { |
| 78 | + backend = fake; |
| 79 | +}; |
| 80 | + |
| 81 | +export class Notification extends EventEmitter { |
| 82 | + /** Notification title (the bold first line). */ |
| 83 | + title: string; |
| 84 | + /** Notification body text. */ |
| 85 | + body: string; |
| 86 | + /** Secondary line shown under the title (macOS; ignored where unsupported). */ |
| 87 | + subtitle: string; |
| 88 | + /** Whether to suppress the notification sound. */ |
| 89 | + silent: boolean; |
| 90 | + |
| 91 | + #handle: NotificationHandle | undefined; |
| 92 | + |
| 93 | + constructor(options: NotificationOptions = {}) { |
| 94 | + super(); |
| 95 | + this.title = options.title ?? ''; |
| 96 | + this.body = options.body ?? ''; |
| 97 | + this.subtitle = options.subtitle ?? ''; |
| 98 | + this.silent = options.silent ?? false; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Whether the host platform can actually deliver notifications. Honest: |
| 103 | + * - Linux: libnotify loaded and `notify_init` succeeded. |
| 104 | + * - macOS: `false` un-bundled (the default notification center is nil without |
| 105 | + * an app bundle); reliable delivery needs packaging (a follow-up). |
| 106 | + */ |
| 107 | + static isSupported(): boolean { |
| 108 | + return getBackend().isSupported(); |
| 109 | + } |
| 110 | + |
| 111 | + /** Display the notification and emit `show`. */ |
| 112 | + show(): void { |
| 113 | + const handle = getBackend().present({ |
| 114 | + title: this.title, |
| 115 | + body: this.body, |
| 116 | + subtitle: this.subtitle, |
| 117 | + silent: this.silent, |
| 118 | + }); |
| 119 | + this.#handle = handle; |
| 120 | + handle.onClosed(() => { |
| 121 | + this.emit('close'); |
| 122 | + }); |
| 123 | + this.emit('show'); |
| 124 | + } |
| 125 | + |
| 126 | + /** Dismiss the notification if it is showing. Idempotent. */ |
| 127 | + close(): void { |
| 128 | + const handle = this.#handle; |
| 129 | + if (handle === undefined) { |
| 130 | + return; |
| 131 | + } |
| 132 | + this.#handle = undefined; |
| 133 | + handle.close(); |
| 134 | + } |
| 135 | +} |
0 commit comments