diff options
author | 2024-08-05 23:12:26 +0200 | |
---|---|---|
committer | 2024-08-05 23:12:26 +0200 | |
commit | 3a3fda83cd15506ee7bb629ae44bd2eab203bb5f (patch) | |
tree | e5e78c9af2a632ff77e02ab3fce6e7d58c515ce7 /widget/src/combo_box.rs | |
parent | 145c3dc8fc4f92c400fbc3f8202ed22e1d498663 (diff) | |
download | iced-3a3fda83cd15506ee7bb629ae44bd2eab203bb5f.tar.gz iced-3a3fda83cd15506ee7bb629ae44bd2eab203bb5f.tar.bz2 iced-3a3fda83cd15506ee7bb629ae44bd2eab203bb5f.zip |
Implement `State::options` for `combo_box`
Diffstat (limited to 'widget/src/combo_box.rs')
-rw-r--r-- | widget/src/combo_box.rs | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 0a4624cb..62785b2c 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -208,12 +208,14 @@ where /// The local state of a [`ComboBox`]. #[derive(Debug, Clone)] -pub struct State<T>(RefCell<Inner<T>>); +pub struct State<T> { + options: Vec<T>, + inner: RefCell<Inner<T>>, +} #[derive(Debug, Clone)] struct Inner<T> { value: String, - options: Vec<T>, option_matchers: Vec<String>, filtered_options: Filtered<T>, } @@ -247,34 +249,44 @@ where .collect(), ); - Self(RefCell::new(Inner { - value, + Self { options, - option_matchers, - filtered_options, - })) + inner: RefCell::new(Inner { + value, + option_matchers, + filtered_options, + }), + } + } + + /// Returns the options of the [`State`]. + /// + /// These are the options provided when the [`State`] + /// was constructed with [`State::new`]. + pub fn options(&self) -> &[T] { + &self.options } fn value(&self) -> String { - let inner = self.0.borrow(); + let inner = self.inner.borrow(); inner.value.clone() } fn with_inner<O>(&self, f: impl FnOnce(&Inner<T>) -> O) -> O { - let inner = self.0.borrow(); + let inner = self.inner.borrow(); f(&inner) } fn with_inner_mut(&self, f: impl FnOnce(&mut Inner<T>)) { - let mut inner = self.0.borrow_mut(); + let mut inner = self.inner.borrow_mut(); f(&mut inner); } fn sync_filtered_options(&self, options: &mut Filtered<T>) { - let inner = self.0.borrow(); + let inner = self.inner.borrow(); inner.filtered_options.sync(options); } @@ -440,7 +452,7 @@ where state.filtered_options.update( search( - &state.options, + &self.state.options, &state.option_matchers, &state.value, ) @@ -589,7 +601,7 @@ where if let Some(selection) = menu.new_selection.take() { // Clear the value and reset the options and menu state.value = String::new(); - state.filtered_options.update(state.options.clone()); + state.filtered_options.update(self.state.options.clone()); menu.menu = menu::State::default(); // Notify the selection |