fix(segmented-button): misaligned icon when text is empty

This commit is contained in:
Michael Aaron Murphy 2024-04-09 10:14:54 +02:00
parent 0caff62a18
commit 552e2da9b0
No known key found for this signature in database
GPG key ID: B2732D4240C9212C

View file

@ -324,6 +324,7 @@ where
button: Entity, button: Entity,
) -> (f32, f32) { ) -> (f32, f32) {
let mut width = 0.0f32; let mut width = 0.0f32;
let mut icon_spacing = 0.0f32;
// Add text to measurement if text was given. // Add text to measurement if text was given.
if let Some((text, entry)) = self if let Some((text, entry)) = self
@ -332,6 +333,8 @@ where
.get(button) .get(button)
.zip(state.paragraphs.entry(button)) .zip(state.paragraphs.entry(button))
{ {
if !text.is_empty() {
icon_spacing = f32::from(self.button_spacing);
let paragraph = entry.or_insert_with(|| { let paragraph = entry.or_insert_with(|| {
crate::Paragraph::with_text(Text { crate::Paragraph::with_text(Text {
content: text, content: text,
@ -348,6 +351,7 @@ where
let size = paragraph.min_bounds(); let size = paragraph.min_bounds();
width += size.width; width += size.width;
} }
}
// Add indent to measurement if found. // Add indent to measurement if found.
if let Some(indent) = self.model.indent(button) { if let Some(indent) = self.model.indent(button) {
@ -356,11 +360,11 @@ where
// Add icon to measurement if icon was given. // Add icon to measurement if icon was given.
if let Some(icon) = self.model.icon(button) { if let Some(icon) = self.model.icon(button) {
width += f32::from(icon.size) + f32::from(self.button_spacing); width += f32::from(icon.size) + icon_spacing;
} else if self.model.is_active(button) { } else if self.model.is_active(button) {
// Add selection icon measurements when widget is a selection widget. // Add selection icon measurements when widget is a selection widget.
if let crate::theme::SegmentedButton::Control = self.style { if let crate::theme::SegmentedButton::Control = self.style {
width += 16.0 + f32::from(self.button_spacing); width += 16.0 + icon_spacing;
} }
} }
@ -1129,9 +1133,9 @@ where
- close_icon_width - close_icon_width
- f32::from(self.button_padding[2]); - f32::from(self.button_padding[2]);
if self.model.text(key).is_some() {
bounds.y = center_y; bounds.y = center_y;
if self.model.text(key).is_some_and(|text| !text.is_empty()) {
// Draw the text for this segmented button or tab. // Draw the text for this segmented button or tab.
renderer.fill_paragraph( renderer.fill_paragraph(
&state.paragraphs[key], &state.paragraphs[key],