diff --git a/src/widget/segmented_button/model/entity.rs b/src/widget/segmented_button/model/entity.rs index 81938630..c9f0997d 100644 --- a/src/widget/segmented_button/model/entity.rs +++ b/src/widget/segmented_button/model/entity.rs @@ -104,6 +104,12 @@ where self.id } + #[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)] + pub fn indent(self, indent: u16) -> Self { + self.model.indent_set(self.id, indent); + self + } + /// Define the position of the item. #[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)] pub fn position(self, position: u16) -> Self { diff --git a/src/widget/segmented_button/model/mod.rs b/src/widget/segmented_button/model/mod.rs index 3179a50a..5f2e72eb 100644 --- a/src/widget/segmented_button/model/mod.rs +++ b/src/widget/segmented_button/model/mod.rs @@ -61,6 +61,9 @@ pub struct Model { /// Icons optionally-defined for each item. pub(super) icons: SecondaryMap, + /// Indent optionally-defined for each item. + pub(super) indents: SecondaryMap, + /// Text optionally-defined for each item. pub(super) text: SecondaryMap>, @@ -293,6 +296,22 @@ where self.order.iter().copied() } + pub fn indent(&self, id: Entity) -> Option { + self.indents.get(id).map(|indent| *indent) + } + + pub fn indent_set(&mut self, id: Entity, indent: u16) -> Option { + if !self.contains_item(id) { + return None; + } + + self.indents.insert(id, indent) + } + + pub fn indent_remove(&mut self, id: Entity) -> Option { + self.indents.remove(id) + } + /// The position of the item in the model. /// /// ```ignore diff --git a/src/widget/segmented_button/widget.rs b/src/widget/segmented_button/widget.rs index e4894bed..bb22a516 100644 --- a/src/widget/segmented_button/widget.rs +++ b/src/widget/segmented_button/widget.rs @@ -82,6 +82,8 @@ where pub(super) button_height: u16, /// Spacing between icon and text in button. pub(super) button_spacing: u16, + /// Spacing for each indent. + pub(super) indent_spacing: u16, /// Desired font for active tabs. pub(super) font_active: Option, /// Desired font for hovered tabs. @@ -127,6 +129,7 @@ where button_padding: [4, 4, 4, 4], button_height: 32, button_spacing: 4, + indent_spacing: 16, font_active: None, font_hovered: None, font_inactive: None, @@ -235,6 +238,11 @@ where button_height = height; } + // Add indent to measurement if found. + if let Some(indent) = self.model.indent(key) { + button_width += f32::from(indent) * f32::from(self.indent_spacing); + } + // Add icon to measurement if icon was given. if let Some(icon) = self.model.icon(key) { button_height = button_height.max(f32::from(icon.size)); @@ -509,6 +517,13 @@ where let y = bounds.center_y(); + // Adjust bounds by indent + if let Some(indent) = self.model.indent(key) { + let adjustment = f32::from(indent) * f32::from(self.indent_spacing); + bounds.x += adjustment; + bounds.width -= adjustment; + } + // Draw the image beside the text. let horizontal_alignment = if let Some(icon) = self.model.icon(key) { bounds.x += f32::from(self.button_padding[0]);