U0004 ui-size-literal¶
Rule: UI code takes spacing, sizes, radii and type sizes from the theme, never writes them as numbers.
| Code | U0004 (stable, never reused) |
| Id | ui-size-literal |
| Category | ui |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | off | |
| ai | off | |
| embedded | off | |
| ui | warn |
sizes written as numbers drift apart between screens and apps |
| egui | warn |
spacing, radii, strokes and type come from ve_theme's tokens (space, radius, stroke, type_scale); widths are learned with ve_dash's steady_width |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
calls |
list of strings | [] |
methods (a name) and functions (Type::name) whose number arguments are sizes |
fields |
list of strings | [] |
style fields whose assigned numbers are sizes |
allowed |
list of strings | ["0"] |
numbers that are no size choice |
theme-crates |
list of strings | ["*_theme", "*-theme"] |
crates that are the theme, besides those the crate map marks theme = true |
theme-paths |
list of strings | ["**/theme.rs", "**/theme/**"] |
files that are the theme |
toolkits |
list of strings | ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"] |
UI toolkits: a crate that depends on one directly is a UI crate, unless the crate map says ui = false |
Why¶
Spacing, radii, stroke widths and type sizes written as numbers in every screen drift apart: one panel uses 6.0, the next 8.0, and a change of the theme reaches none of them. Fixed pixel widths also break the layout when the text, the language or the font changes.
The lint reports number literals, in UI crates outside the theme:
- in the arguments of the calls in
calls: a method by its name (add_space), a function byType::name(Margin::same); an array, a tuple or avec2(..)counts number by number; - assigned to a style field in
fields(spacing_mut().item_spacing.x = 6.0).
Numbers in allowed (0) are not reported. The ui tier knows no toolkit, so calls and fields are empty; the egui tier fills them.
How to fix¶
Take the size from the theme's tokens (in vhrd_egui: t.space.m, t.radius.s, t.stroke.thin, t.type_scale.title.font_id()). A width that must not jump is learned from the content (ve_dash::steady_width), not written down.
Configure¶
[lints.ui-size-literal]
level = "deny"
calls = []
fields = []
allowed = ["0"]
theme-crates = ["*_theme", "*-theme"]
theme-paths = ["**/theme.rs", "**/theme/**"]
toolkits = ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"]
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(ui-size-literal) <reason> (waivers); the code works there too, as in [lints.U0004].