egui_pop_api/
scroll_bars.rs1use egui_pop_core::lock;
9use egui_pop_core::theme::{ScrollBars, ThemeSpec};
10use egui_pop_model::ids::SettingId;
11use egui_pop_model::values::Descriptor;
12
13use crate::app::AppHandle;
14use crate::ctx::{Ctx, CtxError};
15
16pub const SETTING_SCROLL_BARS: SettingId = SettingId::from_static("appearance.scroll_bars");
18
19pub fn scroll_bars(app: &AppHandle) -> ScrollBars {
21 lock(&app.store())
22 .get(&format!("settings/{SETTING_SCROLL_BARS}"))
23 .and_then(|e| e.value.as_choice().and_then(ScrollBars::parse))
24 .unwrap_or_default()
25}
26
27fn asked(setting: ScrollBars, system_always: bool) -> ScrollBars {
30 match setting {
31 ScrollBars::Auto if system_always => ScrollBars::Always,
32 chosen => chosen,
33 }
34}
35
36pub(crate) fn apply(app: &AppHandle, seen: &mut Option<ScrollBars>, spec: &mut ThemeSpec) -> bool {
40 let wanted = asked(scroll_bars(app), app.system_always_shows_scroll_bars());
41 let moved = seen.is_some_and(|before| before != wanted);
42 *seen = Some(wanted);
43 if (wanted != ScrollBars::Auto || moved) && spec.scroll_bars != wanted {
44 spec.scroll_bars = wanted;
45 return true;
46 }
47 false
48}
49
50pub(crate) fn register(ctx: &mut Ctx<'_>) -> Result<(), CtxError> {
51 ctx.setting(
52 SETTING_SCROLL_BARS,
53 Descriptor::choice(
54 "Scroll bars",
55 &ScrollBars::CHOICES,
56 ScrollBars::Auto.as_str(),
57 )
58 .description(
59 "When content overflows: bars as the theme draws them (auto), full bars beside the content (always), or none until the pointer is over it (hover). Auto gives full bars too when the application reports that the system always shows them.",
60 ),
61 )?;
62 Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn the_system_raises_auto_and_changes_no_choice() {
71 assert_eq!(asked(ScrollBars::Auto, false), ScrollBars::Auto);
72 assert_eq!(asked(ScrollBars::Auto, true), ScrollBars::Always);
73 assert_eq!(asked(ScrollBars::Hover, true), ScrollBars::Hover);
74 assert_eq!(asked(ScrollBars::Always, false), ScrollBars::Always);
75 }
76}