Skip to content

Commit da36507

Browse files
committed
A checkbox, radio or switch draws its label beside the mark
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019VxYXZ8BzY8THaUBbQA8TM
1 parent 6730820 commit da36507

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
with:
2121
repository: Codelessly/CodelesslyAPI
2222
# Paired model revision for these runtime actions and widget fields.
23-
ref: 29fb75651c35a5add4cdeee531eff2e492cd1c08
23+
ref: 857997f32ba2782b2ec251a2678346401601adaf
2424
path: CodelesslyAPI
2525
- name: Use the sibling CodelesslyAPI checkout
2626
run: |

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
- **A checkbox, radio or switch draws its `label`** beside the mark (8 px away, centred on it, the control hugging
4+
both); a tap anywhere on it toggles. A list tile no longer draws a hairline of its own: a list draws the dividers
5+
between its rows (`separator`).
6+
37
**Tell the model when its valid UI shows nothing.** Final frames of recorded SSUI generations
48
(tool/stream/streams/live/2026-11-14-ssui) showed blank cards or cards missing their content while the summary's
59
final validation was clean. The causes, and what changed:

‎lib/src/render/controls/toggles.dart‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void _toggleRecipe(NodeBuilder b, Node n, List<RenderBox> chain, ToggleKind kind
9999

100100
/// A checkbox, a switch or a radio: its value, its look in its states, the
101101
/// keyboard and screen readers.
102-
class RenderToggle extends RenderControl {
102+
class RenderToggle extends RenderControl implements LabelledControl {
103103
RenderToggle() {
104104
onPressed = _toggle;
105105
focusNode.onKey = _key;
@@ -141,6 +141,18 @@ class RenderToggle extends RenderControl {
141141
..curve = kind == ToggleKind.switchToggle && n.props.has(SwitchProps.toggleCurve)
142142
? n.prop(SwitchProps.toggleCurve)
143143
: api.AnimationCurve.easeOut;
144+
final label = switch (kind) {
145+
ToggleKind.checkbox => n.prop(CheckboxProps.label),
146+
ToggleKind.switchToggle => n.prop(SwitchProps.label),
147+
ToggleKind.radio => n.prop(RadioProps.label),
148+
};
149+
final style = TextStyle(fontFamily: tokens.fontFamily, fontSize: 13, height: 1.5, color: tokens.primaryText);
150+
if (_labelText.update(label, style, align: TextAlign.start) || label != _label) {
151+
_label = label;
152+
_labelText.layout(double.infinity);
153+
markNeedsLayout();
154+
markNeedsSemanticsUpdate();
155+
}
144156
_follow(_boundValue(n));
145157
if (changed) {
146158
markNeedsLayout();
@@ -303,15 +315,42 @@ class RenderToggle extends RenderControl {
303315

304316
// --- geometry -----------------------------------------------------------------
305317

306-
/// Its size when it hugs: its design size, else SSUI's.
307-
Size get _natural {
318+
String _label = '';
319+
final ControlLabel _labelText = ControlLabel();
320+
321+
bool get _hasLabel => _label.isNotEmpty;
322+
323+
/// The text beside the mark ('' for none).
324+
String get label => _label;
325+
326+
/// The gap between the mark and its label (SSUI's 8).
327+
static const double labelGap = 8;
328+
329+
/// The mark's size when it hugs: its design size, else SSUI's. With a
330+
/// label the mark is always SSUI's and the design size is the whole
331+
/// control's.
332+
Size get _markNatural {
308333
final ssui = switch (_kind) {
309334
ToggleKind.switchToggle => const Size(36, 20),
310335
_ => const Size(16, 16),
311336
};
337+
if (_hasLabel) return ssui;
312338
return Size(_design.width > 0 ? _design.width : ssui.width, _design.height > 0 ? _design.height : ssui.height);
313339
}
314340

341+
/// Its size when it hugs: the mark, and its label beside it.
342+
Size get _natural {
343+
final mark = _markNatural;
344+
if (!_hasLabel) return mark;
345+
return Size(mark.width + labelGap + _labelText.intrinsicWidth.ceilToDouble(),
346+
math.max(mark.height, _labelText.size.height.ceilToDouble()));
347+
}
348+
349+
@override
350+
List<({String text, String? fontFamily, List<Rect> glyphs})> get labels => !_hasLabel || !hasSize
351+
? const []
352+
: [(text: _label, fontFamily: _labelText.fontFamily, glyphs: _labelText.glyphBoxes(_markNatural.width + labelGap, size.height / 2))];
353+
315354
@override
316355
double computeMinIntrinsicWidth(double height) => _natural.width;
317356
@override
@@ -325,10 +364,21 @@ class RenderToggle extends RenderControl {
325364
Size computeDryLayout(covariant BoxConstraints constraints) => constraints.constrain(_natural);
326365

327366
@override
328-
void performLayout() => size = constraints.constrain(_natural);
367+
void performLayout() {
368+
if (_hasLabel) {
369+
final room = constraints.maxWidth - _markNatural.width - labelGap;
370+
_labelText.layout(room.isFinite ? math.max(0, room) : double.infinity);
371+
}
372+
size = constraints.constrain(_natural);
373+
}
329374

330375
/// Where the mark is: a checkbox's box, a radio's ring, a switch's track.
331376
Rect get markRect {
377+
if (_hasLabel) {
378+
// Beside its label: SSUI's mark at the start, centred on the line.
379+
final m = _markNatural;
380+
return Rect.fromLTWH(0, (size.height - m.height) / 2, m.width, m.height);
381+
}
332382
final box = Offset.zero & size;
333383
final n = node;
334384
switch (_kind) {
@@ -439,6 +489,7 @@ class RenderToggle extends RenderControl {
439489
case ToggleKind.switchToggle:
440490
_paintSwitch(canvas);
441491
}
492+
if (_hasLabel) _labelText.paint(canvas, _markNatural.width + labelGap, size.height / 2);
442493
canvas.restore();
443494
}
444495

@@ -565,7 +616,7 @@ class RenderToggle extends RenderControl {
565616
// --- semantics -----------------------------------------------------------------
566617

567618
@override
568-
String get semanticsName => '';
619+
String get semanticsName => _label;
569620

570621
@override
571622
void describeSemanticsConfiguration(SemanticsConfiguration config) {

‎test/controls/toggles_test.dart‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ void main() {
7171
expect(_toggle(tree, 'sw').trackColor, const Color(0xFF4A4A4A), reason: 'SSUI\'s dark secondaryBorder');
7272
});
7373

74+
testWidgets('a label sits beside the mark, 8 px away and centred on it; the control hugs both, and a tap on the label toggles', (tester) async {
75+
final (tree, recorded) = await _show(tester, {
76+
'box': hugging('checkbox', {'label': 'Remember me'}),
77+
'ring': hugging('radio', {'value': 'a', 'label': 'Monthly'}),
78+
'sw': hugging('switch', {'label': 'Notifications'}),
79+
});
80+
for (final id in ['box', 'ring', 'sw']) {
81+
final t = _toggle(tree, id);
82+
final mark = t.markRect, glyphs = t.labels.single.glyphs;
83+
final text = glyphs.reduce((a, b) => a.expandToInclude(b));
84+
expect(mark.left, 0, reason: '$id mark at the start');
85+
expect(text.left, mark.right + RenderToggle.labelGap, reason: '$id gap');
86+
expect(text.center.dy, closeTo(mark.center.dy, .5), reason: '$id label centred on its mark');
87+
expect(t.size.width, greaterThan(mark.width + 40), reason: '$id hugs its label too');
88+
expect(t.size.height, greaterThanOrEqualTo(mark.height));
89+
}
90+
expect(_toggle(tree, 'sw').markRect.size, const Size(36, 20));
91+
expect(_toggle(tree, 'box').semanticsName, 'Remember me');
92+
// The label is part of the control: a tap on it toggles.
93+
final box = _toggle(tree, 'box');
94+
await tester.tapAt(box.localToGlobal(Offset(box.size.width - 4, box.size.height / 2)));
95+
await tester.pump();
96+
expect(box.value, true);
97+
expect(recorded.fired, contains('box changed'));
98+
});
99+
74100
testWidgets('a node with a size keeps the islands\' proportions; compact and fillTrack fill it', (tester) async {
75101
final (tree, _) = await _show(tester, {
76102
'box': {'type': 'checkbox', 'layout': {'width': 40, 'height': 40}},

0 commit comments

Comments
 (0)