Skip to content

Commit e4cad50

Browse files
committed
add button default styles via theme variables, remove buttons on dispose, allow to clear buttonbar
1 parent 35c1f3e commit e4cad50

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/vs/base/browser/ui/button/button.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
text-decoration: none !important;
2626
}
2727

28+
.monaco-button {
29+
color: var(--vscode-button-foreground);
30+
background-color: var(--vscode-button-background);
31+
}
32+
33+
.monaco-button:hover {
34+
background-color: var(--vscode-button-hoverBackground);
35+
}
36+
37+
.monaco-button.secondary {
38+
color: var(--vscode-button-secondaryForeground);
39+
background-color: var(--vscode-button-secondaryBackground);
40+
}
41+
42+
.monaco-button.secondary:hover {
43+
background-color: var(--vscode-button-secondaryHoverBackground);
44+
}
45+
46+
2847
.monaco-button.disabled:focus,
2948
.monaco-button.disabled {
3049
opacity: 0.4 !important;
@@ -90,11 +109,19 @@
90109
.monaco-button-dropdown .monaco-button-dropdown-separator {
91110
padding: 4px 0;
92111
cursor: default;
112+
background-color: var(--vscode-button-background);
113+
border-top: 1px solid var(--vscode-button-border);
114+
border-bottom: 1px solid var(--vscode-button-border);
115+
}
116+
117+
.monaco-button.secondary + .monaco-button-dropdown-separator {
118+
background-color: var(--vscode-button-secondaryBackground);
93119
}
94120

95121
.monaco-button-dropdown .monaco-button-dropdown-separator > div {
96122
height: 100%;
97123
width: 1px;
124+
background-color: var(--vscode-button-separator);
98125
}
99126

100127
.monaco-button-dropdown > .monaco-button.monaco-dropdown-button {

src/vs/base/browser/ui/button/button.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Color } from 'vs/base/common/color';
1616
import { Event as BaseEvent, Emitter } from 'vs/base/common/event';
1717
import { IMarkdownString, isMarkdownString, markdownStringEqual } from 'vs/base/common/htmlContent';
1818
import { KeyCode } from 'vs/base/common/keyCodes';
19-
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
19+
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
2020
import { ThemeIcon } from 'vs/base/common/themables';
2121
import 'vs/css!./button';
2222
import { localize } from 'vs/nls';
@@ -89,6 +89,7 @@ export class Button extends Disposable implements IButton {
8989
this._element.tabIndex = 0;
9090
this._element.setAttribute('role', 'button');
9191

92+
this._element.classList.toggle('secondary', !!options.secondary);
9293
const background = options.secondary ? options.buttonSecondaryBackground : options.buttonBackground;
9394
const foreground = options.secondary ? options.buttonSecondaryForeground : options.buttonForeground;
9495

@@ -154,6 +155,11 @@ export class Button extends Disposable implements IButton {
154155
this._register(this.focusTracker.onDidBlur(() => { if (this.enabled) { this.updateBackground(false); } }));
155156
}
156157

158+
public override dispose(): void {
159+
super.dispose();
160+
this._element.remove();
161+
}
162+
157163
private getContentElements(content: string): HTMLElement[] {
158164
const elements: HTMLSpanElement[] = [];
159165
for (let segment of renderLabelWithIcons(content)) {
@@ -281,7 +287,7 @@ export class Button extends Disposable implements IButton {
281287

282288
export interface IButtonWithDropdownOptions extends IButtonOptions {
283289
readonly contextMenuProvider: IContextMenuProvider;
284-
readonly actions: IAction[];
290+
readonly actions: readonly IAction[];
285291
readonly actionRunner?: IActionRunner;
286292
readonly addPrimaryActionToDropdown?: boolean;
287293
}
@@ -344,6 +350,11 @@ export class ButtonWithDropdown extends Disposable implements IButton {
344350
}));
345351
}
346352

353+
override dispose() {
354+
super.dispose();
355+
this.element.remove();
356+
}
357+
347358
set label(value: string) {
348359
this.button.label = value;
349360
this.action.label = value;
@@ -433,32 +444,42 @@ export class ButtonWithDescription implements IButtonWithDescription {
433444
}
434445
}
435446

436-
export class ButtonBar extends Disposable {
447+
export class ButtonBar {
437448

438-
private _buttons: IButton[] = [];
449+
private readonly _buttons: IButton[] = [];
450+
private readonly _buttonStore = new DisposableStore();
439451

440452
constructor(private readonly container: HTMLElement) {
441-
super();
453+
454+
}
455+
456+
dispose(): void {
457+
this._buttonStore.dispose();
442458
}
443459

444460
get buttons(): IButton[] {
445461
return this._buttons;
446462
}
447463

464+
clear(): void {
465+
this._buttonStore.clear();
466+
this._buttons.length = 0;
467+
}
468+
448469
addButton(options: IButtonOptions): IButton {
449-
const button = this._register(new Button(this.container, options));
470+
const button = this._buttonStore.add(new Button(this.container, options));
450471
this.pushButton(button);
451472
return button;
452473
}
453474

454475
addButtonWithDescription(options: IButtonOptions): IButtonWithDescription {
455-
const button = this._register(new ButtonWithDescription(this.container, options));
476+
const button = this._buttonStore.add(new ButtonWithDescription(this.container, options));
456477
this.pushButton(button);
457478
return button;
458479
}
459480

460481
addButtonWithDropdown(options: IButtonWithDropdownOptions): IButton {
461-
const button = this._register(new ButtonWithDropdown(this.container, options));
482+
const button = this._buttonStore.add(new ButtonWithDropdown(this.container, options));
462483
this.pushButton(button);
463484
return button;
464485
}
@@ -467,7 +488,7 @@ export class ButtonBar extends Disposable {
467488
this._buttons.push(button);
468489

469490
const index = this._buttons.length - 1;
470-
this._register(addDisposableListener(button.element, EventType.KEY_DOWN, e => {
491+
this._buttonStore.add(addDisposableListener(button.element, EventType.KEY_DOWN, e => {
471492
const event = new StandardKeyboardEvent(e);
472493
let eventHandled = true;
473494

0 commit comments

Comments
 (0)