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]