From bfab49106d7d673b38d79d5661ab97d077942f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Fri, 10 Jul 2026 12:09:30 -0400 Subject: [PATCH] fix(tabs): keep the remembered active tab when closing a background tab --- crates/ely_browser_core/src/state/tabs.rs | 18 ++++++++++--- .../tests/tab_active_memory.rs | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 crates/ely_browser_core/tests/tab_active_memory.rs diff --git a/crates/ely_browser_core/src/state/tabs.rs b/crates/ely_browser_core/src/state/tabs.rs index 5739f88..5fdc0d3 100644 --- a/crates/ely_browser_core/src/state/tabs.rs +++ b/crates/ely_browser_core/src/state/tabs.rs @@ -104,18 +104,28 @@ impl BrowserCore { let closed_space_id = closed_tab.space_id().clone(); let closed_profile_id = closed_tab.profile_id().clone(); let was_space_active_tab = self.active_tabs_by_space.get(&closed_space_id) == Some(tab_id); + let was_profile_active_tab = self + .active_tabs_by_space_profile + .get(&(closed_space_id.clone(), closed_profile_id.clone())) + == Some(tab_id); closed_tab.clear_split_id(); self.detach_tab_from_split(tab_id); - self.active_tabs_by_space_profile - .remove(&(closed_space_id.clone(), closed_profile_id.clone())); + if was_profile_active_tab { + self.active_tabs_by_space_profile + .remove(&(closed_space_id.clone(), closed_profile_id.clone())); + } self.archived_tabs.push(ArchivedTab::new(closed_tab, ArchiveSource::ManualClose)); if let Some(next_tab_id) = self.nearest_tab_in_space(&closed_space_id, close_index) { if was_space_active_tab { self.active_tabs_by_space.insert(closed_space_id.clone(), next_tab_id.clone()); } - if let Some(next_profile_tab_id) = - self.nearest_tab_in_space_profile(&closed_space_id, &closed_profile_id, close_index) + if was_profile_active_tab + && let Some(next_profile_tab_id) = self.nearest_tab_in_space_profile( + &closed_space_id, + &closed_profile_id, + close_index, + ) { self.active_tabs_by_space_profile .insert((closed_space_id, closed_profile_id), next_profile_tab_id); diff --git a/crates/ely_browser_core/tests/tab_active_memory.rs b/crates/ely_browser_core/tests/tab_active_memory.rs new file mode 100644 index 0000000..4846298 --- /dev/null +++ b/crates/ely_browser_core/tests/tab_active_memory.rs @@ -0,0 +1,25 @@ +use std::error::Error; + +use ely_browser_core::{BrowserCore, InitialBrowserConfig}; +use ely_domain::{ProfileKind, UrlText}; + +#[test] +fn closing_a_background_tab_keeps_the_remembered_active_tab() -> Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + let first_profile_id = core.active_tab()?.profile_id().clone(); + let tab_a = core.open_tab(UrlText::parse("https://example.com/a")?); + let _tab_b = core.open_tab(UrlText::parse("https://example.com/b")?); + let tab_c = core.open_tab(UrlText::parse("https://example.com/c")?); + core.select_tab(&tab_a)?; + + core.close_tab(&tab_c)?; + + core.create_profile("Second", 0xf54e00, ProfileKind::Standard)?; + let restored = core.select_profile(&first_profile_id)?; + + assert_eq!( + restored, tab_a, + "closing a background tab must not clobber the remembered active tab" + ); + Ok(()) +}