Skip to content

Rework checkboxes #257697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/vs/base/browser/ui/toggle/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,6 @@ abstract class BaseCheckbox extends Widget {
super();

this.applyStyles();

this._register(this.checkbox.onChange(keyboard => {
this.applyStyles();
this._onChange.fire(keyboard);
}));
}

get enabled(): boolean {
Expand Down Expand Up @@ -322,12 +317,9 @@ abstract class BaseCheckbox extends Widget {
export class Checkbox extends BaseCheckbox {
constructor(title: string, isChecked: boolean, styles: ICheckboxStyles) {
const toggle = new Toggle({ title, isChecked, icon: Codicon.check, actionClassName: BaseCheckbox.CLASS_NAME, hoverDelegate: styles.hoverDelegate, ...unthemedToggleStyles });

super(toggle, toggle.domNode, styles);
this._register(toggle);

this.applyStyles();

this._register(toggle);
this._register(this.checkbox.onChange(keyboard => {
this.applyStyles();
this._onChange.fire(keyboard);
Expand All @@ -340,19 +332,27 @@ export class Checkbox extends BaseCheckbox {

set checked(newIsChecked: boolean) {
this.checkbox.checked = newIsChecked;
if (newIsChecked) {
this.applyStyles();
}

protected override applyStyles(enabled?: boolean): void {
if (this.checkbox.checked) {
this.checkbox.setIcon(Codicon.check);
} else {
this.checkbox.setIcon(undefined);
}
this.applyStyles();
super.applyStyles(enabled);
}
}

export class TriStateCheckbox extends BaseCheckbox {
constructor(title: string, initialState: boolean | 'partial', styles: ICheckboxStyles) {
constructor(
title: string,
private _state: boolean | 'partial',
styles: ICheckboxStyles
) {
let icon: ThemeIcon | undefined;
switch (initialState) {
switch (_state) {
case true:
icon = Codicon.check;
break;
Expand All @@ -365,30 +365,40 @@ export class TriStateCheckbox extends BaseCheckbox {
}
const checkbox = new Toggle({
title,
isChecked: initialState === true,
isChecked: _state === true,
icon,
actionClassName: Checkbox.CLASS_NAME,
hoverDelegate: styles.hoverDelegate,
...unthemedToggleStyles
});

super(
checkbox,
checkbox.domNode,
styles
);

this._register(checkbox);
this._register(this.checkbox.onChange(keyboard => {
this._state = this.checkbox.checked;
this.applyStyles();
this._onChange.fire(keyboard);
}));
}

get checked(): boolean | 'partial' {
return this.checkbox.checked;
return this._state;
}

set checked(newState: boolean | 'partial') {
const checked = newState === true;
this.checkbox.checked = checked;
if (this._state !== newState) {
this._state = newState;
this.checkbox.checked = newState === true;
this.applyStyles();
}
}

switch (newState) {
protected override applyStyles(enabled?: boolean): void {
switch (this._state) {
case true:
this.checkbox.setIcon(Codicon.check);
break;
Expand All @@ -399,8 +409,7 @@ export class TriStateCheckbox extends BaseCheckbox {
this.checkbox.setIcon(undefined);
break;
}

this.applyStyles();
super.applyStyles(enabled);
}
}

Expand Down
Loading