Skip to content

Commit

Permalink
Use Vec2b everywhere it makes sense to
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 11, 2023
1 parent 7c68c1f commit dbddd9b
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 62 deletions.
64 changes: 31 additions & 33 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub struct State {
pub offset: Vec2,

/// Were the scroll bars visible last frame?
show_scroll: [bool; 2],
show_scroll: Vec2b,

/// The content were to large to fit large frame.
content_is_too_large: [bool; 2],
content_is_too_large: Vec2b,

/// Did the user interact (hover or drag) the scroll bars last frame?
scroll_bar_interaction: [bool; 2],
scroll_bar_interaction: Vec2b,

/// Momentum, used for kinetic scrolling
#[cfg_attr(feature = "serde", serde(skip))]
Expand All @@ -28,19 +28,19 @@ pub struct State {
/// Is the scroll sticky. This is true while scroll handle is in the end position
/// and remains that way until the user moves the scroll_handle. Once unstuck (false)
/// it remains false until the scroll touches the end position, which reenables stickiness.
scroll_stuck_to_end: [bool; 2],
scroll_stuck_to_end: Vec2b,
}

impl Default for State {
fn default() -> Self {
Self {
offset: Vec2::ZERO,
show_scroll: [false; 2],
content_is_too_large: [false; 2],
scroll_bar_interaction: [false; 2],
show_scroll: Vec2b::FALSE,
content_is_too_large: Vec2b::FALSE,
scroll_bar_interaction: Vec2b::FALSE,
vel: Vec2::ZERO,
scroll_start_offset_from_top_left: [None; 2],
scroll_stuck_to_end: [true; 2],
scroll_stuck_to_end: Vec2b::TRUE,
}
}
}
Expand Down Expand Up @@ -147,9 +147,9 @@ impl ScrollBarVisibility {
#[must_use = "You should call .show()"]
pub struct ScrollArea {
/// Do we have horizontal/vertical scrolling enabled?
scroll_enabled: [bool; 2],
scroll_enabled: Vec2b,

auto_shrink: [bool; 2],
auto_shrink: Vec2b,
max_size: Vec2,
min_scrolled_size: Vec2,
scroll_bar_visibility: ScrollBarVisibility,
Expand All @@ -164,7 +164,7 @@ pub struct ScrollArea {
/// If true for vertical or horizontal the scroll wheel will stick to the
/// end position until user manually changes position. It will become true
/// again once scroll handle makes contact with end.
stick_to_end: [bool; 2],
stick_to_end: Vec2b,
}

impl ScrollArea {
Expand Down Expand Up @@ -195,10 +195,10 @@ impl ScrollArea {

/// Create a scroll area where you decide which axis has scrolling enabled.
/// For instance, `ScrollArea::new([true, false])` enables horizontal scrolling.
pub fn new(scroll_enabled: [bool; 2]) -> Self {
pub fn new(scroll_enabled: impl Into<Vec2b>) -> Self {
Self {
scroll_enabled,
auto_shrink: [true; 2],
scroll_enabled: scroll_enabled.into(),
auto_shrink: Vec2b::TRUE,
max_size: Vec2::INFINITY,
min_scrolled_size: Vec2::splat(64.0),
scroll_bar_visibility: Default::default(),
Expand All @@ -207,7 +207,7 @@ impl ScrollArea {
offset_y: None,
scrolling_enabled: true,
drag_to_scroll: true,
stick_to_end: [false; 2],
stick_to_end: Vec2b::FALSE,
}
}

Expand Down Expand Up @@ -327,8 +327,8 @@ impl ScrollArea {

/// Turn on/off scrolling on the horizontal/vertical axes.
#[inline]
pub fn scroll2(mut self, scroll_enabled: [bool; 2]) -> Self {
self.scroll_enabled = scroll_enabled;
pub fn scroll2(mut self, scroll_enabled: impl Into<Vec2b>) -> Self {
self.scroll_enabled = scroll_enabled.into();
self
}

Expand Down Expand Up @@ -365,10 +365,10 @@ impl ScrollArea {
/// * If `true`, egui will add blank space outside the scroll area.
/// * If `false`, egui will add blank space inside the scroll area.
///
/// Default: `[true; 2]`.
/// Default: `true`.
#[inline]
pub fn auto_shrink(mut self, auto_shrink: [bool; 2]) -> Self {
self.auto_shrink = auto_shrink;
pub fn auto_shrink(mut self, auto_shrink: impl Into<Vec2b>) -> Self {
self.auto_shrink = auto_shrink.into();
self
}

Expand Down Expand Up @@ -406,10 +406,10 @@ struct Prepared {
id: Id,
state: State,

auto_shrink: [bool; 2],
auto_shrink: Vec2b,

/// Does this `ScrollArea` have horizontal/vertical scrolling enabled?
scroll_enabled: [bool; 2],
scroll_enabled: Vec2b,

/// Smoothly interpolated boolean of whether or not to show the scroll bars.
show_bars_factor: Vec2,
Expand Down Expand Up @@ -437,7 +437,7 @@ struct Prepared {
viewport: Rect,

scrolling_enabled: bool,
stick_to_end: [bool; 2],
stick_to_end: Vec2b,
}

impl ScrollArea {
Expand Down Expand Up @@ -470,8 +470,8 @@ impl ScrollArea {
state.offset.x = offset_x.unwrap_or(state.offset.x);
state.offset.y = offset_y.unwrap_or(state.offset.y);

let show_bars: [bool; 2] = match scroll_bar_visibility {
ScrollBarVisibility::AlwaysHidden => [false; 2],
let show_bars: Vec2b = match scroll_bar_visibility {
ScrollBarVisibility::AlwaysHidden => Vec2b::FALSE,
ScrollBarVisibility::VisibleWhenNeeded => state.show_scroll,
ScrollBarVisibility::AlwaysVisible => scroll_enabled,
};
Expand Down Expand Up @@ -769,10 +769,10 @@ impl Prepared {

let outer_rect = Rect::from_min_size(inner_rect.min, inner_rect.size() + current_bar_use);

let content_is_too_large = [
let content_is_too_large = Vec2b::new(
scroll_enabled[0] && inner_rect.width() < content_size.x,
scroll_enabled[1] && inner_rect.height() < content_size.y,
];
);

let max_offset = content_size - inner_rect.size();
let is_hovering_outer_rect = ui.rect_contains_pointer(outer_rect);
Expand All @@ -795,10 +795,8 @@ impl Prepared {
}

let show_scroll_this_frame = match scroll_bar_visibility {
ScrollBarVisibility::AlwaysHidden => [false, false],
ScrollBarVisibility::VisibleWhenNeeded => {
[content_is_too_large[0], content_is_too_large[1]]
}
ScrollBarVisibility::AlwaysHidden => Vec2b::FALSE,
ScrollBarVisibility::VisibleWhenNeeded => content_is_too_large,
ScrollBarVisibility::AlwaysVisible => scroll_enabled,
};

Expand Down Expand Up @@ -1065,12 +1063,12 @@ impl Prepared {
// Only has an effect if stick_to_end is enabled but we save in
// state anyway so that entering sticky mode at an arbitrary time
// has appropriate effect.
state.scroll_stuck_to_end = [
state.scroll_stuck_to_end = Vec2b::new(
(state.offset[0] == available_offset[0])
|| (self.stick_to_end[0] && available_offset[0] < 0.0),
(state.offset[1] == available_offset[1])
|| (self.stick_to_end[1] && available_offset[1] < 0.0),
];
);

state.show_scroll = show_scroll_this_frame;
state.content_is_too_large = content_is_too_large;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl<'open> Window<'open> {
}

/// Enable/disable horizontal/vertical scrolling. `false` by default.
pub fn scroll2(mut self, scroll: [bool; 2]) -> Self {
pub fn scroll2(mut self, scroll: impl Into<Vec2b>) -> Self {
self.scroll = self.scroll.scroll2(scroll);
self
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/apps/custom3d_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl eframe::App for Custom3d {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::both()
.auto_shrink([false; 2])
.auto_shrink(false)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/apps/custom3d_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl eframe::App for Custom3d {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::both()
.auto_shrink([false; 2])
.auto_shrink(false)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/apps/http_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn ui_resource(ui: &mut egui::Ui, resource: &Resource) {
ui.separator();

egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.auto_shrink(false)
.show(ui, |ui| {
egui::CollapsingHeader::new("Response headers")
.default_open(false)
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/apps/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl eframe::App for ImageViewer {
});

egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::new([true, true]).show(ui, |ui| {
egui::ScrollArea::both().show(ui, |ui| {
let mut image = egui::Image::from_uri(&self.current_uri);
image = image.uv(self.image_options.uv);
image = image.bg_fill(self.image_options.bg_fill);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl eframe::App for ColorTestApp {
);
ui.separator();
}
egui::ScrollArea::both().auto_shrink([false; 2]).show(ui, |ui| {
egui::ScrollArea::both().auto_shrink(false).show(ui, |ui| {
self.color_test.ui(ui);
});
});
Expand Down
6 changes: 4 additions & 2 deletions crates/egui_demo_lib/src/demo/context_menu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use egui::Vec2b;

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum Plot {
Expand All @@ -19,7 +21,7 @@ fn sigmoid(x: f64) -> f64 {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ContextMenus {
plot: Plot,
show_axes: [bool; 2],
show_axes: Vec2b,
allow_drag: bool,
allow_zoom: bool,
allow_scroll: bool,
Expand All @@ -33,7 +35,7 @@ impl Default for ContextMenus {
fn default() -> Self {
Self {
plot: Plot::Sin,
show_axes: [true, true],
show_axes: Vec2b::TRUE,
allow_drag: true,
allow_zoom: true,
allow_scroll: true,
Expand Down
10 changes: 4 additions & 6 deletions crates/egui_demo_lib/src/demo/scrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl ScrollAppearance {
ui.separator();

ScrollArea::vertical()
.auto_shrink([false; 2])
.auto_shrink(false)
.scroll_bar_visibility(*visibility)
.show(ui, |ui| {
ui.with_layout(
Expand All @@ -170,7 +170,7 @@ fn huge_content_lines(ui: &mut egui::Ui) {
let text_style = TextStyle::Body;
let row_height = ui.text_style_height(&text_style);
let num_rows = 10_000;
ScrollArea::vertical().auto_shrink([false; 2]).show_rows(
ScrollArea::vertical().auto_shrink(false).show_rows(
ui,
row_height,
num_rows,
Expand All @@ -193,7 +193,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
let num_rows = 10_000;

ScrollArea::vertical()
.auto_shrink([false; 2])
.auto_shrink(false)
.show_viewport(ui, |ui, viewport| {
ui.set_height(row_height * num_rows as f32);

Expand Down Expand Up @@ -292,9 +292,7 @@ impl super::View for ScrollTo {
scroll_bottom |= ui.button("Scroll to bottom").clicked();
});

let mut scroll_area = ScrollArea::vertical()
.max_height(200.0)
.auto_shrink([false; 2]);
let mut scroll_area = ScrollArea::vertical().max_height(200.0).auto_shrink(false);
if go_to_scroll_offset {
scroll_area = scroll_area.vertical_scroll_offset(self.offset);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl super::View for TextLayoutDemo {
};

egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.auto_shrink(false)
.show(ui, |ui| {
let extra_letter_spacing = points_per_pixel * *extra_letter_spacing_pixels as f32;
let line_height = (*line_height_pixels != 0)
Expand Down
6 changes: 4 additions & 2 deletions crates/egui_demo_lib/src/demo/window_options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use egui::Vec2b;

#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WindowOptions {
Expand All @@ -7,7 +9,7 @@ pub struct WindowOptions {
collapsible: bool,
resizable: bool,
constrain: bool,
scroll2: [bool; 2],
scroll2: Vec2b,
disabled_time: f64,

anchored: bool,
Expand All @@ -24,7 +26,7 @@ impl Default for WindowOptions {
collapsible: true,
resizable: true,
constrain: true,
scroll2: [true; 2],
scroll2: Vec2b::TRUE,
disabled_time: f64::NEG_INFINITY,
anchored: false,
anchor: egui::Align2::RIGHT_TOP,
Expand Down
14 changes: 7 additions & 7 deletions crates/egui_extras/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! | fixed size | all available space/minimum | 30% of available width | fixed size |
//! Takes all available height, so if you want something below the table, put it in a strip.
use egui::{Align, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui, Vec2};
use egui::{Align, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui, Vec2, Vec2b};

use crate::{
layout::{CellDirection, CellSize},
Expand Down Expand Up @@ -165,7 +165,7 @@ struct TableScrollOptions {
scroll_offset_y: Option<f32>,
min_scrolled_height: f32,
max_scroll_height: f32,
auto_shrink: [bool; 2],
auto_shrink: Vec2b,
}

impl Default for TableScrollOptions {
Expand All @@ -178,7 +178,7 @@ impl Default for TableScrollOptions {
scroll_offset_y: None,
min_scrolled_height: 200.0,
max_scroll_height: 800.0,
auto_shrink: [true; 2],
auto_shrink: Vec2b::TRUE,
}
}
}
Expand Down Expand Up @@ -335,11 +335,11 @@ impl<'a> TableBuilder<'a> {
/// * If true, add blank space outside the table, keeping the table small.
/// * If false, add blank space inside the table, expanding the table to fit the containing ui.
///
/// Default: `[true; 2]`.
/// Default: `true`.
///
/// See [`ScrollArea::auto_shrink`] for more.
pub fn auto_shrink(mut self, auto_shrink: [bool; 2]) -> Self {
self.scroll_options.auto_shrink = auto_shrink;
pub fn auto_shrink(mut self, auto_shrink: impl Into<Vec2b>) -> Self {
self.scroll_options.auto_shrink = auto_shrink.into();
self
}

Expand Down Expand Up @@ -577,7 +577,7 @@ impl<'a> Table<'a> {
let avail_rect = ui.available_rect_before_wrap();

let mut scroll_area = ScrollArea::new([false, vscroll])
.auto_shrink([true; 2])
.auto_shrink(true)
.drag_to_scroll(drag_to_scroll)
.stick_to_bottom(stick_to_bottom)
.min_scrolled_height(min_scrolled_height)
Expand Down
Loading

0 comments on commit dbddd9b

Please sign in to comment.