It is intuitive to try to scroll the conversation history using the mouse in the TUI, but prior to this change, we only supported scrolling via keyboard events. This PR enables mouse capture upon initialization (and disables it on exit) such that we get `ScrollUp` and `ScrollDown` events in `codex-rs/tui/src/app.rs`. I initially mapped each event to scrolling by one line, but that felt sluggish. I decided to introduce `ScrollEventHelper` so we could debounce scroll events and measure the number of scroll events in a 100ms window to determine the "magnitude" of the scroll event. I put in a basic heuristic to start, but perhaps someone more motivated can play with it over time. `ScrollEventHelper` takes care of handling the atomic fields and thread management to ensure an `AppEvent::Scroll` event is pumped back through the event loop at the appropriate time with the accumulated delta.
42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
use std::io::stdout;
|
|
use std::io::Stdout;
|
|
use std::io::{self};
|
|
|
|
use crossterm::event::DisableMouseCapture;
|
|
use crossterm::event::EnableMouseCapture;
|
|
use ratatui::backend::CrosstermBackend;
|
|
use ratatui::crossterm::execute;
|
|
use ratatui::crossterm::terminal::disable_raw_mode;
|
|
use ratatui::crossterm::terminal::enable_raw_mode;
|
|
use ratatui::crossterm::terminal::EnterAlternateScreen;
|
|
use ratatui::crossterm::terminal::LeaveAlternateScreen;
|
|
use ratatui::Terminal;
|
|
|
|
/// A type alias for the terminal type used in this application
|
|
pub type Tui = Terminal<CrosstermBackend<Stdout>>;
|
|
|
|
/// Initialize the terminal
|
|
pub fn init() -> io::Result<Tui> {
|
|
execute!(stdout(), EnterAlternateScreen)?;
|
|
execute!(stdout(), EnableMouseCapture)?;
|
|
enable_raw_mode()?;
|
|
set_panic_hook();
|
|
Terminal::new(CrosstermBackend::new(stdout()))
|
|
}
|
|
|
|
fn set_panic_hook() {
|
|
let hook = std::panic::take_hook();
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
|
let _ = restore(); // ignore any errors as we are already failing
|
|
hook(panic_info);
|
|
}));
|
|
}
|
|
|
|
/// Restore the terminal to its original state
|
|
pub fn restore() -> io::Result<()> {
|
|
execute!(stdout(), DisableMouseCapture)?;
|
|
execute!(stdout(), LeaveAlternateScreen)?;
|
|
disable_raw_mode()?;
|
|
Ok(())
|
|
}
|