Skip to content
Open
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
25 changes: 11 additions & 14 deletions crates/gpui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl SystemWindowTabController {
.find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));

let current_group = current_group?;
// TODO: `.keys()` returns arbitrary order, what does "next" mean?
let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
let idx = group_ids.iter().position(|g| *g == current_group)?;
let next_idx = (idx + 1) % group_ids.len();
Expand All @@ -320,6 +321,7 @@ impl SystemWindowTabController {
.find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));

let current_group = current_group?;
// TODO: `.keys()` returns arbitrary order, what does "previous" mean?
let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
let idx = group_ids.iter().position(|g| *g == current_group)?;
let prev_idx = if idx == 0 {
Expand All @@ -341,12 +343,9 @@ impl SystemWindowTabController {

/// Get all tabs in the same window.
pub fn tabs(&self, id: WindowId) -> Option<&Vec<SystemWindowTab>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has become such a small helper that I'd consider removing it entirely. It's only called a few times and the majority of the time, we use .position() right after to find the index of tab with the right WindowId. Which could be done right away instead of using the helper first, thus removing an iteration through the values.

let tab_group = self
.tab_groups
.iter()
.find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| *group))?;

self.tab_groups.get(&tab_group)
self.tab_groups
.values()
.find(|tabs| tabs.iter().any(|tab| tab.id == id))
}

/// Initialize the visibility of the system window tab controller.
Expand Down Expand Up @@ -421,7 +420,7 @@ impl SystemWindowTabController {
/// Insert a tab into a tab group.
pub fn add_tab(cx: &mut App, id: WindowId, tabs: Vec<SystemWindowTab>) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let Some(tab) = tabs.clone().into_iter().find(|tab| tab.id == id) else {
let Some(tab) = tabs.iter().find(|tab| tab.id == id).cloned() else {
return;
};

Expand Down Expand Up @@ -484,16 +483,14 @@ impl SystemWindowTabController {
return;
};

let initial_tabs_len = initial_tabs.len();
let mut all_tabs = initial_tabs.clone();
for tabs in controller.tab_groups.values() {
all_tabs.extend(
tabs.iter()
.filter(|tab| !initial_tabs.contains(tab))
.cloned(),
);

for (_, mut tabs) in controller.tab_groups.drain() {
tabs.retain(|tab| !all_tabs[..initial_tabs_len].contains(tab));
all_tabs.extend(tabs);
}

controller.tab_groups.clear();
controller.tab_groups.insert(0, all_tabs);
}

Expand Down
Loading