Skip to content

Commit

Permalink
Move scroll bar spacing settings to a struct ScrollSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 8, 2023
1 parent f218825 commit 954376f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
12 changes: 6 additions & 6 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,8 @@ impl Prepared {
}

// margin on either side of the scroll bar
let inner_margin = animation_t * ui.spacing().scroll_bar_inner_margin;
let outer_margin = animation_t * ui.spacing().scroll_bar_outer_margin;
let inner_margin = animation_t * ui.spacing().scroll.bar_inner_margin;
let outer_margin = animation_t * ui.spacing().scroll.bar_outer_margin;
let mut min_cross = inner_rect.max[1 - d] + inner_margin; // left of vertical scroll (d == 1)
let mut max_cross = outer_rect.max[1 - d] - outer_margin; // right of vertical scroll (d == 1)
let min_main = inner_rect.min[d]; // top of vertical scroll (d == 1)
Expand Down Expand Up @@ -855,7 +855,7 @@ impl Prepared {
),
)
};
let min_handle_size = ui.spacing().scroll_handle_min_length;
let min_handle_size = ui.spacing().scroll.handle_min_length;
if handle_rect.size()[d] < min_handle_size {
handle_rect = Rect::from_center_size(
handle_rect.center(),
Expand Down Expand Up @@ -920,7 +920,7 @@ impl Prepared {

/// Width of a vertical scrollbar, or height of a horizontal scroll bar
fn max_scroll_bar_width_with_margin(ui: &Ui) -> f32 {
ui.spacing().scroll_bar_inner_margin
+ ui.spacing().scroll_bar_width
+ ui.spacing().scroll_bar_outer_margin
ui.spacing().scroll.bar_inner_margin
+ ui.spacing().scroll.bar_width
+ ui.spacing().scroll.bar_outer_margin
}
104 changes: 69 additions & 35 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#![allow(clippy::if_same_then_else)]

use std::collections::BTreeMap;

use epaint::{Rounding, Shadow, Stroke};

use crate::{
ecolor::*, emath::*, ComboBox, CursorIcon, FontFamily, FontId, Response, RichText, WidgetText,
};
use epaint::{Rounding, Shadow, Stroke};
use std::collections::BTreeMap;

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -303,16 +305,8 @@ pub struct Spacing {
/// Height of a combo-box before showing scroll bars.
pub combo_height: f32,

pub scroll_bar_width: f32,

/// Make sure the scroll handle is at least this big
pub scroll_handle_min_length: f32,

/// Margin between contents and scroll bar.
pub scroll_bar_inner_margin: f32,

/// Margin between scroll bar and the outer container (e.g. right of a vertical scroll bar).
pub scroll_bar_outer_margin: f32,
/// Controls the spacing of a [`crate::ScrollArea`].
pub scroll: ScrollSpacing,
}

impl Spacing {
Expand All @@ -333,6 +327,64 @@ impl Spacing {

// ----------------------------------------------------------------------------

/// Controls the spacing of a [`crate::ScrollArea`].
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct ScrollSpacing {
pub bar_width: f32,

/// Make sure the scroll handle is at least this big
pub handle_min_length: f32,

/// Margin between contents and scroll bar.
pub bar_inner_margin: f32,

/// Margin between scroll bar and the outer container (e.g. right of a vertical scroll bar).
pub bar_outer_margin: f32,
}

impl Default for ScrollSpacing {
fn default() -> Self {
Self {
bar_width: 8.0,
handle_min_length: 12.0,
bar_inner_margin: 4.0,
bar_outer_margin: 0.0,
}
}
}

impl ScrollSpacing {
pub fn ui(&mut self, ui: &mut Ui) {
let Self {
bar_width,
handle_min_length,
bar_inner_margin,
bar_outer_margin,
} = self;

ui.horizontal(|ui| {
ui.add(DragValue::new(bar_width).clamp_range(0.0..=32.0));
ui.label("Bar width");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(handle_min_length).clamp_range(0.0..=32.0));
ui.label("Minimum handle length");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(bar_inner_margin).clamp_range(0.0..=32.0));
ui.label("Inner margin");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(bar_outer_margin).clamp_range(0.0..=32.0));
ui.label("Outer margin");
});
}
}

// ----------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Margin {
Expand Down Expand Up @@ -807,10 +859,7 @@ impl Default for Spacing {
icon_spacing: 4.0,
tooltip_width: 600.0,
combo_height: 200.0,
scroll_bar_width: 8.0,
scroll_handle_min_length: 12.0,
scroll_bar_inner_margin: 4.0,
scroll_bar_outer_margin: 0.0,
scroll: Default::default(),
indent_ends_with_horizontal_line: false,
}
}
Expand Down Expand Up @@ -1146,10 +1195,7 @@ impl Spacing {
tooltip_width,
indent_ends_with_horizontal_line,
combo_height,
scroll_bar_width,
scroll_handle_min_length,
scroll_bar_inner_margin,
scroll_bar_outer_margin,
scroll,
} = self;

ui.add(slider_vec2(item_spacing, 0.0..=20.0, "Item spacing"));
Expand All @@ -1176,21 +1222,9 @@ impl Spacing {
ui.add(DragValue::new(text_edit_width).clamp_range(0.0..=1000.0));
ui.label("TextEdit width");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(scroll_bar_width).clamp_range(0.0..=32.0));
ui.label("Scroll-bar width");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(scroll_handle_min_length).clamp_range(0.0..=32.0));
ui.label("Scroll-bar handle min length");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(scroll_bar_inner_margin).clamp_range(0.0..=32.0));
ui.label("Scroll-bar inner margin");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(scroll_bar_outer_margin).clamp_range(0.0..=32.0));
ui.label("Scroll-bar outer margin");

ui.collapsing("Scroll Area", |ui| {
scroll.ui(ui);
});

ui.horizontal(|ui| {
Expand Down
6 changes: 3 additions & 3 deletions crates/egui_extras/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ impl<'a> TableBuilder<'a> {
fn available_width(&self) -> f32 {
self.ui.available_rect_before_wrap().width()
- if self.scroll_options.vscroll {
self.ui.spacing().scroll_bar_inner_margin
+ self.ui.spacing().scroll_bar_width
+ self.ui.spacing().scroll_bar_outer_margin
self.ui.spacing().scroll.bar_inner_margin
+ self.ui.spacing().scroll.bar_width
+ self.ui.spacing().scroll.bar_outer_margin
} else {
0.0
}
Expand Down

0 comments on commit 954376f

Please sign in to comment.