Skip to content

Commit a622774

Browse files
committed
Use wrapper commands for menu actions and simplify context keys
- Add UpdateSelectedPackageAction and UninstallSelectedPackageAction wrapper commands that pass the selected package to the main commands when invoked from the ViewTitle menu - Keep main Update/Uninstall commands for command palette (always show quick pick) - Simplify selectedPackage from boolean context key + property to a single string context key - Add PACKAGES_CAN_RUN_ACTION precondition to Update/Uninstall commands See #12922
1 parent 2d81c97 commit a622774

3 files changed

Lines changed: 65 additions & 25 deletions

File tree

src/vs/workbench/contrib/positronPackages/browser/positronPackages.contribution.ts

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,7 @@ class UninstallPackageAction extends Action2 {
300300
title: nls.localize2('uninstallPackage', 'Uninstall Package'),
301301
category: PACKAGES_CATEGORY,
302302
f1: true,
303-
precondition: POSITRON_PACKAGES_ENABLED,
304-
menu: {
305-
id: MenuId.ViewTitle,
306-
when: ContextKeyExpr.and(PACKAGES_VIEW_VISIBLE, PACKAGES_HAS_SELECTION),
307-
group: 'packages',
308-
order: 4
309-
}
303+
precondition: ContextKeyExpr.and(POSITRON_PACKAGES_ENABLED, PACKAGES_CAN_RUN_ACTION),
310304
});
311305
}
312306
override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {
@@ -377,13 +371,7 @@ class UpdatePackageAction extends Action2 {
377371
title: nls.localize2('updatePackage', 'Update Package'),
378372
category: PACKAGES_CATEGORY,
379373
f1: true,
380-
precondition: POSITRON_PACKAGES_ENABLED,
381-
menu: {
382-
id: MenuId.ViewTitle,
383-
when: ContextKeyExpr.and(PACKAGES_VIEW_VISIBLE, PACKAGES_HAS_SELECTION),
384-
group: 'packages',
385-
order: 3
386-
}
374+
precondition: ContextKeyExpr.and(POSITRON_PACKAGES_ENABLED, PACKAGES_CAN_RUN_ACTION),
387375
});
388376
}
389377
override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {
@@ -494,9 +482,65 @@ class UpdateAllPackagesAction extends Action2 {
494482
}
495483
}
496484

485+
/**
486+
* Menu wrapper for Update Package that passes the selected package to the main command.
487+
*/
488+
class UpdateSelectedPackageAction extends Action2 {
489+
constructor() {
490+
super({
491+
id: 'positronPackages.updateSelectedPackage',
492+
title: nls.localize2('updatePackage', 'Update Package'),
493+
category: PACKAGES_CATEGORY,
494+
precondition: ContextKeyExpr.and(POSITRON_PACKAGES_ENABLED, PACKAGES_CAN_RUN_ACTION),
495+
menu: {
496+
id: MenuId.ViewTitle,
497+
when: ContextKeyExpr.and(PACKAGES_VIEW_VISIBLE, PACKAGES_HAS_SELECTION),
498+
group: 'packages',
499+
order: 3
500+
}
501+
});
502+
}
503+
override async run(accessor: ServicesAccessor): Promise<void> {
504+
const service = accessor.get(IPositronPackagesService);
505+
const commandService = accessor.get(ICommandService);
506+
if (service.selectedPackage) {
507+
await commandService.executeCommand(PACKAGES_UPDATE_COMMAND_ID, service.selectedPackage);
508+
}
509+
}
510+
}
511+
512+
/**
513+
* Menu wrapper for Uninstall Package that passes the selected package to the main command.
514+
*/
515+
class UninstallSelectedPackageAction extends Action2 {
516+
constructor() {
517+
super({
518+
id: 'positronPackages.uninstallSelectedPackage',
519+
title: nls.localize2('uninstallPackage', 'Uninstall Package'),
520+
category: PACKAGES_CATEGORY,
521+
precondition: ContextKeyExpr.and(POSITRON_PACKAGES_ENABLED, PACKAGES_CAN_RUN_ACTION),
522+
menu: {
523+
id: MenuId.ViewTitle,
524+
when: ContextKeyExpr.and(PACKAGES_VIEW_VISIBLE, PACKAGES_HAS_SELECTION),
525+
group: 'packages',
526+
order: 4
527+
}
528+
});
529+
}
530+
override async run(accessor: ServicesAccessor): Promise<void> {
531+
const service = accessor.get(IPositronPackagesService);
532+
const commandService = accessor.get(ICommandService);
533+
if (service.selectedPackage) {
534+
await commandService.executeCommand(PACKAGES_UNINSTALL_COMMAND_ID, service.selectedPackage);
535+
}
536+
}
537+
}
538+
497539
registerAction2(InstallPackageAction);
498540
registerAction2(RefreshPackagesAction);
499541
registerAction2(UninstallPackageAction);
500542
registerAction2(UpdatePackageAction);
501543
registerAction2(UpdateAllPackagesAction);
544+
registerAction2(UpdateSelectedPackageAction);
545+
registerAction2(UninstallSelectedPackageAction);
502546
registerSingleton(IPositronPackagesService, PositronPackagesService, InstantiationType.Delayed);

