chiark / gitweb /
Generalise EditableMenuLine's data update.
authorSimon Tatham <anakin@pobox.com>
Sat, 20 Jan 2024 11:46:22 +0000 (11:46 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 20 Jan 2024 12:21:24 +0000 (12:21 +0000)
This allows the Data parameter type to bake in extra information which
is not affected by the editor providing it with new text. The idea is
to allow one password editor field to know about the password in the
other field, so as to display differently if they don't match.

src/editor.rs
src/options.rs

index 7a2d3ca987c16edc6f4faa9050a9cdd53fb6fe5e..9f1fbe43a61219cb728326c4f12b9305a029fa15 100644 (file)
@@ -751,7 +751,7 @@ pub trait EditableMenuLineData {
 
     fn display(&self) -> ColouredString;
     fn to_text(&self) -> String;
-    fn from_text(text: &str) -> Self;
+    fn update(&mut self, text: &str);
 }
 
 impl EditableMenuLineData for String {
@@ -761,8 +761,8 @@ impl EditableMenuLineData for String {
     fn to_text(&self) -> String {
         self.clone()
     }
-    fn from_text(text: &str) -> Self {
-        text.to_owned()
+    fn update(&mut self, text: &str) {
+        *self = text.to_owned();
     }
 }
 
@@ -781,10 +781,10 @@ impl EditableMenuLineData for Option<String> {
         }
     }
 
-    fn from_text(text: &str) -> Self {
+    fn update(&mut self, text: &str) {
         match text {
-            "" => None,
-            text => Some(text.to_owned()),
+            "" => *self = None,
+            text => *self = Some(text.to_owned()),
         }
     }
 }
@@ -884,7 +884,7 @@ impl<Data: EditableMenuLineData> EditableMenuLine<Data> {
     pub fn handle_keypress(&mut self, key: OurKey) -> bool {
         let (consumed, done) = if let Some(ref mut editor) = self.editor {
             if editor.handle_keypress(key) {
-                self.data = Data::from_text(editor.borrow_text());
+                self.data.update(editor.borrow_text());
                 self.menuline = Self::make_menuline(
                     self.key,
                     &self.description,
@@ -911,8 +911,8 @@ impl<Data: EditableMenuLineData> EditableMenuLine<Data> {
     pub fn is_editing(&self) -> bool {
         self.editor.is_some()
     }
-    pub fn set_data(&mut self, data: Data) {
-        self.data = data;
+    pub fn set_text(&mut self, text: &str) {
+        self.data.update(text);
         self.menuline =
             Self::make_menuline(self.key, &self.description, &self.data);
     }
index fa44fbac59623edb055bb3bf883b7e6f70fc58be..dde5a2ecbb1b72a63c0ca2bcae6042bf2033f964 100644 (file)
@@ -304,8 +304,8 @@ impl EditableMenuLineData for LanguageVector {
         self.0.as_slice().join(",")
     }
 
-    fn from_text(text: &str) -> Self {
-        Self(
+    fn update(&mut self, text: &str) {
+        *self = Self(
             text.split(|c| c == ' ' || c == ',')
                 .filter(|s| !s.is_empty())
                 .map(|s| s.to_owned())