fix: avoid background process when last tab closed

The current behaviour leaves the process running in the background.
Instead, send a `Message::WindowClose` which properly closes the window.
This commit is contained in:
Cheong Lau 2025-10-16 13:03:07 +10:00
parent 2e103dd4eb
commit f16981020b

View file

@ -3766,33 +3766,33 @@ impl Application for App {
} }
} }
Message::TabClose(entity_opt) => { Message::TabClose(entity_opt) => {
let mut tasks = Vec::with_capacity(3); let mut tasks = Vec::with_capacity(2);
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
// Activate closest item // If the last tab is closed, close the window
if let Some(position) = self.tab_model.position(entity) { // Otherwise, activate closest item
let new_position = if position > 0 { if self.tab_model.len() == 1 {
position - 1 tasks.push(Task::future(async move {
} else { cosmic::action::app(Message::WindowClose)
position + 1 }));
}; } else {
if let Some(position) = self.tab_model.position(entity) {
let new_position = if position > 0 {
position - 1
} else {
position + 1
};
if let Some(new_entity) = self.tab_model.entity_at(new_position) { if let Some(new_entity) = self.tab_model.entity_at(new_position) {
tasks.push(self.update(Message::TabActivate(new_entity))); tasks.push(self.update(Message::TabActivate(new_entity)));
}
} }
} }
// Remove item // Remove item
self.tab_model.remove(entity); self.tab_model.remove(entity);
// If that was the last tab, close window
if self.tab_model.iter().next().is_none() {
if let Some(window_id) = self.core.main_window_id() {
tasks.push(window::close(window_id));
}
}
tasks.push(self.update_watcher()); tasks.push(self.update_watcher());
return Task::batch(tasks); return Task::batch(tasks);