Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic and layout issues in dropdown::multi widget #231

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/widget/dropdown/multi/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ impl<'a, Message> iced_core::Overlay<Message, crate::Renderer> for Overlay<'a, M
)
.width(self.width);

let mut node = self
.container
.layout(&mut self.state.children[0], renderer, &limits);
let mut node = self.container.layout(self.state, renderer, &limits);

node.move_to(if space_below > space_above {
position + Vector::new(0.0, self.target_height)
Expand Down Expand Up @@ -517,7 +515,7 @@ where
let mut current_offset = 0.0;

for (elem, elem_height) in visible_options {
let bounds = Rectangle {
let mut bounds = Rectangle {
x: bounds.x,
y: bounds.y + current_offset,
width: bounds.width,
Expand All @@ -532,13 +530,15 @@ where
let item_x = bounds.x + appearance.border_width;
let item_width = bounds.width - appearance.border_width * 2.0;

bounds = Rectangle {
x: item_x,
width: item_width,
..bounds
};

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: item_x,
width: item_width,
..bounds
},
bounds,
border_color: Color::TRANSPARENT,
border_width: 0.0,
border_radius: appearance.border_radius,
Expand All @@ -565,13 +565,15 @@ where
let item_x = bounds.x + appearance.border_width;
let item_width = bounds.width - appearance.border_width * 2.0;

bounds = Rectangle {
x: item_x,
width: item_width,
..bounds
};

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: item_x,
width: item_width,
..bounds
},
bounds,
border_color: Color::TRANSPARENT,
border_width: 0.0,
border_radius: appearance.border_radius,
Expand All @@ -586,9 +588,10 @@ where

let bounds = Rectangle {
x: bounds.x + self.padding.left,
y: bounds.y + self.padding.top,
// TODO: Figure out why it's offset by 8 pixels
y: bounds.y + self.padding.top + 8.0,
width: bounds.width,
height: bounds.height,
height: elem_height,
};
text::Renderer::fill_text(
renderer,
Expand Down Expand Up @@ -618,7 +621,7 @@ where

layout_node.move_to(Point {
x: bounds.x,
y: bounds.y + self.padding.top,
y: bounds.y + (self.padding.vertical() / 2.0) - 4.0,
});

Widget::<Message, crate::Renderer>::draw(
Expand Down
88 changes: 44 additions & 44 deletions src/widget/dropdown/multi/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,25 @@ impl<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static>
self.text_line_height,
self.font,
self.selections.selected.as_ref().and_then(|id| {
self.selections.get(id).map(AsRef::as_ref).zip(
tree.state
.downcast_mut::<State<Item>>()
self.selections.get(id).map(AsRef::as_ref).zip({
let state = tree.state.downcast_mut::<State<Item>>();

if state.selections.is_empty() {
for list in &self.selections.lists {
for (_, item) in &list.options {
state
.selections
.push((item.clone(), crate::Paragraph::new()));
}
}
}

state
.selections
.iter_mut()
.find(|(i, _)| i == id)
.map(|(_, p)| p),
)
.map(|(_, p)| p)
})
}),
)
}
Expand Down Expand Up @@ -382,6 +393,10 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static
on_selected: &'a dyn Fn(Item) -> Message,
) -> Option<overlay::Element<'a, Message, crate::Renderer>> {
if state.is_open {
let description_line_height = text::LineHeight::Absolute(Pixels(
text_line_height.to_absolute(Pixels(text_size)).0 + 4.0,
));

let bounds = layout.bounds();

let menu = Menu::new(
Expand All @@ -397,33 +412,20 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static
None,
)
.width({
let measure = |label: &str, paragraph: &mut crate::Paragraph| {
paragraph.update(Text {
content: label,
bounds: Size::new(f32::MAX, f32::MAX),
size: iced::Pixels(text_size),
line_height: text_line_height,
font: font.unwrap_or_else(|| text::Renderer::default_font(renderer)),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Advanced,
});
paragraph.min_width().round()
};

let measure_description = |label: &str, paragraph: &mut crate::Paragraph| {
paragraph.update(Text {
content: label,
bounds: Size::new(f32::MAX, f32::MAX),
size: iced::Pixels(text_size + 4.0),
line_height: text_line_height,
font: font.unwrap_or_else(|| text::Renderer::default_font(renderer)),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Advanced,
});
paragraph.min_width().round()
};
let measure =
|label: &str, paragraph: &mut crate::Paragraph, line_height: text::LineHeight| {
paragraph.update(Text {
content: label,
bounds: Size::new(f32::MAX, f32::MAX),
size: iced::Pixels(text_size),
line_height,
font: font.unwrap_or_else(|| text::Renderer::default_font(renderer)),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Advanced,
});
paragraph.min_width().round()
};

let mut desc_count = 0;
selections
Expand All @@ -437,21 +439,19 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static
state.descriptions.last_mut().unwrap()
};
desc_count += 1;
measure_description(desc.as_ref(), paragraph)
measure(desc.as_ref(), paragraph, description_line_height)
}

super::menu::OptionElement::Option((option, item)) => {
let paragraph = if let Some(index) =
state.selections.iter().position(|(i, _)| i == item)
{
&mut state.selections[index].1
} else {
state
.selections
.push((item.clone(), crate::Paragraph::new()));
&mut state.selections.last_mut().unwrap().1
};
measure(option.as_ref(), paragraph)
let selection_index = state
.selections
.iter()
.position(|(i, _)| i == item)
.expect("selection missing from state");

let paragraph = &mut state.selections[selection_index].1;

measure(option.as_ref(), paragraph, text_line_height)
}

super::menu::OptionElement::Separator => 1.0,
Expand Down
Loading