From d1360c74b067f3ff6068042652dd70f7b78fbcf1 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 4 Dec 2024 01:00:52 +0000 Subject: [PATCH 1/2] added add_option and extract_options functions to combobox and split build_matcher (the logic to build a single matcher) out from build matchers --- widget/src/combo_box.rs | 54 +++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index dd87c5a1..651df491 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -384,6 +384,41 @@ where &self.options } + /// Adds a `new_option` to the [`State`]. + /// + /// A search is performed immediately after the `new_option` is added so the option will be displayed + /// (or not) depending on if it matches the existing search + pub fn add_option(&mut self, new_option: T) { + // add option to option matchers + self.inner + .borrow_mut() + .option_matchers + .push(build_matcher(&new_option)); + + // add the option to options + self.options.push(new_option); + + // perform a search to update the options displayed + let search_results = search( + &self.options, + &self.inner.borrow().option_matchers, + &self.inner.borrow().value, + ) + .cloned() + .collect(); + + self.inner.borrow_mut().filtered_options.options = search_results; + self.inner.borrow_mut().filtered_options.updated = Instant::now(); + } + + /// clears all options from the combobox returning the options that were in it. + pub fn extract_options(&mut self) -> Vec { + let options = std::mem::replace(&mut self.options, vec![]); + *self = Self::new(vec![]); + + options + } + fn value(&self) -> String { let inner = self.inner.borrow(); @@ -959,12 +994,15 @@ fn build_matchers<'a, T>( where T: Display + 'a, { - options - .into_iter() - .map(|opt| { - let mut matcher = opt.to_string(); - matcher.retain(|c| c.is_ascii_alphanumeric()); - matcher.to_lowercase() - }) - .collect() + options.into_iter().map(|opt| build_matcher(opt)).collect() } + +/// build an individual matcher, a matcher is the string representation of `T` with all non-alphanumeric characters filtered out and all alphanumeric characters mapped to lowercase +fn build_matcher(option: T) -> String +where + T: Display, +{ + let mut matcher = option.to_string(); + matcher.retain(|c| c.is_ascii_alphanumeric()); + matcher.to_lowercase() +} \ No newline at end of file From a862290836e4194de9362cb5499ce3891ce5a527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 18 Nov 2025 23:27:09 +0100 Subject: [PATCH 2/2] Rename `combo_box::State` methods to `push` and `into_options` --- widget/src/combo_box.rs | 45 +++++++++++++---------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 651df491..0e4e1c17 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -384,39 +384,23 @@ where &self.options } - /// Adds a `new_option` to the [`State`]. - /// - /// A search is performed immediately after the `new_option` is added so the option will be displayed - /// (or not) depending on if it matches the existing search - pub fn add_option(&mut self, new_option: T) { - // add option to option matchers - self.inner - .borrow_mut() - .option_matchers - .push(build_matcher(&new_option)); + /// Pushes a new option to the [`State`]. + pub fn push(&mut self, new_option: T) { + let mut inner = self.inner.borrow_mut(); - // add the option to options + inner.option_matchers.push(build_matcher(&new_option)); self.options.push(new_option); - // perform a search to update the options displayed - let search_results = search( - &self.options, - &self.inner.borrow().option_matchers, - &self.inner.borrow().value, - ) - .cloned() - .collect(); - - self.inner.borrow_mut().filtered_options.options = search_results; - self.inner.borrow_mut().filtered_options.updated = Instant::now(); + inner.filtered_options = Filtered::new( + search(&self.options, &inner.option_matchers, &inner.value) + .cloned() + .collect(), + ); } - /// clears all options from the combobox returning the options that were in it. - pub fn extract_options(&mut self) -> Vec { - let options = std::mem::replace(&mut self.options, vec![]); - *self = Self::new(vec![]); - - options + /// Returns ownership of the options of the [`State`]. + pub fn into_options(self) -> Vec { + self.options } fn value(&self) -> String { @@ -994,10 +978,9 @@ fn build_matchers<'a, T>( where T: Display + 'a, { - options.into_iter().map(|opt| build_matcher(opt)).collect() + options.into_iter().map(build_matcher).collect() } -/// build an individual matcher, a matcher is the string representation of `T` with all non-alphanumeric characters filtered out and all alphanumeric characters mapped to lowercase fn build_matcher(option: T) -> String where T: Display, @@ -1005,4 +988,4 @@ where let mut matcher = option.to_string(); matcher.retain(|c| c.is_ascii_alphanumeric()); matcher.to_lowercase() -} \ No newline at end of file +}