Chat: a 回到底部 button once you've scrolled far up

The desktop transcript only followed new messages while it was already
at the bottom; scrolled up through a long chat, the way back was the
wheel. Past 400 points above the newest message (the phone's
threshold), a round ↓ fades in over the bottom middle of the chat, just
above the composer. A press scrolls smoothly to the end and keeps
pulling for up to a second, so words streaming in meanwhile don't leave
it short; then stick-to-bottom takes over again and the button fades
out.

ANYBOT_SHOT_VIEW=longchat fills the chat with 60 messages, to check it
with ANYBOT_SHOT_SCROLL / ANYBOT_SHOT_CLICK.
This commit is contained in:
王性驊 2026-11-10 22:00:34 +08:00
parent 7edc05ba9b
commit 89dbb243dc
3 changed files with 94 additions and 1 deletions

View File

@ -195,6 +195,9 @@ pub struct App {
dirty: bool,
last_save: Instant,
stick_bottom: bool,
/// When 回到底部 was pressed: until the chat reaches its newest message (or a moment passes),
/// it keeps scrolling down, so words that land while it moves don't leave it short.
jump_bottom: Option<f64>,
/// Bot 的電腦 viewer, rebuild dialog and the computers page.
cu: computers_ui::ComputerUi,
vault: vault_ui::VaultUi,
@ -335,6 +338,7 @@ impl App {
dirty: false,
last_save: Instant::now(),
stick_bottom: true,
jump_bottom: None,
cu: Default::default(),
vault: Default::default(),
md: Default::default(),
@ -403,6 +407,16 @@ impl App {
// Optional view for checking the dialogs in a picture.
match std::env::var("ANYBOT_SHOT_VIEW").as_deref() {
Ok("chat") => app.state.selected = 1,
// A chat long enough to scroll far up (回到底部; with ANYBOT_SHOT_SCROLL).
Ok("longchat") => {
app.state.selected = 1;
if let Some(b) = app.state.bot_mut(1) {
for i in 1..=30 {
b.messages.push(Message::user(&format!("第 {i} 個問題:這段要怎麼改比較好?")));
b.messages.push(Message::assistant(&format!("第 {i} 個回答:先把重複的部分抽出來,再把名字取得清楚一點,最後補一個測試確認行為沒變。"), "demo"));
}
}
}
// 頁面: a tracking page in the chat (pagecard), and opened (page).
Ok(v @ ("pagecard" | "page")) => {
app.state.selected = 1;
@ -2166,7 +2180,8 @@ impl App {
let w = ui.available_width();
let col_w = (w - 48.0).min(TRANSCRIPT_MAX_WIDTH);
let pad = ((w - col_w) / 2.0).max(0.0);
egui::ScrollArea::vertical()
let jumping = self.jump_bottom.is_some();
let out = egui::ScrollArea::vertical()
.id_salt(("transcript", id))
.auto_shrink([false, false])
.max_height(avail)
@ -2190,9 +2205,15 @@ impl App {
self.working_row(ui, &bot);
self.approval_card(ui, ctx, &bot);
ui.add_space(16.0);
// Last in the content, so no code block's own scroll area takes it.
if jumping {
ui.scroll_to_cursor(Some(Align::BOTTOM));
}
});
});
});
let from_bottom = (out.content_size.y - out.inner_rect.height() - out.state.clamped_offset().y).max(0.0);
self.jump_bottom_button(ui, out.inner_rect, from_bottom);
let top = ui.cursor().top();
ui.horizontal(|ui| {
ui.add_space(pad);

View File

@ -20,6 +20,15 @@ const SHIMMER_SECS: f64 = 1.4;
const QUICK: [&str; 6] = ["❤️", "👍", "😂", "🎉", "🙏", "👀"];
const MORE: [&str; 24] = ["😊", "😍", "🥹", "😮", "😢", "😡", "🤔", "👏", "🙌", "💪", "🔥", "✨", "✅", "❌", "💯", "🚀", "☕", "🍰", "🌟", "💡", "📌", "👌", "🤝", "🫶"];
/// 回到底部 shows once the chat is scrolled this far above its newest message (the phone's too).
const JUMP_BOTTOM_AFTER: f32 = 400.0;
/// A press keeps pulling the chat down at most this long, so the person can always scroll again.
const JUMP_BOTTOM_SECS: f64 = 1.0;
const JUMP_BOTTOM_FADE: f32 = 0.15;
const JUMP_BOTTOM_SIZE: f32 = 36.0;
/// Between the button and the bottom of the chat (the top of the composer).
const JUMP_BOTTOM_GAP: f32 = 12.0;
/// The original's status phrases (renderer table vit).
const THINKING: [&str; 3] = ["正在仔細思考", "正在琢磨", "正在整理思緒"];
const WORKING: [&str; 5] = ["正在完成", "正在推進", "正在處理", "正在搞定", "正在一點一點推進"];
@ -538,6 +547,52 @@ impl App {
r.on_hover_text(t("Bot 的表情回應"));
}
}
/// 回到底部: a round ↓ over the bottom middle of the chat once it's scrolled well up. A press
/// scrolls smoothly to the newest message; from there the chat follows new words again.
pub(super) fn jump_bottom_button(&mut self, ui: &mut egui::Ui, view: egui::Rect, from_bottom: f32) {
let now = ui.input(|i| i.time);
self.jump_bottom = jump_still_on(self.jump_bottom, from_bottom, now);
if self.jump_bottom.is_some() {
ui.ctx().request_repaint();
}
let id = egui::Id::unique("jump-bottom");
let k = ui.ctx().animate_bool_with_time(id, jump_shows(from_bottom, self.jump_bottom.is_some()), JUMP_BOTTOM_FADE);
if k <= 0.0 {
return;
}
let p = self.pal;
// Fades in rising a few points, like the chat's rows.
let center = egui::pos2(view.center().x, view.bottom() - JUMP_BOTTOM_GAP - JUMP_BOTTOM_SIZE / 2.0 + (1.0 - k) * 6.0);
let rect = egui::Rect::from_center_size(center, vec2(JUMP_BOTTOM_SIZE, JUMP_BOTTOM_SIZE));
let sense = if k > 0.5 { egui::Sense::click() } else { egui::Sense::hover() };
let r = ui.interact(rect, id.with("button"), sense);
let radius = JUMP_BOTTOM_SIZE / 2.0;
let painter = ui.painter();
painter.add(egui::epaint::Shadow { offset: [0, 4], blur: 12, spread: 0, color: Color32::from_black_alpha((31.0 * k) as u8) }.as_shape(rect, radius as u8));
// Opaque first: it sits over the chat's words, and the hover tint is see-through.
painter.circle_filled(center, radius, p.elevated.gamma_multiply(k));
if r.hovered() {
painter.circle_filled(center, radius, p.hover.gamma_multiply(k));
}
painter.circle_stroke(center, radius - 0.5, Stroke::new(1.0, p.border.gamma_multiply(k)));
crate::icons::paint(painter, egui::Rect::from_center_size(center, vec2(16.0, 16.0)), crate::icons::Icon::ArrowDown, p.text2.gamma_multiply(k));
if r.on_hover_text(t("回到最新訊息")).on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
self.jump_bottom = Some(now);
ui.ctx().request_repaint();
}
}
}
/// After a press of 回到底部: still pulling down (`Some`), or done once the chat is at its bottom
/// or [`JUMP_BOTTOM_SECS`] have passed (new words can keep it from ever quite arriving).
fn jump_still_on(since: Option<f64>, from_bottom: f32, now: f64) -> Option<f64> {
since.filter(|t0| from_bottom >= 1.0 && now - t0 <= JUMP_BOTTOM_SECS)
}
/// 回到底部 shows when the chat is scrolled well up, and not while a press is taking it down.
fn jump_shows(from_bottom: f32, jumping: bool) -> bool {
from_bottom > JUMP_BOTTOM_AFTER && !jumping
}
/// "[例行任務]…" → (icon, label) and the words after the tag.
@ -571,4 +626,20 @@ mod tests {
assert!(WORKING.contains(&status_phrase(None, false, false, true, 7).as_str()));
assert!((pop(1.0) - 1.0).abs() < 1e-4 && pop(0.6) > 1.0);
}
#[test]
fn jump_to_bottom_shows_far_up_and_lets_go() {
// Near the newest message: no button. Far up: the button.
assert!(!jump_shows(0.0, false));
assert!(!jump_shows(JUMP_BOTTOM_AFTER, false));
assert!(jump_shows(JUMP_BOTTOM_AFTER + 1.0, false));
// While a press is taking it down, the button is gone.
assert!(!jump_shows(5000.0, true));
// A press keeps pulling until the bottom…
assert_eq!(jump_still_on(Some(10.0), 800.0, 10.2), Some(10.0));
assert_eq!(jump_still_on(Some(10.0), 0.0, 10.2), None);
// …or until a moment has passed, so scrolling up works again.
assert_eq!(jump_still_on(Some(10.0), 800.0, 10.0 + JUMP_BOTTOM_SECS + 0.01), None);
assert_eq!(jump_still_on(None, 800.0, 10.0), None);
}
}

View File

@ -82,6 +82,7 @@ pub const ROWS: &[(&str, &str, &str)] = &[
("看不懂這個時區,請用像 Asia/Taipei 這樣的名稱", "Unknown time zone; use a name like Asia/Taipei", "タイムゾーンを認識できません。Asia/Tokyo のような名前を使ってください"),
("有更新的版本:", "A newer version is available: ", "新しいバージョンがあります:"),
("已是最新版本", "Up to date", "最新です"),
("回到最新訊息", "Jump to latest", "最新のメッセージへ"),
("允許在「你的電腦」上執行", "Allow running on “Your computer”", "「あなたのコンピューター」での実行を許可"),
("🔒 由你的管理員設定,無法變更", "🔒 Set by your administrator; can't be changed", "🔒 管理者が設定しているため変更できません"),
("名稱,例如 工作站", "Name, e.g. Workstation", "名前(例:ワークステーション)"),