Highlight clickable links and controls on hover in the fullscreen TUI (#62390)

## Why

The pointer shape can signal that transcript links are clickable, but the
text itself does not change on hover. A foreground change makes the
clickable region visible.

## What changed

Change the foreground color of hovered transcript and status-line
links, transcript disclosure controls, and the warning notice. Use the
rendered link spans so the highlight follows the visible text, including
wide characters. Recompute the highlight on redraw so it follows the
content under a stationary pointer as the transcript scrolls or updates.

## Testing

Add a mouse-motion regression test that checks highlight and restore
behavior for transcript links, including wide characters and Markdown
table links, as well as status-line links, disclosure controls, and the
warning notice. The test also checks that a URL in the editable draft
keeps its original styling.

GitOrigin-RevId: 4c7da58705e2ad4c710ec6c55727d5a064f3cf1f
This commit is contained in:
Felipe Coury
2026-11-19 01:16:44 +00:00
committed by copyberry
parent c332754aa3
commit b5e1f3ae9d
17 changed files with 389 additions and 71 deletions
+1 -10
View File
@@ -930,16 +930,7 @@ impl App {
tui.frame_requester().schedule_frame();
return Ok(AppRunControl::Continue);
}
let previous = tui.link_hover.position();
tui.link_hover.observe(&event);
let current = tui.link_hover.position();
if previous != current
&& (self.hovered_sidebar_target(previous) != self.hovered_sidebar_target(current)
|| previous.is_some_and(|point| self.transcript_view.has_document_link_at(point))
|| current.is_some_and(|point| self.transcript_view.has_document_link_at(point)))
{
tui.frame_requester().schedule_frame();
}
self.observe_pointer(tui, &event);
if Box::pin(sidebar_close::handle(self, tui, app_server, &event)).await {
return Ok(AppRunControl::Continue);
}
+122 -2
View File
@@ -1,5 +1,6 @@
//! Match hover feedback to transcript links and the pane divider. Keep passive link drags with their original view.
//! Recheck after painting so scrolling and live output cannot leave a hand over stale content.
//! Match pointer and foreground feedback to the last painted click targets.
//! Recheck on repaint so scrolling and live output cannot leave hover on stale content.
//! Keep passive link drags with their original view.
use codex_protocol::ThreadId;
use crossterm::event::KeyModifiers;
@@ -10,10 +11,17 @@ use crossterm::event::MouseEventKind::Moved;
use crossterm::event::MouseEventKind::ScrollDown;
use crossterm::event::MouseEventKind::ScrollUp;
use crossterm::event::MouseEventKind::Up;
use ratatui::buffer::Buffer;
use ratatui::buffer::CellDiffOption;
use ratatui::buffer::CellWidth;
use ratatui::layout::Position;
use ratatui::layout::Rect;
use ratatui::style::Modifier;
use super::App;
use super::side_companion::SideCompanion;
use crate::app_event::AppEvent;
use crate::custom_terminal::osc8_hyperlink_parts;
use crate::terminal_hyperlinks::ReaderLink;
use crate::transcript_view::ViewAction;
use crate::tui::PointerShape;
@@ -21,6 +29,81 @@ use crate::tui::Tui;
use crate::tui::TuiEvent;
impl App {
pub(super) fn observe_pointer(&self, tui: &mut Tui, event: &TuiEvent) {
let previous = tui.link_hover.mouse;
let previous_position = tui.link_hover.position();
tui.link_hover.observe(event);
let current = tui.link_hover.mouse;
if previous == current {
return;
}
let current_position = tui.link_hover.position();
let buffer = tui.terminal.previous_buffer();
if self.hovered_sidebar_target(previous_position)
!= self.hovered_sidebar_target(current_position)
|| [previous_position, current_position]
.into_iter()
.flatten()
.any(|point| self.transcript_view.has_document_link_at(point))
|| self.hover_target_area(self.hover_pointer(tui, previous), buffer)
!= self.hover_target_area(self.hover_pointer(tui, current), buffer)
{
tui.frame_requester().schedule_frame();
}
}
pub(super) fn hover_pointer(&self, tui: &Tui, mouse: Option<MouseEvent>) -> Option<MouseEvent> {
mouse.filter(|mouse| {
tui.is_owned_screen()
&& self.overlay.is_none()
&& !tui.shortcut_hud_contains(*mouse)
&& self.pane_resize.is_none()
&& self.sidebar().is_none_or(|pane| pane.close.is_none())
})
}
pub(super) fn hover_target_area(
&self,
mouse: Option<MouseEvent>,
buf: &Buffer,
) -> Option<Rect> {
let mouse = mouse?;
let point = Position::new(mouse.column, mouse.row);
if !buf.area.contains(point) {
return None;
}
let passive = self
.conversation_frame
.side_pane()
.is_some_and(|panes| panes.passive.contains(point));
let ordinary_input = self.chat_widget.no_modal_or_popup_active();
if !passive {
// Inline approvals allow disclosure in the transcript above the prompt.
if mouse.modifiers.is_empty()
&& (ordinary_input
|| (self.chat_widget.has_active_modal()
&& self.chat_widget.centered_dialog().is_none()))
&& let Some(area) = self.transcript_view.disclosure_hover_area(point)
{
return Some(area);
}
if let Some(area) = self.chat_widget.warning_notice_at(point) {
return Some(area);
}
}
if !ordinary_input {
return None;
}
if self.chat_widget.status_line_area().contains(point)
|| ((passive || !self.transcript_view.is_select_mode())
&& self.transcript_link_at(mouse).is_some())
{
painted_link_area(buf, point)
} else {
None
}
}
pub(super) fn transcript_link_at(&self, mouse: MouseEvent) -> Option<ReaderLink> {
if !mouse.modifiers.is_empty()
&& !mouse
@@ -187,3 +270,40 @@ impl App {
tui.set_pointer_shape(shape)
}
}
/// Read a single visible link fragment from the OSC 8 markers attached by the renderer.
fn painted_link_area(buf: &Buffer, point: Position) -> Option<Rect> {
let mut column = buf.area.left();
let mut start = column;
let mut destination = None;
let right = buf.area.right();
while column < right {
let cell = &buf[(column, point.y)];
let link = if cell.diff_option == CellDiffOption::Skip {
destination
} else {
osc8_hyperlink_parts(cell.symbol()).map(|(url, _)| url)
};
if link != destination {
if column > point.x {
break;
}
start = column;
destination = link;
}
// A wide glyph carries the link on its first column only.
column = column.saturating_add(cell.cell_width().max(1)).min(right);
}
destination.map(|_| Rect::new(start, point.y, column - start, /*height*/ 1))
}
pub(super) fn paint_hover_foreground(area: Rect, buf: &mut Buffer) {
for point in area.intersection(buf.area).positions() {
let cell = &mut buf[point];
if !cell.modifier.contains(Modifier::REVERSED)
&& let Some(foreground) = crate::style::hover_foreground(cell.fg, cell.bg)
{
cell.set_fg(foreground);
}
}
}
+8
View File
@@ -326,6 +326,7 @@ impl App {
let sidebar_image_protocol = self.preview_image_protocol(tui);
let sidebar_motion =
MotionMode::from_animations_enabled(self.local_settings.tui.animations);
let hover_mouse = self.hover_pointer(tui, tui.link_hover.mouse);
// An input refresh opens a fresh terminal buffer. Carry the outer owner's
// completed cells through it when only the conversation is being redrawn.
let backdrop = caller_inset.map(|_| {
@@ -627,8 +628,15 @@ impl App {
frame.cursor_position = None;
self.conversation_frame.close_buttons = buttons;
}
if let Some(area) = self.hover_target_area(hover_mouse, frame.buffer) {
super::link_hover::paint_hover_foreground(area, frame.buffer);
}
self.paint_centered_dialog(frame);
})?;
// A shortcut HUD is painted after the conversation and can move over a stationary pointer.
if hover_mouse != self.hover_pointer(tui, tui.link_hover.mouse) {
frames.schedule_frame();
}
self.finish_conversation(tui, painted)
}
@@ -2339,6 +2339,134 @@ async fn fullscreen_composer_remote_images_leave_an_editable_prompt_row() -> Res
Ok(())
}
#[tokio::test]
async fn clickable_transcript_foreground_follows_mouse_motion() -> Result<()> {
use ratatui::style::Stylize;
let mut app = crate::app::test_support::make_test_app().await;
let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
tui.set_owned_screen(/*owned*/ true)?;
let size = tui.terminal.size()?;
app.local_settings.tui.collapse_turn_details = true;
app.transcript_cells = vec![
user_cell("See https://prompt.example/ for context."),
Arc::new(history_cell::AgentMarkdownCell::new(
"Plain text. [guide](https://example.com/docs) [日本語](https://example.com/ja).\n\n| Item |\n| --- |\n| [Table guide](https://example.com/table) |".into(),
app.config.cwd.as_path(),
)),
Arc::new(history_cell::empty_mcp_output()),
];
app.insert_history_cell(
&mut tui,
Box::new(history_cell::new_warning_event("Example warning".into())),
);
let mut status = HyperlinkLine::default();
status.push_span("PR #42".underlined(), Some("https://example.com/pull/42"));
app.chat_widget.set_status_line(Some(status.into()));
app.chat_widget
.apply_external_edit("https://draft.example/".into());
crate::chatwidget::tests::helpers::set_active_cell(
&mut app.chat_widget,
Box::new(crate::exec_cell::new_active_exec_command(
"hover".into(),
vec!["printf sample".into()],
Vec::new(),
codex_app_server_protocol::CommandExecutionSource::Agent,
/*interaction_input*/ None,
/*animations_enabled*/ false,
)),
);
let render = |app: &mut App, tui: &mut tui::Tui| -> Result<Buffer> {
crate::terminal_palette::with_test_default_colors(/*colors*/ None, || {
app.render_owned_transcript(tui, size)
})?;
Ok(tui.terminal.previous_buffer().clone())
};
let rest = render(&mut app, &mut tui)?;
let text = crate::terminal_hyperlinks::strip_osc8(&buffer_text(&rest));
let position = |label| {
text.lines()
.enumerate()
.find_map(|(row, line)| {
let (prefix, _) = line.split_once(label)?;
Some(ratatui::layout::Position::new(
prefix.chars().count() as u16,
row as u16,
))
})
.unwrap_or_else(|| panic!("Missing {label} in {text}"))
};
let plain = position("Plain text");
let draft = position("https://draft.example/");
let move_to = |point: ratatui::layout::Position, modifiers| {
TuiEvent::Mouse(MouseEvent {
kind: MouseEventKind::Moved,
column: point.x,
row: point.y,
modifiers,
})
};
app.handle_tui_event(&mut tui, &mut server, move_to(draft, KeyModifiers::NONE))
.await?;
assert_eq!(render(&mut app, &mut tui)?[draft], rest[draft]);
let mut styles = Vec::new();
let mut redraws = crate::tui::test_support::record_frame_requests(&mut tui);
for (label, columns, modifiers) in [
("https://prompt.example/", 1, KeyModifiers::NONE),
("guide", 1, KeyModifiers::NONE),
("日本語", 6, KeyModifiers::NONE),
("Table guide", 1, KeyModifiers::NONE),
("MCP docs", 1, KeyModifiers::NONE),
("Running", 1, KeyModifiers::NONE),
("1 warning", 1, KeyModifiers::NONE),
("1 warning", 1, KeyModifiers::CONTROL),
("PR #42", 1, KeyModifiers::NONE),
] {
let start = position(if label == "日本語" { "日" } else { label });
let area = Rect::new(start.x, start.y, columns, /*height*/ 1);
// KeyModifiers displays Control differently across platforms.
let name = if modifiers == KeyModifiers::CONTROL {
format!("{label} (Ctrl)")
} else {
label.to_string()
};
for pointer in area.positions() {
while redraws.try_recv().is_ok() {}
app.handle_tui_event(&mut tui, &mut server, move_to(pointer, modifiers))
.await?;
assert!(redraws.try_recv().is_ok(), "enter {name} at {pointer:?}");
let hover = render(&mut app, &mut tui)?;
assert_eq!(hover[plain], rest[plain]);
for target in area.positions() {
assert_ne!(hover[target].fg, rest[target].fg, "{name} at {pointer:?}");
let mut hovered_cell = hover[target].clone();
hovered_cell.fg = rest[target].fg;
assert_eq!(hovered_cell, rest[target]);
}
if pointer == start {
styles.push(format!(
"{name}: {:?} → {:?}",
rest[start].fg, hover[start].fg
));
}
app.handle_tui_event(&mut tui, &mut server, move_to(plain, KeyModifiers::NONE))
.await?;
let leave = render(&mut app, &mut tui)?;
for target in area.positions() {
assert_eq!(leave[target], rest[target]);
}
}
}
insta::assert_snapshot!(styles.join("\n"));
server.shutdown().await?;
tui.set_owned_screen(/*owned*/ false)?;
Ok(())
}
#[tokio::test]
async fn footer_selection_defers_warning_clicks_and_preserves_the_draft() -> Result<()> {
let mut app = crate::app::test_support::make_test_app().await;
@@ -0,0 +1,14 @@
---
source: tui/src/app/owned_transcript_tests.rs
assertion_line: 2464
expression: "styles.join(\"\\n\")"
---
https://prompt.example/: Rgb(238, 238, 238) → Rgb(121, 180, 249)
guide: Rgb(137, 180, 250) → Reset
日本語: Rgb(137, 180, 250) → Reset
Table guide: Rgb(137, 180, 250) → Reset
MCP docs: Reset → Rgb(99, 168, 248)
Running: Reset → Rgb(99, 168, 248)
1 warning: Rgb(196, 167, 103) → Rgb(99, 168, 248)
1 warning (Ctrl): Rgb(196, 167, 103) → Rgb(99, 168, 248)
PR #42: Reset → Rgb(99, 168, 248)
+1 -28
View File
@@ -388,7 +388,6 @@ use super::footer::render_footer_hint_items;
use super::footer::render_footer_line;
use super::footer::reset_mode_after_activity;
use super::footer::side_conversation_context_line;
use super::footer::status_line_right_indicator_line;
use super::footer::toggle_shortcut_mode;
use super::mention_usage::MentionUsage;
use super::mentions_v2::MentionV2Popup;
@@ -824,6 +823,7 @@ impl ChatComposer {
ide_context_active: false,
status_line_value: None,
labeled_status_line: Default::default(),
status_line_area: std::cell::Cell::default(),
status_line_enabled: false,
side_conversation_context_label: None,
active_agent_label: None,
@@ -1532,29 +1532,6 @@ impl ChatComposer {
self.draft.textarea.vim_mode_indicator_span()
}
fn mode_indicator_line(&self, show_cycle_hint: bool) -> Option<Line<'static>> {
let mut spans: Vec<Span<'static>> = Vec::new();
if let Some(vim_mode) = self.vim_mode_indicator_span() {
spans.push(vim_mode);
}
if let Some(indicators) = status_line_right_indicator_line(
self.footer.collaboration_mode_indicator,
self.footer.goal_status_indicator.as_ref(),
self.footer.ide_context_active,
show_cycle_hint,
) {
if !spans.is_empty() {
spans.push(" | ".dim());
}
spans.extend(indicators.spans);
}
if spans.is_empty() {
None
} else {
Some(Line::from(spans))
}
}
fn right_footer_line_with_context(&self) -> Line<'static> {
let mut line = if self.footer.context_window_hidden {
Line::default()
@@ -1963,10 +1940,6 @@ impl ChatComposer {
self.attachments.local_image_paths()
}
pub(crate) fn status_line_content(&self) -> Option<&StatusLine> {
self.footer.status_line_value.as_ref()
}
pub(crate) fn local_images(&self) -> &[LocalImageAttachment] {
&self.attachments.local_images
}
@@ -418,6 +418,7 @@ pub(super) struct FooterState {
pub(super) ide_context_active: bool,
pub(super) status_line_value: Option<StatusLine>,
pub(super) labeled_status_line: std::cell::RefCell<Option<StatusLine>>,
pub(super) status_line_area: std::cell::Cell<Rect>,
pub(super) status_line_enabled: bool,
pub(super) side_conversation_context_label: Option<String>,
pub(super) active_agent_label: Option<String>,
@@ -3,9 +3,41 @@
//! Shortcut help grows above the input, preserving status and close rows below it even when clipped.
use super::*;
use crate::bottom_pane::footer::status_line_right_indicator_line;
use ratatui::layout::Size;
impl ChatComposer {
pub(crate) fn status_line_area(&self) -> Rect {
self.footer.status_line_area.get()
}
pub(crate) fn status_line_content(&self) -> Option<&StatusLine> {
self.footer.status_line_value.as_ref()
}
pub(super) fn mode_indicator_line(&self, show_cycle_hint: bool) -> Option<Line<'static>> {
let mut spans: Vec<Span<'static>> = Vec::new();
if let Some(vim_mode) = self.vim_mode_indicator_span() {
spans.push(vim_mode);
}
if let Some(indicators) = status_line_right_indicator_line(
self.footer.collaboration_mode_indicator,
self.footer.goal_status_indicator.as_ref(),
self.footer.ide_context_active,
show_cycle_hint,
) {
if !spans.is_empty() {
spans.push(" | ".dim());
}
spans.extend(indicators.spans);
}
if spans.is_empty() {
None
} else {
Some(Line::from(spans))
}
}
pub(crate) fn set_status_line(&mut self, status_line: Option<StatusLine>) -> bool {
if self.footer.status_line_value == status_line {
return false;
@@ -101,6 +133,7 @@ impl ChatComposer {
buf: &mut Buffer,
options: ComposerRenderOptions<'_>,
) {
self.footer.status_line_area.set(area);
if area.is_empty() {
return;
}
@@ -13,11 +13,8 @@ pub(super) fn before_warning(mut area: Rect, warning: Option<Rect>) -> Rect {
}
impl ChatComposer {
pub(crate) fn warning_notice_contains(&self, position: ratatui::layout::Position) -> bool {
self.footer
.warning_notice_area
.get()
.is_some_and(|area| area.contains(position))
pub(crate) fn warning_notice_area(&self) -> Option<Rect> {
self.footer.warning_notice_area.get()
}
pub(super) fn show_warning_notice(&self, options: ComposerRenderOptions<'_>) -> bool {
+4
View File
@@ -1414,6 +1414,10 @@ impl BottomPane {
self.status_widget().is_some()
}
pub(crate) fn status_line_area(&self) -> ratatui::layout::Rect {
self.composer.status_line_area()
}
pub(crate) fn status_line_content(&self) -> Option<&StatusLine> {
self.composer.status_line_content()
}
+4 -2
View File
@@ -33,7 +33,9 @@ impl BottomPane {
self.active_warning().is_some()
}
pub(crate) fn warning_notice_contains(&self, position: ratatui::layout::Position) -> bool {
self.no_modal_or_popup_active() && self.composer.warning_notice_contains(position)
pub(crate) fn warning_notice_area(&self) -> Option<ratatui::layout::Rect> {
self.composer
.warning_notice_area()
.filter(|_| self.no_modal_or_popup_active())
}
}
@@ -84,6 +84,10 @@ impl ChatWidget {
)
}
pub(crate) fn status_line_area(&self) -> ratatui::layout::Rect {
self.bottom_pane.status_line_area()
}
/// Sets the currently rendered footer status-line value.
pub(crate) fn set_status_line(&mut self, status_line: Option<StatusLine>) {
self.bottom_pane.set_status_line(status_line);
+11 -2
View File
@@ -131,6 +131,15 @@ impl super::ChatWidget {
state.synced_cells = Some(cells.to_vec());
}
pub(crate) fn warning_notice_at(
&self,
point: ratatui::layout::Position,
) -> Option<ratatui::layout::Rect> {
self.bottom_pane
.warning_notice_area()
.filter(|area| area.contains(point))
}
pub(crate) fn handle_warning_event(
&mut self,
event: &TuiEvent,
@@ -170,8 +179,8 @@ impl super::ChatWidget {
&& mouse.kind
== crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left)
&& self
.bottom_pane
.warning_notice_contains(ratatui::layout::Position::new(mouse.column, mouse.row))
.warning_notice_at((mouse.column, mouse.row).into())
.is_some()
{
self.open_warnings(cells);
return true;
+1 -1
View File
@@ -57,7 +57,7 @@ mod cursor;
#[path = "custom_terminal_test_support.rs"]
pub(crate) mod test_support;
fn osc8_hyperlink_parts(symbol: &str) -> Option<(&str, &str)> {
pub(crate) fn osc8_hyperlink_parts(symbol: &str) -> Option<(&str, &str)> {
let content = symbol.strip_prefix("\x1b]8;;")?;
let destination_end = content.find('\x07')?;
let destination = &content[..destination_end];
+27 -3
View File
@@ -5,6 +5,7 @@ mod contrast;
use crate::color::blend;
use crate::color::is_light;
use crate::color::perceptual_distance;
use crate::terminal_palette::StdoutColorLevel;
use crate::terminal_palette::best_color;
use crate::terminal_palette::default_bg;
@@ -160,13 +161,36 @@ pub(crate) fn accent_color_on(background: Option<Color>) -> Color {
accent_color_for(background_rgb(background))
}
/// Use the terminal foreground when the control is already close to the hover accent.
pub(crate) fn hover_foreground(foreground: Color, background: Color) -> Option<Color> {
let background = Some(background);
let accent = accent_color_on(background);
if accent == Color::Reset {
return None;
}
let near_accent = explicit_rgb(foreground)
.zip(explicit_rgb(accent))
.is_some_and(|(foreground, accent)| perceptual_distance(foreground, accent) < 20.0);
Some(if foreground == accent || near_accent {
readable_color_on(Color::Reset, background)
} else {
accent
})
}
fn background_rgb(background: Option<Color>) -> Option<(u8, u8, u8)> {
match background {
Some(Color::Rgb(r, g, b)) => Some((r, g, b)),
Some(Color::Indexed(index)) if index >= 16 => {
None | Some(Color::Reset) => default_bg(),
Some(color) => explicit_rgb(color),
}
}
fn explicit_rgb(color: Color) -> Option<(u8, u8, u8)> {
match color {
Color::Rgb(r, g, b) => Some((r, g, b)),
Color::Indexed(index) if index >= 16 => {
Some(crate::terminal_palette::XTERM_COLORS[usize::from(index)])
}
None | Some(Color::Reset) => default_bg(),
_ => None,
}
}
+27 -14
View File
@@ -180,28 +180,41 @@ impl TranscriptView {
}
}
pub(crate) fn disclosure_hover_area(&self, point: ratatui::layout::Position) -> Option<Rect> {
self.disclosure_at(point).map(|(_, area)| area)
}
fn disclosure_at(&self, point: ratatui::layout::Position) -> Option<(&VisibleRow, Rect)> {
if self.is_select_mode()
|| self.detailed
|| self.mode != HistoryRenderMode::Rich
|| !self.area.contains(point)
{
return None;
}
let visible = self.visible.get(usize::from(point.y - self.area.y))?;
if visible.layout.disclosure_row() != Some(visible.row) {
return None;
}
let columns = visible.layout.disclosure_columns();
let area = Rect::new(
self.area.x + columns.start,
point.y,
columns.end - columns.start,
/*height*/ 1,
);
area.contains(point).then_some((visible, area))
}
pub(super) fn toggle_disclosure_at(
&mut self,
cells: &[Arc<dyn HistoryCell>],
column: u16,
row: u16,
) -> bool {
if self.detailed || self.mode != HistoryRenderMode::Rich {
return false;
}
let Some(visible) = row
.checked_sub(self.area.y)
.and_then(|row| self.visible.get(usize::from(row)))
else {
let Some((visible, _)) = self.disclosure_at((column, row).into()) else {
return false;
};
if visible.layout.disclosure_row() != Some(visible.row)
|| column
.checked_sub(self.area.x)
.is_none_or(|column| !visible.layout.disclosure_columns().contains(&column))
{
return false;
}
let Some(index) = self.canonical_activity_index(cells, &visible.activity_ids) else {
return false;
};
+1 -4
View File
@@ -608,10 +608,7 @@ impl TranscriptView {
mode: MouseMode<'_>,
) -> Option<ViewAction> {
let was_following = self.is_following();
if !self.is_select_mode()
&& event.modifiers.is_empty()
&& self.toggle_disclosure_at(cells, event.column, event.row)
{
if event.modifiers.is_empty() && self.toggle_disclosure_at(cells, event.column, event.row) {
// Keep the patch anchored for inspection, then follow again when the prompt closes.
if was_following && let MouseMode::Selection(Some((thread_id, id))) = mode {
self.disclosure.resume_after_approval = Some((thread_id, id.to_owned()));