-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9f93f30
Add scroll_offset to Context, add scroll_to_here() in Ui.
lucaspoffo cd11d6d
Update Scroll Window demo.
lucaspoffo cf1ae0b
Add scroll_target_center_ratio to Context
lucaspoffo 76d6eac
Add scroll_to_me method to Ui
lucaspoffo d9d1066
Update scroll window demo
lucaspoffo e9f1e9f
Add initial offset when creating ScrollArea.
lucaspoffo 2c3c6e5
Update Scroll demo
lucaspoffo 5816dbf
Consider clip_rect_margin when calculating current_scroll and scroll_…
lucaspoffo 5c346e0
Rename center_ratio to center_factor, add documentation to scroll met…
lucaspoffo 69277da
Merge branch 'master' into control-scroll-area-offset
lucaspoffo c581ac0
Update scroll documentation
lucaspoffo bc4556b
Formatting (RustFmt)
lucaspoffo 9e20b40
Formatting (RustFmt)
lucaspoffo 6e84836
Formatting (Clippy)
lucaspoffo ee2d76a
Use const for the columns title
lucaspoffo bb5148c
Clean up comments
lucaspoffo 0149258
Add suggestions
lucaspoffo 305d99f
Fix doc test
lucaspoffo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use crate::{color::*, demos::LOREM_IPSUM_LONG, *}; | ||
|
||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] | ||
#[cfg_attr(feature = "serde", serde(default))] | ||
pub struct Scrolls { | ||
track_item: usize, | ||
tracking: bool, | ||
offset: f32, | ||
} | ||
|
||
impl Default for Scrolls { | ||
fn default() -> Self { | ||
Self { | ||
track_item: 25, | ||
tracking: true, | ||
offset: 0.0, | ||
} | ||
} | ||
} | ||
|
||
impl Scrolls { | ||
pub fn ui(&mut self, ui: &mut Ui) { | ||
ScrollArea::from_max_height(200.0).show(ui, |ui| { | ||
ui.label(LOREM_IPSUM_LONG); | ||
ui.label(LOREM_IPSUM_LONG); | ||
}); | ||
|
||
ui.separator(); | ||
|
||
ui.horizontal(|ui| { | ||
ui.checkbox(&mut self.tracking, "Track") | ||
.on_hover_text("The scroll position will track the selected item"); | ||
ui.add(Slider::usize(&mut self.track_item, 1..=50).text("Track Item")); | ||
}); | ||
let (scroll_offset, _) = ui.horizontal(|ui| { | ||
let scroll_offset = ui.small_button("Scroll Offset").clicked; | ||
ui.add(DragValue::f32(&mut self.offset).speed(1.0).suffix("px")); | ||
scroll_offset | ||
}); | ||
|
||
let scroll_top = ui.button("Scroll to top").clicked; | ||
let scroll_bottom = ui.button("Scroll to bottom").clicked; | ||
if scroll_bottom || scroll_top { | ||
self.tracking = false; | ||
} | ||
|
||
const TITLES: [&str; 3] = ["Top", "Middle", "Bottom"]; | ||
const ALIGNS: [Align; 3] = [Align::Min, Align::Center, Align::Max]; | ||
ui.columns(3, |cols| { | ||
for (i, col) in cols.iter_mut().enumerate() { | ||
col.colored_label(WHITE, TITLES[i]); | ||
let mut scroll_area = ScrollArea::from_max_height(200.0).id_source(i); | ||
if scroll_offset { | ||
self.tracking = false; | ||
scroll_area = scroll_area.scroll_offset(self.offset); | ||
} | ||
|
||
let (current_scroll, max_scroll) = scroll_area.show(col, |ui| { | ||
if scroll_top { | ||
ui.scroll_to_cursor(Align::top()); | ||
} | ||
ui.vertical(|ui| { | ||
for item in 1..=50 { | ||
if self.tracking && item == self.track_item { | ||
let response = ui.colored_label(YELLOW, format!("Item {}", item)); | ||
response.scroll_to_me(ALIGNS[i]); | ||
} else { | ||
ui.label(format!("Item {}", item)); | ||
} | ||
} | ||
}); | ||
|
||
if scroll_bottom { | ||
ui.scroll_to_cursor(Align::bottom()); | ||
} | ||
|
||
let margin = ui.style().visuals.clip_rect_margin; | ||
( | ||
ui.clip_rect().top() - ui.min_rect().top() + margin, | ||
ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin, | ||
) | ||
}); | ||
col.colored_label(WHITE, format!("{:.0}/{:.0}", current_scroll, max_scroll)); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -584,6 +584,28 @@ impl Ui { | |
let painter = Painter::new(self.ctx().clone(), self.layer_id(), clip_rect); | ||
(response, painter) | ||
} | ||
|
||
/// Move the scroll to this cursor position with the specified alignment. | ||
/// | ||
/// ``` | ||
/// # use egui::Align; | ||
/// # let mut ui = &mut egui::Ui::__test(); | ||
/// egui::ScrollArea::auto_sized().show(ui, |ui| { | ||
/// let scroll_bottom = ui.button("Scroll to bottom.").clicked; | ||
/// for i in 0..1000 { | ||
/// ui.label(format!("Item {}", i)); | ||
/// } | ||
/// | ||
/// if scroll_bottom { | ||
/// ui.scroll_to_cursor(Align::bottom()); | ||
/// } | ||
/// }); | ||
/// ``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good example 👍 |
||
pub fn scroll_to_cursor(&mut self, align: Align) { | ||
let scroll_y = self.region.cursor.y; | ||
|
||
self.ctx().frame_state().scroll_target = Some((scroll_y, align)); | ||
} | ||
} | ||
|
||
/// # Adding widgets | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this is the best place to add this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the right place! Maybe could be called simply
to_factor