From 779683de4b6c9bf6951632f55dad95e25f64622c Mon Sep 17 00:00:00 2001 From: filipriec Date: Tue, 15 Apr 2025 00:10:28 +0200 Subject: [PATCH] buffer movement is now working as I would only wish it would. Needs layers implementation, will do in the future --- client/src/components/handlers/buffer_list.rs | 4 +++- client/src/state/app/state.rs | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/client/src/components/handlers/buffer_list.rs b/client/src/components/handlers/buffer_list.rs index 60f13a5..1086036 100644 --- a/client/src/components/handlers/buffer_list.rs +++ b/client/src/components/handlers/buffer_list.rs @@ -48,7 +48,7 @@ pub fn render_buffer_list( let mut current_width = 0; for (i, view) in history.iter().enumerate() { - let is_active = i == history_len - 1; + let is_active = i == app_state.ui.active_buffer_index; let buffer_name = view.display_name(); let buffer_text = format!(" {} ", buffer_name); let text_width = UnicodeWidthStr::width(buffer_text.as_str()); @@ -60,6 +60,7 @@ pub fn render_buffer_list( } if i > 0 { + let prev_is_active = i - 1 == app_state.ui.active_buffer_index; let sep_style = if is_active { separator_style_inactive_to_active } else { @@ -73,6 +74,7 @@ pub fn render_buffer_list( spans.push(Span::styled(buffer_text, text_style)); current_width += text_width; + let next_is_active = i + 1 < history_len && i + 1 == app_state.ui.active_buffer_index; let sep_style = if is_active { separator_style_active_to_inactive } else { diff --git a/client/src/state/app/state.rs b/client/src/state/app/state.rs index b7f49c5..a81d2e1 100644 --- a/client/src/state/app/state.rs +++ b/client/src/state/app/state.rs @@ -49,6 +49,7 @@ pub struct UiState { pub show_register: bool, pub focus_outside_canvas: bool, pub buffer_history: Vec, + pub active_buffer_index: usize, pub dialog: DialogState, } @@ -166,6 +167,7 @@ impl Default for UiState { show_buffer_list: false, focus_outside_canvas: false, buffer_history: vec![AppView::Intro], + active_buffer_index: 0, dialog: DialogState::default(), } } @@ -186,10 +188,18 @@ impl Default for DialogState { } impl UiState { - /// Adds the given view to the history if it's different from the last one. + /// Updates the buffer history and active index. pub fn update_buffer_history(&mut self, view: AppView) { - if self.buffer_history.last() != Some(&view) { - self.buffer_history.push(view.clone()); + let existing_pos = self.buffer_history.iter().position(|v| v == &view); + + match existing_pos { + Some(pos) => { + self.active_buffer_index = pos; + } + None => { + self.buffer_history.push(view.clone()); + self.active_buffer_index = self.buffer_history.len() - 1; + } } } }