1- import type { IComposeWindow } from "src/ghosttext-adaptor/api "
1+ import type { MenuHandler , MenuShownInfo } from ". "
22import type { CommandId , IComposeWindowDetector , ITab } from "./api"
3- import type { ComposeActionNotifier } from "./compose_action_notifier "
3+ import type { CommandHandler } from "./command_handler "
44
5+ /** Redirects events from Thunderbird to the appropriate handlers */
56export class BackgroundEventRouter {
67 static isSingleton = true
78
89 constructor (
9- private readonly composeActionNotifier : ComposeActionNotifier ,
1010 private readonly composeTabDetector : IComposeWindowDetector ,
11+ private readonly commandHandler : CommandHandler ,
12+ private readonly menuHandler : MenuHandler ,
1113 ) { }
1214
1315 /** Handles shortcut key presses defined in the manifest.json */
@@ -17,20 +19,7 @@ export class BackgroundEventRouter {
1719 return Promise . reject ( Error ( "Event dropped" ) )
1820 }
1921
20- return this . runCommand ( command , composeTab )
21- }
22-
23- /** Executes a command in the context of a compose tab */
24- private runCommand ( command : string , composeTab : IComposeWindow ) : Promise < void > {
25- switch ( command as CommandId ) {
26- case "start_ghostbird" :
27- return this . composeActionNotifier . start ( composeTab )
28- case "stop_ghostbird" :
29- return this . composeActionNotifier . stop ( composeTab )
30- case "toggle_ghostbird" :
31- return this . composeActionNotifier . toggle ( composeTab )
32- }
33- // We don't handle default here so that tsc checks for exhaustiveness
22+ return this . commandHandler . runCommand ( command as CommandId , composeTab )
3423 }
3524
3625 /** Handles the toolbar button press */
@@ -42,7 +31,22 @@ export class BackgroundEventRouter {
4231 return Promise . reject ( Error ( "Event dropped" ) )
4332 }
4433
45- return this . composeActionNotifier . start ( composeTab )
34+ return this . commandHandler . runCommand ( "start_ghostbird" , composeTab )
35+ }
36+
37+ /** Handles right-click on the toolbar button */
38+ handleMenuShown ( info : MenuShownInfo , _tab : ITab ) : void | Promise < void > {
39+ return this . menuHandler . handleMenuShown ( info )
40+ }
41+
42+ /** Handles clicks on the item in the toolbar button's context menu */
43+ handleMenuClick ( menuItemId : string , tab : ITab ) : Promise < void > {
44+ let composeTab = this . composeTabDetector . tryWrap ( tab )
45+ if ( ! composeTab ) {
46+ return Promise . reject ( Error ( "Event dropped" ) )
47+ }
48+
49+ return this . menuHandler . handleMenuItemClicked ( menuItemId , composeTab )
4650 }
4751
4852 /** handles one-off messages from content scripts */
0 commit comments