From: Simon Tatham Date: Thu, 11 Jan 2024 17:56:26 +0000 (+0000) Subject: Revise menu width handling via a new trait. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=cd7da63796704843da96b9cdc9f7e14501b0cbfd;p=mastodonochrome.git Revise menu width handling via a new trait. get_widths has become check_widths, which subsumes the processing done at every call site, and will be easier to extend to the next thing I have in mind. Also, the three width-aligning methods for menu lines are now part of the MenuKeypressLineGeneral trait. That only has one implementing type right now, but another one will be along shortly. --- diff --git a/src/menu.rs b/src/menu.rs index c885773..2ca86d6 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::cmp::max; use itertools::Itertools; use super::activity_stack::{ @@ -136,9 +135,7 @@ impl Menu { let mut rmaxwid = 0; for line in &self.lines { if let MenuLine::Key(mk) = line { - let (lwid, rwid) = mk.get_widths(); - lmaxwid = max(lmaxwid, lwid); - rmaxwid = max(rmaxwid, rwid); + mk.check_widths(&mut lmaxwid, &mut rmaxwid); } } for line in &mut self.lines { diff --git a/src/posting.rs b/src/posting.rs index cae1593..9582a99 100644 --- a/src/posting.rs +++ b/src/posting.rs @@ -1,5 +1,4 @@ use itertools::Itertools; -use std::cmp::max; use std::iter::once; use strum::IntoEnumIterator; use sys_locale::get_locale; @@ -158,21 +157,16 @@ impl PostMenu { fn fix_widths(&mut self) -> (usize, usize) { let mut lmaxwid = 0; let mut rmaxwid = 0; - let mut check_widths = |ml: &MenuKeypressLine| { - let (lwid, rwid) = ml.get_widths(); - lmaxwid = max(lmaxwid, lwid); - rmaxwid = max(rmaxwid, rwid); - }; - check_widths(&self.ml_post); - check_widths(&self.ml_cancel); - check_widths(&self.ml_edit); - check_widths(&self.ml_content_warning); - check_widths(&self.ml_language); + self.ml_post.check_widths(&mut lmaxwid, &mut rmaxwid); + self.ml_cancel.check_widths(&mut lmaxwid, &mut rmaxwid); + self.ml_edit.check_widths(&mut lmaxwid, &mut rmaxwid); + self.ml_content_warning.check_widths(&mut lmaxwid, &mut rmaxwid); + self.ml_language.check_widths(&mut lmaxwid, &mut rmaxwid); for vis in Visibility::iter() { - check_widths(&Self::visibility_item(vis)); + Self::visibility_item(vis).check_widths(&mut lmaxwid, &mut rmaxwid); } if let Some(ref ml) = self.editor_prompt { - check_widths(ml); + ml.check_widths(&mut lmaxwid, &mut rmaxwid); } self.ml_post.reset_widths(); diff --git a/src/text.rs b/src/text.rs index 1599b5e..d83c2fb 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1711,6 +1711,12 @@ pub struct MenuKeypressLine { rmaxwid: usize, } +pub trait MenuKeypressLineGeneral { + fn check_widths(&self, lmaxwid: &mut usize, rmaxwid: &mut usize); + fn reset_widths(&mut self); + fn ensure_widths(&mut self, lmaxwid: usize, rmaxwid: usize); +} + impl MenuKeypressLine { pub fn new(key: OurKey, description: ColouredString) -> Self { @@ -1729,13 +1735,18 @@ impl MenuKeypressLine { rmaxwid: rwid, } } +} - pub fn get_widths(&self) -> (usize, usize) { (self.lwid, self.rwid) } - pub fn reset_widths(&mut self) { +impl MenuKeypressLineGeneral for MenuKeypressLine { + fn check_widths(&self, lmaxwid: &mut usize, rmaxwid: &mut usize) { + *lmaxwid = max(*lmaxwid, self.lwid); + *rmaxwid = max(*rmaxwid, self.rwid); + } + fn reset_widths(&mut self) { self.lmaxwid = self.lwid; self.rmaxwid = self.rwid; } - pub fn ensure_widths(&mut self, lmaxwid: usize, rmaxwid: usize) { + fn ensure_widths(&mut self, lmaxwid: usize, rmaxwid: usize) { self.lmaxwid = max(self.lmaxwid, lmaxwid); self.rmaxwid = max(self.rmaxwid, rmaxwid); }