fix: 只能填字的問題卡,聊天框空著時照舊拿到游標

- 最後一輪審查(沒有 P0/P1)的 P2:上一個 commit 讓問題卡在別的欄位有游標時不搶,可是
  聊天框每次送出後都留著游標,結果只能填字的問題卡幾乎拿不到游標。現在只有別的欄位裡有字
  (例如聊天框正在打字)時才不搶;聊天框空著時照 main 一樣把游標交給問題卡。

已驗證: cargo test 整套連跑 5 次全綠(623);新測試 a_question_to_type_takes_the_caret_unless_words_are_being_typed
  (換回上一個 commit 的規則時會失敗)。
未驗證: 實機。
動到審批路徑附近(問題卡的游標):依 CLAUDE.md 需要實機測試+人類簽名,不得自行發佈。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha3KhvSzysuKUobKUTR
This commit is contained in:
王性驊 2026-11-17 20:17:12 +08:00
parent d7a224dcc3
commit 80b980dcc6
1 changed files with 44 additions and 3 deletions

View File

@ -4125,9 +4125,12 @@ impl App {
// settings are open over it (it took the caret from whatever was being typed there); // settings are open over it (it took the caret from whatever was being typed there);
// it gets it once they close. // it gets it once they close.
let dialog_open = self.show_settings || ui.ctx().memory(|m| m.top_modal_layer()).is_some(); let dialog_open = self.show_settings || ui.ctx().memory(|m| m.top_modal_layer()).is_some();
// (Nor while the caret is in another field — the chat's input with words being typed: // (Nor while words are being typed in another field — the chat's input with something
// they and their Enter would go into the answer.) // in it: they and their Enter would go into the answer. The chat's input keeps the caret
let typing_elsewhere = ui.ctx().memory(|m| m.focused()).is_some_and(|f| f != typed_id); // after every send; empty, it lets the question have it.)
let composer = egui::Id::unique(("composer", bot.id));
let draft_empty = self.drafts.get(&bot.id).is_none_or(|d| d.trim().is_empty());
let typing_elsewhere = ui.ctx().memory(|m| m.focused()).is_some_and(|f| f != typed_id && (f != composer || !draft_empty));
if q.options.is_empty() && !dialog_open && !typing_elsewhere && self.ask_focus.as_ref() != Some(&(call_id.clone(), step)) { if q.options.is_empty() && !dialog_open && !typing_elsewhere && self.ask_focus.as_ref() != Some(&(call_id.clone(), step)) {
self.ask_focus = Some((call_id.clone(), step)); self.ask_focus = Some((call_id.clone(), step));
field.request_focus(); field.request_focus();
@ -8042,6 +8045,44 @@ mod tests {
assert!(got_a.try_recv().is_err(), "back on the chat: its card waits a moment again"); assert!(got_a.try_recv().is_err(), "back on the chat: its card waits a moment again");
} }
/// A question to type an answer to starts with the caret in its field — taken from the chat's
/// input when that is empty (it keeps the caret after every send), not from words being typed.
#[test]
fn a_question_to_type_takes_the_caret_unless_words_are_being_typed() {
for words in ["", "我想先問一下"] {
let ctx = egui::Context::default();
let mut app = super::App::new(&ctx, Some(std::env::temp_dir().join("anybot-question-caret.png")));
let id = app.state.bots[0].id;
let bot = app.state.bot(id).unwrap().clone();
let composer = egui::Id::unique(("composer", id));
app.drafts.insert(id, words.into());
let (reply, _got) = std::sync::mpsc::channel();
let qs = crate::ask::parse(&serde_json::json!({"questions": [{"question": "你的名字?"}]}).to_string()).unwrap_or_default();
app.approvals.insert(id, Approval { call_id: "q1".into(), tool: crate::ask::TOOL.into(), reason: crate::ask::to_json(&qs), grant: String::new(), reply, kind: ApprovalKind::Question });
let frame = |app: &mut super::App, focus: bool| {
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
if focus {
ui.memory_mut(|m| m.request_focus(composer));
}
// (As the window draws them: the card in the chat, then the input.)
app.approval_card(ui, &ctx, &bot);
app.composer(ui, &ctx, &bot);
});
out.textures_delta.clear();
};
frame(&mut app, true);
for _ in 0..3 {
frame(&mut app, false);
}
let on_composer = ctx.memory(|m| m.focused()) == Some(composer);
if words.is_empty() {
assert!(!on_composer && ctx.memory(|m| m.focused()).is_some(), "the empty input gives the caret to the question");
} else {
assert!(on_composer, "words being typed keep the caret");
}
}
}
/// Esc that closes a menu closes the menu: it isn't a no to the step waiting in the chat. /// Esc that closes a menu closes the menu: it isn't a no to the step waiting in the chat.
/// And a card that comes into view (a field let go) takes keys only after a moment. /// And a card that comes into view (a field let go) takes keys only after a moment.
#[test] #[test]