Skip to main content

egui_pop_api/
scroll_bars.rs

1//! The `appearance.scroll_bars` setting: `auto` leaves the bars to the theme,
2//! `always` and `hover` decide over it. The system's "always show" raises
3//! `auto` to `always` (`AppBuilder::system_always_shows_scroll_bars`), and
4//! nothing a system says lowers it. Written into the theme's spec
5//! ([`ThemeSpec::scroll_bars`]), so that it reaches egui's style with the
6//! theme, in every scroll area, and stays when the theme changes.
7
8use 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
16/// The setting: `auto`, `always`, `hover`.
17pub const SETTING_SCROLL_BARS: SettingId = SettingId::from_static("appearance.scroll_bars");
18
19/// What the setting says now.
20pub 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
27/// What the setting asks of the theme: the system's "always show" raises
28/// `auto`, and never touches a choice the user made.
29fn asked(setting: ScrollBars, system_always: bool) -> ScrollBars {
30    match setting {
31        ScrollBars::Auto if system_always => ScrollBars::Always,
32        chosen => chosen,
33    }
34}
35
36/// Writes what the setting asks into the spec. A choice stays there, even
37/// through an application's new spec; `auto` leaves the application's spec
38/// alone unless it comes back from another choice.
39pub(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}