added add_option and extract_options functions to combobox and split build_matcher (the logic to build a single matcher) out from build matchers
This commit is contained in:
parent
645643bfd6
commit
d1360c74b0
1 changed files with 46 additions and 8 deletions
|
|
@ -384,6 +384,41 @@ where
|
||||||
&self.options
|
&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<T> {
|
||||||
|
let options = std::mem::replace(&mut self.options, vec![]);
|
||||||
|
*self = Self::new(vec![]);
|
||||||
|
|
||||||
|
options
|
||||||
|
}
|
||||||
|
|
||||||
fn value(&self) -> String {
|
fn value(&self) -> String {
|
||||||
let inner = self.inner.borrow();
|
let inner = self.inner.borrow();
|
||||||
|
|
||||||
|
|
@ -959,12 +994,15 @@ fn build_matchers<'a, T>(
|
||||||
where
|
where
|
||||||
T: Display + 'a,
|
T: Display + 'a,
|
||||||
{
|
{
|
||||||
options
|
options.into_iter().map(|opt| build_matcher(opt)).collect()
|
||||||
.into_iter()
|
}
|
||||||
.map(|opt| {
|
|
||||||
let mut matcher = opt.to_string();
|
/// 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
|
||||||
matcher.retain(|c| c.is_ascii_alphanumeric());
|
fn build_matcher<T>(option: T) -> String
|
||||||
matcher.to_lowercase()
|
where
|
||||||
})
|
T: Display,
|
||||||
.collect()
|
{
|
||||||
|
let mut matcher = option.to_string();
|
||||||
|
matcher.retain(|c| c.is_ascii_alphanumeric());
|
||||||
|
matcher.to_lowercase()
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue