feat(segmented_button): Support adding divider above items in vertical segmented button

This commit is contained in:
Jeremy Soller 2024-09-11 12:51:19 -06:00
parent 05da0a83b2
commit 2faaeddb05
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
4 changed files with 44 additions and 2 deletions

View file

@ -95,6 +95,11 @@ where
self
}
pub fn divider_above(mut self) -> Self {
self.model.0.divider_above_set(self.id, true);
self
}
/// Defines an icon for the item.
///
/// ```ignore

View file

@ -83,6 +83,12 @@ where
self
}
#[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)]
pub fn divider_above(self, divider_above: bool) -> Self {
self.model.divider_above_set(self.id, divider_above);
self
}
/// Define an icon for the item.
///
/// ```ignore

View file

@ -58,6 +58,9 @@ pub struct Model<SelectionMode: Default> {
/// The content used for drawing segmented items.
pub(super) items: SlotMap<Entity, Settings>,
/// Divider optionally-defined for each item.
pub(super) divider_aboves: SecondaryMap<Entity, bool>,
/// Icons optionally-defined for each item.
pub(super) icons: SecondaryMap<Entity, Icon>,
@ -200,6 +203,22 @@ where
.and_then(|storage| storage.remove(id));
}
pub fn divider_above(&self, id: Entity) -> Option<bool> {
self.divider_aboves.get(id).copied()
}
pub fn divider_above_set(&mut self, id: Entity, divider_above: bool) -> Option<bool> {
if !self.contains_item(id) {
return None;
}
self.divider_aboves.insert(id, divider_above)
}
pub fn divider_above_remove(&mut self, id: Entity) -> Option<bool> {
self.divider_aboves.remove(id)
}
/// Enable or disable an item.
///
/// ```ignore

View file

@ -57,7 +57,19 @@ where
.iter()
.copied()
.enumerate()
.map(move |(nth, key)| {
.flat_map(move |(nth, key)| {
let mut divider = None;
if self.model.divider_above(key).unwrap_or(false) && nth > 0 {
let mut divider_bounds = bounds;
divider_bounds.height = 1.0;
divider_bounds.x += f32::from(self.button_padding[0]);
divider_bounds.width -= f32::from(self.button_padding[0]);
divider_bounds.width -= f32::from(self.button_padding[2]);
divider = Some(ItemBounds::Divider(divider_bounds));
bounds.y += divider_bounds.height + spacing;
}
let mut layout_bounds = bounds;
let layout_size = state.internal_layout[nth].0;
@ -66,7 +78,7 @@ where
bounds.y += layout_bounds.height + spacing;
ItemBounds::Button(key, layout_bounds)
std::iter::once(ItemBounds::Button(key, layout_bounds)).chain(divider)
}),
)
}