@@ -6,8 +6,10 @@ import {
66} from "electron" ;
77import { injectable } from "inversify" ;
88import type { DetectedApplication } from "../../../shared/types.js" ;
9+ import { get } from "../../di/container.js" ;
10+ import { MAIN_TOKENS } from "../../di/tokens.js" ;
911import { getMainWindow } from "../../trpc/context.js" ;
10- import { externalAppsStore , getOrRefreshApps } from "../externalApps .js" ;
12+ import type { ExternalAppsService } from "../external-apps/service .js" ;
1113import type {
1214 ActionItemDef ,
1315 ConfirmOptions ,
@@ -33,11 +35,24 @@ import type {
3335export class ContextMenuService {
3436 private readonly ICON_SIZE = 16 ;
3537
38+ private getExternalAppsService ( ) {
39+ return get < ExternalAppsService > ( MAIN_TOKENS . ExternalAppsService ) ;
40+ }
41+
42+ private async getExternalAppsData ( ) {
43+ const service = this . getExternalAppsService ( ) ;
44+ const [ apps , lastUsed ] = await Promise . all ( [
45+ service . getDetectedApps ( ) ,
46+ service . getLastUsed ( ) ,
47+ ] ) ;
48+ return { apps, lastUsedAppId : lastUsed . lastUsedApp } ;
49+ }
50+
3651 async showTaskContextMenu (
3752 input : TaskContextMenuInput ,
3853 ) : Promise < TaskContextMenuResult > {
3954 const { taskTitle, worktreePath } = input ;
40- const apps = await getOrRefreshApps ( ) ;
55+ const { apps, lastUsedAppId } = await this . getExternalAppsData ( ) ;
4156
4257 return this . showMenu < TaskAction > ( [
4358 this . item ( "Rename" , { type : "rename" } ) ,
@@ -58,7 +73,10 @@ export class ContextMenuService {
5873 } ,
5974 ) ,
6075 ...( worktreePath
61- ? [ this . separator ( ) , ...this . externalAppItems < TaskAction > ( apps ) ]
76+ ? [
77+ this . separator ( ) ,
78+ ...this . externalAppItems < TaskAction > ( apps , lastUsedAppId ) ,
79+ ]
6280 : [ ] ) ,
6381 ] ) ;
6482 }
@@ -67,7 +85,7 @@ export class ContextMenuService {
6785 input : FolderContextMenuInput ,
6886 ) : Promise < FolderContextMenuResult > {
6987 const { folderName, folderPath } = input ;
70- const apps = await getOrRefreshApps ( ) ;
88+ const { apps, lastUsedAppId } = await this . getExternalAppsData ( ) ;
7189
7290 return this . showMenu < FolderAction > ( [
7391 this . item (
@@ -84,7 +102,10 @@ export class ContextMenuService {
84102 } ,
85103 ) ,
86104 ...( folderPath
87- ? [ this . separator ( ) , ...this . externalAppItems < FolderAction > ( apps ) ]
105+ ? [
106+ this . separator ( ) ,
107+ ...this . externalAppItems < FolderAction > ( apps , lastUsedAppId ) ,
108+ ]
88109 : [ ] ) ,
89110 ] ) ;
90111 }
@@ -93,7 +114,7 @@ export class ContextMenuService {
93114 input : TabContextMenuInput ,
94115 ) : Promise < TabContextMenuResult > {
95116 const { canClose, filePath } = input ;
96- const apps = await getOrRefreshApps ( ) ;
117+ const { apps, lastUsedAppId } = await this . getExternalAppsData ( ) ;
97118
98119 return this . showMenu < TabAction > ( [
99120 this . item (
@@ -107,7 +128,10 @@ export class ContextMenuService {
107128 this . item ( "Close other tabs" , { type : "close-others" } ) ,
108129 this . item ( "Close tabs to the right" , { type : "close-right" } ) ,
109130 ...( filePath
110- ? [ this . separator ( ) , ...this . externalAppItems < TabAction > ( apps ) ]
131+ ? [
132+ this . separator ( ) ,
133+ ...this . externalAppItems < TabAction > ( apps , lastUsedAppId ) ,
134+ ]
111135 : [ ] ) ,
112136 ] ) ;
113137 }
@@ -125,7 +149,7 @@ export class ContextMenuService {
125149 async showFileContextMenu (
126150 input : FileContextMenuInput ,
127151 ) : Promise < FileContextMenuResult > {
128- const apps = await getOrRefreshApps ( ) ;
152+ const { apps, lastUsedAppId } = await this . getExternalAppsData ( ) ;
129153
130154 return this . showMenu < FileAction > ( [
131155 ...( input . showCollapseAll
@@ -134,17 +158,18 @@ export class ContextMenuService {
134158 this . separator ( ) ,
135159 ]
136160 : [ ] ) ,
137- ...this . externalAppItems < FileAction > ( apps ) ,
161+ ...this . externalAppItems < FileAction > ( apps , lastUsedAppId ) ,
138162 ] ) ;
139163 }
140164
141- private externalAppItems < T > ( apps : DetectedApplication [ ] ) : MenuItemDef < T > [ ] {
165+ private externalAppItems < T > (
166+ apps : DetectedApplication [ ] ,
167+ lastUsedAppId ?: string ,
168+ ) : MenuItemDef < T > [ ] {
142169 if ( apps . length === 0 ) {
143170 return [ this . disabled ( "No external apps detected" ) ] ;
144171 }
145172
146- const lastUsedAppId =
147- externalAppsStore . get ( "externalAppsPrefs" ) ?. lastUsedApp ;
148173 const lastUsedApp = apps . find ( ( app ) => app . id === lastUsedAppId ) || apps [ 0 ] ;
149174 const openIn = ( appId : string ) : T =>
150175 ( { type : "external-app" , action : { type : "open-in-app" , appId } } ) as T ;
0 commit comments