1use egui::Color32;
12use egui::style::ScrollStyle;
13
14use super::window::choice;
15use super::{State, Tokens};
16
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19pub enum Placement {
20 #[default]
23 Over,
24 Beside,
27}
28
29#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
31pub enum Visibility {
32 Always,
34 Rest,
38 #[default]
41 Hover,
42 Scroll,
45}
46
47#[derive(Clone, Copy, Debug, PartialEq)]
49pub struct ScrollBarKit {
50 pub placement: Placement,
51 pub visibility: Visibility,
52 pub width: f32,
54 pub width_rest: f32,
57 pub opacity_rest: f32,
60 pub room: f32,
63 pub min_length: f32,
65 pub margin: f32,
68 pub margin_track: f32,
75 pub track: Color32,
78}
79
80impl Default for ScrollBarKit {
81 fn default() -> Self {
83 let egui = ScrollStyle::floating();
84 ScrollBarKit {
85 placement: Placement::Over,
86 visibility: Visibility::Hover,
87 width: egui.bar_width,
88 width_rest: egui.floating_width,
89 opacity_rest: egui.active_handle_opacity,
90 room: ScrollStyle::thin().floating_allocated_width,
91 min_length: egui.handle_min_length,
92 margin: egui.bar_outer_margin,
93 margin_track: 0.0,
94 track: Color32::TRANSPARENT,
95 }
96 }
97}
98
99impl ScrollBarKit {
100 pub(super) fn compile(
101 t: &Tokens,
102 component: &super::ComponentKit,
103 problems: &mut Vec<String>,
104 ) -> ScrollBarKit {
105 let r = t.resolved();
106 let egui = ScrollBarKit::default();
107 let placement = choice(
108 t,
109 "scroll-bar.placement",
110 &[("over", Placement::Over), ("beside", Placement::Beside)],
111 problems,
112 );
113 let visibility = choice(
114 t,
115 "scroll-bar.visibility",
116 &[
117 ("always", Visibility::Always),
118 ("rest", Visibility::Rest),
119 ("hover", Visibility::Hover),
120 ("scroll", Visibility::Scroll),
121 ],
122 problems,
123 );
124 if visibility == Visibility::Scroll {
125 problems.push(
126 "scroll-bar.visibility: egui has no while-scrolling state, `scroll` is drawn as `hover`"
127 .into(),
128 );
129 }
130 if placement == Placement::Beside && visibility == Visibility::Rest {
131 problems.push(
132 "scroll-bar.visibility: egui draws a bar `beside` the content opaque and at full width, `rest` needs `over`"
133 .into(),
134 );
135 } else if placement == Placement::Beside && visibility != Visibility::Always {
136 problems.push(
137 "scroll-bar.visibility: egui always shows a bar `beside` the content when the content overflows"
138 .into(),
139 );
140 }
141 let width = if component.width > 0.0 {
142 component.width
143 } else {
144 egui.width
145 };
146 let width_rest = r
147 .dimension("scroll-bar.width-rest")
148 .map_or(egui.width_rest, |w| w.max(0.0));
149 if visibility == Visibility::Rest && width_rest > width {
150 problems.push(format!(
151 "scroll-bar.width-rest: {width_rest} is wider than `width`, drawn at {width}"
152 ));
153 }
154 let opacity_rest = r
155 .dimension("scroll-bar.opacity-rest")
156 .unwrap_or(egui.opacity_rest);
157 if !(0.0..=1.0).contains(&opacity_rest) {
158 problems.push(format!(
159 "scroll-bar.opacity-rest: {opacity_rest} is not between 0 and 1"
160 ));
161 }
162 ScrollBarKit {
163 placement,
164 visibility,
165 width,
166 width_rest,
167 opacity_rest: opacity_rest.clamp(0.0, 1.0),
168 room: r
169 .dimension("scroll-bar.room")
170 .map_or(egui.room, |m| m.max(0.0)),
171 min_length: r
172 .dimension("scroll-bar.min-length")
173 .map_or(egui.min_length, |m| m.max(0.0)),
174 margin: r
175 .dimension("scroll-bar.margin")
176 .map_or(egui.margin, |m| m.max(0.0)),
177 margin_track: r
178 .dimension("scroll-bar.margin-track")
179 .map_or(egui.margin_track, |m| m.max(0.0)),
180 track: t
181 .color("scroll-bar.track-fill")
182 .unwrap_or(Color32::TRANSPARENT),
183 }
184 }
185
186 pub fn track_rect(&self, inner: egui::Rect) -> Option<egui::Rect> {
191 if self.margin_track <= 0.0 {
192 return None;
193 }
194 let m = egui::vec2(
196 self.margin_track.min(inner.width() / 2.0),
197 self.margin_track.min(inner.height() / 2.0),
198 );
199 Some(inner.shrink2(m))
200 }
201
202 pub fn seen_at_rest(self) -> ScrollBarKit {
206 ScrollBarKit {
207 visibility: match self.visibility {
208 Visibility::Always => Visibility::Always,
209 _ => Visibility::Rest,
210 },
211 ..self
212 }
213 }
214
215 pub fn style(&self, base: &ScrollStyle) -> ScrollStyle {
222 let egui = ScrollStyle::floating();
223 let mut s = *base;
224 s.floating = self.placement == Placement::Over;
225 s.bar_width = self.width;
226 s.bar_outer_margin = self.margin;
227 s.floating_allocated_width = 0.0;
228 s.foreground_color = true;
229 s.handle_min_length = self.min_length;
230 match self.visibility {
231 Visibility::Always => {
232 s.floating_width = self.width;
233 s.dormant_handle_opacity = 1.0;
234 s.active_handle_opacity = 1.0;
235 s.interact_handle_opacity = 1.0;
236 s.dormant_background_opacity = 1.0;
237 s.active_background_opacity = 1.0;
238 s.interact_background_opacity = 1.0;
239 }
240 Visibility::Rest => {
241 s.floating_width = self.width_rest.min(self.width);
242 s.floating_allocated_width = self.room;
243 s.dormant_handle_opacity = self.opacity_rest;
244 s.active_handle_opacity = egui.active_handle_opacity.max(self.opacity_rest);
246 s.interact_handle_opacity = egui.interact_handle_opacity;
247 s.dormant_background_opacity = 0.0;
249 s.active_background_opacity = 0.0;
250 s.interact_background_opacity = egui.interact_background_opacity;
251 }
252 Visibility::Hover | Visibility::Scroll => {
253 s.floating_width = egui.floating_width.min(self.width);
254 s.dormant_handle_opacity = egui.dormant_handle_opacity;
255 s.active_handle_opacity = egui.active_handle_opacity;
256 s.interact_handle_opacity = egui.interact_handle_opacity;
257 s.dormant_background_opacity = egui.dormant_background_opacity;
258 s.active_background_opacity = egui.active_background_opacity;
259 s.interact_background_opacity = egui.interact_background_opacity;
260 }
261 }
262 if self.track.a() == 0 {
263 s.dormant_background_opacity = 0.0;
266 s.active_background_opacity = 0.0;
267 s.interact_background_opacity = 0.0;
268 }
269 s
270 }
271}
272
273pub(super) fn losses(t: &Tokens, kit: &super::Kit) -> Vec<String> {
277 let mut lost = Vec::new();
278 let (bar, button) = (&kit.scroll_bar, &kit.button);
279 let thumb_differs = [State::REST, State::HOVER, State::ACTIVE]
280 .into_iter()
281 .any(|s| bar.style(s).fill != button.style(s).text)
282 || bar.style(State::REST).radius != button.style(State::REST).radius;
283 if thumb_differs {
284 lost.push(
285 "scroll-bar: egui's scroll areas outside a `ScrollBar` scope (`UiPop::scroll_area`) draw the thumb in the button's text colours and corners"
286 .into(),
287 );
288 }
289 let track = kit.scroll.track;
290 if track.a() > 0 && track != t.surface.sunken {
291 lost.push(
292 "scroll-bar.track-fill: egui's scroll areas outside a `ScrollBar` scope draw the track in `surface.sunken`"
293 .into(),
294 );
295 }
296 lost
297}
298
299#[cfg(test)]
300mod tests {
301 use super::*;
302 use crate::kit::{Kit, Shapes};
303 use egui_pop_tokens::contrast::contrast_ratio;
304 use egui_pop_tokens::{ThemeSpec, Themes};
305
306 fn kit(yaml: &str) -> Kit {
307 let mut themes = Themes::default();
308 themes
309 .add_text("scroll-test", yaml)
310 .expect("test theme reads");
311 let t = Tokens::new(themes.resolve(&ThemeSpec::new("scroll-test")));
312 Kit::compile(&t, &Shapes::default())
313 }
314
315 fn thumb_ratio(thumb: Color32, opacity: f32, surface: Color32) -> f32 {
319 let lift = |c: Color32| {
320 let [r, g, b, _] = c.to_array();
321 let unit = |v: u8| f32::from(v) / 255.0;
322 egui_pop_tokens::Color::rgb(unit(r), unit(g), unit(b))
323 };
324 contrast_ratio(
325 lift(surface.blend(thumb.gamma_multiply(opacity))),
326 lift(surface),
327 )
328 }
329
330 #[test]
335 fn every_built_in_theme_s_thumb_at_rest_reaches_3_to_1() {
336 let mut failures = Vec::new();
337 for name in egui_pop_tokens::BUILT_IN_THEMES {
338 let t = Tokens::new(Themes::default().resolve(&ThemeSpec::new(*name)));
339 let kit = Kit::compile(&t, &Shapes::default());
340 assert!(kit.problems.is_empty(), "{name}: {:?}", kit.problems);
341 let style = kit.scroll.style(&ScrollStyle::default());
342 let thumb = kit.scroll_bar.style(State::REST).fill;
345 let opacity = if style.floating {
346 style.dormant_handle_opacity
347 } else {
348 1.0
349 };
350 let mut line = format!("{name:28} {opacity:.2}");
351 for (surface, fill) in [
352 ("panel", t.surface.panel),
353 ("canvas", t.surface.canvas),
354 ("overlay", t.surface.overlay),
355 ("raised", t.surface.raised),
356 ("sunken", t.surface.sunken),
357 ] {
358 let ratio = thumb_ratio(thumb, opacity, fill);
359 line += &format!(" {surface} {ratio:.2}");
360 if ratio < 3.0 {
361 failures.push(format!("{name}: {ratio:.2}:1 on {surface}"));
362 }
363 }
364 println!("{line}");
365 }
366 assert!(failures.is_empty(), "{failures:?}");
367 }
368
369 #[test]
370 fn built_in_themes_show_quiet_bars_at_rest_but_high_contrast_ones_full_bars() {
371 for name in egui_pop_tokens::BUILT_IN_THEMES {
372 let t = Tokens::new(Themes::default().resolve(&ThemeSpec::new(*name)));
373 let bar = Kit::compile(&t, &Shapes::default()).scroll;
374 if name.contains("high-contrast") {
375 assert_eq!(bar.visibility, Visibility::Always, "{name}");
376 assert_eq!(bar.placement, Placement::Beside, "{name}");
377 assert_eq!(bar.width, 14.0, "{name}");
378 } else {
379 assert_eq!(bar.visibility, Visibility::Rest, "{name}");
380 assert_eq!(bar.placement, Placement::Over, "{name}");
381 assert_eq!(
382 (bar.width, bar.width_rest, bar.room, bar.min_length),
383 (10.0, 4.0, 6.0, 24.0),
384 "{name}"
385 );
386 }
387 }
388 }
389
390 #[test]
391 fn hover_still_gives_egui_s_own_floating_bars() {
392 let k = kit(r##"
393 $extends: "pop-dark"
394 scroll-bar:
395 visibility: {$type: "string", $value: "hover"}
396 min-length: {$type: "dimension", $value: {value: 12, unit: "px"}}
397 "##);
398 assert!(k.problems.is_empty(), "{:?}", k.problems);
399 assert_eq!(
400 k.scroll.style(&ScrollStyle::default()),
401 ScrollStyle::floating()
402 );
403 }
404
405 #[test]
406 fn rest_writes_its_tokens_into_egui_s_style() {
407 let k = kit(r##"
408 $extends: "pop-dark"
409 scroll-bar:
410 visibility: {$type: "string", $value: "rest"}
411 width: {$type: "dimension", $value: {value: 12, unit: "px"}}
412 width-rest: {$type: "dimension", $value: {value: 3, unit: "px"}}
413 opacity-rest: {$type: "number", $value: 0.8}
414 room: {$type: "dimension", $value: {value: 5, unit: "px"}}
415 min-length: {$type: "dimension", $value: {value: 30, unit: "px"}}
416 "##);
417 assert!(k.problems.is_empty(), "{:?}", k.problems);
418 assert_eq!(k.scroll.visibility, Visibility::Rest);
419 let s = k.scroll.style(&ScrollStyle::default());
420 assert!(s.floating);
421 assert_eq!(s.bar_width, 12.0, "the hit area, and the bar at its widest");
422 assert_eq!(s.floating_width, 3.0);
423 assert_eq!(s.floating_allocated_width, 5.0);
424 assert_eq!(s.handle_min_length, 30.0);
425 assert_eq!(s.dormant_handle_opacity, 0.8);
426 assert_eq!(s.active_handle_opacity, 0.8, "coming near never dims it");
427 assert_eq!(s.interact_handle_opacity, 1.0);
428 assert_eq!(s.dormant_background_opacity, 0.0, "no track at rest");
429 assert_eq!(s.active_background_opacity, 0.0);
430 assert!(s.interact_background_opacity > 0.0, "a track over the bar");
431 }
432
433 #[test]
434 fn rest_s_terms_are_kept_out_of_the_other_visibilities() {
435 let k = kit(r##"
436 $extends: "pop-dark"
437 scroll-bar:
438 visibility: {$type: "string", $value: "hover"}
439 width-rest: {$type: "dimension", $value: {value: 3, unit: "px"}}
440 opacity-rest: {$type: "number", $value: 0.8}
441 room: {$type: "dimension", $value: {value: 5, unit: "px"}}
442 "##);
443 let s = k.scroll.style(&ScrollStyle::default());
444 assert_eq!(s.floating_allocated_width, 0.0);
445 assert_eq!(s.dormant_handle_opacity, 0.0);
446 assert_eq!(s.floating_width, ScrollStyle::floating().floating_width);
447 }
448
449 #[test]
450 fn seen_at_rest_never_lowers_an_always_bar() {
451 let mut bar = ScrollBarKit::default();
452 assert_eq!(bar.seen_at_rest().visibility, Visibility::Rest);
453 bar.visibility = Visibility::Scroll;
454 assert_eq!(bar.seen_at_rest().visibility, Visibility::Rest);
455 bar.visibility = Visibility::Always;
456 assert_eq!(bar.seen_at_rest().visibility, Visibility::Always);
457 }
458
459 #[test]
460 fn a_rest_bar_egui_cannot_draw_is_a_problem() {
461 let k = kit(r##"
462 $extends: "pop-dark"
463 scroll-bar:
464 placement: {$type: "string", $value: "beside"}
465 visibility: {$type: "string", $value: "rest"}
466 width-rest: {$type: "dimension", $value: {value: 20, unit: "px"}}
467 opacity-rest: {$type: "number", $value: 1.5}
468 "##);
469 let p = k.problems.join("\n");
470 assert!(p.contains("`rest` needs `over`"), "{p}");
471 assert!(p.contains("wider than `width`"), "{p}");
472 assert!(p.contains("not between 0 and 1"), "{p}");
473 assert_eq!(k.scroll.opacity_rest, 1.0);
474 assert_eq!(
475 k.scroll.style(&ScrollStyle::default()).floating_width,
476 k.scroll.width
477 );
478 }
479
480 #[test]
481 fn what_egui_cannot_draw_of_a_scroll_bar_is_a_problem() {
482 let k = kit(r##"
483 $extends: "pop-dark"
484 scroll-bar:
485 placement: {$type: "string", $value: "beside"}
486 visibility: {$type: "string", $value: "scroll"}
487 track-fill: {$type: "color", $value: "#ff0000"}
488 rest:
489 fill: {$type: "color", $value: "#4b4b4b"}
490 "##);
491 let p = k.problems.join("\n");
492 assert!(p.contains("`scroll` is drawn as `hover`"), "{p}");
493 assert!(p.contains("`beside` the content"), "{p}");
494 assert!(p.contains("scroll-bar: egui's scroll areas"), "{p}");
495 assert!(p.contains("scroll-bar.track-fill"), "{p}");
496 let bad = kit(r##"
497 $extends: "pop-dark"
498 scroll-bar:
499 placement: {$type: "string", $value: "under"}
500 "##);
501 assert_eq!(bad.scroll.placement, Placement::Over);
502 assert_eq!(
503 bad.problems,
504 ["scroll-bar.placement: `under` is not over or beside"]
505 );
506 }
507}