Activate navbar selection on init and when switching tabs

This commit is contained in:
Jason Rodney Hansen 2024-08-14 18:43:10 -06:00
parent 8e8ea237f1
commit 4496a55682

View file

@ -378,7 +378,7 @@ pub struct App {
impl App { impl App {
fn open_tab(&mut self, location: Location) -> Command<Message> { fn open_tab(&mut self, location: Location) -> Command<Message> {
let tab = Tab::new(location.clone(), self.config.tab.clone()); let tab = Tab::new(location.clone(), self.config.tab);
let entity = self let entity = self
.tab_model .tab_model
.insert() .insert()
@ -387,6 +387,9 @@ impl App {
.closable() .closable()
.activate() .activate()
.id(); .id();
self.activate_nav_model_location(&location);
Command::batch([ Command::batch([
self.update_title(), self.update_title(),
self.update_watcher(), self.update_watcher(),
@ -482,6 +485,22 @@ impl App {
cosmic::app::command::set_theme(self.config.app_theme.theme()) cosmic::app::command::set_theme(self.config.app_theme.theme())
} }
fn activate_nav_model_location(&mut self, location: &Location) {
let nav_bar_id = self.nav_model.iter().find(|&id| {
self.nav_model
.data::<Location>(id)
.map(|l| l == location)
.unwrap_or_default()
});
if let Some(id) = nav_bar_id {
self.nav_model.activate(id);
} else {
let active = self.nav_model.active();
segmented_button::Selectable::deactivate(&mut self.nav_model, active);
}
}
fn update_nav_model(&mut self) { fn update_nav_model(&mut self) {
let mut nav_model = segmented_button::ModelBuilder::default(); let mut nav_model = segmented_button::ModelBuilder::default();
for (favorite_i, favorite) in self.config.favorites.iter().enumerate() { for (favorite_i, favorite) in self.config.favorites.iter().enumerate() {
@ -543,6 +562,11 @@ impl App {
} }
self.nav_model = nav_model.build(); self.nav_model = nav_model.build();
let tab_entity = self.tab_model.active();
if let Some(tab) = self.tab_model.data::<Tab>(tab_entity) {
self.activate_nav_model_location(&tab.location.clone());
}
} }
fn update_notification(&mut self) -> Command<Message> { fn update_notification(&mut self) -> Command<Message> {
@ -1113,7 +1137,7 @@ impl Application for App {
fn on_nav_select(&mut self, entity: Entity) -> Command<Self::Message> { fn on_nav_select(&mut self, entity: Entity) -> Command<Self::Message> {
self.search_active = false; self.search_active = false;
self.search_input.clear(); self.search_input.clear();
self.nav_model.activate(entity); self.nav_model.activate(entity);
if let Some(location) = self.nav_model.data::<Location>(entity) { if let Some(location) = self.nav_model.data::<Location>(entity) {
let message = Message::TabMessage(None, tab::Message::Location(location.clone())); let message = Message::TabMessage(None, tab::Message::Location(location.clone()));
@ -1755,6 +1779,11 @@ impl Application for App {
} }
Message::TabActivate(entity) => { Message::TabActivate(entity) => {
self.tab_model.activate(entity); self.tab_model.activate(entity);
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
self.activate_nav_model_location(&tab.location.clone());
}
return self.update_title(); return self.update_title();
} }
Message::TabNext => { Message::TabNext => {
@ -1795,10 +1824,18 @@ impl Application for App {
// Activate closest item // Activate closest item
if let Some(position) = self.tab_model.position(entity) { if let Some(position) = self.tab_model.position(entity) {
if position > 0 { let new_position = if position > 0 {
self.tab_model.activate_position(position - 1); position - 1
} else { } else {
self.tab_model.activate_position(position + 1); position + 1
};
if self.tab_model.activate_position(new_position) {
if let Some(new_entity) = self.tab_model.entity_at(new_position) {
if let Some(tab) = self.tab_model.data::<Tab>(new_entity) {
self.activate_nav_model_location(&tab.location.clone());
}
}
} }
} }
@ -1852,17 +1889,7 @@ impl Application for App {
commands.push(self.update(action.message(Some(entity)))); commands.push(self.update(action.message(Some(entity))));
} }
tab::Command::ChangeLocation(tab_title, tab_path) => { tab::Command::ChangeLocation(tab_title, tab_path) => {
// Activate nav bar item with matching location. self.activate_nav_model_location(&tab_path);
let nav_bar_id = self.nav_model.iter().find(|&id| {
self.nav_model
.data::<Location>(id)
.map(|location| &tab_path == location)
.unwrap_or_default()
});
if let Some(id) = nav_bar_id {
self.nav_model.activate(id);
}
self.tab_model.text_set(entity, tab_title); self.tab_model.text_set(entity, tab_title);
commands.push(Command::batch([ commands.push(Command::batch([