Skip to content

Commit 159f93d

Browse files
Rework checkboxes (#257697)
The state for the TriState checkbox wasn't working correctly, so I reworked it so that it maintained its own state.
1 parent 9d37bcb commit 159f93d

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,6 @@ abstract class BaseCheckbox extends Widget {
273273
super();
274274

275275
this.applyStyles();
276-
277-
this._register(this.checkbox.onChange(keyboard => {
278-
this.applyStyles();
279-
this._onChange.fire(keyboard);
280-
}));
281276
}
282277

283278
get enabled(): boolean {
@@ -322,12 +317,9 @@ abstract class BaseCheckbox extends Widget {
322317
export class Checkbox extends BaseCheckbox {
323318
constructor(title: string, isChecked: boolean, styles: ICheckboxStyles) {
324319
const toggle = new Toggle({ title, isChecked, icon: Codicon.check, actionClassName: BaseCheckbox.CLASS_NAME, hoverDelegate: styles.hoverDelegate, ...unthemedToggleStyles });
325-
326320
super(toggle, toggle.domNode, styles);
327-
this._register(toggle);
328-
329-
this.applyStyles();
330321

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

341333
set checked(newIsChecked: boolean) {
342334
this.checkbox.checked = newIsChecked;
343-
if (newIsChecked) {
335+
this.applyStyles();
336+
}
337+
338+
protected override applyStyles(enabled?: boolean): void {
339+
if (this.checkbox.checked) {
344340
this.checkbox.setIcon(Codicon.check);
345341
} else {
346342
this.checkbox.setIcon(undefined);
347343
}
348-
this.applyStyles();
344+
super.applyStyles(enabled);
349345
}
350346
}
351347

352348
export class TriStateCheckbox extends BaseCheckbox {
353-
constructor(title: string, initialState: boolean | 'partial', styles: ICheckboxStyles) {
349+
constructor(
350+
title: string,
351+
private _state: boolean | 'partial',
352+
styles: ICheckboxStyles
353+
) {
354354
let icon: ThemeIcon | undefined;
355-
switch (initialState) {
355+
switch (_state) {
356356
case true:
357357
icon = Codicon.check;
358358
break;
@@ -365,30 +365,40 @@ export class TriStateCheckbox extends BaseCheckbox {
365365
}
366366
const checkbox = new Toggle({
367367
title,
368-
isChecked: initialState === true,
368+
isChecked: _state === true,
369369
icon,
370370
actionClassName: Checkbox.CLASS_NAME,
371371
hoverDelegate: styles.hoverDelegate,
372372
...unthemedToggleStyles
373373
});
374-
375374
super(
376375
checkbox,
377376
checkbox.domNode,
378377
styles
379378
);
379+
380380
this._register(checkbox);
381+
this._register(this.checkbox.onChange(keyboard => {
382+
this._state = this.checkbox.checked;
383+
this.applyStyles();
384+
this._onChange.fire(keyboard);
385+
}));
381386
}
382387

383388
get checked(): boolean | 'partial' {
384-
return this.checkbox.checked;
389+
return this._state;
385390
}
386391

387392
set checked(newState: boolean | 'partial') {
388-
const checked = newState === true;
389-
this.checkbox.checked = checked;
393+
if (this._state !== newState) {
394+
this._state = newState;
395+
this.checkbox.checked = newState === true;
396+
this.applyStyles();
397+
}
398+
}
390399

391-
switch (newState) {
400+
protected override applyStyles(enabled?: boolean): void {
401+
switch (this._state) {
392402
case true:
393403
this.checkbox.setIcon(Codicon.check);
394404
break;
@@ -399,8 +409,7 @@ export class TriStateCheckbox extends BaseCheckbox {
399409
this.checkbox.setIcon(undefined);
400410
break;
401411
}
402-
403-
this.applyStyles();
412+
super.applyStyles(enabled);
404413
}
405414
}
406415

0 commit comments

Comments
 (0)