diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 0514fdfa..985a31ea 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -67,7 +67,8 @@ pub struct ScrollArea { max_size: Vec2, always_show_scroll: bool, id_source: Option, - offset: Option, + offset_x: Option, + offset_y: Option, /// If false, we ignore scroll events. scrolling_enabled: bool, @@ -108,7 +109,8 @@ impl ScrollArea { max_size: Vec2::INFINITY, always_show_scroll: false, id_source: None, - offset: None, + offset_x: None, + offset_y: None, scrolling_enabled: true, stick_to_end: [false; 2], } @@ -147,12 +149,32 @@ impl ScrollArea { self } + /// Set the horizontal and vertical scroll offset position. + /// + /// See also: [`Self::vertical_scroll_offset`], [`Self::horizontal_scroll_offset`], + /// [`Ui::scroll_to_cursor`](crate::ui::Ui::scroll_to_cursor) and + /// [`Response::scroll_to_me`](crate::Response::scroll_to_me) + pub fn scroll_offset(mut self, offset: Vec2) -> Self { + self.offset_x = Some(offset.x); + self.offset_y = Some(offset.y); + self + } + /// Set the vertical scroll offset position. /// - /// See also: [`Ui::scroll_to_cursor`](crate::ui::Ui::scroll_to_cursor) and + /// See also: [`Self::scroll_offset`], [`Ui::scroll_to_cursor`](crate::ui::Ui::scroll_to_cursor) and /// [`Response::scroll_to_me`](crate::Response::scroll_to_me) - pub fn scroll_offset(mut self, offset: f32) -> Self { - self.offset = Some(Vec2::new(0.0, offset)); + pub fn vertical_scroll_offset(mut self, offset: f32) -> Self { + self.offset_y = Some(offset); + self + } + + /// Set the horizontal scroll offset position. + /// + /// See also: [`Self::scroll_offset`], [`Ui::scroll_to_cursor`](crate::ui::Ui::scroll_to_cursor) and + /// [`Response::scroll_to_me`](crate::Response::scroll_to_me) + pub fn horizontal_scroll_offset(mut self, offset: f32) -> Self { + self.offset_x = Some(offset); self } @@ -251,7 +273,8 @@ impl ScrollArea { max_size, always_show_scroll, id_source, - offset, + offset_x, + offset_y, scrolling_enabled, stick_to_end, } = self; @@ -262,9 +285,8 @@ impl ScrollArea { let id = ui.make_persistent_id(id_source); let mut state = State::load(&ctx, id).unwrap_or_default(); - if let Some(offset) = offset { - state.offset = offset; - } + state.offset.x = offset_x.unwrap_or(state.offset.x); + state.offset.y = offset_y.unwrap_or(state.offset.y); let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui); diff --git a/egui_demo_lib/src/apps/demo/scrolling.rs b/egui_demo_lib/src/apps/demo/scrolling.rs index 11882caf..97d0a7fe 100644 --- a/egui_demo_lib/src/apps/demo/scrolling.rs +++ b/egui_demo_lib/src/apps/demo/scrolling.rs @@ -206,7 +206,7 @@ impl super::View for ScrollTo { .max_height(200.0) .auto_shrink([false; 2]); if go_to_scroll_offset { - scroll_area = scroll_area.scroll_offset(self.offset); + scroll_area = scroll_area.vertical_scroll_offset(self.offset); } ui.separator();