Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/noa-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ impl App {
/// single terminal lock. One acquisition rather than three keeps the
/// key-input path off the io thread's output-batch lock longer than
/// necessary (input-latency under heavy pty output).
fn key_encode_modes(&self, window_id: WindowId) -> (bool, bool, u8) {
fn key_encode_modes(&self, window_id: WindowId) -> (bool, bool, u8, bool) {
self.windows
.get(&window_id)
.and_then(WindowState::focused_surface)
Expand All @@ -930,9 +930,10 @@ impl App {
terminal.modes.app_cursor_keys(),
terminal.modes.app_keypad(),
terminal.kitty_keyboard_flags(),
terminal.modify_other_keys_2,
)
})
.unwrap_or((false, false, 0))
.unwrap_or((false, false, 0, false))
}

fn focus_reporting(&self, window_id: WindowId) -> bool {
Expand Down
4 changes: 3 additions & 1 deletion crates/noa-app/src/app/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,8 @@ impl ApplicationHandler<UserEvent> for App {
) {
return;
}
let (app_cursor_keys, app_keypad, kitty_flags) = self.key_encode_modes(window_id);
let (app_cursor_keys, app_keypad, kitty_flags, modify_other_keys) =
self.key_encode_modes(window_id);
let unmodified_key = event.key_without_modifiers();
// On macOS, Option only acts as Alt when winit stripped its
// composition per `macos-option-as-alt` — i.e. the delivered
Expand All @@ -1095,6 +1096,7 @@ impl ApplicationHandler<UserEvent> for App {
app_cursor_keys,
app_keypad,
kitty_flags,
modify_other_keys,
pressed,
event.repeat,
);
Expand Down
50 changes: 46 additions & 4 deletions crates/noa-app/src/app/session_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ impl App {
.first()
.and_then(|id| self.windows.get(id))
.map(|state| capture_window_frame(&state.window));
let focused_tab = self
.focused
.and_then(|focused| tabs.iter().position(|id| *id == focused))
.unwrap_or(0);
// App-wide focus only identifies the tab of the focused group; a
// background window's selected tab comes from its native tab group.
let focused_tab = saved_focused_tab(tabs, self.focused, |id| {
self.windows
.get(&id)
.and_then(|state| crate::macos_window::native_tab_is_selected(&state.window))
.unwrap_or(false)
});
let tab_sessions = tabs
.iter()
.filter_map(|id| {
Expand Down Expand Up @@ -225,6 +229,18 @@ impl App {
restored_groups.push(tab_ids);
}

// Re-select each group's saved tab before restoring app-wide focus, so
// background windows come back showing the tab they were left on.
#[cfg(target_os = "macos")]
for (group, saved) in restored_groups.iter().zip(&state.windows) {
if saved.focused_tab == 0 || saved.focused_tab >= group.len() {
continue;
}
if let Some(state) = group.first().and_then(|id| self.windows.get(id)) {
state.window.select_tab_at_index(saved.focused_tab);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remap the selected index after failed tab restores

When restoring on macOS, if a tab before the saved selection fails to spawn, the loop continues and group contains only the compacted successful tabs, while saved.focused_tab still indexes the original saved list. Passing that stale index here therefore selects a later tab—or skips selection when it is now out of bounds—so background groups can restore the wrong selected tab. Preserve each successful tab's original index or remap the saved index before calling this method.

Useful? React with 👍 / 👎.

}
}

if let Some(focused_window) = state.focused_window
&& let (Some(group), Some(saved)) = (
restored_groups.get(focused_window),
Expand Down Expand Up @@ -508,6 +524,20 @@ fn orientation_from_session(orientation: session::Orientation) -> SplitOrientati
}
}

/// Index of the tab to record as a group's selected tab: the app-focused
/// window when it belongs to this group, otherwise the natively selected tab,
/// otherwise the first.
fn saved_focused_tab(
tabs: &[WindowId],
focused: Option<WindowId>,
is_selected: impl Fn(WindowId) -> bool,
) -> usize {
focused
.and_then(|focused| tabs.iter().position(|id| *id == focused))
.or_else(|| tabs.iter().position(|id| is_selected(*id)))
.unwrap_or(0)
}

/// Read a window's logical-pixel frame (scale-independent) for persistence.
/// The position may be unavailable on some platforms; the size always is.
fn capture_window_frame(window: &Window) -> session::WindowFrame {
Expand All @@ -529,6 +559,18 @@ fn capture_window_frame(window: &Window) -> session::WindowFrame {
mod tests {
use super::*;

#[test]
fn saved_focused_tab_prefers_app_focus_then_native_selection() {
let tabs: Vec<WindowId> = (0..3).map(|i| WindowId::from(i as u64)).collect();
// Focused window in this group wins.
assert_eq!(saved_focused_tab(&tabs, Some(tabs[2]), |_| false), 2);
// Focus elsewhere: the natively selected tab is recorded.
let other = WindowId::from(99u64);
assert_eq!(saved_focused_tab(&tabs, Some(other), |id| id == tabs[1]), 1);
// Nothing selected: first tab.
assert_eq!(saved_focused_tab(&tabs, None, |_| false), 0);
}

#[test]
fn split_rebuild_keeps_remote_identity_on_the_original_leaf() {
let remote = session::RemotePane {
Expand Down
75 changes: 73 additions & 2 deletions crates/noa-app/src/input/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn encode_key(
app_cursor_keys,
false,
0,
false,
true,
false,
)
Expand All @@ -39,6 +40,11 @@ pub fn encode_key(
/// physical press produces with no modifiers held — used by the Kitty encoder
/// to report the unshifted base key code (Shift+1 must report `1`, not `!`).
///
/// `modify_other_keys` mirrors `Terminal::modify_other_keys_2` (xterm
/// `CSI > 4 ; 2 m`): Character keys pressed with Ctrl/Alt/Super are reported
/// as `CSI 27 ; mods ; codepoint ~` instead of the legacy C0/ESC forms. The
/// Kitty protocol, when active, still takes precedence.
///
/// `alt_sends_esc` says whether Alt held with this press should ESC-prefix the
/// produced text. On macOS the Option key composes characters unless
/// `macos-option-as-alt` claims it, so the caller decides per event; on other
Expand All @@ -54,6 +60,7 @@ pub fn encode_key_with_modes(
app_cursor_keys: bool,
app_keypad: bool,
kitty_flags: u8,
modify_other_keys: bool,
pressed: bool,
repeat: bool,
) -> Option<Vec<u8>> {
Expand Down Expand Up @@ -87,6 +94,16 @@ pub fn encode_key_with_modes(
return None;
}

// xterm modifyOtherKeys level 2: a Character key with Ctrl/Alt/Super
// reports its codepoint after Shift/layout translation plus the modifier
// value, so Ctrl+I is distinguishable from Tab. Shift alone (and Option
// composing text on macOS) stays on the legacy path.
if modify_other_keys
&& let Some(bytes) = modify_other_keys_bytes(logical_key, mods, alt_sends_esc)
{
return Some(bytes);
}

// Ctrl+key -> the corresponding C0 control byte. Checked before the
// general text path since terminals expect Ctrl+A..Z (and the classic
// xterm symbol/digit mappings, e.g. Ctrl+Space=NUL, Ctrl+[=ESC) to send
Expand All @@ -99,12 +116,16 @@ pub fn encode_key_with_modes(
if let (Some(c), None) = (chars.next(), chars.next())
&& let Some(byte) = ctrl_c0_byte(c)
{
return Some(vec![byte]);
// Alt still ESC-prefixes the control byte (Ctrl+Alt+A ->
// ESC 0x01) when it isn't composing text.
return Some(alt_esc_prefixed(vec![byte], mods, alt_sends_esc));
}
}
// winit can report Space as a named key; Ctrl+Space is NUL
// (emacs set-mark and friends).
Key::Named(NamedKey::Space) => return Some(vec![0x00]),
Key::Named(NamedKey::Space) => {
return Some(alt_esc_prefixed(vec![0x00], mods, alt_sends_esc));
}
_ => {}
}
}
Expand Down Expand Up @@ -165,6 +186,7 @@ pub(crate) fn encode_enter_key(kitty_flags: u8) -> Vec<u8> {
false,
false,
kitty_flags,
false,
true,
false,
)
Expand All @@ -189,6 +211,55 @@ fn ctrl_c0_byte(c: char) -> Option<u8> {
Some(byte)
}

/// ESC-prefix `bytes` for Alt, but only when Alt is acting as a modifier
/// (`alt_sends_esc`) rather than composing text via macOS Option.
fn alt_esc_prefixed(mut bytes: Vec<u8>, mods: ModifiersState, alt_sends_esc: bool) -> Vec<u8> {
if mods.alt_key() && alt_sends_esc {
bytes.insert(0, 0x1b);
}
bytes
}

/// xterm `modifyOtherKeys=2` encoding: `CSI 27 ; <mods> ; <codepoint> ~` for
/// a Character (or Space) key pressed with Ctrl, Alt-as-modifier, or Super.
/// The logical key preserves Shift and keyboard-layout translation, so
/// Ctrl+Shift+1 on a US layout reports `33` (`!`) with the Shift bit.
fn modify_other_keys_bytes(
logical_key: &Key,
mods: ModifiersState,
alt_sends_esc: bool,
) -> Option<Vec<u8>> {
let alt = mods.alt_key() && alt_sends_esc;
if !(mods.control_key() || alt || mods.super_key()) {
return None;
}
let codepoint = match logical_key {
Key::Character(s) => {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => c as u32,
_ => return None,
}
}
Key::Named(NamedKey::Space) => u32::from(' '),
_ => return None,
};
let mut value = 1;
if mods.shift_key() {
value += 1;
}
if alt {
value += 2;
}
if mods.control_key() {
value += 4;
}
if mods.super_key() {
value += 8;
}
Some(format!("\x1b[27;{value};{codepoint}~").into_bytes())
}

fn alt_prefixed(mut bytes: Vec<u8>, mods: ModifiersState) -> Vec<u8> {
if mods.alt_key() {
bytes.insert(0, 0x1b);
Expand Down
Loading