Skip to content

Add a scoped style: b.styled(edit, body) (#355) - #358

Merged
samitbasu merged 1 commit into
mainfrom
styled-355
Nov 18, 2026
Merged

samitbasu merged 1 commit into
mainfrom
styled-355

Conversation

@samitbasu

Copy link
Copy Markdown
Owner

Fixes #355.

This adds a scoped style: b.styled(edit, body) builds body with a style of its own, which is the style there changed by edit. Every node and control inside is built, measured and drawn with it. Nothing outside changes, and a nested scope starts from the one around it. It's design (A) from the review: every node carries its style explicitly, with no hidden "current style" slot. #352 (Typeset) builds on it to restyle a page's tables and rich text.

API (for review)

// Builder
pub fn styled(&mut self, edit: impl FnOnce(&mut Style), body: impl FnOnce(&mut Builder<'_, 'f, A>));
pub fn style(&self) -> &Arc<Style>;          // the style here: the context's, or the scope's

// NodeUi (draw closures)
fn node_style(&self) -> Arc<Style>;          // the node's; the context's on any other Ui

// Measure (leaf measure closures)
pub fn style(&self) -> &Arc<Style>;

The readers are public, so widgets outside core (the planned widgets crate) read the same style as core's own controls do.

How the style flows

  • Build: Builder holds style: &Arc<Style> beside its inherited text style.
    • The frame's root takes Style::of(ctx) once per frame.
    • styled clones the style once per scope per frame, applies edit, and lends the new Arc to body. It creates no node, as keyed_under doesn't.
    • Every node records its builder's Arc, which costs one refcount per node, inside a scope or not.
    • Regions (virtual lists) and overlays carry it to the nodes they build later. An overlay declared in a scope therefore inherits the scope.
  • Measure: each node's Arc reaches Measure and the library's own text measuring. Spans lay out with the node's rich, and stock widgets size with the node's input metrics.
  • Draw: the placed node keeps its Arc. The library's drawing reads it: the focus ring, inline-code tints, and the text selection colour, which is now per node instead of one per frame. Draw closures read it through NodeUi::node_style.
  • Every library control now reads b.style() where it read Style::of(b.ctx()): button, checkbox, select, slider, switch, stepper, field box, menu, table, segmented, radio, progress, splitter, scroll bars, mount, text field and tooltip triggers.

Still the context's style, on purpose

  • Font faces (Style::faces): fonts are registered context-wide in egui, so a scope can't change which faces exist. Face resolution and the symbol-font install read the context's style. styled's docs say so.
  • Tooltip timing (style.tooltip.delay and grace): one state machine per frame serves every tooltip.
  • Tooltip content: it's built at the end of the frame from the root builder, so a tooltip on a control inside a scope is drawn in the context's style. This is the one real gap; a follow-up would carry the trigger's style with its request.

TableStyle: new fields for #352

All are optional or default to today's look, so the shadcn table is unchanged.

Field Meaning
cell_padding_y: Option<f32> top and bottom padding of body and footer cells, in place of cell_padding
header_padding_y: Option<f32> top and bottom padding of header cells; the header then sizes by its text, in place of header_height
first_column_inset: Option<f32> the first column's left padding, in place of cell_padding (zero is flush)
rule_below_last: bool a rule under the last body row too

Breaking changes

  • widget::Stock::size(&self, ctx, text, width) gains a style: size(&self, ctx, text, style: &Arc<Style>, width). A stock widget must size with the style it's built in, for example a text field's input metrics inside a scope. Stock::measure passes the leaf's style. In the repo, only test implementations and callers needed it.
  • sizing::Sizer::new(ctx, text) gains the style the same way.

Tests

tests/it/styled.rs, through the real build, measure and draw:

  • a_table_inside_styled_takes_the_override_and_one_outside_does_not: the cell heights differ by exactly the padding change, and the context's style is untouched.
  • a_nested_scope_edits_the_style_of_the_one_around_it: an inner += 10 lands on the outer scope's 20.
  • rich_text_inside_styled_is_measured_and_drawn_with_the_override: inline code's padding (layout space, so measure) widens the paragraph, and the link colour (draw) is the scope's inside and the context's outside.
  • a_draw_closure_reads_the_style_its_node_was_built_in: NodeUi::node_style inside and outside a scope.

tests/it/table.rs::spacing, each set through styled:

  • cell_padding_y_sets_a_body_cells_top_and_bottom_only;
  • header_padding_y_sizes_the_header_by_its_text;
  • a_first_column_inset_moves_only_the_first_columns_text, which covers the header too;
  • rule_below_last_rules_off_the_last_row, which also checks it's off by default.

Mutations, each caught:

  • A scope that starts from the context's style instead of its parent's: the nested test fails.
  • Measuring spans with the context's rich: the rich test fails.
  • Drawing them with the context's rich: the rich test fails.
  • The table reading Style::of(b.ctx()): 6 tests fail.
  • Draw closures not given the node's style: the draw-closure test fails.

support::galleys_of is new (every galley with a text), and galley_of now uses it.

Prose for you

  • TODO(prose) placeholders:
    • Builder::style, Builder::styled (with a doctest), NodeUi::node_style (with a doctest) and Measure::style;
    • the four TableStyle fields.

git diff origin/main HEAD --stat

 src/builder.rs              |  87 ++++++++++++++++++++----
 src/collapse.rs             |   6 +-
 src/contract.rs             |  16 +++++
 src/debug.rs                |   7 +-
 src/draw.rs                 |  51 ++++++++++----
 src/layout_tree.rs          |   6 +-
 src/measure.rs              |  41 ++++++++---
 src/mount.rs                |   2 +-
 src/overlay_chain.rs        |   6 +-
 src/owned/button.rs         |   6 +-
 src/owned/checkbox.rs       |   6 +-
 src/owned/field_box.rs      |   4 +-
 src/owned/menu.rs           |   2 +-
 src/owned/mod.rs            |   2 +-
 src/owned/progress.rs       |   6 +-
 src/owned/radio.rs          |   6 +-
 src/owned/segmented.rs      |   6 +-
 src/owned/select.rs         |   2 +-
 src/owned/slider.rs         |   8 +--
 src/owned/splitter.rs       |   4 +-
 src/owned/stepper.rs        |   6 +-
 src/owned/switch.rs         |   6 +-
 src/owned/table.rs          |  32 +++++----
 src/pipeline.rs             |  37 ++++++++--
 src/prim/text_field.rs      |   2 +-
 src/region.rs               |  10 ++-
 src/scroll.rs               |   3 +-
 src/sizing.rs               |   8 +--
 src/spans.rs                |   9 +--
 src/text.rs                 |   5 ++
 src/tooltip.rs              |   2 +-
 src/widget.rs               |  31 ++++++---
 tests/it/chat_shell_demo.rs |   1 +
 tests/it/main.rs            |   1 +
 tests/it/numbers.rs         |   8 ++-
 tests/it/prims.rs           |  18 +++--
 tests/it/sizing.rs          |   7 +-
 tests/it/style.rs           |   7 +-
 tests/it/styled.rs          | 162 ++++++++++++++++++++++++++++++++++++++++++++
 tests/it/support/mod.rs     |  25 ++++++-
 tests/it/table.rs           | 112 ++++++++++++++++++++++++++++++
 tests/it/widgets.rs         |   7 +-
 42 files changed, 643 insertions(+), 130 deletions(-)

cargo xtask ci exits 0 on a816158 (main cb0c520).

🤖 Generated with Claude Code

https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP

A region of a page can now have a style of its own, as the user decided
for #352: Typeset restyles its page's tables and rich text without
touching the rest of the app.

`b.styled(edit, body)` builds `body` with the style there, changed by
`edit`. Design (A): the style travels with each node where its
inherited text style already does: the builder holds `&Arc<Style>`, every
node records its `Arc`, and measure and draw read the node's. A scope
clones the style once per frame and creates no node; nested scopes
start from the one around them.

Public readers, for widgets inside and outside core: `Builder::style`,
`NodeUi::node_style`, `Measure::style`. Every library control now reads
the scoped style where it read `Style::of(b.ctx())`; the library's own
drawing (focus ring, inline-code tints, selection colour, now per node)
reads the node's.

Left on the context's style on purpose: font faces (fonts are
context-wide), tooltip timing (one state machine a frame), and tooltip
content, which is built from the root at the frame's end.

`TableStyle` gains `cell_padding_y`, `header_padding_y`,
`first_column_inset` and `rule_below_last`, all off by default, for
typeset's tables.

Breaking: `widget::Stock::size` and `sizing::Sizer::new` take the style,
so a stock widget sizes with the style it is built in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP
@samitbasu

Copy link
Copy Markdown
Owner Author

Gate (my run on a816158 with current main): cargo xtask ci exits 0. This implements #355 as decided (design A). There's no visible change; the tooltip-content gap is filed as #359. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scoped style override: b.styled(edit, children)

1 participant