Identify active buffer by id instead of index
So the active buffer doesn't change when buffers are reordered
This commit is contained in:
@ -255,9 +255,9 @@ impl App {
|
||||
.buffers
|
||||
.set_active_index(self.buffers.active_index() as isize - 1)
|
||||
},
|
||||
Action::ToBuffer(buffer_index) => {
|
||||
Action::ToBuffer(buffer_id) => {
|
||||
changes_since_last_render = true;
|
||||
self.buffers.set_active_index(buffer_index)
|
||||
self.buffers.set_active_index(buffer_id)
|
||||
},
|
||||
Action::Resize(w, h) => {
|
||||
changes_since_last_render = true;
|
||||
|
@ -146,7 +146,7 @@ pub struct Buffers {
|
||||
/// steal them, even if they are also their child (or it would cause buffers to move
|
||||
/// every time we re-sort the buffer list)
|
||||
attached_to_parent: HashSet<BufferId>,
|
||||
active_index: usize,
|
||||
active_buffer: BufferId,
|
||||
}
|
||||
|
||||
impl Buffers {
|
||||
@ -156,7 +156,7 @@ impl Buffers {
|
||||
children: HashMap::new(),
|
||||
parents: HashMap::new(),
|
||||
attached_to_parent: HashSet::new(),
|
||||
active_index: 0,
|
||||
active_buffer: BufferId::Log,
|
||||
}
|
||||
}
|
||||
|
||||
@ -321,24 +321,40 @@ impl Buffers {
|
||||
}
|
||||
|
||||
pub fn active_index(&self) -> usize {
|
||||
self.active_index
|
||||
self
|
||||
.buffers
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, buf)| buf.id() == self.active_buffer)
|
||||
.map(|(i, _)| i)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn set_active_index(&mut self, index: isize) {
|
||||
self.active_index = index.wrapping_rem_euclid(self.buffers.len() as isize) as usize;
|
||||
self.active_buffer =
|
||||
self.buffers[index.wrapping_rem_euclid(self.buffers.len() as isize) as usize].id();
|
||||
}
|
||||
|
||||
pub fn set_active_id(&mut self, id: BufferId) {
|
||||
self.active_buffer = id
|
||||
}
|
||||
|
||||
pub fn active_buffer(&self) -> &dyn Buffer {
|
||||
&**self
|
||||
.buffers
|
||||
.get(self.active_index)
|
||||
.expect("Active buffer index does not exist")
|
||||
.iter()
|
||||
.find(|buf| buf.id() == self.active_buffer)
|
||||
.unwrap_or(self.buffers.first().expect("No buffers"))
|
||||
}
|
||||
|
||||
pub fn active_buffer_mut(&mut self) -> &mut Box<dyn Buffer> {
|
||||
self
|
||||
match self
|
||||
.buffers
|
||||
.get_mut(self.active_index)
|
||||
.expect("Active buffer index does not exist")
|
||||
.iter()
|
||||
.position(|buf| buf.id() == self.active_buffer)
|
||||
{
|
||||
Some(i) => self.buffers.get_mut(i).expect("Active buffer disappeared"),
|
||||
None => self.buffers.first_mut().expect("No buffers"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,24 +88,24 @@ impl Component for Buflist {
|
||||
return Ok(None);
|
||||
};
|
||||
let column = (x - last_area.x) / self.config.layout.buflist.column_width;
|
||||
let mut buffer_id: isize =
|
||||
let mut buffer_index: isize =
|
||||
(column as isize) * (last_area.height as isize) + ((y as isize) - (last_area.y as isize));
|
||||
let buffer_id_usize: usize = buffer_id.try_into().expect("negative buffer_id");
|
||||
if buffer_id_usize >= last_num_buffers {
|
||||
let buffer_index_usize: usize = buffer_index.try_into().expect("negative buffer_index");
|
||||
if buffer_index_usize >= last_num_buffers {
|
||||
if self.config.layout.buflist.penultimate_right_overflow {
|
||||
// Clicked on the overflow of the last-but-one column
|
||||
buffer_id -= last_area.height as isize;
|
||||
buffer_index -= last_area.height as isize;
|
||||
} else {
|
||||
// Clicked past the last buffer
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
let buffer_id_usize: usize = buffer_id.try_into().expect("negative buffer_id");
|
||||
if buffer_id_usize >= last_num_buffers {
|
||||
let buffer_index_usize: usize = buffer_index.try_into().expect("negative buffer_index");
|
||||
if buffer_index_usize >= last_num_buffers {
|
||||
tracing::error!("[BUG] Unexpected click past the last buffer");
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(Action::ToBuffer(buffer_id)))
|
||||
Ok(Some(Action::ToBuffer(buffer_index)))
|
||||
},
|
||||
_ => Ok(None),
|
||||
}
|
||||
|
Reference in New Issue
Block a user