From 80b980dcc6ca72f50595b18a6dd40299ecb7519c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Tue, 17 Nov 2026 20:17:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8F=AA=E8=83=BD=E5=A1=AB=E5=AD=97?= =?UTF-8?q?=E7=9A=84=E5=95=8F=E9=A1=8C=E5=8D=A1=EF=BC=8C=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E6=A1=86=E7=A9=BA=E8=91=97=E6=99=82=E7=85=A7=E8=88=8A=E6=8B=BF?= =?UTF-8?q?=E5=88=B0=E6=B8=B8=E6=A8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 最後一輪審查(沒有 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 Claude-Session: https://claude.ai/code/session_01Uirha3KhvSzysuKUobKUTR --- src/app.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index e92d908..c5291d8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4125,9 +4125,12 @@ impl App { // settings are open over it (it took the caret from whatever was being typed there); // it gets it once they close. 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: - // they and their Enter would go into the answer.) - let typing_elsewhere = ui.ctx().memory(|m| m.focused()).is_some_and(|f| f != typed_id); + // (Nor while words are being typed in another field — the chat's input with something + // in it: they and their Enter would go into the answer. The chat's input keeps the caret + // 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)) { self.ask_focus = Some((call_id.clone(), step)); 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"); } + /// 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. /// And a card that comes into view (a field let go) takes keys only after a moment. #[test]