src/vs/workbench/contrib/positronPackages/browser/positronPackagesContextKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const POSITRON_PACKAGES_VIEW_ID = 'workbench.view.positronPackages.view';
1111
// Context keys for the packages view
1212
export const POSITRON_PACKAGES_HAS_ACTIVE_SESSION = new RawContextKey<boolean>('positronPackages.hasActiveSession', false);
1313
export const POSITRON_PACKAGES_IS_BUSY = new RawContextKey<boolean>('positronPackages.isBusy', false);
14-
export const POSITRON_PACKAGES_HAS_SELECTED_PACKAGE = new RawContextKey<boolean>('positronPackages.hasSelectedPackage', false);
14+
export const POSITRON_PACKAGES_SELECTED_PACKAGE = new RawContextKey<string>('positronPackages.selectedPackage', '');
1515

1616
// Context key expressions for menu enablement
1717
export const PACKAGES_VIEW_VISIBLE = ContextKeyExpr.equals('view', POSITRON_PACKAGES_VIEW_ID);
@@ -21,5 +21,5 @@ export const PACKAGES_CAN_RUN_ACTION = ContextKeyExpr.and(
2121
);
2222
export const PACKAGES_HAS_SELECTION = ContextKeyExpr.and(
2323
PACKAGES_CAN_RUN_ACTION,
24-
POSITRON_PACKAGES_HAS_SELECTED_PACKAGE
24+
POSITRON_PACKAGES_SELECTED_PACKAGE.notEqualsTo('')
2525
);

src/vs/workbench/contrib/positronPackages/browser/positronPackagesService.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { ILogService } from '../../../../platform/log/common/log.js';
1212
import { LanguageRuntimeSessionMode } from '../../../services/languageRuntime/common/languageRuntimeService.js';
1313
import { ILanguageRuntimePackage, ILanguageRuntimeSession, IPackageSpec, IRuntimeSessionService } from '../../../services/runtimeSession/common/runtimeSessionService.js';
1414
import { IPositronPackagesService } from './interfaces/positronPackagesService.js';
15-
import { POSITRON_PACKAGES_HAS_ACTIVE_SESSION, POSITRON_PACKAGES_HAS_SELECTED_PACKAGE, POSITRON_PACKAGES_IS_BUSY } from './positronPackagesContextKeys.js';
15+
import { POSITRON_PACKAGES_HAS_ACTIVE_SESSION, POSITRON_PACKAGES_IS_BUSY, POSITRON_PACKAGES_SELECTED_PACKAGE } from './positronPackagesContextKeys.js';
1616
import { IPositronPackagesInstance, PositronPackagesInstance } from './positronPackagesInstance.js';
1717

1818
const TIMEOUT_REFRESH_MS = 5_000; // 5 seconds
@@ -34,10 +34,7 @@ export class PositronPackagesService extends Disposable implements IPositronPack
3434
// Context keys
3535
private readonly _hasActiveSessionContextKey: IContextKey<boolean>;
3636
private readonly _isBusyContextKey: IContextKey<boolean>;
37-
private readonly _hasSelectedPackageContextKey: IContextKey<boolean>;
38-
39-
// Selected package
40-
private _selectedPackage: string | undefined;
37+
private readonly _selectedPackageContextKey: IContextKey<string>;
4138

4239
// Disposables for tracking busy state of the active instance
4340
private readonly _activeInstanceDisposables = this._register(new DisposableStore());
@@ -64,7 +61,7 @@ export class PositronPackagesService extends Disposable implements IPositronPack
6461
// Initialize context keys
6562
this._hasActiveSessionContextKey = POSITRON_PACKAGES_HAS_ACTIVE_SESSION.bindTo(this._contextKeyService);
6663
this._isBusyContextKey = POSITRON_PACKAGES_IS_BUSY.bindTo(this._contextKeyService);
67-
this._hasSelectedPackageContextKey = POSITRON_PACKAGES_HAS_SELECTED_PACKAGE.bindTo(this._contextKeyService);
64+
this._selectedPackageContextKey = POSITRON_PACKAGES_SELECTED_PACKAGE.bindTo(this._contextKeyService);
6865

6966
// Create new instances
7067
this._register(this._runtimeSessionService.onWillStartSession((e) => {
@@ -175,12 +172,11 @@ export class PositronPackagesService extends Disposable implements IPositronPack
175172
}
176173

177174
get selectedPackage(): string | undefined {
178-
return this._selectedPackage;
175+
return this._selectedPackageContextKey.get() || undefined;
179176
}
180177

181178
setSelectedPackage(packageName: string | undefined): void {
182-
this._selectedPackage = packageName;
183-
this._hasSelectedPackageContextKey.set(!!packageName);
179+
this._selectedPackageContextKey.set(packageName ?? '');
184180
}
185181

186182
async refreshPackages(token?: CancellationToken): Promise<ILanguageRuntimePackage[]> {

0 commit comments

Comments
 (0)