Files
codex/codex-rs/tui/src/sidebar_tab_strip.rs
T
Felipe Coury 06023b038f Add mouse controls to hide and restore the TUI sidebar (#64055)
## Why

Hiding the sidebar through keyboard navigation leaves no visible control
for reopening it.

## What changed

Click `›` in the sidebar tab strip to hide the sidebar and return focus
to the parent conversation. Click `‹` above the main transcript to
restore the previous sidebar content. The restore control also appears
when the terminal is too narrow to show the sidebar alongside the main
conversation.

## Testing

Cover hiding and restoring side chats and document previews with the
mouse, including reopening a side chat at narrow terminal widths and
preserving the parent conversation's reading position.

GitOrigin-RevId: 1f0f893b736b61c109e0761abbe3bde3d388e1b6
2026-11-26 02:35:01 +00:00

206 lines
7.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Draw sidebar tabs and hit-test the same visible cells. When space runs out,
//! keep the selected tab visible and reserve the add and hide buttons at the right edge.
//! A one-column gap separates tabs and is excluded from their click targets.
use ratatui::buffer::Buffer;
use ratatui::layout::Position;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::widgets::Widget;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SidebarTab {
Chat(codex_protocol::ThreadId),
Document(uuid::Uuid),
Subagents,
}
/// Adjacent tabs have one unpainted, noninteractive terminal column between them.
const TAB_GAP: u16 = 1;
const CLOSE_WIDTH: u16 = 3;
const MIN_TAB_WIDTH: u16 = 12;
const MAX_TAB_WIDTH: u16 = 22;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum TabState {
Absent,
Active,
Inactive,
}
#[derive(Clone, Copy)]
pub(crate) struct TabStrip<'a, Id> {
pub(crate) ids: &'a [Id],
pub(crate) selected: Option<Id>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TabTarget<Id> {
Tab(Id),
Close(Id),
Overflow { tab: Id, glyph: &'static str },
Add,
Hide,
}
impl<'a, Id: Copy + Eq> TabStrip<'a, Id> {
pub(crate) fn selected_index(self) -> Option<usize> {
self.ids.iter().position(|id| Some(*id) == self.selected)
}
pub(crate) fn strip_targets(self, area: Rect) -> Vec<(TabTarget<Id>, Rect)> {
let width = area.width.saturating_sub(1);
let hide = width.min(3);
let plus = (width - hide).min(3);
let available = width - plus - hide;
let height = area.height.min(1);
let rect = |offset, width| Rect::new(area.x + offset, area.y, width, height);
let mut targets = Vec::new();
if available > 0 && !self.ids.is_empty() {
let preferred = (area.width / 4).clamp(MIN_TAB_WIDTH, MAX_TAB_WIDTH);
let stride = preferred + TAB_GAP;
let overflow = self.ids.len() > usize::from(((available + TAB_GAP) / stride).max(1));
// Tiny strips spend their space on the title.
let gutter = if overflow && available >= 7 { 2 } else { 0 };
let space = available - 2 * gutter;
let width = space.min(preferred);
let count = usize::from((space + TAB_GAP) / (width + TAB_GAP)).min(self.ids.len());
// Use spare space for the extra close cells without reducing the number of tabs.
let room_per_tab = (space + TAB_GAP) / count as u16 - TAB_GAP;
let width = (width + CLOSE_WIDTH - 1).min(room_per_tab);
let start = self
.selected_index()
.map_or(0, |index| (index + 1).saturating_sub(count));
let end = start + count;
if gutter > 0 && start > 0 {
targets.push((
TabTarget::Overflow {
tab: self.ids[start - 1],
glyph: "‹",
},
rect(0, 1),
));
}
let mut x = gutter;
for &id in &self.ids[start..end] {
let close = CLOSE_WIDTH * u16::from(width > CLOSE_WIDTH);
let label = width - close;
targets.push((TabTarget::Tab(id), rect(x, label)));
if close > 0 {
targets.push((TabTarget::Close(id), rect(x + label, close)));
}
x += width + TAB_GAP;
}
if gutter > 0 && end < self.ids.len() {
targets.push((
TabTarget::Overflow {
tab: self.ids[end],
glyph: "›",
},
rect(available - 1, 1),
));
}
}
if plus > 0 {
targets.push((TabTarget::Add, rect(available, plus)));
}
if hide > 0 {
targets.push((TabTarget::Hide, rect(available + plus, hide)));
}
targets
}
pub(crate) fn render(
self,
area: Rect,
buf: &mut Buffer,
add_state: TabState,
selected_state: TabState,
pointer: Option<Position>,
paint: impl Fn(Id, Rect, Style, &mut Buffer),
) -> Vec<(TabTarget<Id>, Rect)> {
let style = |state| match state {
TabState::Active => crate::style::selection_style().not_underlined(),
TabState::Inactive => crate::style::quiet_workspace_style(),
TabState::Absent => crate::style::secondary_text_style()
.bg(Color::Reset)
.not_underlined()
.not_reversed(),
};
let rest = style(TabState::Absent).patch(crate::style::user_message_style());
let rest = rest.fg(crate::style::readable_color_on(
rest.fg.unwrap_or(Color::Reset),
rest.bg,
));
let hover = crate::style::hover_style(style(TabState::Absent).fg(Color::Reset));
let selected_style = match selected_state {
TabState::Active => style(selected_state),
TabState::Inactive => hover.bold(),
TabState::Absent => rest,
};
let targets = self.strip_targets(area);
for &(target, rect) in &targets {
let hovered = pointer.is_some_and(|point| rect.contains(point));
let tab_style = |id| match (
Some(id) == self.selected && selected_state != TabState::Absent,
hovered,
) {
(true, true) => crate::style::hover_style(selected_style),
(true, false) => selected_style,
(false, true) => hover,
(false, false) => rest,
};
match target {
TabTarget::Tab(id) => paint(id, rect, tab_style(id), buf),
TabTarget::Close(id) => {
Line::from(" × ").style(tab_style(id)).render(rect, buf);
}
TabTarget::Overflow { glyph, .. } => {
let arrow = if hovered {
hover
} else {
style(TabState::Absent)
};
Line::from(glyph).style(arrow).render(rect, buf)
}
TabTarget::Add => {
let add = match (hovered, add_state) {
(true, TabState::Active) => crate::style::hover_style(style(add_state)),
(true, TabState::Inactive | TabState::Absent) => hover,
(false, _) => style(add_state),
};
paint_control("+", rect, add, buf);
}
TabTarget::Hide => paint_toggle("›", rect, hovered, buf),
}
}
targets
}
}
/// Paint a sidebar visibility control using the same style in open and collapsed layouts.
pub(crate) fn paint_toggle(glyph: &str, area: Rect, hovered: bool, buf: &mut Buffer) {
let normal = crate::style::secondary_text_style()
.bg(Color::Reset)
.not_underlined()
.not_reversed();
let style = if hovered {
crate::style::hover_style(normal.fg(Color::Reset))
} else {
normal
};
paint_control(glyph, area, style, buf);
}
fn paint_control(glyph: &str, area: Rect, style: Style, buf: &mut Buffer) {
let text = if area.width >= 3 {
format!(" {glyph} ")
} else {
glyph.to_owned()
};
Line::from(text).style(style).render(area, buf);
}