chiark / gitweb /
Revise menu width handling via a new trait.
authorSimon Tatham <anakin@pobox.com>
Thu, 11 Jan 2024 17:56:26 +0000 (17:56 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 12 Jan 2024 12:52:04 +0000 (12:52 +0000)
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.

src/menu.rs
src/posting.rs
src/text.rs

index c8857734e90d51d0b4f724fe60b090fbd100be2f..2ca86d6880e9cf7708a622391892e0d66b9bcc31 100644 (file)
@@ -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 {
index cae15938087df610eaed7de26454e711a23060c0..9582a99ac46d2100b7e6704789ff57b76f77250c 100644 (file)
@@ -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();
index 1599b5e0a496bb411e0d3db62c14499589e4f2cd..d83c2fbb8877c913fef36e5e8e208be5c3c44875 100644 (file)
@@ -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);
     }