`;
+ const box = $("askq"), field = box.querySelector(".own"), go = box.querySelector('[data-go="next"]');
+ // The chat comes to the card when it appears and at each next question.
+ // (Once laid out; a card taller than the screen shows its question, not its buttons.)
+ requestAnimationFrame(() => {
+ const sc = $("scroller"), top = box.getBoundingClientRect().top - sc.getBoundingClientRect().top + sc.scrollTop;
+ sc.scrollTo({top: box.offsetHeight > sc.clientHeight ? top : sc.scrollHeight, behavior: "smooth"});
+ });
const ready = () => { go.disabled = !askAnswer(q, i); };
ready();
const send = async (body) => { ASKF = null; box.innerHTML = ""; try { await api(base() + `api/bots/${(a && a.target) || S.open}/answer`, {json: body}); } catch (e) { toast(e.message); } };
diff --git a/src/app.rs b/src/app.rs
index 59eb196..7d2c112 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -2711,7 +2711,7 @@ impl App {
}
// (An id of its own: the popup stays open while cards come and go above.)
let (rect, _) = at.allocate_exact_size(vec2(26.0, 26.0), egui::Sense::hover());
- let r = at.interact(rect, egui::Id::new(("mode", bot.id, m.end)), egui::Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
+ let r = at.interact(rect, egui::Id::unique(("mode", bot.id, m.end)), egui::Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
if r.hovered() {
at.painter().rect_filled(rect, 7.0, p.hover);
}
@@ -3706,7 +3706,8 @@ impl App {
}
self.sending_bubbles(ui, &bot);
// (What it is doing, and a request, sit above the input: see composer.
- // A hand-off keeps its card here, with the live screen.)
+ // A hand-off keeps its card here, with the live screen; so does a
+ // question — it scrolls with the chat instead of covering it.)
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.
@@ -3917,8 +3918,11 @@ impl App {
fn approval_card(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot) {
let p = self.pal;
let _ = p;
- if self.approvals.get(&bot.id).map(|a| a.kind) == Some(ApprovalKind::Handoff) {
- self.handoff_card(ui, ctx, bot);
+ match self.approvals.get(&bot.id).map(|a| a.kind) {
+ Some(ApprovalKind::Handoff) => self.handoff_card(ui, ctx, bot),
+ Some(ApprovalKind::Question) => self.question_card(ui, ctx, bot),
+ // (The next card comes into view again, even one with the same id.)
+ _ => ui.data_mut(|d| d.remove::<(String, usize)>(egui::Id::unique(("ask-seen", bot.id)))),
}
}
@@ -3930,8 +3934,8 @@ impl App {
/// `enter`: the composer took a plain Enter that is this card's (the box is empty).
fn approval_dock(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot, draft_empty: bool, typing: bool, enter: bool) {
let p = self.pal;
+ // (A question's card is in the chat: see question_card.)
if self.approvals.get(&bot.id).is_some_and(|a| a.kind == ApprovalKind::Question) {
- self.question_dock(ui, ctx, bot);
return;
}
self.ask_forms.remove(&bot.id);
@@ -3991,11 +3995,13 @@ impl App {
self.mark();
}
- /// 問題卡, docked above the input like a request: one question at a time. A choice is one
- /// click (it moves on, and on the last question sends); several choices and typed answers
- /// have 下一題/送出. 「自己回答」 is always there, and so is writing a message instead, which
- /// leaves the card unanswered.
- fn question_dock(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot) {
+ /// 問題卡, the last thing in the chat (it used to be docked above the input, where a card
+ /// with several choices covered most of what the bot had just said): it scrolls with the
+ /// messages, as tall as its content, and the chat comes to it when it appears or moves to
+ /// the next question. One question at a time. A choice is one click (it moves on, and on
+ /// the last question sends); several choices and typed answers have 下一題/送出. 「自己回答」
+ /// is always there, and so is writing a message instead, which leaves the card unanswered.
+ fn question_card(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot) {
let p = self.pal;
let Some(a) = self.approvals.get(&bot.id) else { return };
let call_id = a.call_id.clone();
@@ -4013,12 +4019,11 @@ impl App {
Skip,
}
let mut act = None;
- // (選項再多也不許把輸入框頂出螢幕:卡面照內容估高、照視窗高度封頂,裡面在卡面裡往下捲。)
- let cap = (ui.ctx().content_rect().height() * 0.45).max(160.0);
- let body = ask_card_height(q, ui.available_width(), cap);
+ ui.add_space(10.0);
+ let top = ui.cursor().top();
needs_you_card(ui, p, |ui| {
ui.set_width(ui.available_width());
- egui::ScrollArea::vertical().id_salt(("ask-dock", bot.id, call_id.clone())).max_height(body).min_scrolled_height(body).auto_shrink([false, false]).show(ui, |ui| {
+ ui.vertical(|ui| {
if qs.len() > 1 {
ui.label(RichText::new(format!("{} 問你 · {}/{}", bot.name, step + 1, qs.len())).size(FONT_SM).color(p.text2));
} else {
@@ -4089,6 +4094,18 @@ impl App {
});
});
});
+ // The chat comes to the card once for each question (after that the person scrolls as
+ // they like: up to read what it is about, back down to answer).
+ let seen_id = egui::Id::unique(("ask-seen", bot.id));
+ let now_at = (call_id.clone(), step);
+ // (Not while a search is taking the chat to a message: the card's turn comes after.)
+ if self.goto.is_none() && ui.data(|d| d.get_temp::<(String, usize)>(seen_id)).as_ref() != Some(&now_at) {
+ ui.data_mut(|d| d.insert_temp(seen_id, now_at));
+ let card = egui::Rect::from_min_max(egui::pos2(ui.min_rect().left(), top), egui::pos2(ui.min_rect().right(), ui.cursor().top() + 16.0));
+ // A card taller than the window shows its question, not its buttons.
+ let tall = card.height() > ui.clip_rect().height();
+ ui.scroll_to_rect(card, Some(if tall { Align::TOP } else { Align::BOTTOM }));
+ }
ui.add_space(6.0);
match act {
Some(Act::Skip) => self.answer(ctx, bot.id, ApprovalAnswer::Reject),
@@ -7324,12 +7341,6 @@ fn ask_dock_height(reason: &str, width: f32, cap: f32) -> f32 {
(wrapped_lines(detail, (width - 92.0).max(120.0), cw) * 17.0 + 168.0).min(cap)
}
-/// The question card's content height: header + question + option rows + field + buttons.
-fn ask_card_height(q: &crate::ask::Question, width: f32, cap: f32) -> f32 {
- let q_lines = wrapped_lines(&q.question, (width - 64.0).max(120.0), 16.0);
- (26.0 + q_lines * 21.0 + q.options.len() as f32 * 44.0 + 122.0).min(cap)
-}
-
fn composer_box_h(text_h: f32, max_h: f32) -> f32 {
(text_h + COMPOSER_TOP + 3.0).clamp(28.0, max_h.max(28.0))
}
@@ -9062,10 +9073,6 @@ mod tests {
assert!((1.0..=cap + 0.01).contains(&short), "short {short} cap {cap} w {width}");
assert!((1.0..=cap + 0.01).contains(&long), "long {long} cap {cap} w {width}");
assert!(long >= short, "a longer reason got a shorter card at w {width}");
- let q = |n: usize| crate::ask::Question { question: "請選一個要彙整的資料夾".into(), options: (0..n).map(|i| format!("選項 {i}")).collect(), multiple: false, default: String::new() };
- let (few, many) = (ask_card_height(&q(2), width, cap), ask_card_height(&q(8), width, cap));
- assert!((1.0..=cap + 0.01).contains(&few) && (1.0..=cap + 0.01).contains(&many), "question card over the cap at w {width}");
- assert!(many >= few, "more options made the card shorter at w {width}");
}
}
// The composer's box: at least one row, never past its own cap.