From: Simon Tatham Date: Tue, 2 Jan 2024 13:56:46 +0000 (+0000) Subject: Editor: make the cursor position persist. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c575bff6116c4596ff48e9cefe1911844f0244e2;p=mastodonochrome.git Editor: make the cursor position persist. Then we'll still have it available to refer to when processing keystrokes. --- diff --git a/src/editor.rs b/src/editor.rs index 343db26..5ed64cb 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -718,6 +718,7 @@ struct Composer { core: EditorCore, regions: Vec, layout: Vec, + cursor_pos: Option<(usize, usize)>, scanner: &'static Scan, conf: InstanceStatusConfig, last_size: Option<(usize, usize)>, @@ -738,6 +739,7 @@ impl Composer { }, regions: Vec::new(), layout: Vec::new(), + cursor_pos: None, scanner: Scan::get(), conf, last_size: None, @@ -965,11 +967,23 @@ impl Composer { Some(cs) } + fn determine_cursor_pos(&self) -> Option<(usize, usize)> { + let cursor_cell_index = self.layout + .partition_point(|cell| cell.pos < self.core.point); + if let Some(cell) = self.layout.get(cursor_cell_index) { + if cell.pos == self.core.point { + return Some((cell.x, cell.y)); + } + } + None // _probably_ shouldn't happen unless something is confused + } + fn post_update(&mut self) { self.regions = self.make_regions(&self.core); if let Some((w, _h)) = self.last_size { self.layout = self.layout(w); } + self.cursor_pos = self.determine_cursor_pos(); } } @@ -1184,15 +1198,11 @@ impl ActivityState for Composer { } let mut cursor_pos = CursorPosition::None; - let cursor_cell_index = self.layout - .partition_point(|cell| cell.pos < self.core.point); - if let Some(cell) = self.layout.get(cursor_cell_index) { - if cell.pos == self.core.point { - if let Some(y) = cell.y.checked_sub(ytop) { - let y = y + ystart; - if y < h { - cursor_pos = CursorPosition::At(cell.x, y); - } + if let Some((cx, cy)) = self.cursor_pos { + if let Some(y) = cy.checked_sub(ytop) { + let cy = y + ystart; + if cy < h { + cursor_pos = CursorPosition::At(cx, cy); } } }