Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

Keep message jumps from snapping back to the bottom (#66)

shiahod824a03a9abe
diff --git a/apps/rustcord/src/ui/chat.rs b/apps/rustcord/src/ui/chat.rs
index 6fa6c34..cba00f3 100644
--- a/apps/rustcord/src/ui/chat.rs
+++ b/apps/rustcord/src/ui/chat.rs
@@ -94,6 +94,14 @@ fn jump_id(ch: Id) -> egui::Id {
egui::Id::new(("jump_to", ch.0))
}
+fn message_scroll_area(ch: Id, follow_bottom: bool) -> ScrollArea {
+ ScrollArea::vertical()
+ .id_salt(("messages", ch.0))
+ .stick_to_bottom(follow_bottom)
+ .animated(follow_bottom)
+ .auto_shrink([false, false])
+}
+
/// Body text size (Discord uses 16px).
/// The chat text size (Discord's 16 px unless changed in Accessibility).
fn text_px() -> f32 {
@@ -1670,10 +1678,7 @@ impl RustCord {
// How far older messages added above moved what you were reading:
// put straight back into the scroll position (see below).
let mut keep_place = 0.0f32;
- let scroll = ScrollArea::vertical()
- .id_salt(("messages", ch.0))
- .stick_to_bottom(!restore_ready && !jump_pending)
- .auto_shrink([false, false])
+ let scroll = message_scroll_area(ch, !restore_ready && !jump_pending)
.show_viewport(ui, |ui, viewport| {
ui.set_style(list_style.clone());
ui.set_width(ui.available_width());
@@ -4682,3 +4687,39 @@ impl RustCord {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn a_jump_from_the_bottom_keeps_the_target_in_view() {
+ let ctx = egui::Context::default();
+ let mut offset = 0.0;
+ for frame in 0..6 {
+ let input = egui::RawInput {
+ screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(500.0, 300.0))),
+ time: Some(frame as f64 / 60.0),
+ ..Default::default()
+ };
+ let jump = frame == 3;
+ let _ = ctx.run(input, |ctx| {
+ CentralPanel::default().show(ctx, |ui| {
+ let list = message_scroll_area(Id(1), !jump).show(ui, |ui| {
+ ui.add_space(300.0);
+ if jump {
+ let row = Rect::from_min_size(ui.cursor().min, vec2(1.0, 30.0));
+ ui.scroll_to_rect(row, Some(Align::Center));
+ }
+ ui.add_space(2100.0);
+ });
+ offset = list.state.offset.y;
+ });
+ });
+ if frame == 2 {
+ assert!(offset > 2000.0);
+ }
+ }
+ assert!((60.0..300.0).contains(&offset), "offset after jumping: {offset}");
+ }
+}