Skip to content

Commit

Permalink
Add three pre-sets of scroll bars: solid, thin, floating
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 9, 2023
1 parent 4a9f30a commit 49c805e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ impl Prepared {
1.0
};

let handle_color = if scroll_style.floating {
let handle_color = if scroll_style.foreground_color {
visuals.fg_stroke.color
} else {
visuals.bg_fill
Expand Down
69 changes: 66 additions & 3 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ pub struct ScrollStyle {
/// so as to always show a thin scroll bar.
pub floating_allocated_width: f32,

/// If true, use colors with more contrast. Good for floating scroll bars.
pub foreground_color: bool,

/// The opaqueness of the background when the user is neither scrolling
/// nor hovering the scroll area.
///
Expand Down Expand Up @@ -412,15 +415,24 @@ pub struct ScrollStyle {

impl Default for ScrollStyle {
fn default() -> Self {
Self::solid()
}
}

impl ScrollStyle {
/// Solid scroll bars that always use up space
pub fn solid() -> Self {
Self {
floating: false,
bar_width: 8.0,
bar_width: 6.0,
handle_min_length: 12.0,
bar_inner_margin: 4.0,
bar_outer_margin: 0.0,
floating_width: 2.0,
floating_allocated_width: 0.0,

foreground_color: false,

dormant_background_opacity: 0.0,
active_background_opacity: 0.4,
interact_background_opacity: 0.7,
Expand All @@ -430,9 +442,44 @@ impl Default for ScrollStyle {
interact_handle_opacity: 1.0,
}
}
}

impl ScrollStyle {
/// Thin scroll bars that expand on hover
pub fn thin() -> Self {
Self {
floating: true,
bar_width: 12.0,
floating_allocated_width: 6.0,
foreground_color: false,

dormant_background_opacity: 1.0,
dormant_handle_opacity: 1.0,

active_background_opacity: 1.0,
active_handle_opacity: 1.0,

// Be tranlucent when expanded so we can see the content
interact_background_opacity: 0.6,
interact_handle_opacity: 0.6,

..Self::solid()
}
}

/// No scroll bars until you hover the scroll area,
/// at which time they appear faintly, and then expand
/// when you hover the scroll bars.
pub fn floating() -> Self {
Self {
floating: true,
bar_width: 12.0,
foreground_color: true,
floating_allocated_width: 0.0,
dormant_background_opacity: 0.0,
dormant_handle_opacity: 0.0,
..Self::solid()
}
}

/// Width of a solid vertical scrollbar, or height of a horizontal scroll bar, when it is at its widest.
pub fn allocated_width(&self) -> f32 {
if self.floating {
Expand All @@ -443,6 +490,13 @@ impl ScrollStyle {
}

pub fn ui(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| {
ui.label("Presets:");
ui.selectable_value(self, Self::solid(), "Solid");
ui.selectable_value(self, Self::thin(), "Thin");
ui.selectable_value(self, Self::floating(), "Floating");
});

let Self {
floating,
bar_width,
Expand All @@ -452,6 +506,8 @@ impl ScrollStyle {
floating_width,
floating_allocated_width,

foreground_color,

dormant_background_opacity,
active_background_opacity,
interact_background_opacity,
Expand Down Expand Up @@ -489,6 +545,13 @@ impl ScrollStyle {
ui.add(DragValue::new(bar_outer_margin).clamp_range(0.0..=32.0));
ui.label("Outer margin");
});

ui.horizontal(|ui| {
ui.label("Color:");
ui.selectable_value(foreground_color, false, "Background");
ui.selectable_value(foreground_color, true, "Foreground");
});

if *floating {
crate::Grid::new("opacity").show(ui, |ui| {
fn opacity_ui(ui: &mut Ui, opacity: &mut f32) {
Expand Down

0 comments on commit 49c805e

Please sign in to comment.