feat(widget): Reimplement FlexRow as iced::Widget
This commit is contained in:
parent
aa2dfe0ea5
commit
d973dafba7
4 changed files with 286 additions and 137 deletions
70
src/widget/flex_row/layout.rs
Normal file
70
src/widget/flex_row/layout.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Copyright 2023 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use crate::{Element, Renderer};
|
||||
use iced_core::layout::{Limits, Node};
|
||||
use iced_core::{Padding, Point, Size};
|
||||
|
||||
pub fn resolve<Message>(
|
||||
renderer: &Renderer,
|
||||
limits: &Limits,
|
||||
items: &[Element<'_, Message>],
|
||||
padding: Padding,
|
||||
column_spacing: f32,
|
||||
row_spacing: f32,
|
||||
) -> Node {
|
||||
let limits = limits.pad(padding);
|
||||
|
||||
let mut nodes = Vec::with_capacity(items.len());
|
||||
|
||||
let max_flex_width = limits.max().width;
|
||||
let mut flex_width = 0.0f32;
|
||||
let mut flex_height = 0.0f32;
|
||||
|
||||
let mut current_row_width = 0.0f32;
|
||||
let mut current_row_height = 0.0f32;
|
||||
|
||||
let mut row_buffer = Vec::with_capacity(8);
|
||||
|
||||
for child in items {
|
||||
// Calculate the dimensions of the item.
|
||||
let child_node = child.as_widget().layout(renderer, &limits);
|
||||
let size = child_node.size();
|
||||
|
||||
// Calculate the required additional width to fit the item into the current row.
|
||||
let required_width = size.width
|
||||
+ if row_buffer.is_empty() {
|
||||
0.0
|
||||
} else {
|
||||
row_spacing
|
||||
};
|
||||
|
||||
// If it fits, add it to the current row, or create a new one.
|
||||
if current_row_width + required_width <= max_flex_width {
|
||||
current_row_width += required_width;
|
||||
current_row_height = current_row_height.max(size.height);
|
||||
|
||||
row_buffer.push(child_node);
|
||||
} else {
|
||||
if flex_height != 0.0f32 {
|
||||
flex_height += column_spacing;
|
||||
}
|
||||
|
||||
let mut pos_x = 0.0f32;
|
||||
let pos_y = flex_height;
|
||||
|
||||
for mut child_node in row_buffer.drain(..) {
|
||||
child_node.move_to(Point::new(pos_x, pos_y));
|
||||
pos_x += row_spacing + child_node.size().width;
|
||||
nodes.push(child_node);
|
||||
}
|
||||
|
||||
flex_height += current_row_height;
|
||||
flex_width = flex_width.max(current_row_width);
|
||||
current_row_width = 0.0f32;
|
||||
}
|
||||
}
|
||||
|
||||
let flex_size = limits.resolve(Size::new(flex_width, flex_height));
|
||||
Node::with_children(flex_size.pad(padding), nodes)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue