Struct relm4::factory::DynamicIndex
source · pub struct DynamicIndex { /* private fields */ }
Expand description
A dynamic index that updates automatically when items are shifted inside a factory container.
For example a FactoryVecDeque
has an insert
method that allows users to insert data at arbitrary positions.
If we insert at the front all following widgets will be moved by one which would
invalidate their indices.
To allow widgets in a factory container to send messages with valid indices
this type ensures that the indices is always up to date.
Never send an index as usize
but always as DynamicIndex
to the update function because messages can be queued up and stale by the time they are handled.
DynamicIndex
is a smart pointer so cloning will work similar to std::rc::Rc
and will create
a pointer to the same data.
In short: only call current_index
from the update function
where you actually need the index as usize
.
Implementations§
source§impl DynamicIndex
impl DynamicIndex
sourcepub fn current_index(&self) -> usize
pub fn current_index(&self) -> usize
Get the current index number.
This value is updated by the factory container and might change after each update function.
Examples found in repository?
More examples
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
let mut counters_guard = self.counters.guard();
match msg {
AppMsg::AddCounter => {
counters_guard.push_back(self.created_widgets);
self.created_widgets = self.created_widgets.wrapping_add(1);
}
AppMsg::RemoveCounter => {
counters_guard.pop_back();
}
AppMsg::SendFront(index) => {
counters_guard.move_front(index.current_index());
}
AppMsg::MoveDown(index) => {
let index = index.current_index();
let new_index = index + 1;
// Already at the end?
if new_index < counters_guard.len() {
counters_guard.move_to(index, new_index);
}
}
AppMsg::MoveUp(index) => {
let index = index.current_index();
// Already at the start?
if index != 0 {
counters_guard.move_to(index, index - 1);
}
}
}
}
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
match message {
AppMsg::AddCounters => {
let text = self.entry.text();
if let Ok(v) = text.parse::<i32>() {
let mut guard = self.counters.guard();
if v.is_positive() {
// add as many counters as user entered
for _ in 0..v {
guard.push_back(self.created_counters);
self.created_counters += 1;
}
} else if v.is_negative() {
// remove counters
for _ in v..0 {
guard.pop_front();
}
}
// clearing the entry value clears the entry widget
self.entry.set_text("");
}
}
AppMsg::Clicked(index) => {
if let Some(counter) = self.counters.guard().get_mut(index.current_index()) {
counter.value = counter.value.wrapping_sub(1);
}
}
}
}
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
let mut counters_guard = self.counters.guard();
match msg {
AppMsg::AddCounter => {
counters_guard.push_back(self.created_widgets);
self.created_widgets = self.created_widgets.wrapping_add(1);
}
AppMsg::RemoveCounter => {
counters_guard.pop_back();
}
AppMsg::SendFront(index) => {
counters_guard.move_front(index.current_index());
}
AppMsg::MoveDown(index) => {
let index = index.current_index();
let new_index = index + 1;
// Already at the end?
if new_index < counters_guard.len() {
counters_guard.move_to(index, new_index);
}
}
AppMsg::MoveUp(index) => {
let index = index.current_index();
// Already at the start?
if index != 0 {
counters_guard.move_to(index, index - 1);
}
}
AppMsg::Remove(index) => {
counters_guard.remove(index.current_index());
}
}
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
let mut counters_guard = self.counters.guard();
match msg {
AppMsg::AddCounter => {
counters_guard.push_back(self.created_widgets);
self.created_widgets = self.created_widgets.wrapping_add(1);
}
AppMsg::RemoveCounter => {
counters_guard.pop_back();
}
AppMsg::SendFront(index) => {
counters_guard.move_front(index.current_index());
}
AppMsg::MoveDown(index) => {
let index = index.current_index();
let new_index = index + 1;
// Already at the end?
if new_index < counters_guard.len() {
counters_guard.move_to(index, new_index);
}
}
AppMsg::MoveUp(index) => {
let index = index.current_index();
// Already at the start?
if index != 0 {
counters_guard.move_to(index, index - 1);
}
}
AppMsg::Remove(index) => {
counters_guard.remove(index.current_index());
}
}
}