fix: 等你允許的卡片:同編號的卡分得開、新卡一到就重新等;問題卡不搶正在打字的游標

- 第十輪審查沒有 P0/P1(也確認 Enter 快捷鍵在實際 App 照常可用:每個 pass 都會畫到卡片)。
  照它的 P2 收尾:
  - 卡片編號會重複(沒給編號的服務每一輪都是 call_0、call_1…):記住的上一幀狀態改成每個 Bot
    各一份,卡片用「到達時的隨機鍵+編號+理由」分辨;新卡一到(on_new_approval)就重新計時。
  - 沒有選項的問題卡在游標停在別的欄位(例如正在聊天框打字)時不搶游標。
  - 測試補上放開按鍵,「很快按第二下」真的是第二下;加上兩個 Bot 同編號的情況。

已驗證: cargo test 整套連跑 5 次全綠(622);a_new_card_or_a_second_enter_is_not_a_yes 的同編號情況
  拿掉修正時會失敗。
未驗證: 實機按鍵手感。
動到審批路徑(approval_dock、on_new_approval):依 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:07:38 +08:00
parent 1b714f76fd
commit d7a224dcc3
1 changed files with 30 additions and 5 deletions

View File

@ -3990,8 +3990,10 @@ impl App {
// What was kept is this card's, from the frame just before (not another bot's card, not // What was kept is this card's, from the frame just before (not another bot's card, not
// from before the person came back to this chat): otherwise it counts as blocked. // from before the person came back to this chat): otherwise it counts as blocked.
let composer = egui::Id::unique(("composer", bot.id)); let composer = egui::Id::unique(("composer", bot.id));
let seen = egui::Id::unique("approval-dock-caret"); // (Each bot's own; a card is told apart by its arrival key, its call id and its words —
let card = self.approvals.get(&bot.id).map(|a| a.call_id.clone()).unwrap_or_default(); // call ids repeat: a service that gives none gets call_0, call_1… every round.)
let seen = egui::Id::unique(("approval-dock-caret", bot.id));
let card = self.approvals.get(&bot.id).map(|a| format!("{}|{}|{}", self.ask_keys.get(&bot.id).map(String::as_str).unwrap_or(""), a.call_id, a.reason)).unwrap_or_default();
let pass = ctx.cumulative_pass_nr(); let pass = ctx.cumulative_pass_nr();
let kept: Option<(Option<egui::Id>, bool, u64, String)> = ctx.data(|d| d.get_temp(seen)); let kept: Option<(Option<egui::Id>, bool, u64, String)> = ctx.data(|d| d.get_temp(seen));
let fresh = kept.as_ref().is_some_and(|k| k.2 + 1 == pass && k.3 == card); let fresh = kept.as_ref().is_some_and(|k| k.2 + 1 == pass && k.3 == card);
@ -4123,7 +4125,10 @@ 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();
if q.options.is_empty() && !dialog_open && self.ask_focus.as_ref() != Some(&(call_id.clone(), step)) { // (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);
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();
} }
@ -4315,6 +4320,8 @@ impl App {
/// attention (the Dock icon bounces when AnyBot isn't in front, and the 提示音 rings). /// attention (the Dock icon bounces when AnyBot isn't in front, and the 提示音 rings).
fn on_new_approval(&mut self, ctx: &egui::Context, bot_id: u64) { fn on_new_approval(&mut self, ctx: &egui::Context, bot_id: u64) {
self.popup_hidden.remove(&bot_id); self.popup_hidden.remove(&bot_id);
// (A new card waits its own moment before it takes Enter / Esc.)
self.ask_shown.remove(&bot_id);
let key = crate::httpd::random_hex(16); let key = crate::httpd::random_hex(16);
self.ask_keys.insert(bot_id, key.clone()); self.ask_keys.insert(bot_id, key.clone());
// (A brain on a server has nobody to hear it.) // (A brain on a server has nobody to hear it.)
@ -7994,8 +8001,9 @@ mod tests {
app.ask_shown.insert(id, Instant::now() - std::time::Duration::from_secs(2)); app.ask_shown.insert(id, Instant::now() - std::time::Duration::from_secs(2));
frame(&mut app, vec![enter.clone()], false); frame(&mut app, vec![enter.clone()], false);
assert_eq!(steer.lock().unwrap().len(), 1, "the words went"); assert_eq!(steer.lock().unwrap().len(), 1, "the words went");
// The same Enter held a moment (its repeat), and a quick second one: neither is the yes. // The same Enter held a moment (its repeat), let go, and a quick second press: neither is the yes.
frame(&mut app, vec![egui::Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: true, modifiers: egui::Modifiers::NONE }], false); frame(&mut app, vec![egui::Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: true, modifiers: egui::Modifiers::NONE }], false);
frame(&mut app, vec![egui::Event::Key { key: Key::Enter, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::NONE }], false);
frame(&mut app, vec![enter.clone()], false); frame(&mut app, vec![enter.clone()], false);
let r = got.try_recv(); let r = got.try_recv();
assert!(!matches!(r, Ok(ApprovalAnswer::Once)) && app.approvals.contains_key(&id), "the Enter right after sending isn't a yes"); assert!(!matches!(r, Ok(ApprovalAnswer::Once)) && app.approvals.contains_key(&id), "the Enter right after sending isn't a yes");
@ -8013,8 +8021,25 @@ mod tests {
frame2(&mut app2, vec![]); frame2(&mut app2, vec![]);
app2.ask_shown.insert(id2, Instant::now() - std::time::Duration::from_secs(2)); app2.ask_shown.insert(id2, Instant::now() - std::time::Duration::from_secs(2));
let got2 = card(&mut app2, id2, "c2"); let got2 = card(&mut app2, id2, "c2");
frame2(&mut app2, vec![enter]); frame2(&mut app2, vec![enter.clone()]);
assert!(got2.try_recv().is_err(), "a card seen for no time isn't answered"); assert!(got2.try_recv().is_err(), "a card seen for no time isn't answered");
// Two bots, each with a card called 「call_0」 (a service that gives no ids): looking at
// one a while, then back to the other — its first frame doesn't take an Enter.
let (ctx3, mut app3) = setup("anybot-same-call-id.png");
let (a, b) = (app3.state.bots[0].id, app3.state.bots[1].id);
let (bot_a, bot_b) = (app3.state.bot(a).unwrap().clone(), app3.state.bot(b).unwrap().clone());
let got_a = card(&mut app3, a, "call_0");
let _got_b = card(&mut app3, b, "call_0");
let show = |app: &mut super::App, bot: &crate::model::Bot, events: Vec<egui::Event>| {
let mut out = ctx3.run_ui(egui::RawInput { events, ..Default::default() }, |ui| app.composer(ui, &ctx3, bot));
out.textures_delta.clear();
};
show(&mut app3, &bot_a, vec![]);
show(&mut app3, &bot_b, vec![]);
show(&mut app3, &bot_b, vec![]);
app3.ask_shown.insert(a, Instant::now() - std::time::Duration::from_secs(2));
show(&mut app3, &bot_a, vec![enter]);
assert!(got_a.try_recv().is_err(), "back on the chat: its card waits a moment again");
} }
/// 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.