Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 6 additions & 211 deletions crates/sidebar/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ use util::path_list::PathList;
use workspace::{
CloseWindow, FocusWorkspaceSidebar, MultiWorkspace, MultiWorkspaceEvent, NextProject,
NextThread, Open, OpenMode, PreviousProject, PreviousThread, ProjectGroupKey, SaveIntent,
ShowFewerThreads, ShowMoreThreads, Sidebar as WorkspaceSidebar, SidebarSide, Toast,
ToggleWorkspaceSidebar, Workspace, notifications::NotificationId, sidebar_side_context_menu,
Sidebar as WorkspaceSidebar, SidebarSide, Toast, ToggleWorkspaceSidebar, Workspace,
notifications::NotificationId, sidebar_side_context_menu,
};

use zed_actions::OpenRecent;
Expand Down Expand Up @@ -83,7 +83,6 @@ gpui::actions!(
const DEFAULT_WIDTH: Pixels = px(300.0);
const MIN_WIDTH: Pixels = px(200.0);
const MAX_WIDTH: Pixels = px(800.0);
const DEFAULT_THREADS_SHOWN: usize = 5;

#[derive(Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
enum SerializedSidebarView {
Expand Down Expand Up @@ -227,10 +226,6 @@ enum ListEntry {
has_threads: bool,
},
Thread(ThreadEntry),
ViewMore {
key: ProjectGroupKey,
is_fully_expanded: bool,
},
}

#[cfg(test)]
Expand All @@ -255,7 +250,6 @@ impl ListEntry {
ListEntry::ProjectHeader { key, .. } => multi_workspace
.workspaces_for_project_group(key, cx)
.unwrap_or_default(),
ListEntry::ViewMore { .. } => Vec::new(),
}
}
}
Expand Down Expand Up @@ -482,17 +476,6 @@ impl Sidebar {
.unwrap_or(false)
}

fn group_extra_batches(&self, key: &ProjectGroupKey, cx: &App) -> usize {
self.multi_workspace
.upgrade()
.and_then(|mw| {
mw.read(cx)
.group_state_by_key(key)
.and_then(|state| state.visible_thread_count)
})
.unwrap_or(0)
}

fn set_group_expanded(&self, key: &ProjectGroupKey, expanded: bool, cx: &mut Context<Self>) {
if let Some(mw) = self.multi_workspace.upgrade() {
mw.update(cx, |mw, cx| {
Expand All @@ -504,22 +487,6 @@ impl Sidebar {
}
}

fn set_group_visible_thread_count(
&self,
key: &ProjectGroupKey,
count: Option<usize>,
cx: &mut Context<Self>,
) {
if let Some(mw) = self.multi_workspace.upgrade() {
mw.update(cx, |mw, cx| {
if let Some(state) = mw.group_state_by_key_mut(key) {
state.visible_thread_count = count;
}
mw.serialize(cx);
});
}
}

fn is_active_workspace(&self, workspace: &Entity<Workspace>, cx: &App) -> bool {
self.multi_workspace
.upgrade()
Expand Down Expand Up @@ -1250,55 +1217,13 @@ impl Sidebar {
continue;
}

let total = threads.len();

let extra_batches = self.group_extra_batches(&group_key, cx);
let threads_to_show =
DEFAULT_THREADS_SHOWN + (extra_batches * DEFAULT_THREADS_SHOWN);
let count = threads_to_show.min(total);

let mut promoted_threads: HashSet<agent_ui::ThreadId> = HashSet::new();

// Build visible entries in a single pass. Threads within
// the cutoff are always shown. Threads beyond it are shown
// only if they should be promoted (running, waiting, or
// focused)
for (index, thread) in threads.into_iter().enumerate() {
let is_hidden = index >= count;

if is_hidden {
let is_notified = notified_threads.contains(&thread.metadata.thread_id);
let is_promoted = thread.status == AgentThreadStatus::Running
|| thread.status == AgentThreadStatus::WaitingForConfirmation
|| is_notified
|| self.active_entry.as_ref().is_some_and(|active| {
active.matches_entry(&ListEntry::Thread(thread.clone()))
});
if is_promoted {
promoted_threads.insert(thread.metadata.thread_id);
}
let is_in_promoted = promoted_threads.contains(&thread.metadata.thread_id);
if !is_in_promoted {
continue;
}
}

for thread in threads {
if let Some(sid) = &thread.metadata.session_id {
current_session_ids.insert(sid.clone());
}
current_thread_ids.insert(thread.metadata.thread_id);
entries.push(thread.into());
}

let visible = count + promoted_threads.len();
let is_fully_expanded = visible >= total;

if total > DEFAULT_THREADS_SHOWN {
entries.push(ListEntry::ViewMore {
key: group_key.clone(),
is_fully_expanded,
});
}
}
}

Expand Down Expand Up @@ -1414,10 +1339,6 @@ impl Sidebar {
)
}
ListEntry::Thread(thread) => self.render_thread(ix, thread, is_active, is_selected, cx),
ListEntry::ViewMore {
key,
is_fully_expanded,
} => self.render_view_more(ix, key, *is_fully_expanded, is_selected, cx),
};

if is_group_header_after_first {
Expand Down Expand Up @@ -1485,8 +1406,6 @@ impl Sidebar {
};

let key_for_toggle = key.clone();
let key_for_collapse = key.clone();
let view_more_expanded = self.group_extra_batches(key, cx) > 0;

let label = if highlight_positions.is_empty() {
Label::new(label.clone())
Expand Down Expand Up @@ -1632,30 +1551,6 @@ impl Sidebar {
},
))
})
.when(has_threads && view_more_expanded && !is_collapsed, |this| {
this.child(
IconButton::new(
SharedString::from(format!(
"{id_prefix}project-header-collapse-{ix}",
)),
IconName::ListCollapse,
)
.icon_size(IconSize::Small)
.tooltip(Tooltip::text("Show Fewer Threads"))
.on_click(cx.listener({
let key_for_collapse = key_for_collapse.clone();
move |this, _, _window, cx| {
this.selection = None;
this.set_group_visible_thread_count(
&key_for_collapse,
None,
cx,
);
this.update_entries(cx);
}
})),
)
})
.child(self.render_project_header_ellipsis_menu(ix, id_prefix, key, cx)),
)
.tooltip(Tooltip::element({
Expand Down Expand Up @@ -2146,18 +2041,6 @@ impl Sidebar {
}
}
}
ListEntry::ViewMore {
key,
is_fully_expanded,
..
} => {
let key = key.clone();
if *is_fully_expanded {
self.reset_thread_group_expansion(&key, cx);
} else {
self.expand_thread_group(&key, cx);
}
}
}
}

