mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Add read-only document previews to the TUI sidebar (#61888)
## What changed - Open transcript file links in sidebar tabs when the TUI workspace is enabled, resolving and reading files through the focused thread's app server and rendering their contents as Markdown. - Support pager keys, mouse scrolling, switching between document and chat tabs, and closing previews with `Esc`. Reuse existing previews and allow failed opens to be retried. - Show loading and error states, and prevent late document results from taking focus back after the user switches views. - Adapt tab widths to the available space, preserve filename extensions in truncated labels, and keep divider dragging aligned to exact terminal columns. ## Testing Add coverage for workspace-gated link opening, loading and error states, retrying, custom pager bindings, multiple previews, tab reuse, closing during loading, and preserving focus when results arrive late. Update tab and divider assertions and snapshots. GitOrigin-RevId: e979b757c623c29a8ed50277ec929655d783ad18
This commit is contained in:
@@ -285,6 +285,7 @@ mod side_focus;
|
||||
mod side_pane;
|
||||
mod sidebar;
|
||||
mod sidebar_close;
|
||||
mod sidebar_document;
|
||||
pub(crate) mod sidebar_loading;
|
||||
mod startup;
|
||||
pub(crate) mod startup_bootstrap;
|
||||
@@ -870,6 +871,7 @@ impl App {
|
||||
app_server: &mut AppServerSession,
|
||||
event: TuiEvent,
|
||||
) -> Result<AppRunControl> {
|
||||
self.transcript_view.document_links = self.document_preview_available(tui, app_server);
|
||||
self.transcript_view
|
||||
.sync_patch_approval(self.chat_widget.current_patch_approval());
|
||||
// A chooser action must never decide a modal that has not been painted yet.
|
||||
|
||||
@@ -96,6 +96,9 @@ impl App {
|
||||
| AppEvent::SideChatArchived { .. }
|
||||
| AppEvent::SavedChatPrepared { .. } | AppEvent::SavedChatReleased { .. }
|
||||
| AppEvent::StartQueuedSavedChat
|
||||
| AppEvent::OpenSidebarDocument { .. }
|
||||
| AppEvent::SidebarDocumentResolved { .. }
|
||||
| AppEvent::SidebarDocumentLoaded { .. }
|
||||
| AppEvent::SavedSideChatsLoaded { .. }
|
||||
| AppEvent::SideReplayFinished(_)
|
||||
| AppEvent::ResetTranscriptForThreadSwitch
|
||||
@@ -1036,6 +1039,7 @@ impl App {
|
||||
self.transcript_view.replace_group(cells, range.clone(), &replacement);
|
||||
cells.splice(range, [replacement]);
|
||||
}
|
||||
self.transcript_view.document_links = false;
|
||||
self.side_companions.insert(thread_id, SideCompanion {
|
||||
display,
|
||||
parked_tail: self.transcript_cells.len(),
|
||||
@@ -3579,6 +3583,42 @@ impl App {
|
||||
Box::pin(self.open_saved_chat(tui, app_server, target)).await?;
|
||||
}
|
||||
}
|
||||
AppEvent::OpenSidebarDocument { backend_id, source, path } => {
|
||||
if app_server.backend_id() == backend_id
|
||||
&& self.current_displayed_thread_id() == Some(source)
|
||||
&& self.document_preview_available(tui, app_server)
|
||||
{
|
||||
Box::pin(self.open_document(tui, app_server, source, path)).await?;
|
||||
}
|
||||
}
|
||||
AppEvent::SidebarDocumentResolved { backend_id, parent, source, request, original_path, path, } => {
|
||||
if backend_id == app_server.backend_id()
|
||||
&& self.sidebar_parent() == Some(parent)
|
||||
&& let Some(pane) = self.sidebars.get_mut(&Some(parent))
|
||||
{
|
||||
// Late opens can add their tabs without taking back the user's focus.
|
||||
let requested = pane.focus_document_request == Some(request);
|
||||
if requested {
|
||||
pane.focus_document_request = None;
|
||||
}
|
||||
let focus = requested
|
||||
&& self.current_displayed_thread_id() == Some(source)
|
||||
&& self.document_preview_available(tui, app_server);
|
||||
match path {
|
||||
Ok(path) => Box::pin(self.open_resolved_document(tui, app_server, source, original_path, path, focus)).await?,
|
||||
Err(error) if focus => self.chat_widget.add_error_message(format!("Could not open file: {error}")),
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
AppEvent::SidebarDocumentLoaded { backend_id, parent, request, result } => {
|
||||
if let Some(doc) = self.sidebars.get_mut(&Some(parent)).and_then(|pane| pane.documents.get_mut(&request))
|
||||
&& doc.backend == backend_id
|
||||
{
|
||||
doc.loaded(result);
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
}
|
||||
AppEvent::SavedSideChatsLoaded { backend_id, parent, request, result } => {
|
||||
use crate::app_event::SavedSideChatData;
|
||||
use crate::sidebar_chooser::Discovery;
|
||||
|
||||
@@ -287,6 +287,16 @@ impl App {
|
||||
};
|
||||
return contexts.with_voice_toggle(&self.keymap);
|
||||
}
|
||||
if self.panel_focus() == Some(super::sidebar::PaneFocus::Sidebar)
|
||||
&& self.chat_widget.no_modal_or_popup_active()
|
||||
&& matches!(
|
||||
self.sidebar()
|
||||
.and_then(sidebar::SidebarPane::visible_content),
|
||||
Some(sidebar::SidebarContent::Document(_))
|
||||
)
|
||||
{
|
||||
return KeymapContextSet::new(KeymapContext::Pager).with_voice_toggle(&self.keymap);
|
||||
}
|
||||
if self.transcript_view.is_search_editing() && self.chat_widget.no_modal_or_popup_active() {
|
||||
return KeymapContextSet::new(KeymapContext::Editor).with_voice_toggle(&self.keymap);
|
||||
}
|
||||
|
||||
@@ -297,7 +297,14 @@ impl App {
|
||||
}
|
||||
let highlight_divider =
|
||||
self.pane_resize.is_some() || tui.link_hover.shape == tui::PointerShape::Resize;
|
||||
let hovered_target = self.hovered_sidebar_target(tui.link_hover.position());
|
||||
let pointer = tui
|
||||
.link_hover
|
||||
.mouse
|
||||
.filter(|mouse| self.pane_resize.is_none() && !tui.shortcut_hud_contains(*mouse))
|
||||
.map(|mouse| (mouse.column, mouse.row).into());
|
||||
let sidebar_parent = self.sidebar_parent();
|
||||
let sidebar_motion =
|
||||
MotionMode::from_animations_enabled(self.local_settings.tui.animations);
|
||||
// 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(|_| {
|
||||
@@ -349,8 +356,20 @@ impl App {
|
||||
}
|
||||
Some(sidebar::SidebarLayout::Panel { sidebar, focus, .. }) => {
|
||||
let focused = *focus == PaneFocus::Sidebar;
|
||||
if focused {
|
||||
frame.cursor_position = None;
|
||||
}
|
||||
let resize_hint = self.pane_resize.map(|_| side_pane::RESIZE_HINT);
|
||||
if let Some(creation) = self.sidebar().and_then(|pane| pane.creation.as_ref()) {
|
||||
if let Some(doc) = self
|
||||
.sidebars
|
||||
.get_mut(&sidebar_parent)
|
||||
.and_then(|pane| pane.active_document())
|
||||
{
|
||||
let [_, body] = side_pane::tab_and_body(*sidebar);
|
||||
doc.paint(body, frame.buffer, sidebar_motion, &frames, resize_hint);
|
||||
} else if let Some(creation) =
|
||||
self.sidebar().and_then(|pane| pane.creation.as_ref())
|
||||
{
|
||||
use sidebar::CreationState;
|
||||
ratatui::widgets::Clear.render(*sidebar, frame.buffer);
|
||||
let mut body = sidebar.inner(ratatui::layout::Margin::new(1, 1));
|
||||
@@ -422,26 +441,53 @@ impl App {
|
||||
frames: &frames,
|
||||
},
|
||||
);
|
||||
let chooser = if focused {
|
||||
}
|
||||
if let Some(pane) = self.sidebar()
|
||||
&& (pane.creation.is_none()
|
||||
|| matches!(
|
||||
pane.visible_content(),
|
||||
Some(sidebar::SidebarContent::Document(_))
|
||||
))
|
||||
{
|
||||
let document = matches!(
|
||||
pane.visible_content(),
|
||||
Some(sidebar::SidebarContent::Document(_))
|
||||
);
|
||||
let chooser = if document {
|
||||
TabState::Absent
|
||||
} else if focused {
|
||||
TabState::Active
|
||||
} else {
|
||||
TabState::Inactive
|
||||
};
|
||||
if document {
|
||||
ratatui::widgets::Clear
|
||||
.render(side_pane::tab_and_body(*sidebar)[0], frame.buffer);
|
||||
}
|
||||
crate::sidebar_chooser::tab_strip(
|
||||
*sidebar,
|
||||
chooser,
|
||||
TabState::Absent,
|
||||
tabs,
|
||||
hovered_target,
|
||||
if document && focused {
|
||||
TabState::Active
|
||||
} else if document {
|
||||
TabState::Inactive
|
||||
} else {
|
||||
TabState::Absent
|
||||
},
|
||||
crate::sidebar_chooser::ChatTabs {
|
||||
label_style,
|
||||
..pane.chat_tabs()
|
||||
},
|
||||
pointer
|
||||
.and_then(|point| pane.chat_tabs().strip().hit(*sidebar, point))
|
||||
.map(|(target, _)| target),
|
||||
|id| pane.document_title(id),
|
||||
frame.buffer,
|
||||
);
|
||||
}
|
||||
frame
|
||||
.image_placements
|
||||
.retain(|image| !sidebar.intersects(image.area));
|
||||
if focused {
|
||||
frame.cursor_position = None;
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
@@ -456,7 +502,10 @@ impl App {
|
||||
label_style,
|
||||
..pane.chat_tabs()
|
||||
},
|
||||
hovered_target,
|
||||
pointer
|
||||
.and_then(|point| pane.chat_tabs().strip().hit(strip, point))
|
||||
.map(|(target, _)| target),
|
||||
|id| pane.document_title(id),
|
||||
frame.buffer,
|
||||
);
|
||||
}
|
||||
@@ -1193,7 +1242,19 @@ impl App {
|
||||
}
|
||||
}
|
||||
} else if let ViewAction::OpenLink(url) = action {
|
||||
self.open_url_in_browser(url);
|
||||
if url.starts_with("file://")
|
||||
|| crate::terminal_hyperlinks::web_destination(&url).is_some()
|
||||
{
|
||||
self.open_url_in_browser(url);
|
||||
} else if self.transcript_view.document_links
|
||||
&& let Some(source) = self.current_displayed_thread_id()
|
||||
{
|
||||
self.app_event_tx.send(AppEvent::OpenSidebarDocument {
|
||||
backend_id: app_server.backend_id(),
|
||||
source,
|
||||
path: codex_app_server_client::AppServerPath::from_app_server(url),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if was_select_mode && !self.transcript_view.is_select_mode() {
|
||||
|
||||
@@ -414,6 +414,12 @@ impl App {
|
||||
self.app_event_tx = AppEventSender::new(tx);
|
||||
self.app_server_backend_id = Some(session.backend_id());
|
||||
*app_event_rx = rx;
|
||||
for pane in self.sidebars.values_mut() {
|
||||
pane.focus_document_request = None;
|
||||
for doc in pane.documents.values_mut() {
|
||||
doc.connection_replaced();
|
||||
}
|
||||
}
|
||||
// The old timer targets the discarded channel, so a new preview can be scheduled.
|
||||
self.config_notification_test_pending = None;
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::keymap::ListAction;
|
||||
use crate::sidebar_chooser;
|
||||
use crate::sidebar_chooser::ChooserAction;
|
||||
use crate::sidebar_chooser::Discovery;
|
||||
use crate::sidebar_tab_strip::SidebarTab;
|
||||
use crate::sidebar_tab_strip::TabTarget;
|
||||
use crate::transcript_view::TranscriptView;
|
||||
|
||||
@@ -19,7 +20,7 @@ impl App {
|
||||
pub(super) fn hovered_sidebar_target(
|
||||
&self,
|
||||
point: Option<ratatui::layout::Position>,
|
||||
) -> Option<TabTarget<ThreadId>> {
|
||||
) -> Option<TabTarget<SidebarTab>> {
|
||||
let point = point?;
|
||||
let strip = self.conversation_frame.sidebar_tab_strip()?;
|
||||
if self.pane_resize.is_some() || !strip.contains(point) {
|
||||
@@ -28,16 +29,7 @@ impl App {
|
||||
let pane = self.sidebar()?;
|
||||
let tabs = pane.chat_tabs().strip();
|
||||
let target = tabs.hit(strip, point)?.0;
|
||||
let available = if self.conversation_frame.chat_strip.is_some() {
|
||||
match target {
|
||||
TabTarget::Tab(id) | TabTarget::Overflow { tab: id, .. } => {
|
||||
tabs.selected != Some(id)
|
||||
}
|
||||
TabTarget::Add => true,
|
||||
}
|
||||
} else {
|
||||
pane.creation.is_none()
|
||||
};
|
||||
let available = self.chooser_focus().is_none() || pane.creation.is_none();
|
||||
available.then_some(target)
|
||||
}
|
||||
|
||||
@@ -103,12 +95,35 @@ impl App {
|
||||
});
|
||||
}
|
||||
|
||||
async fn activate_sidebar_tab(
|
||||
pub(super) async fn activate_sidebar_tab(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
id: ThreadId,
|
||||
tab: SidebarTab,
|
||||
) -> Result<()> {
|
||||
if let SidebarTab::Document(id) = tab {
|
||||
if let Some(parent) = self.active_side_parent_thread_id() {
|
||||
self.select_agent_thread(tui, server, parent).await?;
|
||||
if self.current_displayed_thread_id() != Some(parent) {
|
||||
return Ok(());
|
||||
}
|
||||
self.focus_returned_parent_composer(parent).await?;
|
||||
}
|
||||
if self
|
||||
.sidebar()
|
||||
.and_then(|pane| pane.documents.get(&id))
|
||||
.is_some_and(|doc| doc.backend == server.backend_id())
|
||||
{
|
||||
self.zoomed_thread = None;
|
||||
self.sidebar_mut()
|
||||
.show(SidebarContent::Document(id), PaneFocus::Sidebar);
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let SidebarTab::Chat(id) = tab else {
|
||||
unreachable!()
|
||||
};
|
||||
let current = self.current_displayed_thread_id();
|
||||
if current == Some(id) {
|
||||
return Ok(());
|
||||
@@ -121,13 +136,42 @@ impl App {
|
||||
result
|
||||
}
|
||||
|
||||
async fn step_sidebar_tab(
|
||||
pub(super) async fn close_sidebar_tab(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
tab: SidebarTab,
|
||||
) -> Result<()> {
|
||||
match tab {
|
||||
SidebarTab::Chat(id) => sidebar_close::request(self, tui, server, id, CloseTab).await,
|
||||
SidebarTab::Document(id) => {
|
||||
let pane = self.sidebar_mut();
|
||||
let active = pane.visible_content() == Some(SidebarContent::Document(id));
|
||||
pane.remove_document(id);
|
||||
if active {
|
||||
self.zoomed_thread = None;
|
||||
self.sidebar_mut()
|
||||
.show(SidebarContent::Chooser, PaneFocus::Sidebar);
|
||||
if let Some(previous) = self
|
||||
.sidebar()
|
||||
.and_then(|pane| pane.recent_tab(/*excluding*/ None))
|
||||
{
|
||||
self.activate_sidebar_tab(tui, server, previous).await?;
|
||||
}
|
||||
}
|
||||
tui.frame_requester().schedule_frame();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn step_sidebar_tab(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
step: isize,
|
||||
) -> Result<()> {
|
||||
if let Some(id) = self.sidebar().and_then(|pane| pane.adjacent_chat(step)) {
|
||||
if let Some(id) = self.sidebar().and_then(|pane| pane.adjacent_tab(step)) {
|
||||
self.activate_sidebar_tab(tui, server, id).await?;
|
||||
}
|
||||
Ok(())
|
||||
@@ -169,7 +213,7 @@ impl App {
|
||||
return true;
|
||||
}
|
||||
if let Some(pane) = pane
|
||||
&& (frame.chat_strip.is_some() || pane.creation.is_none())
|
||||
&& (self.chooser_focus().is_none() || pane.creation.is_none())
|
||||
&& let Some(strip) = frame.sidebar_tab_strip()
|
||||
&& strip.contains(point)
|
||||
{
|
||||
@@ -182,10 +226,9 @@ impl App {
|
||||
(MouseEventKind::ScrollDown, _) => {
|
||||
self.step_sidebar_tab(tui, server, /*step*/ 1).await
|
||||
}
|
||||
(_, Some((TabTarget::Tab(id), area)))
|
||||
if middle || sidebar_chooser::close_cell(area).contains(point) =>
|
||||
{
|
||||
sidebar_close::request(self, tui, server, id, CloseTab).await
|
||||
(_, Some((TabTarget::Close(id), _)))
|
||||
| (MouseEventKind::Down(MouseButton::Middle), Some((TabTarget::Tab(id), _))) => {
|
||||
self.close_sidebar_tab(tui, server, id).await
|
||||
}
|
||||
(_, Some((TabTarget::Add, _))) if !middle => {
|
||||
self.open_sidebar_tabs(tui, server).await
|
||||
@@ -203,6 +246,13 @@ impl App {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if matches!(
|
||||
self.sidebar()
|
||||
.and_then(sidebar::SidebarPane::visible_content),
|
||||
Some(SidebarContent::Document(_))
|
||||
) {
|
||||
return self.handle_document_input(tui, event);
|
||||
}
|
||||
if self.chooser_focus().is_none() {
|
||||
return false;
|
||||
}
|
||||
@@ -364,6 +414,12 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if matches!(action, ChooserAction::Back)
|
||||
&& let Some(tab @ SidebarTab::Document(_)) = pane.recent_tab(/*excluding*/ None)
|
||||
{
|
||||
if let Err(err) = self.activate_sidebar_tab(tui, server, tab).await {
|
||||
self.chat_widget.add_error_message(err.to_string());
|
||||
}
|
||||
} else {
|
||||
let target = match action {
|
||||
ChooserAction::Open(index) => pane.chat_tabs().choice(index),
|
||||
@@ -383,7 +439,7 @@ impl App {
|
||||
);
|
||||
return true;
|
||||
}
|
||||
let saved = !pane.chat_tabs().ids.contains(&id);
|
||||
let saved = !pane.chat_tabs().ids.contains(&SidebarTab::Chat(id));
|
||||
if saved && let Some(parent) = self.current_displayed_thread_id() {
|
||||
let target = super::sidebar_loading::Target {
|
||||
backend_id: server.backend_id(),
|
||||
@@ -432,13 +488,36 @@ impl App {
|
||||
key: KeyEvent,
|
||||
) -> bool {
|
||||
let bindings = &self.keymap.app;
|
||||
let close = self.current_displayed_thread_id().filter(|id| {
|
||||
bindings.close_sidebar_tab.is_pressed(key) && self.side_threads.contains_key(id)
|
||||
});
|
||||
let panel_focus = self.panel_focus();
|
||||
let document = if panel_focus == Some(PaneFocus::Sidebar) {
|
||||
self.sidebar()
|
||||
.and_then(sidebar::SidebarPane::visible_content)
|
||||
.and_then(|content| match content {
|
||||
SidebarContent::Document(id) => Some(id),
|
||||
_ => None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let close = if let Some(id) = document {
|
||||
(bindings.close_sidebar_tab.is_pressed(key)
|
||||
|| crate::key_hint::plain(KeyCode::Esc).is_press(key))
|
||||
.then_some(SidebarTab::Document(id))
|
||||
} else {
|
||||
self.current_displayed_thread_id()
|
||||
.filter(|id| {
|
||||
bindings.close_sidebar_tab.is_pressed(key) && self.side_threads.contains_key(id)
|
||||
})
|
||||
.map(SidebarTab::Chat)
|
||||
};
|
||||
let left = bindings.focus_pane_left.is_pressed(key);
|
||||
let tab_step = if bindings.sidebar_up.is_pressed(key) {
|
||||
let tab_step = if bindings.sidebar_up.is_pressed(key)
|
||||
|| (document.is_some() && crate::key_hint::plain(KeyCode::Left).is_press(key))
|
||||
{
|
||||
Some(-1)
|
||||
} else if bindings.sidebar_down.is_pressed(key) {
|
||||
} else if bindings.sidebar_down.is_pressed(key)
|
||||
|| (document.is_some() && crate::key_hint::plain(KeyCode::Right).is_press(key))
|
||||
{
|
||||
Some(1)
|
||||
} else {
|
||||
None
|
||||
@@ -449,9 +528,8 @@ impl App {
|
||||
bindings.focus_main_composer.is_pressed(key) && self.main_composer_shortcut_available();
|
||||
let sidebar = bindings.focus_sidebar_content.is_pressed(key);
|
||||
let tabs = bindings.focus_sidebar_tabs.is_pressed(key) && self.sidebar_chooser_available();
|
||||
let panel_focus = self.panel_focus();
|
||||
let hide = bindings.hide_pane.is_pressed(key)
|
||||
|| (panel_focus == Some(PaneFocus::Sidebar)
|
||||
|| (self.chooser_focus() == Some(PaneFocus::Sidebar)
|
||||
&& crate::key_hint::ctrl(KeyCode::Char('c')).is_press(key));
|
||||
let width_change = if bindings.move_divider_left.is_pressed(key) {
|
||||
2
|
||||
@@ -515,8 +593,8 @@ impl App {
|
||||
&& !on_child
|
||||
&& (self.transcript_view.has_active_interaction()
|
||||
|| self.backtrack.overlay_preview_active);
|
||||
let result = if let Some(id) = close {
|
||||
sidebar_close::request(self, tui, app_server, id, CloseTab).await
|
||||
let result = if let Some(tab) = close {
|
||||
self.close_sidebar_tab(tui, app_server, tab).await
|
||||
} else if let Some(step) = tab_step {
|
||||
self.step_sidebar_tab(tui, app_server, step).await
|
||||
} else if tabs {
|
||||
@@ -533,6 +611,7 @@ impl App {
|
||||
Ok(())
|
||||
} else if panel_focus.is_some() {
|
||||
if hide || (sidebar && panel_focus == Some(PaneFocus::Sidebar)) {
|
||||
self.zoomed_thread = None;
|
||||
self.focus_current_parent_view(tui);
|
||||
self.sidebar_mut().hide();
|
||||
} else {
|
||||
@@ -581,6 +660,7 @@ impl App {
|
||||
self.chat_widget.add_error_message(err.to_string());
|
||||
}
|
||||
let view_changed = leaves_transcript
|
||||
|| matches!(close, Some(SidebarTab::Document(_)))
|
||||
|| self.current_displayed_thread_id() != previous_thread
|
||||
|| ((main || hide) && panel_focus == Some(PaneFocus::Sidebar));
|
||||
let suppression = if view_changed && !direct_tab_switch {
|
||||
|
||||
@@ -167,7 +167,7 @@ pub(super) fn sidebar_areas(app: &App, tui: &Tui, area: Rect, fraction: f64) ->
|
||||
.filter(|drag| {
|
||||
!drag.guide_only && drag.matches(area, app) && app.conversation_focus_available(tui)
|
||||
})
|
||||
.and_then(|drag| drag.percent());
|
||||
.map(|drag| drag.width);
|
||||
let saved = app.local_settings.tui.sidebar_width_percent;
|
||||
let keyboard_width = app
|
||||
.pane_resize
|
||||
@@ -175,12 +175,13 @@ pub(super) fn sidebar_areas(app: &App, tui: &Tui, area: Rect, fraction: f64) ->
|
||||
.and_then(|mode| mode.preview_width);
|
||||
let child_width = match (keyboard_width, preview, app.exact_sidebar_width) {
|
||||
(Some(width), _, _) => sidebar_width(i32::from(width), area.width),
|
||||
(_, None, Some((total, width)))
|
||||
(None, Some(width), _) => width,
|
||||
(None, None, Some((total, width)))
|
||||
if total == area.width && saved == Some(width_percent(width, total)) =>
|
||||
{
|
||||
width
|
||||
}
|
||||
_ => preferred_width(area.width, preview.or(saved)),
|
||||
_ => preferred_width(area.width, saved),
|
||||
};
|
||||
// A newly focused composer needs a cell even on the first opening frame.
|
||||
let open = target.is_some();
|
||||
@@ -410,15 +411,22 @@ impl App {
|
||||
let Some(mut drag) = self.divider_drag.take() else {
|
||||
return Ok(false);
|
||||
};
|
||||
let screen = tui.prepare_draw_size()?.into();
|
||||
let screen = tui.terminal.last_known_screen_size.into();
|
||||
if drag.matches(screen, self) && visible_split(self, tui, screen).is_some() {
|
||||
let width = i32::from(drag.initial_width) + i32::from(drag.press)
|
||||
- i32::from(mouse.column);
|
||||
drag.width = sidebar_width(width, screen.width);
|
||||
let width = sidebar_width(width, screen.width);
|
||||
let changed = drag.width != width;
|
||||
drag.width = width;
|
||||
if mouse.kind == Drag(Left) {
|
||||
self.divider_drag = Some(drag);
|
||||
} else if let Some(percent) = drag.percent() {
|
||||
self.save_sidebar_width(percent).await;
|
||||
if !changed {
|
||||
return Ok(true);
|
||||
}
|
||||
} else if let Some(percent) = drag.percent()
|
||||
&& self.save_sidebar_width(percent).await
|
||||
{
|
||||
self.exact_sidebar_width = Some((screen.width, width));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -530,13 +538,9 @@ pub(super) fn paint_divider(
|
||||
) {
|
||||
// Draw after both panes registered their images. Clearing a guide to the left
|
||||
// of retained pixels can erase the rest of that terminal row.
|
||||
let guide = drag.filter(|drag| drag.guide_only).map_or(divider, |drag| {
|
||||
drag.screen.right()
|
||||
- drag.percent().map_or(drag.initial_width, |p| {
|
||||
preferred_width(drag.screen.width, Some(p))
|
||||
})
|
||||
- GAP
|
||||
});
|
||||
let guide = drag
|
||||
.filter(|drag| drag.guide_only)
|
||||
.map_or(divider, |drag| drag.screen.right() - drag.width - GAP);
|
||||
let style = if highlight_divider || drag.is_some() {
|
||||
crate::style::accent_style()
|
||||
} else {
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::app_server_session::SideChatStorage;
|
||||
use crate::app_server_session::side_chat_prompt_preview;
|
||||
use crate::sidebar_chooser::ChatTabs;
|
||||
use crate::sidebar_chooser::Discovery;
|
||||
use crate::sidebar_tab_strip::SidebarTab;
|
||||
use crate::tui::Tui;
|
||||
use codex_app_server_protocol::ServerNotification;
|
||||
use codex_app_server_protocol::ThreadItemPayload;
|
||||
@@ -26,6 +27,7 @@ pub(super) enum SidebarContent {
|
||||
#[default]
|
||||
Chooser,
|
||||
Chat(ThreadId),
|
||||
Document(uuid::Uuid),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -70,7 +72,7 @@ pub(super) enum ChooserSelection {
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) struct SidebarInputState {
|
||||
pub(super) revision: usize,
|
||||
pub(super) selected_chat: Option<ThreadId>,
|
||||
pub(super) selected_tab: Option<SidebarTab>,
|
||||
pub(super) choice: ChooserSelection,
|
||||
}
|
||||
|
||||
@@ -78,8 +80,10 @@ pub(super) struct SidebarInputState {
|
||||
pub(super) struct SidebarPane {
|
||||
pub(super) creation: Option<SideCreation>,
|
||||
pub(super) close: Option<super::sidebar_close::Confirmation>,
|
||||
pub(super) documents: HashMap<uuid::Uuid, super::sidebar_document::Document>,
|
||||
pub(super) focus_document_request: Option<uuid::Uuid>,
|
||||
content: SidebarContent,
|
||||
tabs: Vec<ThreadId>,
|
||||
tabs: Vec<SidebarTab>,
|
||||
// The chooser keeps the same order when an attached chat becomes a live tab.
|
||||
choices: Vec<SideChat>,
|
||||
// A snapshot of the visible rows at the last explicit chooser refresh.
|
||||
@@ -88,7 +92,7 @@ pub(super) struct SidebarPane {
|
||||
pub(super) discovery: Discovery,
|
||||
pub(super) closing_chats: HashMap<ThreadId, JoinHandle<()>>,
|
||||
// Visit order is independent of the order in which tabs appear.
|
||||
visits: Vec<ThreadId>,
|
||||
visits: Vec<SidebarTab>,
|
||||
pub(super) choice: usize,
|
||||
tabs_revision: usize,
|
||||
focus: Option<PaneFocus>,
|
||||
@@ -97,15 +101,20 @@ pub(super) struct SidebarPane {
|
||||
|
||||
impl SidebarPane {
|
||||
pub(super) fn show(&mut self, content: SidebarContent, focus: PaneFocus) {
|
||||
self.focus_document_request = None;
|
||||
match content {
|
||||
SidebarContent::Chat(id) => {
|
||||
self.add_tab(id);
|
||||
self.visits.retain(|visited| *visited != id);
|
||||
self.visits.push(id);
|
||||
self.visit(SidebarTab::Chat(id));
|
||||
}
|
||||
SidebarContent::Chooser if self.content != content || self.focus.is_none() => {
|
||||
self.reset_choice();
|
||||
}
|
||||
SidebarContent::Document(id) => {
|
||||
let tab = SidebarTab::Document(id);
|
||||
self.add_document_tab(id);
|
||||
self.visit(tab);
|
||||
}
|
||||
SidebarContent::Chooser => {}
|
||||
}
|
||||
if content != SidebarContent::Chooser || focus == PaneFocus::Main {
|
||||
@@ -123,7 +132,7 @@ impl SidebarPane {
|
||||
ids: &self.tabs,
|
||||
choices: &self.choices,
|
||||
latest_prompts: &self.latest_prompts,
|
||||
selected: self.selected_chat(),
|
||||
selected: self.visits.last().copied(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +149,7 @@ impl SidebarPane {
|
||||
let selection = self.chat_tabs().choice(self.choice);
|
||||
let keep_new = self.choice == 0 && !self.choices.is_empty();
|
||||
for chat in &mut self.choices {
|
||||
if !self.tabs.contains(&chat.id) {
|
||||
if !self.tabs.contains(&SidebarTab::Chat(chat.id)) {
|
||||
continue;
|
||||
}
|
||||
if let Some(saved) = chats.iter_mut().find(|saved| saved.id == chat.id) {
|
||||
@@ -172,19 +181,68 @@ impl SidebarPane {
|
||||
.unwrap_or(usize::from(!self.choices.is_empty()));
|
||||
}
|
||||
|
||||
pub(super) fn previous_chat(&self, target: ThreadId) -> Option<ThreadId> {
|
||||
self.visits.iter().rev().copied().find(|id| *id != target)
|
||||
fn visit(&mut self, tab: SidebarTab) {
|
||||
self.visits.retain(|visited| *visited != tab);
|
||||
self.visits.push(tab);
|
||||
}
|
||||
|
||||
pub(super) fn adjacent_chat(&self, step: isize) -> Option<ThreadId> {
|
||||
let selected = self.selected_chat()?;
|
||||
pub(super) fn recent_tab(&self, excluding: Option<SidebarTab>) -> Option<SidebarTab> {
|
||||
self.visits
|
||||
.iter()
|
||||
.rev()
|
||||
.copied()
|
||||
.find(|tab| Some(*tab) != excluding)
|
||||
}
|
||||
|
||||
pub(super) fn adjacent_tab(&self, step: isize) -> Option<SidebarTab> {
|
||||
let Some(selected) = self.recent_tab(/*excluding*/ None) else {
|
||||
return if step < 0 {
|
||||
self.tabs.last().copied()
|
||||
} else {
|
||||
self.tabs.first().copied()
|
||||
};
|
||||
};
|
||||
let index = self.tabs.iter().position(|id| *id == selected)?;
|
||||
let next = (index as isize + step).rem_euclid(self.tabs.len() as isize);
|
||||
Some(self.tabs[next as usize])
|
||||
}
|
||||
|
||||
pub(super) fn selected_chat(&self) -> Option<ThreadId> {
|
||||
self.visits.last().copied()
|
||||
self.visits.iter().rev().find_map(|tab| match *tab {
|
||||
SidebarTab::Chat(id) => Some(id),
|
||||
SidebarTab::Document(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn active_document(&mut self) -> Option<&mut super::sidebar_document::Document> {
|
||||
let SidebarContent::Document(id) = self.content else {
|
||||
return None;
|
||||
};
|
||||
self.documents.get_mut(&id)
|
||||
}
|
||||
|
||||
pub(super) fn document_title(&self, id: uuid::Uuid) -> String {
|
||||
let Some(document) = self.documents.get(&id) else {
|
||||
return String::new();
|
||||
};
|
||||
let path = document.path.inferred_native_path_string();
|
||||
path.rsplit(['/', '\\']).next().unwrap_or(&path).to_owned()
|
||||
}
|
||||
|
||||
pub(super) fn add_document_tab(&mut self, id: uuid::Uuid) {
|
||||
let tab = SidebarTab::Document(id);
|
||||
if !self.tabs.contains(&tab) {
|
||||
self.tabs.push(tab);
|
||||
self.tabs_revision += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn remove_document(&mut self, id: uuid::Uuid) {
|
||||
self.documents.remove(&id);
|
||||
let tab = SidebarTab::Document(id);
|
||||
self.tabs.retain(|other| *other != tab);
|
||||
self.visits.retain(|other| *other != tab);
|
||||
self.tabs_revision += 1;
|
||||
}
|
||||
|
||||
pub(super) fn input_state(&self) -> SidebarInputState {
|
||||
@@ -198,27 +256,27 @@ impl SidebarPane {
|
||||
};
|
||||
SidebarInputState {
|
||||
revision: self.tabs_revision,
|
||||
selected_chat: tabs.selected,
|
||||
selected_tab: tabs.selected,
|
||||
choice,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_tab(&mut self, id: ThreadId) {
|
||||
if !self.tabs.contains(&id) {
|
||||
if !self.tabs.contains(&SidebarTab::Chat(id)) {
|
||||
if self.chat_tabs().choice_index(id).is_none() {
|
||||
self.choices.push(id.into());
|
||||
}
|
||||
self.tabs.push(id);
|
||||
self.tabs.push(SidebarTab::Chat(id));
|
||||
self.tabs_revision += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn chat_ready(&mut self, child: ThreadId) {
|
||||
self.creation = None;
|
||||
if !self.tabs.contains(&child) {
|
||||
if !self.tabs.contains(&SidebarTab::Chat(child)) {
|
||||
self.add_tab(child);
|
||||
// A completed background fork must not replace the user's selection.
|
||||
self.visits.insert(0, child);
|
||||
self.visits.insert(0, SidebarTab::Chat(child));
|
||||
}
|
||||
if self.content == SidebarContent::Chooser
|
||||
&& let Some(selected) = self.selected_chat()
|
||||
@@ -228,18 +286,24 @@ impl SidebarPane {
|
||||
}
|
||||
|
||||
pub(super) fn hide(&mut self) {
|
||||
self.focus_document_request = None;
|
||||
self.opening = None;
|
||||
self.focus = None;
|
||||
}
|
||||
|
||||
pub(super) fn remove_chat(&mut self, id: ThreadId, storage: SideChatStorage) {
|
||||
let Some(index) = self.tabs.iter().position(|tab| *tab == id) else {
|
||||
let Some(index) = self
|
||||
.tabs
|
||||
.iter()
|
||||
.position(|tab| *tab == SidebarTab::Chat(id))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
self.close.take_if(|close| close.target == id);
|
||||
self.tabs.remove(index);
|
||||
self.tabs_revision += 1;
|
||||
self.visits.retain(|visited| *visited != id);
|
||||
self.visits
|
||||
.retain(|visited| *visited != SidebarTab::Chat(id));
|
||||
if storage == SideChatStorage::Temporary {
|
||||
self.remove_choice(id);
|
||||
}
|
||||
@@ -412,7 +476,7 @@ impl App {
|
||||
pub(super) fn panel_focus(&self) -> Option<PaneFocus> {
|
||||
let pane = self.sidebar()?;
|
||||
match pane.content {
|
||||
SidebarContent::Chooser => pane.focus,
|
||||
SidebarContent::Chooser | SidebarContent::Document(_) => pane.focus,
|
||||
SidebarContent::Chat(_) => None,
|
||||
}
|
||||
}
|
||||
@@ -483,7 +547,7 @@ pub(super) fn content_layout(
|
||||
[main, sidebar]: [Rect; 2],
|
||||
) -> Option<SidebarLayout> {
|
||||
match content {
|
||||
SidebarContent::Chooser => (app.sidebar_chooser_available()
|
||||
SidebarContent::Chooser | SidebarContent::Document(_) => (app.sidebar_chooser_available()
|
||||
&& (focus == PaneFocus::Main || app.chat_widget.completion_can_yield_focus()))
|
||||
.then_some(SidebarLayout::Panel {
|
||||
main,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
use super::*;
|
||||
use crate::app_server_session::SideChatStorage;
|
||||
use crate::footer_hint;
|
||||
use crate::sidebar_tab_strip::SidebarTab;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::Margin;
|
||||
use ratatui::widgets::Widget;
|
||||
@@ -220,18 +221,25 @@ async fn execute(
|
||||
let Some(side) = app.side_threads.get(&target) else {
|
||||
return Ok(());
|
||||
};
|
||||
let parent = side.parent_thread_id;
|
||||
if app.current_displayed_thread_id() == Some(target) {
|
||||
let next = match action {
|
||||
Action::CloseTab => app.sidebar().and_then(|pane| pane.previous_chat(target)),
|
||||
Action::Archive => None,
|
||||
}
|
||||
.unwrap_or(side.parent_thread_id);
|
||||
app.select_agent_thread(tui, server, next).await?;
|
||||
if app.current_displayed_thread_id() != Some(next) {
|
||||
Action::CloseTab => app
|
||||
.sidebar()
|
||||
.and_then(|pane| pane.recent_tab(Some(SidebarTab::Chat(target))))
|
||||
.unwrap_or(SidebarTab::Chat(parent)),
|
||||
Action::Archive => SidebarTab::Chat(parent),
|
||||
};
|
||||
app.activate_sidebar_tab(tui, server, next).await?;
|
||||
let destination_thread = match next {
|
||||
SidebarTab::Chat(id) => id,
|
||||
SidebarTab::Document(_) => parent,
|
||||
};
|
||||
if app.current_displayed_thread_id() != Some(destination_thread) {
|
||||
return Ok(());
|
||||
}
|
||||
if action == Action::Archive {
|
||||
app.focus_returned_parent_composer(next).await?;
|
||||
app.focus_returned_parent_composer(parent).await?;
|
||||
}
|
||||
}
|
||||
let Some(side) = app.side_threads.get_mut(&target) else {
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
//! Read-only Markdown previews use the focused thread's workspace and app-server.
|
||||
|
||||
use super::sidebar::PaneFocus;
|
||||
use super::sidebar::SidebarContent;
|
||||
use super::*;
|
||||
use crate::motion::MotionMode;
|
||||
use crate::sidebar_tab_strip::SidebarTab;
|
||||
use crate::terminal_hyperlinks::HyperlinkLine;
|
||||
use crate::terminal_hyperlinks::visible_lines;
|
||||
use crate::transcript_view::TranscriptView;
|
||||
use codex_app_server_client::AppServerPath;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::Position;
|
||||
use ratatui::widgets::Widget;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FileCell {
|
||||
source: String,
|
||||
}
|
||||
|
||||
impl HistoryCell for FileCell {
|
||||
fn raw_lines(&self) -> Vec<Line<'static>> {
|
||||
crate::history_cell::raw_lines_from_source(&self.source)
|
||||
}
|
||||
|
||||
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
|
||||
visible_lines(self.display_hyperlink_lines(width))
|
||||
}
|
||||
|
||||
fn transcript_hyperlink_lines(&self, width: u16) -> Vec<HyperlinkLine> {
|
||||
self.display_hyperlink_lines(width)
|
||||
}
|
||||
|
||||
fn display_hyperlink_lines(&self, width: u16) -> Vec<HyperlinkLine> {
|
||||
crate::markdown_render::render_markdown_lines_with_width_cwd_and_hidden_link_destinations(
|
||||
&self.source,
|
||||
Some(width.into()),
|
||||
/*cwd*/ None,
|
||||
&crate::markdown_render::hide_web_link_destination,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct Document {
|
||||
pub(super) path: PathUri,
|
||||
pub(super) backend: crate::backend_id::BackendId,
|
||||
state: State,
|
||||
}
|
||||
|
||||
enum State {
|
||||
Loading(Instant),
|
||||
Failed(String),
|
||||
Ready {
|
||||
view: Box<TranscriptView>,
|
||||
cells: [Arc<dyn HistoryCell>; 1],
|
||||
},
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub(super) fn connection_replaced(&mut self) {
|
||||
if matches!(self.state, State::Loading(_)) {
|
||||
self.state = State::Failed("Connection changed. Reopen this file to try again.".into());
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn loaded(&mut self, result: Result<String, String>) {
|
||||
self.state = match result {
|
||||
Ok(source) => {
|
||||
let cells: [Arc<dyn HistoryCell>; 1] = [Arc::new(FileCell { source })];
|
||||
let mut view = Box::<TranscriptView>::default();
|
||||
view.jump_to_beginning(&cells);
|
||||
State::Ready { view, cells }
|
||||
}
|
||||
Err(error) => State::Failed(error),
|
||||
};
|
||||
}
|
||||
|
||||
pub(super) fn paint(
|
||||
&mut self,
|
||||
area: Rect,
|
||||
buf: &mut Buffer,
|
||||
motion: MotionMode,
|
||||
frames: &tui::FrameRequester,
|
||||
resize_hint: Option<&str>,
|
||||
) {
|
||||
ratatui::widgets::Clear.render(area, buf);
|
||||
let mut body = area.inner(ratatui::layout::Margin::new(1, 0));
|
||||
if let Some(hint) = resize_hint
|
||||
&& body.height > 1
|
||||
{
|
||||
body.height -= 1;
|
||||
crate::sidebar_chooser::resize_hint(hint, body.width).render(
|
||||
Rect {
|
||||
y: body.bottom(),
|
||||
height: 1,
|
||||
..body
|
||||
},
|
||||
buf,
|
||||
);
|
||||
}
|
||||
match &mut self.state {
|
||||
State::Loading(started) => {
|
||||
let glyph = crate::motion::loading_glyph(*started, motion, frames);
|
||||
Line::from(vec![
|
||||
ratatui::text::Span::styled(glyph, crate::style::accent_style()),
|
||||
" ".into(),
|
||||
"Opening…".dim(),
|
||||
])
|
||||
.render(body, buf);
|
||||
}
|
||||
State::Failed(error) => {
|
||||
Paragraph::new(format!("Could not open file: {error}"))
|
||||
.wrap(ratatui::widgets::Wrap { trim: false })
|
||||
.render(body, buf);
|
||||
}
|
||||
State::Ready { view, cells } => view.render(body, buf, cells),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(super) fn document_preview_available(
|
||||
&self,
|
||||
tui: &tui::Tui,
|
||||
server: &AppServerSession,
|
||||
) -> bool {
|
||||
self.conversation_focus_available(tui)
|
||||
&& self.sidebar_chooser_available()
|
||||
&& server.thread_params_mode() != crate::app_server_session::ThreadParamsMode::Cloud
|
||||
&& self.current_displayed_thread_id().is_some()
|
||||
}
|
||||
|
||||
pub(super) async fn open_document(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
source: ThreadId,
|
||||
path: AppServerPath,
|
||||
) -> Result<()> {
|
||||
let Some(parent) = self.sidebar_parent() else {
|
||||
return Ok(());
|
||||
};
|
||||
let resolver = server.request_view();
|
||||
let backend_id = resolver.backend_id();
|
||||
let tx = self.app_event_tx.clone();
|
||||
let request = uuid::Uuid::new_v4();
|
||||
self.sidebar_mut().focus_document_request = Some(request);
|
||||
tokio::spawn(async move {
|
||||
let resolved = resolver
|
||||
.resolve_thread_file(source, &path)
|
||||
.await
|
||||
.map_err(|error| format!("{error:#}"));
|
||||
tx.send(AppEvent::SidebarDocumentResolved {
|
||||
backend_id,
|
||||
parent,
|
||||
source,
|
||||
request,
|
||||
original_path: path,
|
||||
path: resolved,
|
||||
});
|
||||
});
|
||||
tui.frame_requester().schedule_frame();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn open_resolved_document(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
source: ThreadId,
|
||||
original_path: AppServerPath,
|
||||
path: PathUri,
|
||||
focus: bool,
|
||||
) -> Result<()> {
|
||||
let Some(parent) = self.sidebar_parent() else {
|
||||
return Ok(());
|
||||
};
|
||||
let backend_id = server.backend_id();
|
||||
let existing = self.sidebar().and_then(|pane| {
|
||||
pane.documents.iter().find_map(|(&id, doc)| {
|
||||
(doc.backend == backend_id && doc.path == path)
|
||||
.then_some((id, matches!(doc.state, State::Failed(_))))
|
||||
})
|
||||
});
|
||||
if let Some((id, failed)) = existing {
|
||||
if !focus {
|
||||
return Ok(());
|
||||
}
|
||||
if !failed {
|
||||
return self
|
||||
.activate_sidebar_tab(tui, server, SidebarTab::Document(id))
|
||||
.await;
|
||||
}
|
||||
self.sidebar_mut().remove_document(id);
|
||||
}
|
||||
let request = uuid::Uuid::new_v4();
|
||||
let pane = self.sidebar_mut();
|
||||
pane.documents.insert(
|
||||
request,
|
||||
Document {
|
||||
path: path.clone(),
|
||||
backend: backend_id,
|
||||
state: State::Loading(Instant::now()),
|
||||
},
|
||||
);
|
||||
pane.add_document_tab(request);
|
||||
if focus {
|
||||
self.activate_sidebar_tab(tui, server, SidebarTab::Document(request))
|
||||
.await?;
|
||||
} else {
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
let reader = server.request_view();
|
||||
let tx = self.app_event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let result = reader
|
||||
.read_document(source, &original_path, &path)
|
||||
.await
|
||||
.map_err(|error| format!("{error:#}"));
|
||||
tx.send(AppEvent::SidebarDocumentLoaded {
|
||||
backend_id,
|
||||
parent,
|
||||
request,
|
||||
result,
|
||||
});
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn handle_document_input(&mut self, tui: &mut tui::Tui, event: &TuiEvent) -> bool {
|
||||
let focused = self.panel_focus() == Some(PaneFocus::Sidebar);
|
||||
let area = self.conversation_frame.panel_area();
|
||||
let Some(pane) = self.sidebars.get_mut(&self.sidebar_parent()) else {
|
||||
return false;
|
||||
};
|
||||
let Some(SidebarContent::Document(id)) = pane.visible_content() else {
|
||||
return false;
|
||||
};
|
||||
match event {
|
||||
TuiEvent::Key(key) if focused => {
|
||||
if key.kind != KeyEventKind::Release
|
||||
&& let Some(Document {
|
||||
state: State::Ready { view, cells },
|
||||
..
|
||||
}) = pane.documents.get_mut(&id)
|
||||
{
|
||||
view.navigate_pager(*key, cells, &self.keymap.pager);
|
||||
}
|
||||
}
|
||||
TuiEvent::Paste(_) if focused => {}
|
||||
TuiEvent::Mouse(mouse) if mouse.kind != MouseEventKind::Moved => {
|
||||
let inside =
|
||||
area.is_some_and(|area| area.contains(Position::new(mouse.column, mouse.row)));
|
||||
if mouse.kind == MouseEventKind::Down(MouseButton::Left) {
|
||||
pane.show(
|
||||
SidebarContent::Document(id),
|
||||
if inside {
|
||||
PaneFocus::Sidebar
|
||||
} else {
|
||||
PaneFocus::Main
|
||||
},
|
||||
);
|
||||
}
|
||||
if !inside {
|
||||
return false;
|
||||
}
|
||||
if let Some(Document {
|
||||
state: State::Ready { view, cells },
|
||||
..
|
||||
}) = pane.documents.get_mut(&id)
|
||||
&& matches!(
|
||||
mouse.kind,
|
||||
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown
|
||||
)
|
||||
{
|
||||
view.mouse_scroll_speed = self.transcript_view.mouse_scroll_speed;
|
||||
let _ = view.handle_mouse(*mouse, cells);
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
tui.frame_requester().schedule_frame();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "sidebar_document_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,250 @@
|
||||
#![cfg(test)]
|
||||
#![cfg(not(windows))]
|
||||
|
||||
use super::*;
|
||||
use crate::app::owned_transcript::tests::attach_thread;
|
||||
use crate::app::test_support::enable_side_retention;
|
||||
use crate::app::test_support::side_frame;
|
||||
use crate::app::tests::make_test_app_with_channels;
|
||||
use crate::history_cell::AgentMarkdownCell;
|
||||
use codex_config::types::KeybindingSpec;
|
||||
use codex_config::types::KeybindingsSpec;
|
||||
use crossterm::event::MouseEvent;
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::layout::Size;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
#[tokio::test]
|
||||
async fn focused_markdown_link_opens_read_only_preview_only_with_workspace() -> Result<()> {
|
||||
let (mut app, mut events, _ops) = make_test_app_with_channels().await;
|
||||
app.local_settings.transcript_mode = crate::transcript_mode::TranscriptMode::Owned;
|
||||
app.config.features.disable(Feature::TuiWorkspace)?;
|
||||
app.local_settings.tui.animations = false;
|
||||
let dir = tempfile::tempdir()?;
|
||||
app.config.cwd =
|
||||
codex_utils_absolute_path::AbsolutePathBuf::try_from(dir.path().canonicalize()?)?;
|
||||
let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
|
||||
let parent = server.start_thread(&app.config).await?.session.thread_id;
|
||||
attach_thread(&mut app, parent);
|
||||
app.transcript_cells = vec".into(),
|
||||
dir.path(),
|
||||
))];
|
||||
|
||||
let size = Size::new(/*width*/ 120, /*height*/ 12);
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
tui.set_owned_screen(/*owned*/ true)?;
|
||||
tui.terminal = crate::tui::Terminal::with_screen_size_and_cursor_position_for_test(
|
||||
ratatui::backend::CrosstermBackend::new(std::io::stdout()),
|
||||
size,
|
||||
Default::default(),
|
||||
);
|
||||
let screen = side_frame(&mut app, &mut tui, size)?;
|
||||
let (column, row) = screen
|
||||
.lines()
|
||||
.enumerate()
|
||||
.find_map(|(row, line)| {
|
||||
let byte = line.find("guide.md")?;
|
||||
Some((line[..byte].width() as u16, row as u16))
|
||||
})
|
||||
.expect("the local link's path is visible");
|
||||
let press = MouseEvent {
|
||||
kind: MouseEventKind::Down(MouseButton::Left),
|
||||
column,
|
||||
row,
|
||||
modifiers: KeyModifiers::NONE,
|
||||
};
|
||||
let modified_press = MouseEvent {
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..press
|
||||
};
|
||||
Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Mouse(modified_press))).await?;
|
||||
assert!(
|
||||
std::iter::from_fn(|| events.try_recv().ok())
|
||||
.all(|event| !matches!(event, AppEvent::OpenSidebarDocument { .. }))
|
||||
);
|
||||
assert_eq!(app.panel_focus(), None);
|
||||
|
||||
app.local_settings.tui.keymap.pager.jump_bottom =
|
||||
Some(KeybindingsSpec::One(KeybindingSpec("ctrl-x end".into())));
|
||||
enable_side_retention(&mut app)?;
|
||||
let enabled = side_frame(&mut app, &mut tui, size)?;
|
||||
assert_eq!(
|
||||
enabled.lines().nth(row as usize),
|
||||
screen.lines().nth(row as usize)
|
||||
);
|
||||
let release = MouseEvent {
|
||||
kind: MouseEventKind::Up(MouseButton::Left),
|
||||
..press
|
||||
};
|
||||
for mouse in [press, release] {
|
||||
Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Mouse(mouse))).await?;
|
||||
}
|
||||
let open = std::iter::from_fn(|| events.try_recv().ok())
|
||||
.find(|event| matches!(event, AppEvent::OpenSidebarDocument { .. }))
|
||||
.expect("plain click queues a document from the focused transcript");
|
||||
app.zoomed_thread = Some(parent);
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, open)).await?;
|
||||
let mut next_document_event = async |resolved: bool| loop {
|
||||
let event = tokio::time::timeout(std::time::Duration::from_secs(5), events.recv())
|
||||
.await
|
||||
.expect("document event arrives")
|
||||
.expect("document event sender");
|
||||
if (resolved && matches!(event, AppEvent::SidebarDocumentResolved { .. }))
|
||||
|| (!resolved && matches!(event, AppEvent::SidebarDocumentLoaded { .. }))
|
||||
{
|
||||
break event;
|
||||
}
|
||||
};
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
let loading = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(loading.contains("Opening…"), "{loading}");
|
||||
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(false).await)).await?;
|
||||
let error = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(error.contains("Could not open file"), "{error}");
|
||||
|
||||
std::fs::write(
|
||||
dir.path().join("guide.md"),
|
||||
format!(
|
||||
"# Guide\n\nFirst section.\n\n{}Last section.\n\n[Next](./notes.md)",
|
||||
"Middle.\n\n".repeat(12)
|
||||
),
|
||||
)?;
|
||||
let path = AppServerPath::from_app_server("guide.md");
|
||||
app.open_document(&mut tui, &mut server, parent, path.clone())
|
||||
.await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
let mut other_backend =
|
||||
Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
|
||||
assert_ne!(other_backend.backend_id(), server.backend_id());
|
||||
Box::pin(app.handle_event(
|
||||
&mut tui,
|
||||
&mut other_backend,
|
||||
next_document_event(false).await,
|
||||
))
|
||||
.await?;
|
||||
other_backend.shutdown().await?;
|
||||
let screen = side_frame(&mut app, &mut tui, Size::new(/*width*/ 76, /*height*/ 12))?;
|
||||
let strip = app
|
||||
.conversation_frame
|
||||
.sidebar_tab_strip()
|
||||
.expect("tab strip");
|
||||
let buffer = tui.terminal.previous_buffer();
|
||||
let strip_text = (strip.x..strip.right())
|
||||
.map(|x| buffer[(x, strip.y)].symbol())
|
||||
.collect::<String>();
|
||||
let tab = strip_text.split_whitespace().collect::<Vec<_>>().join(" ");
|
||||
let visible = std::iter::once(tab.as_str())
|
||||
.chain(
|
||||
screen
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|row| row.ends_with("# Guide") || row.ends_with("First section."))
|
||||
.map(|row| row.rsplit('│').next().unwrap_or(row).trim()),
|
||||
)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
insta::assert_snapshot!(visible, @"
|
||||
guide.md × +
|
||||
# Guide
|
||||
First section.
|
||||
");
|
||||
for key in [
|
||||
KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL),
|
||||
KeyCode::End.into(),
|
||||
] {
|
||||
Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(key))).await?;
|
||||
}
|
||||
let end = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(end.contains("Last section."), "{end}");
|
||||
|
||||
std::fs::write(dir.path().join("notes.md"), "# Notes")?;
|
||||
app.open_document(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
parent,
|
||||
AppServerPath::from_app_server("notes.md"),
|
||||
)
|
||||
.await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(false).await)).await?;
|
||||
let two = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(
|
||||
two.lines()
|
||||
.any(|line| line.contains("guide.md") && line.contains("notes.md")),
|
||||
"{two}"
|
||||
);
|
||||
assert!(two.contains("# Notes"), "{two}");
|
||||
Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into())))
|
||||
.await?;
|
||||
let returned = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(returned.contains("Last section."), "{returned}");
|
||||
let repeat = crossterm::event::KeyEvent::new_with_kind(
|
||||
KeyCode::Esc,
|
||||
KeyModifiers::NONE,
|
||||
KeyEventKind::Repeat,
|
||||
);
|
||||
assert!(app.route_key_chord_event(&mut tui, repeat).is_none());
|
||||
assert_eq!(app.sidebar().unwrap().documents.len(), 1);
|
||||
Box::pin(app.handle_tui_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
TuiEvent::Key(KeyEvent::new_with_kind(
|
||||
KeyCode::Esc,
|
||||
KeyModifiers::NONE,
|
||||
KeyEventKind::Release,
|
||||
)),
|
||||
))
|
||||
.await?;
|
||||
app.open_document(&mut tui, &mut server, parent, path)
|
||||
.await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
assert_eq!(app.sidebar().unwrap().documents.len(), 1);
|
||||
app.open_document(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
parent,
|
||||
AppServerPath::from_app_server("closed.md"),
|
||||
)
|
||||
.await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into())))
|
||||
.await?;
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(false).await)).await?;
|
||||
assert_eq!(app.sidebar().unwrap().documents.len(), 1);
|
||||
app.open_document(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
parent,
|
||||
AppServerPath::from_app_server("notes.md"),
|
||||
)
|
||||
.await?;
|
||||
app.overlay = Some(crate::pager_overlay::Overlay::Transcript(
|
||||
crate::pager_overlay::TranscriptOverlay::new(Vec::new(), app.keymap.pager.clone()),
|
||||
));
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(true).await)).await?;
|
||||
assert_eq!(app.sidebar().unwrap().documents.len(), 2);
|
||||
assert!(app.overlay.take().is_some());
|
||||
Box::pin(app.handle_event(&mut tui, &mut server, next_document_event(false).await)).await?;
|
||||
let after_overlay = side_frame(&mut app, &mut tui, size)?;
|
||||
assert!(after_overlay.contains("Last section."), "{after_overlay}");
|
||||
let child = ThreadId::new();
|
||||
app.side_threads.insert(child, SideThreadState::new(parent));
|
||||
let (stream, display) = app.chat_widget.take_side_display(/*width*/ 120);
|
||||
assert!(app.transcript_view.document_links);
|
||||
Box::pin(app.handle_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
AppEvent::ParkSideCompanion {
|
||||
pair: child,
|
||||
thread_id: parent,
|
||||
stream,
|
||||
display,
|
||||
},
|
||||
))
|
||||
.await?;
|
||||
assert!(!app.side_companions[&parent].view.document_links);
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
---
|
||||
source: tui/src/app/tests.rs
|
||||
assertion_line: 4913
|
||||
expression: header.trim_end()
|
||||
---
|
||||
‹ Fix cache× +
|
||||
‹ Fix cache × +
|
||||
|
||||
@@ -1563,6 +1563,8 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
continue;
|
||||
}
|
||||
AppEvent::SavedSideChatsLoaded { backend_id, .. }
|
||||
| AppEvent::SidebarDocumentResolved { backend_id, .. }
|
||||
| AppEvent::SidebarDocumentLoaded { backend_id, .. }
|
||||
| AppEvent::StartupThreadStarted { backend_id, .. } => {
|
||||
backends.slot(*backend_id).unwrap_or(displayed_session)
|
||||
}
|
||||
|
||||
@@ -4970,7 +4970,7 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
))
|
||||
.await?;
|
||||
} else {
|
||||
assert!(columns.iter().all(|x| *x == divider - 2));
|
||||
assert!(columns.iter().all(|x| *x == divider - 1));
|
||||
// A fresh transcript press cancels a drag whose release was lost.
|
||||
for kind in [MouseEventKind::Down(left), MouseEventKind::Up(left)] {
|
||||
boxed_future!(app.handle_tui_event(
|
||||
@@ -4986,9 +4986,9 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
}
|
||||
}
|
||||
// Text preview, graphics guide, and the divider on the protected image row.
|
||||
insta::assert_snapshot!(format!("{previews:?}"), @"[197, 197, 199]");
|
||||
insta::assert_snapshot!(format!("{previews:?}"), @"[198, 198, 199]");
|
||||
side_frame(&mut app, &mut tui, wide)?;
|
||||
assert_eq!(divider_at(&tui, 0), 197);
|
||||
assert_eq!(divider_at(&tui, 0), 198);
|
||||
let saved: toml::Value = toml::from_str(&std::fs::read_to_string(&path)?)?;
|
||||
assert_eq!(saved["tui"]["sidebar_width_percent"].as_integer(), Some(34));
|
||||
|
||||
@@ -5384,12 +5384,13 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
let header = frame.lines().next().unwrap().split_once('│').unwrap().1;
|
||||
insta::assert_snapshot!(
|
||||
header.split_inclusive('×').take(2).collect::<String>(),
|
||||
@" ☁︎ Invest…× ☁︎ Fix ca…×"
|
||||
@" ☁︎ Investigate …× ☁︎ Fix cache ×"
|
||||
);
|
||||
keys!(prefix, chooser_key);
|
||||
let frame = side_frame(&mut app, &mut tui, even)?;
|
||||
let labels = frame
|
||||
.lines()
|
||||
.skip(1)
|
||||
.filter_map(|line| line.split_once('│').map(|(_, pane)| pane))
|
||||
.filter(|pane| pane.contains("Investigate rendering") || pane.contains("Fix cache"))
|
||||
.map(|pane| pane.trim().trim_start_matches(['›', '○', ' ']))
|
||||
@@ -5410,16 +5411,26 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
let narrow_frame = side_frame(&mut app, &mut tui, narrow)?;
|
||||
let header = narrow_frame.lines().next().unwrap();
|
||||
insta::assert_snapshot!("side_chat_tab_overflow", header.trim_end());
|
||||
let arrow = header.chars().position(|c| c == '‹').expect("previous tab");
|
||||
let strip = app.conversation_frame.chat_strip.unwrap();
|
||||
let arrow_x = header.chars().position(|c| c == '‹').expect("previous tab") as u16;
|
||||
let buffer = tui.terminal.previous_buffer();
|
||||
let arrow_cell = &buffer[(arrow_x, strip.y)];
|
||||
let add_cell = &buffer[(strip.right() - 3, strip.y)];
|
||||
let idle_style = arrow_cell.style();
|
||||
assert_eq!(idle_style, add_cell.style());
|
||||
|
||||
let hover = mouse(MouseEventKind::Moved, arrow_x, strip.y);
|
||||
boxed_future!(app.handle_tui_event(&mut tui, &mut app_server, hover)).await?;
|
||||
side_frame(&mut app, &mut tui, narrow)?;
|
||||
assert_ne!(
|
||||
tui.terminal.previous_buffer()[(arrow_x, strip.y)].style(),
|
||||
idle_style
|
||||
);
|
||||
for (kind, expected) in [
|
||||
(MouseEventKind::Down(MouseButton::Left), side_thread_id),
|
||||
(MouseEventKind::ScrollDown, sibling),
|
||||
] {
|
||||
let event = mouse(
|
||||
kind,
|
||||
arrow as u16,
|
||||
app.conversation_frame.chat_strip.unwrap().y,
|
||||
);
|
||||
let event = mouse(kind, arrow_x, strip.y);
|
||||
boxed_future!(app.handle_tui_event(&mut tui, &mut app_server, event)).await?;
|
||||
drain_app_events(&mut app, &mut tui, &mut app_server, &mut app_event_rx).await?;
|
||||
assert_eq!(app.current_displayed_thread_id(), Some(expected));
|
||||
@@ -5433,7 +5444,22 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
assert!(side_frame(&mut app, &mut tui, even)?.contains(child_row));
|
||||
let strip = app.conversation_frame.chat_strip.unwrap();
|
||||
let down = MouseEventKind::Down(MouseButton::Left);
|
||||
let click = mouse(down, strip.x + 12, strip.y);
|
||||
let sibling_tab = app
|
||||
.sidebar()
|
||||
.unwrap()
|
||||
.chat_tabs()
|
||||
.strip()
|
||||
.strip_targets(strip)
|
||||
.into_iter()
|
||||
.find_map(|(target, area)| {
|
||||
(target
|
||||
== crate::sidebar_tab_strip::TabTarget::Tab(
|
||||
crate::sidebar_tab_strip::SidebarTab::Chat(sibling),
|
||||
))
|
||||
.then_some(area)
|
||||
})
|
||||
.expect("sibling tab");
|
||||
let click = mouse(down, sibling_tab.x, strip.y);
|
||||
boxed_future!(app.handle_tui_event(&mut tui, &mut app_server, click)).await?;
|
||||
drain_app_events(&mut app, &mut tui, &mut app_server, &mut app_event_rx).await?;
|
||||
assert_eq!(app.current_displayed_thread_id(), Some(sibling));
|
||||
@@ -5517,7 +5543,6 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
assert_eq!(app.chooser_focus(), expected_focus, "{focus}");
|
||||
side_frame(&mut app, &mut tui, even)?;
|
||||
let strip = app.conversation_frame.sidebar_tab_strip().unwrap();
|
||||
let chooser = app.conversation_frame.chat_strip.is_none();
|
||||
let add = strip.right() - 3;
|
||||
let tabs = app
|
||||
.sidebar()
|
||||
@@ -5526,20 +5551,22 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
|
||||
.strip()
|
||||
.strip_targets(strip);
|
||||
let [inactive, selected] = tabs
|
||||
.into_iter()
|
||||
.filter_map(|(target, area)| {
|
||||
matches!(target, crate::sidebar_tab_strip::TabTarget::Tab(_)).then_some(area)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.try_into()
|
||||
.expect("both side-chat tabs are visible");
|
||||
assert_eq!(selected.x, inactive.right() + 1);
|
||||
assert_eq!(selected.x, inactive.right() + 2);
|
||||
assert_eq!(inactive.width, selected.width);
|
||||
let [inactive, selected] = [inactive, selected].map(|area| area.x + 1);
|
||||
for (state, column, row, palette, redraw) in [
|
||||
("rest", selected, strip.y + 1, Some(colors), None),
|
||||
("hover", inactive, strip.y, Some(colors), Some(true)),
|
||||
("fallback", inactive + 1, strip.y, None, Some(false)),
|
||||
("selected", selected, strip.y, Some(colors), Some(true)),
|
||||
("leave", selected, strip.y + 1, Some(colors), Some(chooser)),
|
||||
("leave", selected, strip.y + 1, Some(colors), Some(true)),
|
||||
("light rest", selected, strip.y + 1, Some(light), None),
|
||||
("light hover", inactive, strip.y, Some(light), Some(true)),
|
||||
("light add", add, strip.y, Some(light), Some(true)),
|
||||
|
||||
@@ -522,6 +522,25 @@ pub(crate) enum AppEvent {
|
||||
request: uuid::Uuid,
|
||||
},
|
||||
StartQueuedSavedChat,
|
||||
OpenSidebarDocument {
|
||||
backend_id: crate::backend_id::BackendId,
|
||||
source: ThreadId,
|
||||
path: codex_app_server_client::AppServerPath,
|
||||
},
|
||||
SidebarDocumentResolved {
|
||||
backend_id: crate::backend_id::BackendId,
|
||||
parent: ThreadId,
|
||||
source: ThreadId,
|
||||
request: uuid::Uuid,
|
||||
original_path: codex_app_server_client::AppServerPath,
|
||||
path: std::result::Result<codex_utils_path_uri::PathUri, String>,
|
||||
},
|
||||
SidebarDocumentLoaded {
|
||||
backend_id: crate::backend_id::BackendId,
|
||||
parent: ThreadId,
|
||||
request: uuid::Uuid,
|
||||
result: Result<String, String>,
|
||||
},
|
||||
SavedSideChatsLoaded {
|
||||
backend_id: crate::backend_id::BackendId,
|
||||
parent: ThreadId,
|
||||
|
||||
@@ -28,10 +28,6 @@ use serde_json::json;
|
||||
use uuid::Uuid;
|
||||
|
||||
impl AppServerSession {
|
||||
#[cfg_attr(
|
||||
not(test),
|
||||
expect(dead_code, reason = "used by the sidebar viewer in the next change")
|
||||
)]
|
||||
pub(crate) async fn resolve_thread_file(
|
||||
&self,
|
||||
source: ThreadId,
|
||||
@@ -76,10 +72,6 @@ impl AppServerSession {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
not(test),
|
||||
expect(dead_code, reason = "used by the sidebar viewer in the next change")
|
||||
)]
|
||||
pub(crate) async fn read_document(
|
||||
&self,
|
||||
source: ThreadId,
|
||||
|
||||
@@ -112,3 +112,41 @@ pub(crate) fn truncate_line_with_ellipsis(line: Line<'static>, max_width: usize)
|
||||
#[cfg(test)]
|
||||
#[path = "line_truncation_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
pub(crate) fn filename_label(name: &str, width: usize) -> String {
|
||||
let extension = name
|
||||
.rsplit_once('.')
|
||||
.filter(|(_, ext)| !ext.contains(['/', '\\']))
|
||||
.map_or(0, |(_, ext)| crate::width::display_width(ext) + 1);
|
||||
middle_label_with_suffix(name, width, extension)
|
||||
}
|
||||
|
||||
fn middle_label_with_suffix(text: &str, width: usize, suffix_width: usize) -> String {
|
||||
use crate::width::display_width;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
if width == 0 {
|
||||
return String::new();
|
||||
}
|
||||
if display_width(text) <= width {
|
||||
return text.to_owned();
|
||||
}
|
||||
let mut prefix = (width - 1) / 2;
|
||||
if suffix_width < width - 1 {
|
||||
prefix = prefix.min(width - 1 - suffix_width);
|
||||
}
|
||||
let (mut label, rest, prefix_width) = crate::live_wrap::take_prefix_by_width(text, prefix);
|
||||
let mut remaining = width - prefix_width - 1;
|
||||
let mut suffix = rest.len();
|
||||
for (index, grapheme) in rest.grapheme_indices(true).rev() {
|
||||
let cells = display_width(grapheme);
|
||||
if cells > remaining {
|
||||
break;
|
||||
}
|
||||
remaining -= cells;
|
||||
suffix = index;
|
||||
}
|
||||
label.push('…');
|
||||
label.push_str(&rest[suffix..]);
|
||||
label
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::keymap::ListAction;
|
||||
use crate::keymap::RuntimeKeymap;
|
||||
use crate::line_truncation::truncate_line_with_ellipsis_if_overflow;
|
||||
use crate::live_wrap::take_prefix_by_width;
|
||||
use crate::sidebar_tab_strip::SidebarTab;
|
||||
use crate::sidebar_tab_strip::TabState;
|
||||
use crate::sidebar_tab_strip::TabStrip;
|
||||
use crate::sidebar_tab_strip::TabTarget;
|
||||
@@ -127,15 +128,6 @@ pub(crate) fn footer(
|
||||
|
||||
const CHAT_TAB: &str = " Side chat ";
|
||||
|
||||
pub(crate) fn close_cell(tab: Rect) -> Rect {
|
||||
Rect::new(
|
||||
tab.right().saturating_sub(1),
|
||||
tab.y,
|
||||
u16::from(tab.width >= 3),
|
||||
tab.height,
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) enum Discovery {
|
||||
#[default]
|
||||
@@ -173,11 +165,11 @@ pub(crate) struct ChatTabs<'a> {
|
||||
pub(crate) label_style: ThreadLabelStyle,
|
||||
pub(crate) discovery: &'a Discovery,
|
||||
pub(crate) opening: Option<&'a Opening>,
|
||||
pub(crate) ids: &'a [ThreadId],
|
||||
pub(crate) ids: &'a [SidebarTab],
|
||||
// Chooser index zero is reserved for New side chat.
|
||||
pub(crate) choices: &'a [SideChat],
|
||||
pub(crate) latest_prompts: &'a HashMap<ThreadId, String>,
|
||||
pub(crate) selected: Option<ThreadId>,
|
||||
pub(crate) selected: Option<SidebarTab>,
|
||||
}
|
||||
|
||||
impl<'a> ChatTabs<'a> {
|
||||
@@ -231,11 +223,10 @@ impl<'a> ChatTabs<'a> {
|
||||
.map(|index| index + 1)
|
||||
}
|
||||
|
||||
pub(crate) fn strip(self) -> TabStrip<'a, ThreadId> {
|
||||
pub(crate) fn strip(self) -> TabStrip<'a, SidebarTab> {
|
||||
TabStrip {
|
||||
ids: self.ids,
|
||||
selected: self.selected,
|
||||
tab_width: CHAT_TAB.len() as u16,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,7 +236,8 @@ pub(crate) fn tab_strip(
|
||||
chooser: TabState,
|
||||
chat: TabState,
|
||||
tabs: ChatTabs<'_>,
|
||||
hovered_target: Option<TabTarget<ThreadId>>,
|
||||
hovered_target: Option<TabTarget<SidebarTab>>,
|
||||
document_title: impl Fn(uuid::Uuid) -> String,
|
||||
buf: &mut Buffer,
|
||||
) {
|
||||
tabs.strip().render(
|
||||
@@ -255,25 +247,28 @@ pub(crate) fn tab_strip(
|
||||
chat,
|
||||
hovered_target,
|
||||
|id, area, style, buf| {
|
||||
let close = close_cell(area);
|
||||
let label = Rect::new(area.x, area.y, area.width - close.width, area.height);
|
||||
let title = match tabs.choice_index(id) {
|
||||
Some(number) => match side_chat_label(&tabs.choices[number - 1]) {
|
||||
Some(name) => {
|
||||
let mut title = tabs.label_style.render(
|
||||
Line::from(name.to_owned()),
|
||||
usize::from(label.width.saturating_sub(1)),
|
||||
);
|
||||
title.spans.insert(0, " ".into());
|
||||
title
|
||||
}
|
||||
None if tabs.choices.len() > 1 => Line::from(format!(" Side {number} ")),
|
||||
let title = match id {
|
||||
SidebarTab::Document(id) => Line::from(crate::line_truncation::filename_label(
|
||||
&document_title(id),
|
||||
usize::from(area.width),
|
||||
)),
|
||||
SidebarTab::Chat(id) => match tabs.choice_index(id) {
|
||||
Some(number) => match side_chat_label(&tabs.choices[number - 1]) {
|
||||
Some(name) => {
|
||||
let mut title = tabs.label_style.render(
|
||||
Line::from(name.to_owned()),
|
||||
usize::from(area.width.saturating_sub(1)),
|
||||
);
|
||||
title.spans.insert(0, " ".into());
|
||||
title
|
||||
}
|
||||
None if tabs.choices.len() > 1 => Line::from(format!(" Side {number} ")),
|
||||
None => Line::from(CHAT_TAB),
|
||||
},
|
||||
None => Line::from(CHAT_TAB),
|
||||
},
|
||||
None => Line::from(CHAT_TAB),
|
||||
};
|
||||
title.style(style).render(label, buf);
|
||||
Line::from("×").style(style).render(close, buf);
|
||||
title.style(style).render(area, buf);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,16 @@ 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),
|
||||
}
|
||||
|
||||
/// Adjacent tabs have one unpainted, noninteractive terminal column between them.
|
||||
const TAB_GAP: u16 = 1;
|
||||
const MIN_TAB_WIDTH: u16 = 12;
|
||||
const MAX_TAB_WIDTH: u16 = 22;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum TabState {
|
||||
@@ -24,12 +32,12 @@ pub(crate) enum TabState {
|
||||
pub(crate) struct TabStrip<'a, Id> {
|
||||
pub(crate) ids: &'a [Id],
|
||||
pub(crate) selected: Option<Id>,
|
||||
pub(crate) tab_width: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum TabTarget<Id> {
|
||||
Tab(Id),
|
||||
Close(Id),
|
||||
Overflow { tab: Id, glyph: &'static str },
|
||||
Add,
|
||||
}
|
||||
@@ -39,48 +47,63 @@ impl<'a, Id: Copy + Eq> TabStrip<'a, Id> {
|
||||
self.ids.iter().position(|id| Some(*id) == self.selected)
|
||||
}
|
||||
|
||||
pub(crate) fn strip_targets(
|
||||
self,
|
||||
area: Rect,
|
||||
) -> impl Iterator<Item = (TabTarget<Id>, Rect)> + 'a {
|
||||
pub(crate) fn strip_targets(self, area: Rect) -> Vec<(TabTarget<Id>, Rect)> {
|
||||
let width = area.width.saturating_sub(1);
|
||||
let plus = width.min(3);
|
||||
let available = width - plus;
|
||||
let height = area.height.min(1);
|
||||
let stride = self.tab_width.saturating_add(TAB_GAP);
|
||||
let overflow = self.ids.len() > usize::from(((available + TAB_GAP) / stride).max(1));
|
||||
// Reserve both arrow gutters so the tabs stay in place while switching.
|
||||
let gutter = if overflow && available >= 7 { 2 } else { 0 };
|
||||
let space = available - 2 * gutter;
|
||||
let tab_width = space.min(self.tab_width);
|
||||
let count = if tab_width == 0 {
|
||||
0
|
||||
} else {
|
||||
usize::from((space + TAB_GAP) / (tab_width + TAB_GAP))
|
||||
};
|
||||
let first = self
|
||||
.selected_index()
|
||||
.map_or(0, |index| (index + 1).saturating_sub(count));
|
||||
let end = (first + count).min(self.ids.len());
|
||||
let rect = move |offset, width| Rect::new(area.x + offset, area.y, width, height);
|
||||
let arrow = |chat, glyph, x| (TabTarget::Overflow { tab: chat, glyph }, rect(x, 1));
|
||||
let left = (gutter > 0 && first > 0).then(|| arrow(self.ids[first - 1], "‹", 0));
|
||||
let right =
|
||||
(gutter > 0 && end < self.ids.len()).then(|| arrow(self.ids[end], "›", available - 1));
|
||||
let tabs = self.ids[first..end].iter().enumerate().map(move |(i, id)| {
|
||||
(
|
||||
TabTarget::Tab(*id),
|
||||
rect(gutter + i as u16 * (tab_width + TAB_GAP), tab_width),
|
||||
)
|
||||
});
|
||||
left.into_iter()
|
||||
.chain(tabs)
|
||||
.chain(right)
|
||||
.chain([(TabTarget::Add, rect(available, plus))])
|
||||
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());
|
||||
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 = u16::from(width >= 3);
|
||||
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),
|
||||
));
|
||||
}
|
||||
}
|
||||
targets.push((TabTarget::Add, rect(available, plus)));
|
||||
targets
|
||||
}
|
||||
|
||||
pub(crate) fn hit(self, area: Rect, point: Position) -> Option<(TabTarget<Id>, Rect)> {
|
||||
self.strip_targets(area)
|
||||
.into_iter()
|
||||
.find(|(_, rect)| rect.contains(point))
|
||||
}
|
||||
|
||||
@@ -119,14 +142,35 @@ impl<'a, Id: Copy + Eq> TabStrip<'a, Id> {
|
||||
TabTarget::Tab(id) => {
|
||||
let tab_style =
|
||||
if Some(id) == self.selected && selected_state != TabState::Absent {
|
||||
selected_style
|
||||
if hovered {
|
||||
crate::style::hover_style(selected_style)
|
||||
} else {
|
||||
selected_style
|
||||
}
|
||||
} else {
|
||||
quiet
|
||||
};
|
||||
paint(id, rect, tab_style, buf);
|
||||
}
|
||||
TabTarget::Close(id) => {
|
||||
let close = if Some(id) == self.selected && selected_state != TabState::Absent {
|
||||
if hovered {
|
||||
crate::style::hover_style(selected_style)
|
||||
} else {
|
||||
selected_style
|
||||
}
|
||||
} else {
|
||||
quiet
|
||||
};
|
||||
Line::from("×").style(close).render(rect, buf);
|
||||
}
|
||||
TabTarget::Overflow { glyph, .. } => {
|
||||
Line::from(glyph).style(quiet).render(rect, buf)
|
||||
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) {
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ parent-29
|
||||
New activity · esc latest
|
||||
|
||||
100 columns, reopened
|
||||
parent-24 │ Side chat× +
|
||||
parent-24 │ Side chat × +
|
||||
│child-24
|
||||
parent-25 │
|
||||
│child-25
|
||||
@@ -43,7 +43,7 @@ parent-29 │
|
||||
│ GPT-6.1-Sol default · Agent
|
||||
|
||||
99 columns, reopened
|
||||
Side chat× +
|
||||
Side chat × +
|
||||
child-24
|
||||
|
||||
child-25
|
||||
|
||||
@@ -3,7 +3,7 @@ source: tui/src/app/tests.rs
|
||||
expression: "format!(\"Zoomed\\n{zoomed}\\nRestored\\n{restored}\\nEqualized\\n{equalized}\")"
|
||||
---
|
||||
Zoomed
|
||||
Side chat× +
|
||||
Side chat × +
|
||||
• unfinished answer
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ Zoomed
|
||||
GPT-6.1-Sol default · Agent
|
||||
tab to queue message · Side from main thread · ⌃/ to switch · ⌃c to close
|
||||
Restored
|
||||
parent-24 │ Side chat× +
|
||||
parent-24 │ Side chat × +
|
||||
│• unfinished answer
|
||||
parent-25 │
|
||||
│
|
||||
@@ -41,7 +41,7 @@ parent-29
|
||||
GPT-6.1-Sol default │ GPT-6.1-Sol default · Agent
|
||||
│ tab to queue message · Side from main thread · ⌃/ to switch · ⌃c to close
|
||||
Equalized
|
||||
parent-24 │ Side chat× +
|
||||
parent-24 │ Side chat × +
|
||||
│• unfinished answer
|
||||
parent-25 │
|
||||
│
|
||||
|
||||
@@ -5,7 +5,7 @@ expression: "tab_styles.join(\"\\n\")"
|
||||
sidebar rest inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(0, 0, 46)/Rgb(99, 168, 248)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
sidebar hover inactive Rgb(224, 224, 224)/Rgb(72, 72, 72)/NONE selected Rgb(0, 0, 46)/Rgb(99, 168, 248)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
sidebar fallback inactive Reset/Reset/UNDERLINED selected Reset/Reset/BOLD | REVERSED add Reset/Reset/NONE
|
||||
sidebar selected inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(0, 0, 46)/Rgb(99, 168, 248)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
sidebar selected inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(0, 0, 46)/Rgb(133, 187, 248)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
sidebar leave inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(0, 0, 46)/Rgb(99, 168, 248)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
sidebar light rest inactive Rgb(102, 103, 117)/Rgb(228, 231, 236)/NONE selected Rgb(0, 0, 46)/Rgb(164, 205, 251)/BOLD add Rgb(108, 109, 123)/Reset/NONE
|
||||
sidebar light hover inactive Rgb(80, 80, 106)/Rgb(218, 221, 226)/NONE selected Rgb(0, 0, 46)/Rgb(164, 205, 251)/BOLD add Rgb(108, 109, 123)/Reset/NONE
|
||||
@@ -15,7 +15,7 @@ sidebar add fallback inactive Reset/Reset/NONE selected Reset/Reset/BOLD | REV
|
||||
main rest inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(224, 224, 224)/Rgb(72, 72, 72)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
main hover inactive Rgb(224, 224, 224)/Rgb(72, 72, 72)/NONE selected Rgb(224, 224, 224)/Rgb(72, 72, 72)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
main fallback inactive Reset/Reset/UNDERLINED selected Reset/Reset/BOLD | UNDERLINED add Reset/Reset/NONE
|
||||
main selected inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(224, 224, 224)/Rgb(72, 72, 72)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
main selected inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(244, 244, 244)/Rgb(112, 112, 112)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
main leave inactive Rgb(151, 151, 151)/Rgb(48, 48, 48)/NONE selected Rgb(224, 224, 224)/Rgb(72, 72, 72)/BOLD add Rgb(142, 142, 142)/Reset/NONE
|
||||
main light rest inactive Rgb(102, 103, 117)/Rgb(228, 231, 236)/NONE selected Rgb(80, 80, 106)/Rgb(218, 221, 226)/BOLD add Rgb(108, 109, 123)/Reset/NONE
|
||||
main light hover inactive Rgb(80, 80, 106)/Rgb(218, 221, 226)/NONE selected Rgb(80, 80, 106)/Rgb(218, 221, 226)/BOLD add Rgb(108, 109, 123)/Reset/NONE
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
source: tui/src/app/tests.rs
|
||||
expression: header.trim_end()
|
||||
---
|
||||
Investig…× Fix cache× +
|
||||
Investigate re…× Fix cache × +
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
source: tui/src/app/tests.rs
|
||||
expression: "chooser.lines().filter_map(|line|\nline.split_once('│').map(|(_, sidebar)|\nsidebar.trim_end())).collect::<Vec<_>>().join(\"\\n\")"
|
||||
---
|
||||
Side chat× +
|
||||
Side chat × +
|
||||
|
||||
Open in sidebar
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
source: tui/src/app/tests.rs
|
||||
expression: panel
|
||||
---
|
||||
parent-24 │ Side chat× +
|
||||
parent-24 │ Side chat × +
|
||||
│child-24
|
||||
parent-25 │
|
||||
│child-25
|
||||
|
||||
Reference in New Issue
Block a user