ScrollArea: add methods to control horizontal scroll offset

Closes https://github.com/emilk/egui/issues/864
This commit is contained in:
Emil Ernerfeldt 2021-11-01 19:43:27 +01:00
parent b9368aba13
commit 9378cd5c6e
2 changed files with 32 additions and 10 deletions

View file

@ -67,7 +67,8 @@ pub struct ScrollArea {
max_size: Vec2, max_size: Vec2,
always_show_scroll: bool, always_show_scroll: bool,
id_source: Option<Id>, id_source: Option<Id>,
offset: Option<Vec2>, offset_x: Option<f32>,
offset_y: Option<f32>,
/// If false, we ignore scroll events. /// If false, we ignore scroll events.
scrolling_enabled: bool, scrolling_enabled: bool,
@ -108,7 +109,8 @@ impl ScrollArea {
max_size: Vec2::INFINITY, max_size: Vec2::INFINITY,
always_show_scroll: false, always_show_scroll: false,
id_source: None, id_source: None,
offset: None, offset_x: None,
offset_y: None,
scrolling_enabled: true, scrolling_enabled: true,
stick_to_end: [false; 2], stick_to_end: [false; 2],
} }
@ -147,12 +149,32 @@ impl ScrollArea {
self 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. /// 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) /// [`Response::scroll_to_me`](crate::Response::scroll_to_me)
pub fn scroll_offset(mut self, offset: f32) -> Self { pub fn vertical_scroll_offset(mut self, offset: f32) -> Self {
self.offset = Some(Vec2::new(0.0, offset)); 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 self
} }
@ -251,7 +273,8 @@ impl ScrollArea {
max_size, max_size,
always_show_scroll, always_show_scroll,
id_source, id_source,
offset, offset_x,
offset_y,
scrolling_enabled, scrolling_enabled,
stick_to_end, stick_to_end,
} = self; } = self;
@ -262,9 +285,8 @@ impl ScrollArea {
let id = ui.make_persistent_id(id_source); let id = ui.make_persistent_id(id_source);
let mut state = State::load(&ctx, id).unwrap_or_default(); let mut state = State::load(&ctx, id).unwrap_or_default();
if let Some(offset) = offset { state.offset.x = offset_x.unwrap_or(state.offset.x);
state.offset = offset; state.offset.y = offset_y.unwrap_or(state.offset.y);
}
let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui); let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui);

View file

@ -206,7 +206,7 @@ impl super::View for ScrollTo {
.max_height(200.0) .max_height(200.0)
.auto_shrink([false; 2]); .auto_shrink([false; 2]);
if go_to_scroll_offset { if go_to_scroll_offset {
scroll_area = scroll_area.scroll_offset(self.offset); scroll_area = scroll_area.vertical_scroll_offset(self.offset);
} }
ui.separator(); ui.separator();