Rename combo_box::State methods to push and into_options
This commit is contained in:
parent
d1360c74b0
commit
a862290836
1 changed files with 14 additions and 31 deletions
|
|
@ -384,39 +384,23 @@ where
|
||||||
&self.options
|
&self.options
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a `new_option` to the [`State`].
|
/// Pushes a new option to the [`State`].
|
||||||
///
|
pub fn push(&mut self, new_option: T) {
|
||||||
/// A search is performed immediately after the `new_option` is added so the option will be displayed
|
let mut inner = self.inner.borrow_mut();
|
||||||
/// (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
|
inner.option_matchers.push(build_matcher(&new_option));
|
||||||
self.options.push(new_option);
|
self.options.push(new_option);
|
||||||
|
|
||||||
// perform a search to update the options displayed
|
inner.filtered_options = Filtered::new(
|
||||||
let search_results = search(
|
search(&self.options, &inner.option_matchers, &inner.value)
|
||||||
&self.options,
|
.cloned()
|
||||||
&self.inner.borrow().option_matchers,
|
.collect(),
|
||||||
&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.
|
/// Returns ownership of the options of the [`State`].
|
||||||
pub fn extract_options(&mut self) -> Vec<T> {
|
pub fn into_options(self) -> Vec<T> {
|
||||||
let options = std::mem::replace(&mut self.options, vec![]);
|
self.options
|
||||||
*self = Self::new(vec![]);
|
|
||||||
|
|
||||||
options
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn value(&self) -> String {
|
fn value(&self) -> String {
|
||||||
|
|
@ -994,10 +978,9 @@ fn build_matchers<'a, T>(
|
||||||
where
|
where
|
||||||
T: Display + 'a,
|
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<T>(option: T) -> String
|
fn build_matcher<T>(option: T) -> String
|
||||||
where
|
where
|
||||||
T: Display,
|
T: Display,
|
||||||
|
|
@ -1005,4 +988,4 @@ where
|
||||||
let mut matcher = option.to_string();
|
let mut matcher = option.to_string();
|
||||||
matcher.retain(|c| c.is_ascii_alphanumeric());
|
matcher.retain(|c| c.is_ascii_alphanumeric());
|
||||||
matcher.to_lowercase()
|
matcher.to_lowercase()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue