16
16
17
17
import { inject , injectable } from 'inversify' ;
18
18
import * as React from 'react' ;
19
+ import { ContextKeyService } from '../../context-key-service' ;
19
20
import { CommandRegistry , Disposable , DisposableCollection , MenuCommandExecutor , MenuModelRegistry , MenuPath , nls } from '../../../common' ;
20
21
import { Anchor , ContextMenuAccess , ContextMenuRenderer } from '../../context-menu-renderer' ;
21
22
import { LabelIcon , LabelParser } from '../../label-parser' ;
@@ -41,12 +42,15 @@ export class TabBarToolbar extends ReactWidget {
41
42
protected inline = new Map < string , TabBarToolbarItem | ReactTabBarToolbarItem > ( ) ;
42
43
protected more = new Map < string , TabBarToolbarItem > ( ) ;
43
44
45
+ protected contextKeyListener : Disposable | undefined ;
46
+
44
47
@inject ( CommandRegistry ) protected readonly commands : CommandRegistry ;
45
48
@inject ( LabelParser ) protected readonly labelParser : LabelParser ;
46
49
@inject ( MenuModelRegistry ) protected readonly menus : MenuModelRegistry ;
47
50
@inject ( MenuCommandExecutor ) protected readonly menuCommandExecutor : MenuCommandExecutor ;
48
51
@inject ( ContextMenuRenderer ) protected readonly contextMenuRenderer : ContextMenuRenderer ;
49
52
@inject ( TabBarToolbarRegistry ) protected readonly toolbarRegistry : TabBarToolbarRegistry ;
53
+ @inject ( ContextKeyService ) protected readonly contextKeyService : ContextKeyService ;
50
54
51
55
constructor ( ) {
52
56
super ( ) ;
@@ -60,13 +64,22 @@ export class TabBarToolbar extends ReactWidget {
60
64
updateItems ( items : Array < TabBarToolbarItem | ReactTabBarToolbarItem > , current : Widget | undefined ) : void {
61
65
this . inline . clear ( ) ;
62
66
this . more . clear ( ) ;
67
+
68
+ const contextKeys = new Set < string > ( ) ;
63
69
for ( const item of items . sort ( TabBarToolbarItem . PRIORITY_COMPARATOR ) . reverse ( ) ) {
64
70
if ( 'render' in item || item . group === undefined || item . group === 'navigation' ) {
65
71
this . inline . set ( item . id , item ) ;
66
72
} else {
67
73
this . more . set ( item . id , item ) ;
68
74
}
75
+
76
+ if ( item . when ) {
77
+ this . contextKeyService . parseKeys ( item . when ) ?. forEach ( key => contextKeys . add ( key ) ) ;
78
+ }
69
79
}
80
+
81
+ this . updateContextKeyListener ( contextKeys ) ;
82
+
70
83
this . setCurrent ( current ) ;
71
84
if ( ! items . length ) {
72
85
this . hide ( ) ;
@@ -97,6 +110,17 @@ export class TabBarToolbar extends ReactWidget {
97
110
}
98
111
}
99
112
113
+ protected updateContextKeyListener ( contextKeys : Set < string > ) : void {
114
+ this . contextKeyListener ?. dispose ( ) ;
115
+ if ( contextKeys . size > 0 ) {
116
+ this . contextKeyListener = this . contextKeyService . onDidChange ( event => {
117
+ if ( event . affects ( contextKeys ) ) {
118
+ this . update ( ) ;
119
+ }
120
+ } ) ;
121
+ }
122
+ }
123
+
100
124
protected render ( ) : React . ReactNode {
101
125
return < React . Fragment >
102
126
{ this . renderMore ( ) }
@@ -124,7 +148,8 @@ export class TabBarToolbar extends ReactWidget {
124
148
classNames . push ( iconClass ) ;
125
149
}
126
150
const tooltip = item . tooltip || ( command && command . label ) ;
127
- const toolbarItemClassNames = this . getToolbarItemClassNames ( command ?. id ?? item . command ) ;
151
+
152
+ const toolbarItemClassNames = this . getToolbarItemClassNames ( item ) ;
128
153
if ( item . menuPath && ! item . command ) { toolbarItemClassNames . push ( 'enabled' ) ; }
129
154
return < div key = { item . id }
130
155
ref = { this . onRender }
@@ -139,13 +164,13 @@ export class TabBarToolbar extends ReactWidget {
139
164
</ div > ;
140
165
}
141
166
142
- protected getToolbarItemClassNames ( commandId : string | undefined ) : string [ ] {
167
+ protected getToolbarItemClassNames ( item : AnyToolbarItem ) : string [ ] {
143
168
const classNames = [ TabBarToolbar . Styles . TAB_BAR_TOOLBAR_ITEM ] ;
144
- if ( commandId ) {
145
- if ( this . commandIsEnabled ( commandId ) ) {
169
+ if ( item . command ) {
170
+ if ( this . commandIsEnabled ( item . command ) && this . evaluateWhenClause ( item . when ) ) {
146
171
classNames . push ( 'enabled' ) ;
147
172
}
148
- if ( this . commandIsToggled ( commandId ) ) {
173
+ if ( this . commandIsToggled ( item . command ) ) {
149
174
classNames . push ( 'toggled' ) ;
150
175
}
151
176
}
@@ -221,11 +246,20 @@ export class TabBarToolbar extends ReactWidget {
221
246
return this . commands . isToggled ( command , this . current ) ;
222
247
}
223
248
249
+ protected evaluateWhenClause ( whenClause : string | undefined ) : boolean {
250
+ return whenClause ? this . contextKeyService . match ( whenClause ) : true ;
251
+ }
252
+
224
253
protected executeCommand = ( e : React . MouseEvent < HTMLElement > ) => {
225
254
e . preventDefault ( ) ;
226
255
e . stopPropagation ( ) ;
227
256
228
257
const item : AnyToolbarItem | undefined = this . inline . get ( e . currentTarget . id ) ;
258
+
259
+ if ( ! this . evaluateWhenClause ( item ?. when ) ) {
260
+ return ;
261
+ }
262
+
229
263
if ( item ?. command && item . menuPath ) {
230
264
this . menuCommandExecutor . executeCommand ( item . menuPath , item . command , this . current ) ;
231
265
} else if ( item ?. command ) {
@@ -245,7 +279,6 @@ export class TabBarToolbar extends ReactWidget {
245
279
protected onMouseUpEvent = ( e : React . MouseEvent < HTMLElement > ) => {
246
280
e . currentTarget . classList . remove ( 'active' ) ;
247
281
} ;
248
-
249
282
}
250
283
251
284
export namespace TabBarToolbar {
0 commit comments