Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual scrolling #81

Merged
merged 18 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ impl Prepared {

let content_size = content_ui.min_size();

if let Some(offset_y) = content_ui.ctx().frame_state().scroll_offset_y() {
state.offset.y = offset_y;
}

let inner_rect = Rect::from_min_size(
inner_rect.min,
vec2(
Expand Down
11 changes: 11 additions & 0 deletions egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) struct FrameState {
/// How much space is used by panels.
used_by_panels: Rect,
// TODO: move some things from `Memory` to here
scroll_offset: Option<f32>
}

impl Default for FrameState {
Expand All @@ -47,6 +48,7 @@ impl Default for FrameState {
available_rect: Rect::invalid(),
unused_rect: Rect::invalid(),
used_by_panels: Rect::invalid(),
scroll_offset: None
}
}
}
Expand All @@ -56,6 +58,7 @@ impl FrameState {
self.available_rect = input.screen_rect();
self.unused_rect = input.screen_rect();
self.used_by_panels = Rect::nothing();
self.scroll_offset = None;
}

/// How much space is still available after panels has been added.
Expand Down Expand Up @@ -97,6 +100,14 @@ impl FrameState {
self.unused_rect = Rect::nothing(); // Nothing left unused after this
self.used_by_panels = self.used_by_panels.union(panel_rect);
}

pub(crate) fn set_scroll_offset_y(&mut self, offset: f32) {
self.scroll_offset = Some(offset);
}

pub(crate) fn scroll_offset_y(&self) -> Option<f32> {
self.scroll_offset
}
}

// ----------------------------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions egui/src/demos/demo_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ pub struct DemoWindow {
layout: LayoutDemo,
tree: Tree,
box_painting: BoxPainting,
ratio: f32,
}

impl Default for DemoWindow {
fn default() -> DemoWindow {
DemoWindow {
num_columns: 2,
ratio: 0.0,

widgets: Default::default(),
colors: Default::default(),
Expand Down Expand Up @@ -69,7 +71,28 @@ impl DemoWindow {
.default_open(false)
.show(ui, |ui| {
ScrollArea::from_max_height(200.0).show(ui, |ui| {
let middle = ui.button("Go middle").clicked;
let bottom = ui.button("Go bottom").clicked;
let not_working = ui.button("Go middle (not working)").clicked;
ui.add(Slider::f32(&mut self.ratio, 0.0..=1.0).text("ratio"));
ui.label(LOREM_IPSUM_LONG);
ui.label(LOREM_IPSUM_LONG);
if middle {
ui.scroll_to_here(self.ratio);
}

// FIXME: inside the children Ui, scroll_to_here does not work.
ui.horizontal(|ui| {
if not_working {
ui.scroll_to_here(0.5);
}
});
ui.label("Middle");
ui.label(LOREM_IPSUM_LONG);
ui.label(LOREM_IPSUM_LONG);
if bottom {
ui.scroll_to_here(1.0);
}
});
lucaspoffo marked this conversation as resolved.
Show resolved Hide resolved
});

Expand Down
8 changes: 8 additions & 0 deletions egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,14 @@ impl Ui {
let clip_rect = self.clip_rect().intersect(rect); // Make sure we don't paint out of bounds
Painter::new(self.ctx().clone(), self.layer_id(), clip_rect)
}

pub fn scroll_to_here(&mut self, center_ratio: f32) {
lucaspoffo marked this conversation as resolved.
Show resolved Hide resolved
// This is the imgui aproach to set the scroll relative to the position
// 0.0: top of widget / 0.5: midle of widget / 1.0: bottom of widget
assert!(center_ratio >= 0.0 && center_ratio <= 1.0);
let offset = self.min_rect().height() - self.clip_rect().height() * center_ratio;
self.ctx().frame_state().set_scroll_offset_y(offset);
lucaspoffo marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// # Adding widgets
Expand Down