mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
Show pending key chords in a shared floating shortcut guide (#60637)
## What changed - Replace footer chord hints with a floating guide that shows available completions after a 350 ms delay, including custom prefixes. - Share guide rendering across conversations, transcript overlays, the session picker, startup drafts, hook review, and daemon recovery. Preserve workspace-specific pane labels and filter startup drafts to supported actions. - Adapt the guide to short inline viewports, retain `Esc` cancellation, and redraw when pending chords clear. Consume clicks on the guide so they do not activate controls beneath it. ## Testing Extend tests and snapshots for delayed reveal, timeout, custom prefixes, compact and inline layouts, startup-safe completions, and picker redraws after chord completion or cancellation. GitOrigin-RevId: afa2fe3ae1b5fbea4cd871c17d35206e749c842c
This commit is contained in:
@@ -28,6 +28,7 @@ use crossterm::event::KeyModifiers;
|
||||
use std::time::Duration;
|
||||
use tokio::time::Instant;
|
||||
|
||||
pub(crate) const CHORD_REVEAL_DELAY: Duration = Duration::from_millis(/*millis*/ 350);
|
||||
pub(crate) const KEY_CHORD_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 1);
|
||||
|
||||
const FIRST_DISPATCH_FUNCTION_KEY: u8 = codex_config::types::MAX_FUNCTION_KEY + 1;
|
||||
@@ -555,6 +556,48 @@ pub(crate) struct PendingChord {
|
||||
}
|
||||
|
||||
impl PendingChord {
|
||||
pub(crate) fn actions(self, keymap: &RuntimeChordKeymap) -> Vec<(KeymapActionId, Vec<String>)> {
|
||||
let mut actions = Vec::<(KeymapActionId, Vec<String>)>::new();
|
||||
for binding in self.completions(keymap) {
|
||||
let action = binding.action;
|
||||
let key = binding.chord.completion.display_label();
|
||||
if let Some((_, keys)) = actions.iter_mut().find(|(id, _)| *id == action) {
|
||||
keys.push(key);
|
||||
} else {
|
||||
actions.push((action, vec![key]));
|
||||
}
|
||||
}
|
||||
actions
|
||||
}
|
||||
|
||||
pub(crate) fn guide(
|
||||
self,
|
||||
keymap: &RuntimeChordKeymap,
|
||||
is_available: impl Fn(KeymapActionId) -> bool,
|
||||
) -> Option<crate::bottom_pane::ShortcutHud> {
|
||||
use crate::bottom_pane::ShortcutHud;
|
||||
use ratatui::style::Stylize;
|
||||
let rows: Vec<_> = self
|
||||
.actions(keymap)
|
||||
.into_iter()
|
||||
.filter(|(action, _)| is_available(*action))
|
||||
.map(|(action, keys)| {
|
||||
ShortcutHud::item(
|
||||
keys.join(" / "),
|
||||
crate::keymap_setup::action_label(action.action).into(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
if rows.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(ShortcutHud::new(
|
||||
self.prefix.display_label(),
|
||||
rows,
|
||||
vec!["Esc cancel".dim().into()],
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn completions(
|
||||
self,
|
||||
keymap: &RuntimeChordKeymap,
|
||||
@@ -579,57 +622,39 @@ impl KeyChordMatcher {
|
||||
self.pending.is_some()
|
||||
}
|
||||
|
||||
/// Only workspace leaders use the passive overlay. Other chord prefixes retain their footer.
|
||||
/// Workspace leaders support pane-specific labels and wheel passthrough.
|
||||
pub(crate) fn workspace_leader(&self) -> Option<PendingChord> {
|
||||
self.pending
|
||||
.filter(|p| p.persistent && p.contexts.0 & PANE_DIRECTION != 0)
|
||||
}
|
||||
|
||||
/// Derive one presentation for the pending chord from the matcher that owns it.
|
||||
pub(crate) fn pending_hint_items(
|
||||
pub(crate) fn visible_pending(
|
||||
&self,
|
||||
frames: &crate::tui::FrameRequester,
|
||||
) -> Option<PendingChord> {
|
||||
let pending = self.pending?;
|
||||
let elapsed = pending.started_at.elapsed();
|
||||
if elapsed < CHORD_REVEAL_DELAY {
|
||||
frames.schedule_frame_in(CHORD_REVEAL_DELAY - elapsed);
|
||||
return None;
|
||||
}
|
||||
if !pending.persistent {
|
||||
let remaining = KEY_CHORD_TIMEOUT.checked_sub(elapsed)?;
|
||||
if remaining.is_zero() {
|
||||
return None;
|
||||
}
|
||||
frames.schedule_frame_in(remaining);
|
||||
}
|
||||
Some(pending)
|
||||
}
|
||||
|
||||
pub(crate) fn guide(
|
||||
&self,
|
||||
keymap: &RuntimeChordKeymap,
|
||||
width: u16,
|
||||
) -> Option<Vec<(String, String)>> {
|
||||
let pending = self.pending?;
|
||||
let mut items = vec![(pending.prefix.display_label(), "then".to_string())];
|
||||
for binding in pending.completions(keymap).take(/*n*/ 6) {
|
||||
let label = match binding.action.action {
|
||||
"open_presets" if pending.persistent => "presets",
|
||||
"change_host" => "cycle",
|
||||
"focus_activity" => "activity",
|
||||
"find_transcript" => "find",
|
||||
"open_warnings" => "warnings",
|
||||
action => action,
|
||||
};
|
||||
items.push((
|
||||
binding.chord.completion.display_label(),
|
||||
label.replace('_', " "),
|
||||
));
|
||||
}
|
||||
items.push(("esc".to_string(), "cancel".to_string()));
|
||||
if crate::bottom_pane::footer_hint_items_line(&items).width() > usize::from(width) {
|
||||
let compact: Vec<_> = items[1..]
|
||||
.iter()
|
||||
.map(|(key, action)| {
|
||||
let action = match action.as_str() {
|
||||
"activity" => "inspect",
|
||||
action => action,
|
||||
};
|
||||
(key.clone(), action.to_string())
|
||||
})
|
||||
.collect();
|
||||
let mut fitting = Vec::new();
|
||||
for item in compact {
|
||||
fitting.push(item);
|
||||
if crate::bottom_pane::footer_hint_items_line(&fitting).width() > usize::from(width)
|
||||
{
|
||||
fitting.pop();
|
||||
}
|
||||
}
|
||||
return Some(fitting);
|
||||
}
|
||||
Some(items)
|
||||
frames: &crate::tui::FrameRequester,
|
||||
) -> Option<crate::bottom_pane::ShortcutHud> {
|
||||
let pending = self.visible_pending(frames)?;
|
||||
pending.guide(keymap, |_| true)
|
||||
}
|
||||
|
||||
pub(crate) fn cancel(&mut self) -> bool {
|
||||
|
||||
@@ -597,12 +597,10 @@ fn activity_context_preserves_list_chords_and_its_remapped_focus_toggle() {
|
||||
KeymapContextSet::activity(),
|
||||
);
|
||||
assert_eq!(
|
||||
matcher.pending_hint_items(&runtime.chords, /*width*/ 80),
|
||||
Some(vec![
|
||||
("⌃x".to_string(), "then".to_string()),
|
||||
("c".to_string(), "jump top".to_string()),
|
||||
("esc".to_string(), "cancel".to_string()),
|
||||
]),
|
||||
matcher.pending.unwrap().actions(&runtime.chords)[0]
|
||||
.0
|
||||
.action,
|
||||
"jump_top"
|
||||
);
|
||||
matcher.cancel();
|
||||
let combined = KeymapContextSet::new(KeymapContext::List).with(KeymapContext::Global);
|
||||
@@ -614,36 +612,6 @@ fn activity_context_preserves_list_chords_and_its_remapped_focus_toggle() {
|
||||
assert!(!matcher.is_pending());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_hint_skips_whole_oversized_configured_completions() {
|
||||
let config = serde_json::from_value(serde_json::json!({"global": {
|
||||
"copy": "ctrl-x ctrl-alt-shift-f12", "focus_activity": "ctrl-x alt-shift-f11"
|
||||
}}))
|
||||
.unwrap();
|
||||
let runtime = RuntimeKeymap::from_config(&config).unwrap();
|
||||
let mut matcher = KeyChordMatcher::default();
|
||||
matcher.advance(
|
||||
KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL),
|
||||
&runtime.chords,
|
||||
KeymapContextSet::new(KeymapContext::Global),
|
||||
);
|
||||
let mut scenes = Vec::new();
|
||||
for width in [80, 32, 10] {
|
||||
let hints = matcher.pending_hint_items(&runtime.chords, width).unwrap();
|
||||
let line = crate::bottom_pane::footer_hint_items_line(&hints);
|
||||
assert!(line.width() <= usize::from(width));
|
||||
scenes.push(format!("{width}: {line}"));
|
||||
}
|
||||
assert_eq!(
|
||||
scenes,
|
||||
[
|
||||
"80: ⌃x then · ⇧⌥f11 activity · ⌃⇧⌥f12 copy · esc cancel",
|
||||
"32: ⇧⌥f11 inspect · ⌃⇧⌥f12 copy",
|
||||
"10: esc cancel",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warning_context_keeps_local_chords_ahead_of_copy_and_hides_unreachable_hints() {
|
||||
let config = serde_json::from_value(serde_json::json!({
|
||||
@@ -656,14 +624,9 @@ fn warning_context_keeps_local_chords_ahead_of_copy_and_hides_unreachable_hints(
|
||||
let contexts = KeymapContextSet::warnings();
|
||||
let prefix = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
|
||||
matcher.advance(prefix, &runtime.chords, contexts);
|
||||
assert_eq!(
|
||||
matcher.pending_hint_items(&runtime.chords, /*width*/ 80),
|
||||
Some(vec![
|
||||
("⌃x".into(), "then".into()),
|
||||
("c".into(), "jump bottom".into()),
|
||||
("esc".into(), "cancel".into()),
|
||||
])
|
||||
);
|
||||
let actions = matcher.pending.unwrap().actions(&runtime.chords);
|
||||
assert_eq!(actions.len(), 1);
|
||||
assert_eq!(actions[0].0.action, "jump_bottom");
|
||||
let KeyChordMatch::Completed(event) =
|
||||
matcher.advance(KeyCode::Char('c').into(), &runtime.chords, contexts)
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user