Skip to content

Commit 2d57f99

Browse files
committed
feat: show notifications on connection/disconnection
1 parent 8dbfea2 commit 2d57f99

7 files changed

Lines changed: 90 additions & 6 deletions

File tree

‎locales.toml‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,36 @@ en = "Toggle connection with the text editor"
3232
ja = "テキストエディタとの接続を切り替え"
3333

3434
[options_server_port]
35+
# Description of the option item for the server port.
3536
en = "GhostText destination port"
3637
ja = "GhostText接続先ポート"
3738

3839
[options_enable_notifications]
40+
# Description of the option item for enabling notifications.
3941
en = "Show notifications"
4042
ja = "通知を表示する"
4143

4244
[options_saved]
43-
en = "Options saved"
45+
# Message shown when the options has been saved successfully.
46+
en = "Saved"
4447
ja = "保存しました"
4548

4649
[options_error]
50+
# Message shown when failed to save the options.
4751
en = "Error saving options"
4852
ja = "保存に失敗しました"
53+
54+
[src_client_status_active]
55+
# Message shown when Ghostbird has connected to the server.
56+
en = "Connected to the text editor"
57+
ja = "テキストエディタに接続しました"
58+
59+
[src_client_status_inactive]
60+
# Message shown when Ghostbird has disconnected from the server.
61+
en = "Disconnected from the text editor"
62+
ja = "テキストエディタから切断しました"
63+
64+
[src_client_status_error]
65+
# Message shown when Ghostbird has some problem connecting to the server.
66+
en = "Failed to connect to the text editor"
67+
ja = "テキストエディタに接続できませんでした"

‎manifest_template.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"permissions": [
2323
"alarms",
2424
"compose",
25+
"notifications",
2526
"scripting",
2627
"storage"
2728
],

‎src/ghosttext-adaptor/api.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import type { IMessagePort } from "src/ghosttext-runner"
2+
import type { MessageId } from "src/util"
3+
4+
/** Wraps the Notification API */
5+
export interface INotificationTray {
6+
/** Shows a notification */
7+
showNotification(iconUrl: string, messageId: MessageId): Promise<void>
8+
}
29

310
/** Wrapper for `fetch` and `WebSocket` */
411
export interface IWebClient {

‎src/ghosttext-adaptor/email_editor.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@ import type {
77
IStatusIndicator,
88
} from "src/ghosttext-runner"
99
import type { EmailState } from "src/ghosttext-session"
10-
import type { IComposeWindow, IGhostServerPort } from "./api"
10+
import type { MessageId } from "src/util"
11+
import type { IComposeWindow, IGhostServerPort, INotificationTray } from "./api"
1112

1213
export class EmailEditor implements IClientEditor, IStatusIndicator {
14+
private lastStatus: ClientStatus | undefined
15+
1316
constructor(
17+
private readonly notificationTray: INotificationTray,
1418
private readonly composeWindow: IComposeWindow,
1519
private readonly port: IGhostServerPort,
1620
private readonly options: ClientOptions,
1721
) {}
1822

1923
update(status: ClientStatus): Promise<void> {
20-
return this.composeWindow.setIcon(imageForStatus(status))
24+
const icon = imageForStatus(status)
25+
26+
if (status !== (this.lastStatus ?? "inactive")) {
27+
this.notificationTray.showNotification(icon, messageForStatus(status))
28+
}
29+
this.lastStatus = status
30+
31+
return this.composeWindow.setIcon(icon)
2132
}
2233

2334
async getState(): Promise<EmailState> {
@@ -63,3 +74,14 @@ function imageForStatus(status: ClientStatus): string {
6374
return "red.svg"
6475
}
6576
}
77+
78+
function messageForStatus(status: ClientStatus): MessageId {
79+
switch (status) {
80+
case "active":
81+
return "src_client_status_active"
82+
case "inactive":
83+
return "src_client_status_inactive"
84+
case "error":
85+
return "src_client_status_error"
86+
}
87+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { EmailEditor } from "src/ghosttext-adaptor/email_editor"
22
import type { IClientOptionsLoader } from "src/ghosttext-runner"
3-
import type { IComposeWindow, IGhostServerPort } from "."
3+
import type { IComposeWindow, IGhostServerPort, INotificationTray } from "."
44

55
export class EmailEditorFactory {
66
static isSingleton = true
77

8-
constructor(private readonly clientOptionsLoader: IClientOptionsLoader) {}
8+
constructor(
9+
private readonly clientOptionsLoader: IClientOptionsLoader,
10+
private readonly notificationTray: INotificationTray,
11+
) {}
912

1013
async create(tab: IComposeWindow, port: IGhostServerPort): Promise<EmailEditor> {
1114
const options = await this.clientOptionsLoader.load()
12-
return new EmailEditor(tab, port, options)
15+
return new EmailEditor(this.notificationTray, tab, port, options)
1316
}
1417
}

‎src/thunderbird/background_util/manifest_info.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ export class ManifestInfo implements ICommandConfig, IManifestInfo {
1414
getId(): string {
1515
return this.messenger.runtime.id
1616
}
17+
18+
getShortName(): string {
19+
return this.messenger.runtime.getManifest().short_name ?? "<unknown>"
20+
}
1721
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { INotificationTray } from "src/ghosttext-adaptor"
2+
import type { MessageId } from "src/util"
3+
import type { ManifestInfo } from "."
4+
5+
export class NotificationTray implements INotificationTray {
6+
static isSingleton = true
7+
8+
private title: string | undefined
9+
10+
constructor(
11+
readonly messenger: typeof globalThis.messenger,
12+
readonly manifestInfo: ManifestInfo,
13+
) {}
14+
15+
async showNotification(iconUrl: string, messageId: MessageId): Promise<void> {
16+
await this.messenger.notifications.create(this.makeNotification(iconUrl, messageId))
17+
}
18+
19+
private makeNotification(iconUrl: string, messageId: MessageId): messenger.notifications.CreateNotificationOptions {
20+
this.title ??= this.manifestInfo.getShortName()
21+
return {
22+
type: "basic",
23+
title: this.title,
24+
iconUrl,
25+
message: this.messenger.i18n.getMessage(messageId),
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)