Expand Down Expand Up @@ -2718,7 +2601,7 @@ impl Sidebar {
self.update_entries(cx);
}
}
Some(ListEntry::Thread(_) | ListEntry::ViewMore { .. }) => {
Some(ListEntry::Thread(_)) => {
for i in (0..ix).rev() {
if let Some(ListEntry::ProjectHeader { key, .. }) = self.contents.entries.get(i)
{
Expand All @@ -2745,7 +2628,7 @@ impl Sidebar {
// Find the group header for the current selection.
let header_ix = match self.contents.entries.get(ix) {
Some(ListEntry::ProjectHeader { .. }) => Some(ix),
Some(ListEntry::Thread(_) | ListEntry::ViewMore { .. }) => (0..ix).rev().find(|&i| {
Some(ListEntry::Thread(_)) => (0..ix).rev().find(|&i| {
matches!(
self.contents.entries.get(i),
Some(ListEntry::ProjectHeader { .. })
Expand Down Expand Up @@ -3448,7 +3331,6 @@ impl Sidebar {
timestamp,
})
}
_ => None,
})
.collect();

Expand Down Expand Up @@ -3855,38 +3737,6 @@ impl Sidebar {
.anchor(gpui::Corner::BottomRight)
}

fn render_view_more(
&self,
ix: usize,
key: &ProjectGroupKey,
is_fully_expanded: bool,
is_selected: bool,
cx: &mut Context<Self>,
) -> AnyElement {
let key = key.clone();
let id = SharedString::from(format!("view-more-{}", ix));

let label: SharedString = if is_fully_expanded {
"Collapse".into()
} else {
"View More".into()
};

ThreadItem::new(id, label)
.focused(is_selected)
.icon_visible(false)
.title_label_color(Color::Muted)
.on_click(cx.listener(move |this, _, _window, cx| {
this.selection = None;
if is_fully_expanded {
this.reset_thread_group_expansion(&key, cx);
} else {
this.expand_thread_group(&key, cx);
}
}))
.into_any_element()
}

fn new_thread_in_group(
&mut self,
_: &NewThreadInGroup,
Expand Down Expand Up @@ -3943,7 +3793,7 @@ impl Sidebar {
let ix = self.selection?;
match self.contents.entries.get(ix) {
Some(ListEntry::ProjectHeader { key, .. }) => Some(key.clone()),
Some(ListEntry::Thread(_) | ListEntry::ViewMore { .. }) => {
Some(ListEntry::Thread(_)) => {
(0..ix)
.rev()
.find_map(|i| match self.contents.entries.get(i) {
Expand Down Expand Up @@ -4120,59 +3970,6 @@ impl Sidebar {
self.cycle_thread_impl(false, window, cx);
}

fn expand_thread_group(&mut self, project_group_key: &ProjectGroupKey, cx: &mut Context<Self>) {
let current = self.group_extra_batches(project_group_key, cx);
self.set_group_visible_thread_count(project_group_key, Some(current + 1), cx);
self.update_entries(cx);
}

fn reset_thread_group_expansion(
&mut self,
project_group_key: &ProjectGroupKey,
cx: &mut Context<Self>,
) {
self.set_group_visible_thread_count(project_group_key, None, cx);
self.update_entries(cx);
}

fn collapse_thread_group(
&mut self,
project_group_key: &ProjectGroupKey,
cx: &mut Context<Self>,
) {
let batches = self.group_extra_batches(project_group_key, cx);
match batches {
0 => return,
1 => self.set_group_visible_thread_count(project_group_key, None, cx),
_ => self.set_group_visible_thread_count(project_group_key, Some(batches - 1), cx),
}
self.update_entries(cx);
}

fn on_show_more_threads(
&mut self,
_: &ShowMoreThreads,
_window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(active_key) = self.active_project_group_key(cx) else {
return;
};
self.expand_thread_group(&active_key, cx);
}

fn on_show_fewer_threads(
&mut self,
_: &ShowFewerThreads,
_window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(active_key) = self.active_project_group_key(cx) else {
return;
};
self.collapse_thread_group(&active_key, cx);
}

fn render_no_results(&self, cx: &mut Context<Self>) -> impl IntoElement {
let has_query = self.has_filter_query(cx);
let message = if has_query {
Expand Down Expand Up @@ -4729,8 +4526,6 @@ impl Render for Sidebar {
.on_action(cx.listener(Self::on_previous_project))
.on_action(cx.listener(Self::on_next_thread))
.on_action(cx.listener(Self::on_previous_thread))
.on_action(cx.listener(Self::on_show_more_threads))
.on_action(cx.listener(Self::on_show_fewer_threads))
.on_action(cx.listener(|this, _: &OpenRecent, window, cx| {
this.recent_projects_popover_handle.toggle(window, cx);
}))
Expand Down
Loading
Loading