diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 1cacef47808..e50041119b8 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -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))] @@ -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, } } } @@ -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, @@ -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 { @@ -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) -> 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(), @@ -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, } } @@ -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) -> Self { + self.scroll_enabled = scroll_enabled.into(); self } @@ -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) -> Self { + self.auto_shrink = auto_shrink.into(); self } @@ -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, @@ -437,7 +437,7 @@ struct Prepared { viewport: Rect, scrolling_enabled: bool, - stick_to_end: [bool; 2], + stick_to_end: Vec2b, } impl ScrollArea { @@ -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, }; @@ -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); @@ -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, }; @@ -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; diff --git a/crates/egui/src/containers/window.rs b/crates/egui/src/containers/window.rs index af72ff98b9c..14e878ad969 100644 --- a/crates/egui/src/containers/window.rs +++ b/crates/egui/src/containers/window.rs @@ -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) -> Self { self.scroll = self.scroll.scroll2(scroll); self } diff --git a/crates/egui_demo_app/src/apps/custom3d_glow.rs b/crates/egui_demo_app/src/apps/custom3d_glow.rs index ff545bb61d4..3175cf4faab 100644 --- a/crates/egui_demo_app/src/apps/custom3d_glow.rs +++ b/crates/egui_demo_app/src/apps/custom3d_glow.rs @@ -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; diff --git a/crates/egui_demo_app/src/apps/custom3d_wgpu.rs b/crates/egui_demo_app/src/apps/custom3d_wgpu.rs index 29bbfaafe60..1bac26ed31d 100644 --- a/crates/egui_demo_app/src/apps/custom3d_wgpu.rs +++ b/crates/egui_demo_app/src/apps/custom3d_wgpu.rs @@ -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; diff --git a/crates/egui_demo_app/src/apps/http_app.rs b/crates/egui_demo_app/src/apps/http_app.rs index 90be24dd280..a118a90fcb6 100644 --- a/crates/egui_demo_app/src/apps/http_app.rs +++ b/crates/egui_demo_app/src/apps/http_app.rs @@ -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) diff --git a/crates/egui_demo_app/src/apps/image_viewer.rs b/crates/egui_demo_app/src/apps/image_viewer.rs index 6bc6cc05bea..9754972bdd2 100644 --- a/crates/egui_demo_app/src/apps/image_viewer.rs +++ b/crates/egui_demo_app/src/apps/image_viewer.rs @@ -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); diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index e5f45887439..fe32733ad66 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -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); }); }); diff --git a/crates/egui_demo_lib/src/demo/context_menu.rs b/crates/egui_demo_lib/src/demo/context_menu.rs index 96375d72e07..ffcc73379d8 100644 --- a/crates/egui_demo_lib/src/demo/context_menu.rs +++ b/crates/egui_demo_lib/src/demo/context_menu.rs @@ -1,3 +1,5 @@ +use egui::Vec2b; + #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] enum Plot { @@ -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, @@ -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, diff --git a/crates/egui_demo_lib/src/demo/scrolling.rs b/crates/egui_demo_lib/src/demo/scrolling.rs index ddcf95550a0..30855f77f45 100644 --- a/crates/egui_demo_lib/src/demo/scrolling.rs +++ b/crates/egui_demo_lib/src/demo/scrolling.rs @@ -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( @@ -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, @@ -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); @@ -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); } diff --git a/crates/egui_demo_lib/src/demo/text_layout.rs b/crates/egui_demo_lib/src/demo/text_layout.rs index f2512ae8c18..fa02a717572 100644 --- a/crates/egui_demo_lib/src/demo/text_layout.rs +++ b/crates/egui_demo_lib/src/demo/text_layout.rs @@ -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) diff --git a/crates/egui_demo_lib/src/demo/window_options.rs b/crates/egui_demo_lib/src/demo/window_options.rs index a69f6cf7929..228047263e1 100644 --- a/crates/egui_demo_lib/src/demo/window_options.rs +++ b/crates/egui_demo_lib/src/demo/window_options.rs @@ -1,3 +1,5 @@ +use egui::Vec2b; + #[derive(Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct WindowOptions { @@ -7,7 +9,7 @@ pub struct WindowOptions { collapsible: bool, resizable: bool, constrain: bool, - scroll2: [bool; 2], + scroll2: Vec2b, disabled_time: f64, anchored: bool, @@ -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, diff --git a/crates/egui_extras/src/table.rs b/crates/egui_extras/src/table.rs index 901a7a5aa90..1aa23eec7b2 100644 --- a/crates/egui_extras/src/table.rs +++ b/crates/egui_extras/src/table.rs @@ -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}, @@ -165,7 +165,7 @@ struct TableScrollOptions { scroll_offset_y: Option, min_scrolled_height: f32, max_scroll_height: f32, - auto_shrink: [bool; 2], + auto_shrink: Vec2b, } impl Default for TableScrollOptions { @@ -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, } } } @@ -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) -> Self { + self.scroll_options.auto_shrink = auto_shrink.into(); self } @@ -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) diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index e21f7122429..e276f517771 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -496,6 +496,7 @@ impl Plot { } /// Whether or not to show the background [`Rect`]. + /// /// Can be useful to disable if the plot is overlaid over existing content. /// Default: `true`. pub fn show_background(mut self, show: bool) -> Self { @@ -505,7 +506,7 @@ impl Plot { /// Show axis labels and grid tick values on the side of the plot. /// - /// Default: `[true; 2]`. + /// Default: `true`. pub fn show_axes(mut self, show: impl Into) -> Self { self.show_axes = show.into(); self @@ -513,7 +514,7 @@ impl Plot { /// Show a grid overlay on the plot. /// - /// Default: `[true; 2]`. + /// Default: `true`. pub fn show_grid(mut self, show: impl Into) -> Self { self.show_grid = show.into(); self diff --git a/crates/emath/src/vec2b.rs b/crates/emath/src/vec2b.rs index 84417f8458a..e422c9ae524 100644 --- a/crates/emath/src/vec2b.rs +++ b/crates/emath/src/vec2b.rs @@ -1,12 +1,15 @@ /// Two bools, one for each axis (X and Y). -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Vec2b { pub x: bool, pub y: bool, } impl Vec2b { + pub const FALSE: Self = Self { x: false, y: false }; + pub const TRUE: Self = Self { x: true, y: true }; + #[inline] pub fn new(x: bool, y: bool) -> Self { Self { x, y } @@ -31,3 +34,27 @@ impl From<[bool; 2]> for Vec2b { Vec2b { x, y } } } + +impl std::ops::Index for Vec2b { + type Output = bool; + + #[inline(always)] + fn index(&self, index: usize) -> &bool { + match index { + 0 => &self.x, + 1 => &self.y, + _ => panic!("Vec2b index out of bounds: {index}"), + } + } +} + +impl std::ops::IndexMut for Vec2b { + #[inline(always)] + fn index_mut(&mut self, index: usize) -> &mut bool { + match index { + 0 => &mut self.x, + 1 => &mut self.y, + _ => panic!("Vec2b index out of bounds: {index}"), + } + } +} diff --git a/examples/images/src/main.rs b/examples/images/src/main.rs index d942a21c98f..be0364e21ed 100644 --- a/examples/images/src/main.rs +++ b/examples/images/src/main.rs @@ -25,7 +25,7 @@ struct MyApp {} impl eframe::App for MyApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { egui::CentralPanel::default().show(ctx, |ui| { - egui::ScrollArea::new([true, true]).show(ui, |ui| { + egui::ScrollArea::both().show(ui, |ui| { ui.image(egui::include_image!("ferris.svg")); ui.add( diff --git a/examples/keyboard_events/src/main.rs b/examples/keyboard_events/src/main.rs index a51bf19086c..581ae11eb74 100644 --- a/examples/keyboard_events/src/main.rs +++ b/examples/keyboard_events/src/main.rs @@ -26,7 +26,7 @@ impl eframe::App for Content { self.text.clear(); } ScrollArea::vertical() - .auto_shrink([false; 2]) + .auto_shrink(false) .stick_to_bottom(true) .show(ui, |ui| { ui.label(&self.text);