fix(tui/scroll): translate wheel events in alt screen

Closes just-every/code#168.
This commit is contained in:
James Peter
2025-09-16 21:44:11 +10:00
parent 0f254d9ec8
commit 764cd27612
+19
View File
@@ -2,6 +2,7 @@ use std::io::Result;
use std::io::Stdout;
use std::io::stdout;
use std::io::BufWriter;
use std::io::Write;
use codex_core::config::Config;
use crossterm::cursor::MoveTo;
@@ -76,6 +77,7 @@ pub fn init(config: &Config) -> Result<(Tui, TerminalInfo)> {
crate::syntax_highlight::init_highlight_from_config(&config.tui.highlight);
execute!(stdout(), EnableBracketedPaste)?;
enable_alternate_scroll_mode()?;
// Enable focus change events so we can detect when the terminal window/tab
// regains focus and proactively repaint the UI (helps terminals that clear
// their alt‑screen buffer while unfocused). However, certain environments
@@ -250,6 +252,7 @@ pub fn restore() -> Result<()> {
execute!(stdout(), DisableBracketedPaste)?;
// Best‑effort: disable focus change notifications if supported.
let _ = execute!(stdout(), DisableFocusChange);
disable_alternate_scroll_mode()?;
execute!(stdout(), DisableMouseCapture)?;
disable_raw_mode()?;
// Leave alternate screen mode
@@ -266,6 +269,7 @@ pub fn restore() -> Result<()> {
pub fn leave_alt_screen_only() -> Result<()> {
// Best effort: disable mouse capture so selection/scroll works naturally.
let _ = execute!(stdout(), DisableMouseCapture);
disable_alternate_scroll_mode()?;
// Also disable bracketed paste and focus tracking to avoid escape sequences
// being echoed into the normal buffer by some terminals.
let _ = execute!(stdout(), DisableBracketedPaste);
@@ -299,6 +303,7 @@ pub fn enter_alt_screen_only(theme_fg: ratatui::style::Color, theme_bg: ratatui:
let _ = execute!(stdout(), EnableFocusChange);
}
let _ = execute!(stdout(), EnableBracketedPaste);
enable_alternate_scroll_mode()?;
execute!(
stdout(),
crossterm::terminal::EnterAlternateScreen,
@@ -311,6 +316,20 @@ pub fn enter_alt_screen_only(theme_fg: ratatui::style::Color, theme_bg: ratatui:
Ok(())
}
fn enable_alternate_scroll_mode() -> Result<()> {
let mut handle = stdout();
handle.write_all(b"\x1b[?1007h")?;
handle.flush()?;
Ok(())
}
fn disable_alternate_scroll_mode() -> Result<()> {
let mut handle = stdout();
handle.write_all(b"\x1b[?1007l")?;
handle.flush()?;
Ok(())
}
/// Clear the current screen (normal buffer) with the theme background and reset cursor.
// Removed: clear_screen_with_theme — we no longer hard-clear the normal buffer in terminal mode.