@@ -20,16 +20,37 @@ export interface ILocalizedString {
2020 original : string ;
2121}
2222
23+ export type Icon = { dark ?: URI ; light ?: URI ; } | ThemeIcon ;
24+ export type ToggleAwareIcon = { toggled ?: Icon , untoggled ?: Icon } ;
25+ export type ToggleAwareTitle = { toggled ?: string | ILocalizedString , untoggled ?: string | ILocalizedString } ;
26+
2327export interface ICommandAction {
2428 id : string ;
25- title : string | ILocalizedString ;
29+ title : string | ILocalizedString | ToggleAwareTitle ;
2630 category ?: string | ILocalizedString ;
27- icon ?: { dark ?: URI ; light ?: URI ; } | ThemeIcon ;
31+ icon ?: Icon | ToggleAwareIcon ;
2832 precondition ?: ContextKeyExpression ;
2933 toggled ?: ContextKeyExpression ;
3034}
3135
32- export type ISerializableCommandAction = UriDto < ICommandAction > ;
36+ export function isToggleAwareTitle ( thing : unknown ) : thing is ToggleAwareTitle {
37+ return thing && typeof thing === 'object'
38+ && ( ( typeof ( thing as ToggleAwareTitle ) . toggled === 'string' || typeof ( thing as ToggleAwareTitle ) . toggled === 'object' )
39+ || ( typeof ( thing as ToggleAwareTitle ) . untoggled === 'string' || typeof ( thing as ToggleAwareTitle ) . untoggled === 'object' ) ) ;
40+ }
41+
42+ export function isIcon ( thing : unknown ) : thing is Icon {
43+ if ( ThemeIcon . isThemeIcon ( thing ) ) {
44+ return true ;
45+ }
46+ return thing && typeof thing === 'object'
47+ && ( ( thing as { dark ?: URI , light ?: URI } ) . dark instanceof URI || ( thing as { dark ?: URI , light ?: URI } ) . light instanceof URI ) ;
48+ }
49+
50+ export function isToggleAwareIcon ( thing : unknown ) : thing is ToggleAwareIcon {
51+ return thing && typeof thing === 'object'
52+ && ( isIcon ( ( thing as ToggleAwareIcon ) . toggled ) || isIcon ( ( thing as ToggleAwareIcon ) . untoggled ) ) ;
53+ }
3354
3455export interface IMenuItem {
3556 command : ICommandAction ;
@@ -260,9 +281,18 @@ export class SubmenuItemAction extends Action {
260281 }
261282}
262283
284+ export type ISerializableMenuItemAction = UriDto < {
285+ id : string ;
286+ title : string | ILocalizedString ;
287+ category : string | ILocalizedString | undefined ;
288+ icon : Icon | undefined ;
289+ } > ;
290+
263291export class MenuItemAction extends ExecuteCommandAction {
264292
265- readonly item : ICommandAction ;
293+ readonly title : string | ILocalizedString ;
294+ readonly category : string | ILocalizedString | undefined ;
295+ readonly icon : Icon | undefined ;
266296 readonly alt : MenuItemAction | undefined ;
267297
268298 private _options : IMenuActionOptions ;
@@ -274,14 +304,17 @@ export class MenuItemAction extends ExecuteCommandAction {
274304 @IContextKeyService contextKeyService : IContextKeyService ,
275305 @ICommandService commandService : ICommandService
276306 ) {
277- typeof item . title === 'string' ? super ( item . id , item . title , commandService ) : super ( item . id , item . title . value , commandService ) ;
307+ super ( item . id , '' , commandService ) ;
308+ this . title = ( isToggleAwareTitle ( item . title ) ? this . _checked ? item . title . toggled : item . title . untoggled : item . title ) || '' ;
309+ this . _label = typeof this . title === 'string' ? this . title : this . title . value ;
278310 this . _cssClass = undefined ;
279311 this . _enabled = ! item . precondition || contextKeyService . contextMatchesRules ( item . precondition ) ;
280312 this . _checked = Boolean ( item . toggled && contextKeyService . contextMatchesRules ( item . toggled ) ) ;
313+ this . category = item . category ;
314+ this . icon = isToggleAwareIcon ( item . icon ) ? this . checked ? item . icon . toggled : item . icon . untoggled : item . icon ;
281315
282316 this . _options = options || { } ;
283317
284- this . item = item ;
285318 this . alt = alt ? new MenuItemAction ( alt , undefined , this . _options , contextKeyService , commandService ) : undefined ;
286319 }
287320
@@ -305,6 +338,15 @@ export class MenuItemAction extends ExecuteCommandAction {
305338
306339 return super . run ( ...runArgs ) ;
307340 }
341+
342+ serialize ( ) : ISerializableMenuItemAction {
343+ return {
344+ id : this . id ,
345+ title : this . title ,
346+ category : this . category ,
347+ icon : this . icon
348+ } ;
349+ }
308350}
309351
310352export class SyncActionDescriptor {
0 commit comments