From: Simon Tatham Date: Sat, 20 Jan 2024 11:46:22 +0000 (+0000) Subject: Generalise EditableMenuLine's data update. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c89810e6f36d5d2a725eb97bb2375af5d9e5252a;p=mastodonochrome.git Generalise EditableMenuLine's data update. 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. --- diff --git a/src/editor.rs b/src/editor.rs index 7a2d3ca..9f1fbe4 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -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 { } } - 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 EditableMenuLine { 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 EditableMenuLine { 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); } diff --git a/src/options.rs b/src/options.rs index fa44fba..dde5a2e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -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())