diff --git a/CHANGELOG.md b/CHANGELOG.md index a5d18eb4d3..fb0f9d5822 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ You can find its changes [documented below](#070---2021-01-01). - Shell: windows implementation from content_insets ([#1592] by [@HoNile]) - Shell: IME API and macOS IME implementation ([#1619] by [@lord]) - Scroll::content_must_fill and a few other new Scroll methods ([#1635] by [@cmyr]) +- Added ListIter implementations for OrdMap ([#1641] by [@Lejero]) ### Changed @@ -426,6 +427,7 @@ Last release without a changelog :( [@HoNile]: https://github.com/HoNile [@SecondFlight]: https://github.com/SecondFlight [@lord]: https://github.com/lord +[@Lejero]: https://github.com/Lejero [#599]: https://github.com/linebender/druid/pull/599 [#611]: https://github.com/linebender/druid/pull/611 @@ -632,6 +634,7 @@ Last release without a changelog :( [#1619]: https://github.com/linebender/druid/pull/1619 [#1634]: https://github.com/linebender/druid/pull/1634 [#1635]: https://github.com/linebender/druid/pull/1635 +[#1641]: https://github.com/linebender/druid/pull/1641 [Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master [0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0 diff --git a/druid/src/widget/list.rs b/druid/src/widget/list.rs index e298e43367..56d39a4200 100644 --- a/druid/src/widget/list.rs +++ b/druid/src/widget/list.rs @@ -20,7 +20,7 @@ use std::f64; use std::sync::Arc; #[cfg(feature = "im")] -use crate::im::Vector; +use crate::im::{OrdMap, Vector}; use crate::kurbo::{Point, Rect, Size}; @@ -97,7 +97,6 @@ pub trait ListIter: Data { /// Return data length. fn data_len(&self) -> usize; } - #[cfg(feature = "im")] impl ListIter for Vector { fn for_each(&self, mut cb: impl FnMut(&T, usize)) { @@ -117,6 +116,37 @@ impl ListIter for Vector { } } +//An implementation for ListIter<(K, V)> has been ommitted due to problems +//with how the List Widget handles the reordering of its data. +#[cfg(feature = "im")] +impl ListIter for OrdMap +where + K: Data + Ord, + V: Data, +{ + fn for_each(&self, mut cb: impl FnMut(&V, usize)) { + for (i, item) in self.iter().enumerate() { + let ret = (item.0.to_owned(), item.1.to_owned()); + cb(&ret.1, i); + } + } + + fn for_each_mut(&mut self, mut cb: impl FnMut(&mut V, usize)) { + for (i, item) in self.clone().iter().enumerate() { + let mut ret = item.1.clone(); + cb(&mut ret, i); + + if !item.1.same(&ret) { + self[&item.0] = ret; + } + } + } + + fn data_len(&self) -> usize { + self.len() + } +} + // S == shared data type #[cfg(feature = "im")] impl ListIter<(S, T)> for (S, Vector) {