@@ -14,7 +14,6 @@ import { Color } from 'vs/base/common/color';
14
14
import { Emitter , Event as BaseEvent } from 'vs/base/common/event' ;
15
15
import { KeyCode } from 'vs/base/common/keyCodes' ;
16
16
import { Disposable , IDisposable } from 'vs/base/common/lifecycle' ;
17
- import { mixin } from 'vs/base/common/objects' ;
18
17
import { localize } from 'vs/nls' ;
19
18
import 'vs/css!./button' ;
20
19
@@ -24,22 +23,28 @@ export interface IButtonOptions extends IButtonStyles {
24
23
readonly secondary ?: boolean ;
25
24
}
26
25
26
+ export type CSSValueString = string ;
27
+
27
28
export interface IButtonStyles {
28
- buttonBackground ?: Color ;
29
- buttonHoverBackground ?: Color ;
30
- buttonForeground ?: Color ;
31
- buttonSeparator ?: Color ;
32
- buttonSecondaryBackground ?: Color ;
33
- buttonSecondaryHoverBackground ?: Color ;
34
- buttonSecondaryForeground ?: Color ;
35
- buttonBorder ?: Color ;
29
+ readonly buttonBackground ?: CSSValueString ;
30
+ readonly buttonHoverBackground ?: CSSValueString ;
31
+ readonly buttonForeground ?: CSSValueString ;
32
+ readonly buttonSeparator ?: CSSValueString ;
33
+ readonly buttonSecondaryBackground ?: CSSValueString ;
34
+ readonly buttonSecondaryHoverBackground ?: CSSValueString ;
35
+ readonly buttonSecondaryForeground ?: CSSValueString ;
36
+ readonly buttonBorder ?: CSSValueString ;
36
37
}
37
38
38
- const defaultOptions : IButtonStyles = {
39
- buttonBackground : Color . fromHex ( '#0E639C' ) ,
40
- buttonHoverBackground : Color . fromHex ( '#006BB3' ) ,
41
- buttonSeparator : Color . white ,
42
- buttonForeground : Color . white
39
+ export const defaultOptions : IButtonStyles = {
40
+ buttonBackground : '#0E639C' ,
41
+ buttonHoverBackground : '#006BB3' ,
42
+ buttonSeparator : Color . white . toString ( ) ,
43
+ buttonForeground : Color . white . toString ( ) ,
44
+ buttonBorder : undefined ,
45
+ buttonSecondaryBackground : undefined ,
46
+ buttonSecondaryForeground : undefined ,
47
+ buttonSecondaryHoverBackground : undefined
43
48
} ;
44
49
45
50
export interface IButton extends IDisposable {
@@ -48,7 +53,6 @@ export interface IButton extends IDisposable {
48
53
label : string ;
49
54
icon : CSSIcon ;
50
55
enabled : boolean ;
51
- style ( styles : IButtonStyles ) : void ;
52
56
focus ( ) : void ;
53
57
hasFocus ( ) : boolean ;
54
58
}
@@ -62,40 +66,31 @@ export class Button extends Disposable implements IButton {
62
66
protected _element : HTMLElement ;
63
67
protected options : IButtonOptions ;
64
68
65
- private buttonBackground : Color | undefined ;
66
- private buttonHoverBackground : Color | undefined ;
67
- private buttonForeground : Color | undefined ;
68
- private buttonSecondaryBackground : Color | undefined ;
69
- private buttonSecondaryHoverBackground : Color | undefined ;
70
- private buttonSecondaryForeground : Color | undefined ;
71
- private buttonBorder : Color | undefined ;
72
-
73
69
private _onDidClick = this . _register ( new Emitter < Event > ( ) ) ;
74
70
get onDidClick ( ) : BaseEvent < Event > { return this . _onDidClick . event ; }
75
71
76
72
private focusTracker : IFocusTracker ;
77
73
78
- constructor ( container : HTMLElement , options ? : IButtonOptions ) {
74
+ constructor ( container : HTMLElement , options : IButtonOptions ) {
79
75
super ( ) ;
80
76
81
- this . options = options || Object . create ( null ) ;
82
- mixin ( this . options , defaultOptions , false ) ;
83
-
84
- this . buttonForeground = this . options . buttonForeground ;
85
- this . buttonBackground = this . options . buttonBackground ;
86
- this . buttonHoverBackground = this . options . buttonHoverBackground ;
87
-
88
- this . buttonSecondaryForeground = this . options . buttonSecondaryForeground ;
89
- this . buttonSecondaryBackground = this . options . buttonSecondaryBackground ;
90
- this . buttonSecondaryHoverBackground = this . options . buttonSecondaryHoverBackground ;
91
-
92
- this . buttonBorder = this . options . buttonBorder ;
77
+ this . options = options ;
93
78
94
79
this . _element = document . createElement ( 'a' ) ;
95
80
this . _element . classList . add ( 'monaco-button' ) ;
96
81
this . _element . tabIndex = 0 ;
97
82
this . _element . setAttribute ( 'role' , 'button' ) ;
98
83
84
+ const background = options . secondary ? options . buttonSecondaryBackground : options . buttonBackground ;
85
+ const foreground = options . secondary ? options . buttonSecondaryForeground : options . buttonForeground ;
86
+ const border = options . buttonBorder ;
87
+
88
+ this . _element . style . color = foreground || '' ;
89
+ this . _element . style . backgroundColor = background || '' ;
90
+ if ( border ) {
91
+ this . _element . style . border = `1px solid ${ border } ` ;
92
+ }
93
+
99
94
container . appendChild ( this . _element ) ;
100
95
101
96
this . _register ( Gesture . addTarget ( this . _element ) ) ;
@@ -129,65 +124,29 @@ export class Button extends Disposable implements IButton {
129
124
130
125
this . _register ( addDisposableListener ( this . _element , EventType . MOUSE_OVER , e => {
131
126
if ( ! this . _element . classList . contains ( 'disabled' ) ) {
132
- this . setHoverBackground ( ) ;
127
+ this . updateBackground ( true ) ;
133
128
}
134
129
} ) ) ;
135
130
136
131
this . _register ( addDisposableListener ( this . _element , EventType . MOUSE_OUT , e => {
137
- this . applyStyles ( ) ; // restore standard styles
132
+ this . updateBackground ( false ) ; // restore standard styles
138
133
} ) ) ;
139
134
140
135
// Also set hover background when button is focused for feedback
141
136
this . focusTracker = this . _register ( trackFocus ( this . _element ) ) ;
142
- this . _register ( this . focusTracker . onDidFocus ( ( ) => { if ( this . enabled ) { this . setHoverBackground ( ) ; } } ) ) ;
143
- this . _register ( this . focusTracker . onDidBlur ( ( ) => { if ( this . enabled ) { this . applyStyles ( ) ; } } ) ) ;
144
-
145
- this . applyStyles ( ) ;
137
+ this . _register ( this . focusTracker . onDidFocus ( ( ) => { if ( this . enabled ) { this . updateBackground ( true ) ; } } ) ) ;
138
+ this . _register ( this . focusTracker . onDidBlur ( ( ) => { if ( this . enabled ) { this . updateBackground ( false ) ; } } ) ) ;
146
139
}
147
140
148
- private setHoverBackground ( ) : void {
149
- let hoverBackground ;
141
+ private updateBackground ( hover : boolean ) : void {
142
+ let background ;
150
143
if ( this . options . secondary ) {
151
- hoverBackground = this . buttonSecondaryHoverBackground ? this . buttonSecondaryHoverBackground . toString ( ) : null ;
144
+ background = hover ? this . options . buttonSecondaryHoverBackground : this . options . buttonSecondaryBackground ;
152
145
} else {
153
- hoverBackground = this . buttonHoverBackground ? this . buttonHoverBackground . toString ( ) : null ;
154
- }
155
- if ( hoverBackground ) {
156
- this . _element . style . backgroundColor = hoverBackground ;
146
+ background = hover ? this . options . buttonHoverBackground : this . options . buttonBackground ;
157
147
}
158
- }
159
-
160
- style ( styles : IButtonStyles ) : void {
161
- this . buttonForeground = styles . buttonForeground ;
162
- this . buttonBackground = styles . buttonBackground ;
163
- this . buttonHoverBackground = styles . buttonHoverBackground ;
164
- this . buttonSecondaryForeground = styles . buttonSecondaryForeground ;
165
- this . buttonSecondaryBackground = styles . buttonSecondaryBackground ;
166
- this . buttonSecondaryHoverBackground = styles . buttonSecondaryHoverBackground ;
167
- this . buttonBorder = styles . buttonBorder ;
168
-
169
- this . applyStyles ( ) ;
170
- }
171
-
172
- private applyStyles ( ) : void {
173
- if ( this . _element ) {
174
- let background , foreground ;
175
- if ( this . options . secondary ) {
176
- foreground = this . buttonSecondaryForeground ? this . buttonSecondaryForeground . toString ( ) : '' ;
177
- background = this . buttonSecondaryBackground ? this . buttonSecondaryBackground . toString ( ) : '' ;
178
- } else {
179
- foreground = this . buttonForeground ? this . buttonForeground . toString ( ) : '' ;
180
- background = this . buttonBackground ? this . buttonBackground . toString ( ) : '' ;
181
- }
182
-
183
- const border = this . buttonBorder ? this . buttonBorder . toString ( ) : '' ;
184
-
185
- this . _element . style . color = foreground ;
148
+ if ( background ) {
186
149
this . _element . style . backgroundColor = background ;
187
-
188
- this . _element . style . borderWidth = border ? '1px' : '' ;
189
- this . _element . style . borderStyle = border ? 'solid' : '' ;
190
- this . _element . style . borderColor = border ;
191
150
}
192
151
}
193
152
@@ -293,6 +252,21 @@ export class ButtonWithDropdown extends Disposable implements IButton {
293
252
this . separatorContainer . appendChild ( this . separator ) ;
294
253
this . element . appendChild ( this . separatorContainer ) ;
295
254
255
+ // Separator styles
256
+ const border = options . buttonBorder ;
257
+ if ( border ) {
258
+ this . separatorContainer . style . borderTopWidth = '1px' ;
259
+ this . separatorContainer . style . borderTopStyle = 'solid' ;
260
+ this . separatorContainer . style . borderTopColor = border ;
261
+
262
+ this . separatorContainer . style . borderBottomWidth = '1px' ;
263
+ this . separatorContainer . style . borderBottomStyle = 'solid' ;
264
+ this . separatorContainer . style . borderBottomColor = border ;
265
+ }
266
+ this . separatorContainer . style . backgroundColor = options . buttonBackground ?. toString ( ) ?? '' ;
267
+ this . separator . style . backgroundColor = options . buttonSeparator ?. toString ( ) ?? '' ;
268
+
269
+
296
270
this . dropdownButton = this . _register ( new Button ( this . element , { ...options , title : false , supportIcons : true } ) ) ;
297
271
this . dropdownButton . element . title = localize ( "button dropdown more actions" , 'More Actions...' ) ;
298
272
this . dropdownButton . element . setAttribute ( 'aria-haspopup' , 'true' ) ;
@@ -330,25 +304,6 @@ export class ButtonWithDropdown extends Disposable implements IButton {
330
304
return this . button . enabled ;
331
305
}
332
306
333
- style ( styles : IButtonStyles ) : void {
334
- this . button . style ( styles ) ;
335
- this . dropdownButton . style ( styles ) ;
336
-
337
- // Separator
338
- const border = styles . buttonBorder ? styles . buttonBorder . toString ( ) : '' ;
339
-
340
- this . separatorContainer . style . borderTopWidth = border ? '1px' : '' ;
341
- this . separatorContainer . style . borderTopStyle = border ? 'solid' : '' ;
342
- this . separatorContainer . style . borderTopColor = border ;
343
-
344
- this . separatorContainer . style . borderBottomWidth = border ? '1px' : '' ;
345
- this . separatorContainer . style . borderBottomStyle = border ? 'solid' : '' ;
346
- this . separatorContainer . style . borderBottomColor = border ;
347
-
348
- this . separatorContainer . style . backgroundColor = styles . buttonBackground ?. toString ( ) ?? '' ;
349
- this . separator . style . backgroundColor = styles . buttonSeparator ?. toString ( ) ?? '' ;
350
- }
351
-
352
307
focus ( ) : void {
353
308
this . button . focus ( ) ;
354
309
}
@@ -363,7 +318,7 @@ export class ButtonWithDescription extends Button implements IButtonWithDescript
363
318
private _labelElement : HTMLElement ;
364
319
private _descriptionElement : HTMLElement ;
365
320
366
- constructor ( container : HTMLElement , options ? : IButtonOptions ) {
321
+ constructor ( container : HTMLElement , options : IButtonOptions ) {
367
322
super ( container , options ) ;
368
323
369
324
this . _element . classList . add ( 'monaco-description-button' ) ;
@@ -412,13 +367,13 @@ export class ButtonBar extends Disposable {
412
367
return this . _buttons ;
413
368
}
414
369
415
- addButton ( options ? : IButtonOptions ) : IButton {
370
+ addButton ( options : IButtonOptions ) : IButton {
416
371
const button = this . _register ( new Button ( this . container , options ) ) ;
417
372
this . pushButton ( button ) ;
418
373
return button ;
419
374
}
420
375
421
- addButtonWithDescription ( options ? : IButtonOptions ) : IButtonWithDescription {
376
+ addButtonWithDescription ( options : IButtonOptions ) : IButtonWithDescription {
422
377
const button = this . _register ( new ButtonWithDescription ( this . container , options ) ) ;
423
378
this . pushButton ( button ) ;
424
379
return button ;
0 commit comments