remove redundant clones, use mul_add on f32s

This commit is contained in:
Cheong Lau 2025-10-11 16:24:38 +10:00 committed by Michael Murphy
parent bd438a8581
commit 1d6a43486e
12 changed files with 69 additions and 69 deletions

View file

@ -199,15 +199,14 @@ impl<Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer> for Ove
)
.width(self.width);
let mut node = self.container.layout(self.state, renderer, &limits);
let node = self.container.layout(self.state, renderer, &limits);
node = node.clone().move_to(if space_below > space_above {
let node_size = node.size();
node.move_to(if space_below > space_above {
position + Vector::new(0.0, self.target_height)
} else {
position - Vector::new(0.0, node.size().height)
});
node
position - Vector::new(0.0, node_size.height)
})
}
fn on_event(
@ -513,7 +512,7 @@ where
OptionElement::Option((option, item)) => {
let (color, font) = if self.selected_option.as_ref() == Some(&item) {
let item_x = bounds.x + appearance.border_width;
let item_width = bounds.width - appearance.border_width * 2.0;
let item_width = appearance.border_width.mul_add(-2.0, bounds.width);
bounds = Rectangle {
x: item_x,
@ -551,7 +550,7 @@ where
(appearance.selected_text_color, crate::font::semibold())
} else if self.hovered_option.as_ref() == Some(item) {
let item_x = bounds.x + appearance.border_width;
let item_width = bounds.width - appearance.border_width * 2.0;
let item_width = appearance.border_width.mul_add(-2.0, bounds.width);
bounds = Rectangle {
x: item_x,

View file

@ -432,44 +432,46 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static
};
let mut desc_count = 0;
selections
.elements()
.map(|element| match element {
super::menu::OptionElement::Description(desc) => {
let paragraph = if state.descriptions.len() > desc_count {
&mut state.descriptions[desc_count]
} else {
state.descriptions.push(crate::Plain::default());
state.descriptions.last_mut().unwrap()
};
desc_count += 1;
measure(desc.as_ref(), paragraph, description_line_height)
}
padding.horizontal().mul_add(
2.0,
selections
.elements()
.map(|element| match element {
super::menu::OptionElement::Description(desc) => {
let paragraph = if state.descriptions.len() > desc_count {
&mut state.descriptions[desc_count]
} else {
state.descriptions.push(crate::Plain::default());
state.descriptions.last_mut().unwrap()
};
desc_count += 1;
measure(desc.as_ref(), paragraph, description_line_height)
}
super::menu::OptionElement::Option((option, item)) => {
let selection_index = state.selections.iter().position(|(i, _)| i == item);
super::menu::OptionElement::Option((option, item)) => {
let selection_index =
state.selections.iter().position(|(i, _)| i == item);
let selection_index = match selection_index {
Some(index) => index,
None => {
state
.selections
.push((item.clone(), crate::Plain::default()));
state.selections.len() - 1
}
};
let selection_index = match selection_index {
Some(index) => index,
None => {
state
.selections
.push((item.clone(), crate::Plain::default()));
state.selections.len() - 1
}
};
let paragraph = &mut state.selections[selection_index].1;
let paragraph = &mut state.selections[selection_index].1;
measure(option.as_ref(), paragraph, text_line_height)
}
measure(option.as_ref(), paragraph, text_line_height)
}
super::menu::OptionElement::Separator => 1.0,
})
.fold(0.0, |next, current| current.max(next))
+ gap
super::menu::OptionElement::Separator => 1.0,
})
.fold(0.0, |next, current| current.max(next)),
) + gap
+ 16.0
+ (padding.horizontal() * 2.0)
})
.padding(padding)
.text_size(text_size);