Route TUI voice controls through the app (#47380)

## What changed

- Handle voice toggle and mute shortcuts in `App`, and route `/voice`, `/voice stop`, and `/voice mute` through thread-scoped events that ignore requests for a different current thread.
- Allow function-key voice toggle bindings and chords beyond chat, including transcript browsing and search, while preserving existing surface bindings and raw key capture.
- Exit transcript browsing when toggling voice, avoid matching completed chords again, and preserve startup submission handling before dispatch.

## Testing

Add regression coverage for voice chords outside chat, binding conflicts, key capture, transcript browsing, mute dispatch, and startup submission cancellation with draft preservation.

GitOrigin-RevId: c4d110ca8a2e1840247a7fe84cd69c6b7cdb36d7
This commit is contained in:
Eric Traut
2026-09-22 22:43:29 +00:00
committed by copyberry
parent 90e653014a
commit 72ec34ee9a
20 changed files with 363 additions and 216 deletions
+41 -2
View File
@@ -269,6 +269,7 @@ mod tui_mode_picker;
mod user_verification;
mod user_verification_errors;
mod user_verification_requests;
mod voice_owner;
#[cfg(test)]
#[path = "app/warnings_tests.rs"]
mod warnings_tests;
@@ -940,7 +941,21 @@ impl App {
};
self.cancel_primed_browsing_for_event(&event);
if self.handle_owned_transcript_event(tui, app_server, &event)? {
let voice_toggle = |app: &Self, key: KeyEvent| {
key.kind == KeyEventKind::Press
&& app
.active_keymap_contexts()
.contains_action(crate::keymap::KeymapActionId {
context: crate::keymap::KeymapContext::Chat,
action: "toggle_voice",
})
&& app.keymap.chat.toggle_voice.is_pressed(key)
};
// Find consumes otherwise-unhandled keys; let enabled voice controls reach App.
if !matches!(&event, TuiEvent::Key(key)
if voice_toggle(self, *key) && !self.transcript_view.owns_interaction_key(*key))
&& self.handle_owned_transcript_event(tui, app_server, &event)?
{
return Ok(AppRunControl::Continue);
}
// Leave browsing before unhandled editing input reaches shortcuts or offline input.
@@ -955,13 +970,37 @@ impl App {
{
self.cancel_transcript_browsing(tui);
// The first lookup used browsing contexts; retry after restoring composer contexts.
if let TuiEvent::Key(key) = event {
// Completed chords already identify an action and must not be matched again.
if let TuiEvent::Key(key) = event
&& !crate::keymap::is_dispatch_token_event(key)
{
let Some(key) = self.route_key_chord_event(tui, key) else {
return Ok(AppRunControl::Continue);
};
event = TuiEvent::Key(key);
}
}
if let TuiEvent::Key(key_event) = &event
&& voice_toggle(self, *key_event)
{
self.cancel_transcript_browsing(tui);
if !self.chat_widget.handle_startup_submission_key(*key_event) {
self.control_voice(crate::app_event::VoiceControl::Toggle);
}
return Ok(AppRunControl::Continue);
}
if let TuiEvent::Key(key_event) = &event
&& key_event.kind == KeyEventKind::Press
&& self
.active_keymap_contexts()
.contains(crate::keymap::KeymapContext::Voice)
&& self.keymap.chat.toggle_voice_mute.is_pressed(*key_event)
{
if !self.chat_widget.handle_startup_submission_key(*key_event) {
self.control_voice(crate::app_event::VoiceControl::Mute);
}
return Ok(AppRunControl::Continue);
}
if self.reconnect.offline
&& !self.chat_widget.keymap_contexts().is_warnings()
&& !matches!(&self.overlay, Some(Overlay::Transcript(_)))
+5
View File
@@ -1894,6 +1894,11 @@ impl App {
}
return Ok(control);
}
AppEvent::VoiceControl { thread_id, control } => {
if thread_id == self.chat_widget.thread_id() {
self.control_voice(control);
}
}
AppEvent::RealtimeWebrtcOfferCreated {
thread_id,
attempt_id,
+8 -7
View File
@@ -190,26 +190,26 @@ impl App {
} else {
KeymapContext::Pager
};
let contexts = KeymapContextSet::new(context);
let contexts = KeymapContextSet::new(context).with_voice_toggle(&self.keymap);
return if self.backtrack.overlay_preview_active && context == KeymapContext::Pager {
KeymapContextSet::browsing()
KeymapContextSet::browsing().with_voice_toggle(&self.keymap)
} else {
contexts
};
}
if self.transcript_view.is_search_active() && self.chat_widget.no_modal_or_popup_active() {
return KeymapContextSet::new(KeymapContext::Editor);
return KeymapContextSet::new(KeymapContext::Editor).with_voice_toggle(&self.keymap);
}
if self.transcript_view.is_activity_focused() && self.chat_widget.no_modal_or_popup_active()
{
return KeymapContextSet::activity();
return KeymapContextSet::activity().with_voice_toggle(&self.keymap);
}
if self.backtrack.overlay_preview_active && self.chat_widget.no_modal_or_popup_active() {
return KeymapContextSet::browsing();
return KeymapContextSet::browsing().with_voice_toggle(&self.keymap);
}
let voice_available = self.chat_widget.realtime_microphone_shortcut_available();
let contexts = self.chat_widget.keymap_contexts();
if self.chat_widget.no_modal_or_popup_active() {
let contexts = if self.chat_widget.no_modal_or_popup_active() {
let contexts = contexts
.with(KeymapContext::Global)
.with(KeymapContext::Chat);
@@ -225,7 +225,8 @@ impl App {
}
} else {
contexts
}
};
contexts.with_voice_toggle(&self.keymap)
}
pub(super) async fn launch_external_editor(&mut self, tui: &mut tui::Tui) {
@@ -375,12 +375,14 @@ async fn browsing_requires_fresh_escape_presses_and_ignores_confirmation_repeats
)
.await?;
assert!(!app.backtrack.overlay_preview_active);
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::PageUp.into()))
.await?;
assert!(!app.backtrack.primed);
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))
.await?;
assert!(!app.backtrack.overlay_preview_active);
for key in [KeyCode::PageUp, KeyCode::F(8)] {
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(key.into()))
.await?;
assert!(!app.backtrack.primed);
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))
.await?;
assert!(!app.backtrack.overlay_preview_active);
}
app.handle_tui_event(
&mut tui,
&mut server,
@@ -416,6 +418,33 @@ async fn browsing_requires_fresh_escape_presses_and_ignores_confirmation_repeats
!std::iter::from_fn(|| events.try_recv().ok())
.any(|event| matches!(event, AppEvent::RevertSessionForPromptEdit { .. }))
);
for (binding, keys) in [
("f8", vec![KeyCode::F(8)]),
("f8 v", vec![KeyCode::F(8), KeyCode::Char('v')]),
] {
app.keymap = RuntimeKeymap::from_config(&toml::from_str(&format!(
"[chat]\ntoggle_voice = '{binding}'"
))?)
.expect("valid voice binding");
let thread_id = app.chat_widget.thread_id().unwrap();
crate::chatwidget::activate_voice_for_thread(&mut app.chat_widget, thread_id);
for key in keys {
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(key.into()))
.await?;
}
assert_eq!(
(
app.backtrack.overlay_preview_active,
app.chat_widget.realtime_conversation_is_running()
),
(false, false)
);
for _ in 0..2 {
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))
.await?;
}
assert!(app.backtrack.overlay_preview_active);
}
app.keymap = RuntimeKeymap::from_config(&toml::from_str(
"[global]\nopen_external_editor = [\"f6 f7\"]",
)?)
+32
View File
@@ -796,6 +796,16 @@ async fn transcript_fixed_keys_take_precedence_over_pager_chord_prefixes() -> Re
);
app.reset_external_editor_state(&mut tui);
}
for voice in ["f9", "f9 v"] {
app.keymap = RuntimeKeymap::from_config(&toml::from_str(&format!(
"[chat]\ntoggle_voice = '{voice}'\n[pager]\nclose_transcript = 'f9'"
))?)
.unwrap();
app.open_transcript_overlay(&mut tui);
press(&mut app, &mut tui, &mut app_server, KeyCode::F(9).into()).await?;
assert!(!app.transcript_view.is_detailed());
assert!(!app.key_chord_matcher.is_pending());
}
tui.set_owned_screen(/*owned*/ false)?;
app_server.shutdown().await?;
Ok(())
@@ -863,3 +873,25 @@ async fn selection_after_live_commit_uses_the_refreshed_frame() -> Result<()> {
app_server.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn key_capture_receives_voice_shortcut_and_chord_prefix() -> Result<()> {
let (mut app, mut rx, _op_rx) = super::make_test_app_with_channels().await;
let mut app_server = start_config_write_test_app_server(&app).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
for config in ["", "[chat]\ntoggle_voice = 'f8 v'"] {
app.keymap = RuntimeKeymap::from_config(&toml::from_str(config)?).unwrap();
app.chat_widget.open_keymap_capture(
"composer".into(),
"submit".into(),
crate::app_event::KeymapEditIntent::ReplaceAll,
crate::app_event::KeymapCaptureMode::SingleKey,
&app.keymap,
);
press(&mut app, &mut tui, &mut app_server, KeyCode::F(8).into()).await?;
assert!(
matches!(rx.try_recv()?, crate::app_event::AppEvent::KeymapCaptured { key, .. } if key == "f8")
);
}
Ok(())
}
@@ -98,3 +98,24 @@ async fn rejected_voice_stop_clears_the_owned_session() -> Result<()> {
}
Ok(())
}
#[tokio::test]
async fn voice_mute_shortcut_reaches_the_active_widget() -> Result<()> {
let (mut app, mut events, _) = make_test_app_with_channels().await;
crate::chatwidget::activate_voice_for_thread(&mut app.chat_widget, ThreadId::new());
let mut server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
app.handle_tui_event(
&mut tui,
&mut server,
tui::TuiEvent::Key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)),
)
.await?;
// The fixture has no microphone handle, so reaching mute reports that limitation.
assert!(std::iter::from_fn(|| events.try_recv().ok()).any(|event| {
matches!(event, AppEvent::InsertHistoryCell(cell) if cell.display_lines(/*width*/ 80)
.iter().any(|line| line.to_string().contains("Start voice mode before muting")))
}));
server.shutdown().await?;
Ok(())
}
+17
View File
@@ -0,0 +1,17 @@
//! Dispatches voice controls through the app before task navigation can change their target.
use super::*;
impl App {
pub(super) fn control_voice(&mut self, control: crate::app_event::VoiceControl) {
use crate::app_event::VoiceControl;
match control {
VoiceControl::Toggle if !self.reconnect.offline => {
self.chat_widget.toggle_realtime_conversation()
}
VoiceControl::Toggle => {}
VoiceControl::Stop => self.chat_widget.stop_realtime_conversation(),
VoiceControl::Mute => self.chat_widget.toggle_realtime_microphone(),
}
}
}
+13
View File
@@ -67,6 +67,14 @@ use codex_realtime_webrtc::StartedRealtimeWebrtcSession;
use crate::history_cell::HistoryCell;
/// Global voice controls always apply to the one call's owner.
#[derive(Clone, Copy, Debug)]
pub(crate) enum VoiceControl {
Toggle,
Stop,
Mute,
}
/// Confirmed server lifecycle operations available from the agents dashboard.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AgentsOverviewAction {
@@ -1082,6 +1090,11 @@ pub(crate) enum AppEvent {
/// Move visible completed voice captions into history in one app event.
CommitRealtimeTranscriptHistory,
VoiceControl {
thread_id: Option<ThreadId>,
control: VoiceControl,
},
/// Finish buffering initial resume replay after all replay events have been queued.
EndInitialHistoryReplayBuffer,
@@ -107,18 +107,6 @@ impl ChatWidget {
return;
}
if key_event.kind == KeyEventKind::Press
&& self.chat_keymap.toggle_voice.is_pressed(key_event)
&& self.bottom_pane.no_modal_or_popup_active()
{
self.toggle_realtime_conversation();
return;
}
if self.handle_realtime_microphone_shortcut(key_event) {
return;
}
match key_event {
KeyEvent {
code: KeyCode::Char(c),
+1 -4
View File
@@ -15,7 +15,6 @@ use crate::app_event::AppEvent;
use crate::bottom_pane::VoiceStripPhase;
use crate::bottom_pane::VoiceStripState;
use crate::history_cell;
use crate::key_hint::KeyBindingListExt;
use crate::motion::MotionMode;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::UserInput;
@@ -25,8 +24,6 @@ use codex_protocol::models::MessagePhase;
use codex_realtime_webrtc::RealtimeWebrtcSession;
use codex_realtime_webrtc::RealtimeWebrtcSessionHandle;
use codex_realtime_webrtc::StartedRealtimeWebrtcSession;
use crossterm::event::KeyEvent;
use crossterm::event::KeyEventKind;
use futures::future::AbortHandle;
use std::collections::HashMap;
use std::collections::VecDeque;
@@ -331,7 +328,7 @@ impl ChatWidget {
self.request_redraw();
}
pub(super) fn stop_realtime_conversation(&mut self) {
pub(crate) fn stop_realtime_conversation(&mut self) {
self.realtime_conversation.startup_retry = StartupRetry::Used;
if matches!(
self.realtime_conversation.phase,
@@ -1,10 +1,10 @@
//! Live voice capture controls, activity meters, and interruption presentation.
//! Meter samples remain bounded and the current thread owns every shortcut.
//! Meter samples remain bounded; App routes shortcuts to the voice owner.
use super::*;
impl ChatWidget {
pub(in crate::chatwidget) fn toggle_realtime_microphone(&mut self) {
pub(crate) fn toggle_realtime_microphone(&mut self) {
let muted = !self.realtime_conversation.microphone_muted;
if let Some(handle) = self.realtime_conversation.handle.as_ref() {
if let Err(error) = handle.set_microphone_muted(muted) {
@@ -39,17 +39,6 @@ impl ChatWidget {
&& self.bottom_pane.no_modal_or_popup_active()
}
pub(crate) fn handle_realtime_microphone_shortcut(&mut self, key_event: KeyEvent) -> bool {
if key_event.kind != KeyEventKind::Press
|| !self.chat_keymap.toggle_voice_mute.is_pressed(key_event)
|| !self.realtime_microphone_shortcut_available()
{
return false;
}
self.toggle_realtime_microphone();
true
}
pub(in crate::chatwidget) fn realtime_microphone_is_listening(&self) -> bool {
!self.realtime_conversation.microphone_muted
&& self.realtime_conversation.thread_id.is_some()
@@ -1,73 +1,16 @@
//! Rendering and key routing regressions for live recording controls.
//! Rendering and capture-state regressions for live recording controls.
use super::*;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyEventKind;
use crossterm::event::KeyModifiers;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn voice_mute_shortcut_only_handles_active_current_thread_presses() {
let (mut chat, _sender, mut events, mut ops) = make_chatwidget_manual_with_sender().await;
let thread_id = activate_voice(&mut chat);
let shortcut = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
assert!(!chat.handle_realtime_microphone_shortcut(KeyEvent::new(
KeyCode::Char('m'),
KeyModifiers::CONTROL,
)));
for kind in [KeyEventKind::Repeat, KeyEventKind::Release] {
assert!(!chat.handle_realtime_microphone_shortcut(KeyEvent { kind, ..shortcut }));
}
for phase in [
RealtimeConversationPhase::Inactive,
RealtimeConversationPhase::Starting,
RealtimeConversationPhase::Stopping,
] {
chat.realtime_conversation.phase = phase;
assert!(!chat.handle_realtime_microphone_shortcut(shortcut));
}
chat.realtime_conversation.phase = RealtimeConversationPhase::Active;
chat.thread_id = Some(ThreadId::new());
assert!(!chat.handle_realtime_microphone_shortcut(shortcut));
chat.thread_id = Some(thread_id);
assert!(events.try_recv().is_err());
assert!(chat.handle_realtime_microphone_shortcut(shortcut));
let Ok(AppEvent::InsertHistoryCell(cell)) = events.try_recv() else {
panic!("a missing microphone handle should fail closed with the existing voice error");
};
assert!(
cell.display_lines(/*width*/ 80)
.iter()
.any(|line| line.to_string().contains("Start voice mode before muting"))
);
assert!(!chat.realtime_conversation.microphone_muted);
assert!(ops.try_recv().is_err());
}
#[tokio::test]
async fn voice_mute_shortcut_accepts_raw_terminal_control_bytes() {
let (mut chat, _sender, mut events, mut ops) = make_chatwidget_manual_with_sender().await;
async fn voice_mute_keymap_updates_the_composer_hint() {
let (mut chat, _sender, _events, _ops) = make_chatwidget_manual_with_sender().await;
activate_voice(&mut chat);
assert!(chat.handle_realtime_microphone_shortcut(KeyEvent::new(
KeyCode::Char('\u{18}'),
KeyModifiers::NONE,
)));
assert!(events.try_recv().is_ok());
assert!(!chat.realtime_conversation.microphone_muted);
assert!(ops.try_recv().is_err());
}
#[tokio::test]
async fn voice_mute_keymap_updates_the_active_handler_and_composer_hint() {
let (mut chat, _sender, mut events, _ops) = make_chatwidget_manual_with_sender().await;
activate_voice(&mut chat);
let custom = KeyEvent::new(KeyCode::F(8), KeyModifiers::NONE);
for (binding, hint, handles_key) in [("'f8'", "f8 mute", true), ("[]", "/voice mute", false)] {
for (binding, hint) in [("'f8'", "f8 mute"), ("[]", "/voice mute")] {
let config = toml::from_str::<codex_config::types::TuiKeymap>(&format!(
"[chat]\ntoggle_voice_mute = {binding}"
))
@@ -75,16 +18,6 @@ async fn voice_mute_keymap_updates_the_active_handler_and_composer_hint() {
let runtime = crate::keymap::RuntimeKeymap::from_config(&config).unwrap();
chat.apply_keymap_update(config, &runtime);
assert!(render_bottom_popup(&chat, /*width*/ 80).contains(hint));
assert!(!chat.handle_realtime_microphone_shortcut(KeyEvent::new(
KeyCode::Char('x'),
KeyModifiers::CONTROL
)));
assert_eq!(
chat.handle_realtime_microphone_shortcut(custom),
handles_key
);
assert_eq!(events.try_recv().is_ok(), handles_key);
assert!(!chat.realtime_conversation.microphone_muted);
}
}
@@ -718,77 +651,3 @@ async fn compact_voice_meters_keep_real_speaker_history_when_the_microphone_is_m
chat.update_realtime_footer();
assert!(!render_bottom_popup(&chat, /*width*/ 45).contains("codex"));
}
#[tokio::test]
async fn voice_toggle_shortcut_uses_the_slash_command_start_guard_and_preserves_draft() {
let (mut chat, _sender, mut events, mut ops) = make_chatwidget_manual_with_sender().await;
let config =
toml::from_str::<codex_config::types::TuiKeymap>("[chat]\ntoggle_voice = 'f8'").unwrap();
let runtime = crate::keymap::RuntimeKeymap::from_config(&config).unwrap();
chat.apply_keymap_update(config, &runtime);
chat.set_side_conversation_active(/*active*/ true);
chat.bottom_pane
.set_composer_text("draft".to_string(), Vec::new(), Vec::new());
chat.handle_key_event(KeyEvent::new(KeyCode::F(8), KeyModifiers::NONE));
let Ok(AppEvent::InsertHistoryCell(cell)) = events.try_recv() else {
panic!("voice should report that side conversations are unsupported");
};
assert!(
cell.display_lines(/*width*/ 80)
.iter()
.any(|line| line.to_string().contains("side conversations"))
);
assert!(render_bottom_popup(&chat, /*width*/ 80).contains("draft"));
assert!(!chat.realtime_conversation_is_running());
assert!(ops.try_recv().is_err());
}
#[tokio::test]
async fn voice_toggle_shortcut_stops_only_on_press_outside_popups() {
let (mut chat, _sender, _events, mut ops) = make_chatwidget_manual_with_sender().await;
let config =
toml::from_str::<codex_config::types::TuiKeymap>("[chat]\ntoggle_voice = 'f8'").unwrap();
let runtime = crate::keymap::RuntimeKeymap::from_config(&config).unwrap();
chat.apply_keymap_update(config, &runtime);
let thread_id = activate_voice(&mut chat);
let shortcut = KeyEvent::new(KeyCode::F(8), KeyModifiers::NONE);
for kind in [KeyEventKind::Repeat, KeyEventKind::Release] {
chat.handle_key_event(KeyEvent { kind, ..shortcut });
assert!(chat.realtime_conversation_is_running());
}
chat.bottom_pane
.set_composer_text("/".to_string(), Vec::new(), Vec::new());
chat.handle_key_event(shortcut);
assert!(chat.realtime_conversation_is_running());
assert!(ops.try_recv().is_err());
chat.bottom_pane
.set_composer_text(String::new(), Vec::new(), Vec::new());
chat.handle_key_event(shortcut);
assert!(!chat.realtime_conversation_is_running());
assert!(
matches!(ops.try_recv(), Ok(AppCommand::RealtimeConversationStop { thread_id: stopped }) if stopped == thread_id)
);
}
#[tokio::test]
async fn voice_toggle_shortcut_respects_live_remapping_and_unbinding() {
let (mut chat, _sender, _events, mut ops) = make_chatwidget_manual_with_sender().await;
for (binding, stops) in [("'f9'", true), ("[]", false)] {
let config = toml::from_str::<codex_config::types::TuiKeymap>(&format!(
"[chat]\ntoggle_voice = {binding}"
))
.unwrap();
let runtime = crate::keymap::RuntimeKeymap::from_config(&config).unwrap();
chat.apply_keymap_update(config, &runtime);
activate_voice(&mut chat);
chat.handle_key_event(KeyEvent::new(KeyCode::F(8), KeyModifiers::NONE));
assert!(chat.realtime_conversation_is_running());
chat.handle_key_event(KeyEvent::new(KeyCode::F(9), KeyModifiers::NONE));
assert_eq!(chat.realtime_conversation_is_running(), !stops);
assert_eq!(ops.try_recv().is_ok(), stops);
}
}
+12 -3
View File
@@ -359,7 +359,10 @@ impl ChatWidget {
}
}
SlashCommand::Voice => {
self.toggle_realtime_conversation();
self.app_event_tx.send(AppEvent::VoiceControl {
thread_id: self.thread_id(),
control: crate::app_event::VoiceControl::Toggle,
});
}
SlashCommand::Side | SlashCommand::Btw => {
self.request_empty_side_conversation(cmd);
@@ -787,8 +790,14 @@ impl ChatWidget {
}
SlashCommand::Voice => match trimmed.to_ascii_lowercase().as_str() {
"settings" => self.app_event_tx.send(AppEvent::OpenRealtimeSettings),
"mute" => self.toggle_realtime_microphone(),
"stop" => self.stop_realtime_conversation(),
"mute" => self.app_event_tx.send(AppEvent::VoiceControl {
thread_id: self.thread_id(),
control: crate::app_event::VoiceControl::Mute,
}),
"stop" => self.app_event_tx.send(AppEvent::VoiceControl {
thread_id: self.thread_id(),
control: crate::app_event::VoiceControl::Stop,
}),
_ => self.add_error_message("Usage: /voice [settings|mute|stop]".to_string()),
},
SlashCommand::Ide => {
@@ -91,7 +91,7 @@ impl ChatWidget {
}
}
pub(super) fn handle_startup_submission_key(&mut self, key: KeyEvent) -> bool {
pub(crate) fn handle_startup_submission_key(&mut self, key: KeyEvent) -> bool {
if self.input_queue.startup_submission.is_none()
|| self.startup_submission_has_protected_input()
|| !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
@@ -135,3 +135,34 @@ async fn startup_submission_does_not_confirm_merged_destination_text() {
);
assert_no_submit_op(&mut op_rx);
}
#[tokio::test]
async fn voice_shortcut_cancels_startup_submission_before_app_dispatch() -> color_eyre::Result<()> {
let mut app = crate::app::test_support::make_test_app().await;
let mut server = Box::pin(crate::start_embedded_app_server_for_picker(
app.chat_widget.config_ref(),
))
.await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
app.chat_widget.bottom_pane.insert_str("confirmed draft");
let mut draft = Some(app.chat_widget.bottom_pane.composer_draft_snapshot());
app.chat_widget
.bottom_pane
.set_composer_text(String::new(), Vec::new(), Vec::new());
app.chat_widget
.restore_startup_input_when_ready(&mut draft, &mut true);
assert!(app.chat_widget.input_queue.startup_submission.is_some());
app.handle_tui_event(
&mut tui,
&mut server,
crate::tui::TuiEvent::Key(KeyCode::F(8).into()),
)
.await?;
assert!(app.chat_widget.input_queue.startup_submission.is_none());
assert_eq!(
app.chat_widget.composer_text_with_pending(),
"confirmed draft"
);
server.shutdown().await?;
Ok(())
}
+2
View File
@@ -49,6 +49,7 @@ mod voice_tests;
#[path = "keymap/global_find_tests.rs"]
mod global_find_tests;
pub(crate) use bindings::KeymapActionId;
pub(crate) use bindings::KeymapContext;
pub(crate) use bindings::bindings_for_action;
pub(crate) use bindings::keymap_action_id;
@@ -59,6 +60,7 @@ pub(crate) use chords::KeyChordMatch;
pub(crate) use chords::KeyChordMatcher;
pub(crate) use chords::KeymapContextSet;
pub(crate) use chords::RuntimeChordKeymap;
pub(crate) use chords::is_dispatch_token_event;
/// Runtime keymap used by TUI input handlers.
///
+58 -1
View File
@@ -44,6 +44,9 @@ pub(crate) struct KeymapContextSet(u32);
const ACTIVITY_FOCUS: u32 = 1 << 13;
const TRANSCRIPT_CLOSE: u32 = 1 << 15;
const WARNINGS_FOCUS: u32 = 1 << 16;
const VOICE_TOGGLE: u32 = 1 << 17;
const RAW_KEY_CAPTURE: u32 = 1 << 18;
const VOICE_TOGGLE_BLOCKED: u32 = 1 << 19;
const TRANSCRIPT_BROWSING: u32 = 1 << 14;
@@ -82,8 +85,62 @@ impl KeymapContextSet {
self.0 & WARNINGS_FOCUS != 0
}
pub(crate) const fn raw_key_capture() -> Self {
Self(RAW_KEY_CAPTURE)
}
/// Only function-key voice bindings extend beyond chat; other surfaces own editing keys.
/// Existing surface bindings and chord prefixes still win.
pub(crate) fn with_voice_toggle(self, keymap: &RuntimeKeymap) -> Self {
let voice = KeymapActionId {
context: KeymapContext::Chat,
action: "toggle_voice",
};
let mut prefixes = keymap
.chat
.toggle_voice
.iter()
.map(|key| (*key, None))
.chain(
keymap
.chords
.bindings
.iter()
.filter(|binding| binding.action == voice)
.map(|binding| (binding.chord.prefix, Some(binding.chord.completion))),
);
// Only the native chat path combines Chat and Global; dialogs can include Chat alone.
let native_chat =
self.contains(KeymapContext::Chat) && self.contains(KeymapContext::Global);
let conflicts = prefixes.any(|(prefix, completion)| {
(!native_chat && !matches!(prefix.parts().0, KeyCode::F(_)))
|| runtime_action_bindings(keymap).any(|binding| {
binding.id != voice
&& self.contains_action(binding.id)
&& binding.bindings.iter().any(|key| {
normalize_chord_binding(*key) == normalize_chord_binding(prefix)
})
})
|| keymap.chords.bindings.iter().any(|binding| {
binding.action != voice
&& self.contains_action(binding.action)
&& binding.chord.prefix == normalize_chord_binding(prefix)
&& completion.is_none_or(|key| key == binding.chord.completion)
})
});
if self.0 & RAW_KEY_CAPTURE != 0 || conflicts {
Self(self.0 | VOICE_TOGGLE_BLOCKED)
} else {
Self(self.0 | VOICE_TOGGLE)
}
}
/// Whether this input path can dispatch the action, including focus-specific exclusions.
pub(crate) fn contains_action(self, action: KeymapActionId) -> bool {
if action.context == KeymapContext::Chat && action.action == "toggle_voice" {
return self.0 & VOICE_TOGGLE_BLOCKED == 0
&& (self.0 & VOICE_TOGGLE != 0 || self.contains(KeymapContext::Chat));
}
self.contains(action.context)
&& (self.0 & ACTIVITY_FOCUS == 0
|| action.context != KeymapContext::Global
@@ -498,7 +555,7 @@ pub(crate) fn is_dispatch_token(binding: KeyBinding) -> bool {
)
}
fn is_dispatch_token_event(event: KeyEvent) -> bool {
pub(crate) fn is_dispatch_token_event(event: KeyEvent) -> bool {
matches!(
event.code,
KeyCode::F(FIRST_DISPATCH_FUNCTION_KEY..=LAST_DISPATCH_FUNCTION_KEY)
+72 -22
View File
@@ -133,32 +133,82 @@ fn voice_toggle_default_yields_to_existing_shortcuts_and_chord_prefixes() {
}
#[test]
fn voice_toggle_chord_dispatches_in_chat_without_an_active_voice_session() {
let config = toml::from_str::<TuiKeymap>("[chat]\ntoggle_voice = 'f8 v'").unwrap();
fn global_voice_toggle_yields_to_overlay_bindings() {
for (voice, overlay, conflicts, global) in [
("f9", "f9", true, true),
("f9 v", "f9 v", true, true),
("f9", "f9 v", true, true),
("f9 v", "f9", true, true),
("ctrl-5", "ctrl-]", true, false),
("ctrl-5 v", "ctrl-]", true, false),
("tab", "f9", false, false),
("tab v", "f9", false, false),
("f9 v", "f9 t", false, true),
] {
let config = toml::from_str::<TuiKeymap>(&format!(
"[composer]\nqueue = []\n[chat]\ntoggle_voice = '{voice}'\n[agents]\nresume = '{overlay}'"
))
.unwrap();
let runtime = RuntimeKeymap::from_config(&config).expect("existing bindings stay valid");
let action = keymap_action_id("chat", "toggle_voice").unwrap();
assert!(
KeymapContextSet::new(KeymapContext::Chat)
.with(KeymapContext::Global)
.with_voice_toggle(&runtime)
.contains_action(action)
);
for (context, enabled) in [
(KeymapContext::Chat, global),
(KeymapContext::Agents, global && !conflicts),
(KeymapContext::Pager, global),
] {
assert_eq!(
KeymapContextSet::new(context)
.with_voice_toggle(&runtime)
.contains_action(action),
enabled
);
}
}
}
#[test]
fn voice_toggle_chord_dispatches_in_overview_without_an_active_voice_session() {
let config = toml::from_str::<TuiKeymap>(
"[chat]\ntoggle_voice = 'f8 v'\n[global]\nopen_transcript = 'f8 t'",
)
.unwrap();
let runtime = RuntimeKeymap::from_config(&config).unwrap();
let mut matcher = KeyChordMatcher::default();
let prefix = KeyEvent::new(KeyCode::F(8), KeyModifiers::NONE);
assert_eq!(
matcher.advance(
prefix,
for contexts in [
KeymapContextSet::new(KeymapContext::List),
KeymapContextSet::raw_key_capture().with_voice_toggle(&runtime),
] {
assert_eq!(
matcher.advance(prefix, &runtime.chords, contexts),
KeyChordMatch::PassThrough
);
}
for contexts in [
KeymapContextSet::new(KeymapContext::Agents),
KeymapContextSet::default(),
KeymapContextSet::new(KeymapContext::Chat).with(KeymapContext::Global),
] {
let contexts = contexts.with_voice_toggle(&runtime);
assert!(matches!(
matcher.advance(prefix, &runtime.chords, contexts),
KeyChordMatch::Pending(_)
));
let KeyChordMatch::Completed(dispatch) = matcher.advance(
KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE),
&runtime.chords,
KeymapContextSet::new(KeymapContext::Pager)
),
KeyChordMatch::PassThrough
);
let contexts = KeymapContextSet::new(KeymapContext::Chat);
assert!(matches!(
matcher.advance(prefix, &runtime.chords, contexts),
KeyChordMatch::Pending(_)
));
let KeyChordMatch::Completed(dispatch) = matcher.advance(
KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE),
&runtime.chords,
contexts,
) else {
panic!("voice toggle chord completes")
};
assert!(runtime.chat.toggle_voice.is_pressed(dispatch));
contexts,
) else {
panic!("voice toggle chord completes")
};
assert!(runtime.chat.toggle_voice.is_pressed(dispatch));
}
}
#[test]
+4
View File
@@ -130,6 +130,10 @@ impl Renderable for KeymapCaptureView {
}
impl BottomPaneView for KeymapCaptureView {
fn keymap_contexts(&self) -> crate::keymap::KeymapContextSet {
crate::keymap::KeymapContextSet::raw_key_capture()
}
fn handle_key_event(&mut self, key_event: KeyEvent) {
if key_event.kind == KeyEventKind::Release
|| (self.capture_mode == KeymapCaptureMode::Chord
+4
View File
@@ -150,6 +150,10 @@ impl Renderable for KeymapDebugView {
}
impl BottomPaneView for KeymapDebugView {
fn keymap_contexts(&self) -> crate::keymap::KeymapContextSet {
crate::keymap::KeymapContextSet::raw_key_capture()
}
fn handle_key_event(&mut self, key_event: KeyEvent) {
if key_event.kind == KeyEventKind::Release {
return;