pub struct FactoryHashMap<K, C: FactoryComponent, S = RandomState> { /* private fields */ }Expand description
A container similar to HashMap that can be used to store
values of type FactoryComponent.
Implementations§
Source§impl<K, C> FactoryHashMap<K, C, RandomState>where
C: FactoryComponent,
impl<K, C> FactoryHashMap<K, C, RandomState>where
C: FactoryComponent,
Sourcepub fn builder() -> FactoryHashMapBuilder<K, C>
pub fn builder() -> FactoryHashMapBuilder<K, C>
Creates a new FactoryHashMap.
Examples found in repository?
144 fn init(
145 counter: Self::Init,
146 root: Self::Root,
147 sender: ComponentSender<Self>,
148 ) -> ComponentParts<Self> {
149 let counters = FactoryHashMap::builder().launch_default().detach();
150
151 let model = App {
152 created_widgets: counter,
153 counters,
154 entry_buffer: gtk::EntryBuffer::default(),
155 };
156
157 let counter_stack = model.counters.widget();
158 let widgets = view_output!();
159
160 ComponentParts { model, widgets }
161 }Source§impl<K, C, S> FactoryHashMap<K, C, S>where
C: FactoryComponent,
impl<K, C, S> FactoryHashMap<K, C, S>where
C: FactoryComponent,
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the FactoryHashMap.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the FactoryHashMap is empty.
Sourcepub const fn widget(&self) -> &C::ParentWidget
pub const fn widget(&self) -> &C::ParentWidget
Returns the widget all components are attached to.
Examples found in repository?
144 fn init(
145 counter: Self::Init,
146 root: Self::Root,
147 sender: ComponentSender<Self>,
148 ) -> ComponentParts<Self> {
149 let counters = FactoryHashMap::builder().launch_default().detach();
150
151 let model = App {
152 created_widgets: counter,
153 counters,
154 entry_buffer: gtk::EntryBuffer::default(),
155 };
156
157 let counter_stack = model.counters.widget();
158 let widgets = view_output!();
159
160 ComponentParts { model, widgets }
161 }Sourcepub fn iter(&self) -> impl ExactSizeIterator<Item = (&K, &C)> + FusedIterator
pub fn iter(&self) -> impl ExactSizeIterator<Item = (&K, &C)> + FusedIterator
An iterator visiting all key-value pairs in arbitrary order.
Sourcepub fn values(&self) -> impl ExactSizeIterator<Item = &C> + FusedIterator
pub fn values(&self) -> impl ExactSizeIterator<Item = &C> + FusedIterator
Returns an iterator over the factory components.
Sourcepub fn keys(&self) -> impl ExactSizeIterator<Item = &K> + FusedIterator
pub fn keys(&self) -> impl ExactSizeIterator<Item = &K> + FusedIterator
Returns an iterator over the keys of the hash map.
Source§impl<K, C> FactoryHashMap<K, C, RandomState>
impl<K, C> FactoryHashMap<K, C, RandomState>
Sourcepub fn from_vec(
component_vec: Vec<(K, C::Init)>,
widget: C::ParentWidget,
) -> Self
pub fn from_vec( component_vec: Vec<(K, C::Init)>, widget: C::ParentWidget, ) -> Self
Creates a FactoryHashMap from a Vec.
Source§impl<K, C, S> FactoryHashMap<K, C, S>
impl<K, C, S> FactoryHashMap<K, C, S>
Sourcepub fn get(&self, key: &K) -> Option<&C>
pub fn get(&self, key: &K) -> Option<&C>
Tries to get an immutable reference to the model of one element.
Returns None if key is invalid.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<FactoryElementGuard<'_, C>>
pub fn get_mut(&mut self, key: &K) -> Option<FactoryElementGuard<'_, C>>
Tries to get a mutable reference to the model of one element.
Returns None if key is invalid.
Examples found in repository?
163 fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164 match msg {
165 AppMsg::AddCounter => {
166 let index = self.entry_buffer.text().to_string();
167 if !index.is_empty() {
168 self.counters.insert(index.clone(), self.created_widgets);
169 // Change focus to the currently created element
170 self.counters.set_visible(&index);
171 self.created_widgets = self.created_widgets.wrapping_add(1);
172 }
173 }
174 AppMsg::Increment(key) => {
175 let mut elem = self.counters.get_mut(&key).unwrap();
176 elem.value = elem.value.saturating_add(1);
177 }
178 AppMsg::Decrement(key) => {
179 let mut elem = self.counters.get_mut(&key).unwrap();
180 elem.value = elem.value.saturating_sub(1);
181 }
182 AppMsg::RemoveCounter(key) => {
183 self.counters.remove(&key);
184 }
185 AppMsg::UpdateView => (),
186 }
187 }Sourcepub fn insert(&mut self, key: K, init: C::Init) -> Option<C>
pub fn insert(&mut self, key: K, init: C::Init) -> Option<C>
Inserts a new factory component into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.
Examples found in repository?
163 fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164 match msg {
165 AppMsg::AddCounter => {
166 let index = self.entry_buffer.text().to_string();
167 if !index.is_empty() {
168 self.counters.insert(index.clone(), self.created_widgets);
169 // Change focus to the currently created element
170 self.counters.set_visible(&index);
171 self.created_widgets = self.created_widgets.wrapping_add(1);
172 }
173 }
174 AppMsg::Increment(key) => {
175 let mut elem = self.counters.get_mut(&key).unwrap();
176 elem.value = elem.value.saturating_add(1);
177 }
178 AppMsg::Decrement(key) => {
179 let mut elem = self.counters.get_mut(&key).unwrap();
180 elem.value = elem.value.saturating_sub(1);
181 }
182 AppMsg::RemoveCounter(key) => {
183 self.counters.remove(&key);
184 }
185 AppMsg::UpdateView => (),
186 }
187 }Sourcepub fn remove(&mut self, key: &K) -> Option<C>
pub fn remove(&mut self, key: &K) -> Option<C>
Removes a key from the map, returning the factory component at the key if the key was previously in the map.
Examples found in repository?
163 fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164 match msg {
165 AppMsg::AddCounter => {
166 let index = self.entry_buffer.text().to_string();
167 if !index.is_empty() {
168 self.counters.insert(index.clone(), self.created_widgets);
169 // Change focus to the currently created element
170 self.counters.set_visible(&index);
171 self.created_widgets = self.created_widgets.wrapping_add(1);
172 }
173 }
174 AppMsg::Increment(key) => {
175 let mut elem = self.counters.get_mut(&key).unwrap();
176 elem.value = elem.value.saturating_add(1);
177 }
178 AppMsg::Decrement(key) => {
179 let mut elem = self.counters.get_mut(&key).unwrap();
180 elem.value = elem.value.saturating_sub(1);
181 }
182 AppMsg::RemoveCounter(key) => {
183 self.counters.remove(&key);
184 }
185 AppMsg::UpdateView => (),
186 }
187 }Source§impl<K, C> FactoryHashMap<K, C, RandomState>
impl<K, C> FactoryHashMap<K, C, RandomState>
Sourcepub fn set_visible(&self, key: &K) -> bool
pub fn set_visible(&self, key: &K) -> bool
Makes the element at a given key visible in a gtk::Stack.
Returns true on success, otherwise false.
Examples found in repository?
163 fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164 match msg {
165 AppMsg::AddCounter => {
166 let index = self.entry_buffer.text().to_string();
167 if !index.is_empty() {
168 self.counters.insert(index.clone(), self.created_widgets);
169 // Change focus to the currently created element
170 self.counters.set_visible(&index);
171 self.created_widgets = self.created_widgets.wrapping_add(1);
172 }
173 }
174 AppMsg::Increment(key) => {
175 let mut elem = self.counters.get_mut(&key).unwrap();
176 elem.value = elem.value.saturating_add(1);
177 }
178 AppMsg::Decrement(key) => {
179 let mut elem = self.counters.get_mut(&key).unwrap();
180 elem.value = elem.value.saturating_sub(1);
181 }
182 AppMsg::RemoveCounter(key) => {
183 self.counters.remove(&key);
184 }
185 AppMsg::UpdateView => (),
186 }
187 }Trait Implementations§
Source§impl<K, C> Clone for FactoryHashMap<K, C, RandomState>
Implements the Clone Trait for FactoryHashMap if the component implements CloneableFactoryComponent.
impl<K, C> Clone for FactoryHashMap<K, C, RandomState>
Implements the Clone Trait for FactoryHashMap if the component implements CloneableFactoryComponent.