feat(segmented_button): Support adding divider above items in vertical segmented button
This commit is contained in:
parent
05da0a83b2
commit
2faaeddb05
4 changed files with 44 additions and 2 deletions
|
|
@ -95,6 +95,11 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn divider_above(mut self) -> Self {
|
||||||
|
self.model.0.divider_above_set(self.id, true);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Defines an icon for the item.
|
/// Defines an icon for the item.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,12 @@ where
|
||||||
self
|
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.
|
/// Define an icon for the item.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,9 @@ pub struct Model<SelectionMode: Default> {
|
||||||
/// The content used for drawing segmented items.
|
/// The content used for drawing segmented items.
|
||||||
pub(super) items: SlotMap<Entity, Settings>,
|
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.
|
/// Icons optionally-defined for each item.
|
||||||
pub(super) icons: SecondaryMap<Entity, Icon>,
|
pub(super) icons: SecondaryMap<Entity, Icon>,
|
||||||
|
|
||||||
|
|
@ -200,6 +203,22 @@ where
|
||||||
.and_then(|storage| storage.remove(id));
|
.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.
|
/// Enable or disable an item.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,19 @@ where
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
.copied()
|
||||||
.enumerate()
|
.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 mut layout_bounds = bounds;
|
||||||
|
|
||||||
let layout_size = state.internal_layout[nth].0;
|
let layout_size = state.internal_layout[nth].0;
|
||||||
|
|
@ -66,7 +78,7 @@ where
|
||||||
|
|
||||||
bounds.y += layout_bounds.height + spacing;
|
bounds.y += layout_bounds.height + spacing;
|
||||||
|
|
||||||
ItemBounds::Button(key, layout_bounds)
|
std::iter::once(ItemBounds::Button(key, layout_bounds)).chain(divider)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue