hyprforge_ui/density.rs
1//! Row/header/type-ramp sizing every Hyprforge app shares, derived from
2//! font metrics rather than copied out of a mockup as pixel constants.
3//!
4//! Files' design came first and moved here when Settings adopted the
5//! same one, so the two draw a row, a bar and a corner the same size.
6//! What only Files needs — the grid, its menus, its sidebar — stayed in
7//! `hyprforge-files-core`.
8//!
9//! The design specifies a 40px header, 28px rows, a 15px type ramp and a
10//! 6px-inner/12px-outer radius pair — but those numbers are only true at
11//! the theme's default text size and 100% [`FontScale`]. Pillar 7 puts
12//! font scaling in the shared layer for exactly this reason: a `28.0`
13//! literal for row height clips its own text the moment someone raises
14//! the scale past 1.0, because the text grows and the row does not. So
15//! every design pixel value here is stored as its *ratio* to
16//! [`BASE_TEXT_SIZE`] at identity scale.
17//!
18//! Two different things come out of that ratio, deliberately kept apart:
19//!
20//! - A **text size** ([`ROW_TEXT_BASE`], [`META_TEXT_BASE`]) is left
21//! unscaled and handed to [`crate::widgets::scaled_text`] (or
22//! [`meta_text`](crate::widgets::meta_text)) the same way
23//! [`BASE_TEXT_SIZE`] itself already is everywhere else in this crate
24//! — applying `FontScale` a second time here would compound it rather
25//! than replace it, the exact mistake [`FontScale::apply`]'s own doc
26//! warns against.
27//! - A **layout dimension** ([`row_height`], [`bar_height`]) is a
28//! function that returns the final, already-scaled pixel value,
29//! because a container's `Length::Fixed` needs a concrete number, not
30//! something a widget will scale for it later.
31//!
32//! At 100% the numbers below reproduce the design's own values exactly
33//! (pinned in the tests); above 100% the row, the header and the text
34//! inside them grow together instead of the text alone overflowing a row
35//! that stayed put.
36//!
37//! Radii take the same "ratio, not constant" shape but from
38//! [`hyprforge_look::Theme::rounding`] instead of text size: the design's
39//! outer/inner pair is preserved as a *relationship* (inner is exactly
40//! half the outer) rather than as two independent magic numbers, so a
41//! user who changes Hyprland's own `decoration:rounding` gets an inner
42//! radius that stays visibly tighter than the outer one instead of one
43//! that quietly drifts to match or overtake it.
44
45use crate::theme::{self, FontScale, BASE_TEXT_SIZE};
46
47/// design px / [`BASE_TEXT_SIZE`], at identity scale.
48const ROW_HEIGHT_RATIO: f32 = 28.0 / BASE_TEXT_SIZE;
49
50/// The primary text size inside a row — a file's name — **unscaled**:
51/// feed this straight to `scaled_text`/`meta_text` as the `base_size`
52/// argument, which applies `FontScale` itself, the same way every other
53/// call site feeds it [`BASE_TEXT_SIZE`]. It is already in the same
54/// "logical px before scale" unit `BASE_TEXT_SIZE` is, so no further
55/// division by it is needed — this constant *is* the design's 15px type
56/// ramp at identity scale, not a ratio applied to something else.
57pub const ROW_TEXT_BASE: f32 = 15.0;
58
59/// The metadata text size inside a row — size, modified, kind — also
60/// unscaled. The design only names the 15px primary size; this keeps the
61/// 13/15 relationship the crate's earlier hand-picked `13.0`-vs-`14.0`
62/// pairing already had, expressed as a ratio of the primary size instead
63/// of a second independent constant.
64pub const META_TEXT_BASE: f32 = ROW_TEXT_BASE * (13.0 / 15.0);
65
66/// Half the outer radius — see the module doc for why this is a
67/// relationship, not a second magic number.
68const INNER_OUTER_RATIO: f32 = 0.5;
69
70/// A list row's final height in logical pixels, `FontScale` already
71/// applied. `28.0` at [`FontScale::default`]; grows proportionally above
72/// 100%.
73pub fn row_height(scale: FontScale) -> f32 {
74 scale.apply(BASE_TEXT_SIZE) * ROW_HEIGHT_RATIO
75}
76
77/// The window's own corner radius — the design's "12px outer". Taken
78/// straight from the active [`hyprforge_look::Theme`] rather than
79/// duplicated: `Theme::rounding` already *is* Hyprland's own
80/// `decoration:rounding`, and it happens to default to 12, which is a
81/// measured fact about the default theme rather than a coincidence this
82/// function relies on.
83pub fn outer_radius() -> f32 {
84 theme::active().rounding as f32
85}
86
87/// A row's/sidebar button's corner radius — the design's "6px inner".
88/// See the module doc: this is deliberately half of [`outer_radius`]
89/// rather than its own constant.
90pub fn inner_radius() -> f32 {
91 outer_radius() * INNER_OUTER_RATIO
92}
93
94/// A card's corner — a setting row, a hero card, a table row. The
95/// mockup's 9px, and its most common radius by far.
96///
97/// Three quarters of the window's radius: between the window (12) and a
98/// top-level control (6), because a card is a level between the two.
99pub fn card_radius() -> f32 {
100 outer_radius() * CARD_OUTER_RATIO
101}
102
103const CARD_OUTER_RATIO: f32 = 0.75;
104
105/// The header bar's height. 44px at 100% scale.
106///
107/// Taller than the 28px controls inside it on purpose: the difference is
108/// what makes the bar read as a *plane* the controls sit on, rather than
109/// as a row of controls with a background colour.
110pub fn bar_height(scale: FontScale) -> f32 {
111 scale.apply(BAR_HEIGHT_BASE)
112}
113
114/// A text field — the path bar, the search field. 28px at 100%.
115pub fn field_height(scale: FontScale) -> f32 {
116 scale.apply(FIELD_HEIGHT_BASE)
117}
118
119/// A glyph button — the nav cluster, a view-mode segment. 26px at 100%.
120///
121/// Two pixels shorter than a field, deliberately: enough difference that
122/// a button feels pressable and a field feels typeable, close enough
123/// that the row still baselines cleanly.
124pub fn glyph_button(scale: FontScale) -> f32 {
125 scale.apply(GLYPH_BUTTON_BASE)
126}
127
128/// The radius on something nested *inside* a control — the path bar's
129/// current-directory chip, a segment inside the view-mode track.
130///
131/// Third level of a deliberate ladder: 12 on the window, 6 on a
132/// top-level control, 4 on something inside one. A nested thing sharing
133/// its parent's radius reads as a second parent rather than as a child.
134pub fn nested_radius() -> f32 {
135 inner_radius() * NESTED_RADIUS_FRACTION
136}
137
138const BAR_HEIGHT_BASE: f32 = 44.0;
139const FIELD_HEIGHT_BASE: f32 = 28.0;
140const GLYPH_BUTTON_BASE: f32 = 26.0;
141const NESTED_RADIUS_FRACTION: f32 = 4.0 / 6.0;
142
143/// A sidebar section heading — `PLACES`, `PINNED`, `TRASH`.
144///
145/// Smaller than the meta text it is built from, because a heading in a
146/// sidebar is a signpost rather than something to read: it should be
147/// findable when looked for and invisible when not.
148pub const SECTION_LABEL_BASE: f32 = META_TEXT_BASE * 0.85;
149
150
151/// A setting row's least height — the design's 52px card row at 100%.
152///
153/// A floor, not a height: a row carrying a hint line under its label
154/// grows past it rather than clipping the hint, and the floor is what
155/// keeps a page of one-line rows on a steady rhythm.
156pub fn setting_row_height(scale: FontScale) -> f32 {
157 scale.apply(SETTING_ROW_HEIGHT_BASE)
158}
159
160const SETTING_ROW_HEIGHT_BASE: f32 = 52.0;
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 // These pin the design's literal numbers at the scale they were
167 // measured at. `Theme::rounding` defaults to 12 and nothing in this
168 // crate's test suite ever calls `theme::init`, so the outer/inner
169 // assertions below hold regardless of test order — `theme::init`'s
170 // `OnceLock` only ever accepts the first call in the whole test
171 // binary, and a test that installed a theme would decide it for
172 // every other test here.
173
174 #[test]
175 fn at_default_scale_every_number_matches_the_design_exactly() {
176 let scale = FontScale::default();
177 assert_eq!(row_height(scale), 28.0);
178 assert_eq!(scale.apply(ROW_TEXT_BASE), 15.0);
179 assert_eq!(outer_radius(), 12.0, "Theme::rounding's default");
180 assert_eq!(inner_radius(), 6.0, "half the outer radius");
181 assert_eq!(card_radius(), 9.0, "the mockup's card corner");
182 }
183
184 #[test]
185 fn at_125_percent_every_size_grows_proportionally_not_clipped() {
186 let scale = FontScale(1.25);
187 assert_eq!(row_height(scale), 28.0 * 1.25);
188 assert_eq!(scale.apply(ROW_TEXT_BASE), 15.0 * 1.25);
189 }
190
191 /// The property the whole module exists for: whatever the scale, the
192 /// text drawn in a row must fit inside that row. A fixed 28px row
193 /// beside text that grows with `FontScale` would violate this past
194 /// some scale; deriving both from the same base never can, because
195 /// they share the same `FontScale::apply(BASE_TEXT_SIZE)` factor.
196 #[test]
197 fn text_never_exceeds_the_row_it_is_in() {
198 for tenths in 5..=30 {
199 let scale = FontScale(tenths as f32 / 10.0);
200 let text = scale.apply(ROW_TEXT_BASE);
201 let row = row_height(scale);
202 assert!(text < row, "row text {text} must fit inside row height {row} at scale {tenths}");
203 }
204 }
205
206 /// A setting row is a floor for a one-line row, so it must hold one
207 /// line of row text at any scale — and still be the design's 52px at
208 /// 100%.
209 #[test]
210 fn a_setting_row_holds_its_label_at_every_scale() {
211 assert_eq!(setting_row_height(FontScale::default()), 52.0);
212 for tenths in 5..=30 {
213 let scale = FontScale(tenths as f32 / 10.0);
214 assert!(scale.apply(ROW_TEXT_BASE) < setting_row_height(scale));
215 }
216 }
217
218 #[test]
219 fn metadata_text_stays_smaller_than_the_primary_size() {
220 let scale = FontScale(1.25);
221 assert!(scale.apply(META_TEXT_BASE) < scale.apply(ROW_TEXT_BASE));
222 }
223
224 #[test]
225 fn inner_radius_stays_tighter_than_outer_whatever_rounding_is() {
226 // outer_radius() reads the live theme rather than a constant, so
227 // this asserts the *relationship* the design cares about rather
228 // than a specific number — see the module doc.
229 assert!(inner_radius() < outer_radius());
230 assert_eq!(inner_radius(), outer_radius() * 0.5);
231 }
232}