2023-09-20 16:14:40 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use crate::{Element, Renderer};
|
|
|
|
|
use derive_setters::Setters;
|
|
|
|
|
use iced_core::event::{self, Event};
|
|
|
|
|
use iced_core::widget::{Operation, Tree};
|
|
|
|
|
use iced_core::{
|
2025-03-21 03:17:59 +01:00
|
|
|
Clipboard, Layout, Length, Padding, Rectangle, Shell, Vector, Widget, layout, mouse, overlay,
|
|
|
|
|
renderer,
|
2023-09-20 16:14:40 +02:00
|
|
|
};
|
|
|
|
|
|
2024-11-28 15:24:15 +01:00
|
|
|
/// Responsively generates rows and columns of widgets based on its dimensions.
|
2023-09-20 16:14:40 +02:00
|
|
|
#[derive(Setters)]
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub struct FlexRow<'a, Message> {
|
|
|
|
|
#[setters(skip)]
|
|
|
|
|
children: Vec<Element<'a, Message>>,
|
|
|
|
|
/// Sets the padding around the widget.
|
2024-05-29 23:41:48 +02:00
|
|
|
#[setters(into)]
|
2023-09-20 16:14:40 +02:00
|
|
|
padding: Padding,
|
|
|
|
|
/// Sets the space between each column of items.
|
|
|
|
|
column_spacing: u16,
|
|
|
|
|
/// Sets the space between each item in a row.
|
|
|
|
|
row_spacing: u16,
|
|
|
|
|
/// Sets the width.
|
|
|
|
|
width: Length,
|
2024-05-30 12:37:32 +02:00
|
|
|
/// Sets minimum width of items that grow.
|
|
|
|
|
#[setters(into)]
|
|
|
|
|
min_item_width: Option<f32>,
|
2023-09-20 16:14:40 +02:00
|
|
|
/// Sets the max width
|
|
|
|
|
max_width: f32,
|
2024-05-30 12:37:32 +02:00
|
|
|
/// Defines how content will be aligned horizontally.
|
|
|
|
|
#[setters(skip)]
|
|
|
|
|
align_items: Option<taffy::AlignItems>,
|
|
|
|
|
/// Defines how content will be aligned vertically.
|
|
|
|
|
#[setters(skip)]
|
|
|
|
|
justify_items: Option<taffy::AlignItems>,
|
2024-05-29 23:55:50 +02:00
|
|
|
/// Defines how the content will be justified.
|
|
|
|
|
#[setters(into)]
|
|
|
|
|
justify_content: Option<crate::widget::JustifyContent>,
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message> FlexRow<'a, Message> {
|
2024-05-29 23:41:48 +02:00
|
|
|
pub(crate) const fn new(children: Vec<Element<'a, Message>>) -> Self {
|
2023-09-20 16:14:40 +02:00
|
|
|
Self {
|
|
|
|
|
children,
|
|
|
|
|
padding: Padding::ZERO,
|
|
|
|
|
column_spacing: 4,
|
|
|
|
|
row_spacing: 4,
|
|
|
|
|
width: Length::Shrink,
|
2024-05-30 12:37:32 +02:00
|
|
|
min_item_width: None,
|
2023-09-20 16:14:40 +02:00
|
|
|
max_width: f32::INFINITY,
|
2024-05-30 12:37:32 +02:00
|
|
|
align_items: None,
|
|
|
|
|
justify_items: None,
|
2024-05-29 23:55:50 +02:00
|
|
|
justify_content: None,
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-29 23:41:48 +02:00
|
|
|
|
2024-05-30 12:37:32 +02:00
|
|
|
/// Defines how content will be aligned horizontally.
|
|
|
|
|
pub fn align_items(mut self, alignment: iced::Alignment) -> Self {
|
|
|
|
|
self.align_items = Some(match alignment {
|
|
|
|
|
iced::Alignment::Center => taffy::AlignItems::Center,
|
|
|
|
|
iced::Alignment::Start => taffy::AlignItems::Start,
|
|
|
|
|
iced::Alignment::End => taffy::AlignItems::End,
|
|
|
|
|
});
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Defines how content will be aligned vertically.
|
|
|
|
|
pub fn justify_items(mut self, alignment: iced::Alignment) -> Self {
|
|
|
|
|
self.justify_items = Some(match alignment {
|
|
|
|
|
iced::Alignment::Center => taffy::AlignItems::Center,
|
|
|
|
|
iced::Alignment::Start => taffy::AlignItems::Start,
|
|
|
|
|
iced::Alignment::End => taffy::AlignItems::End,
|
|
|
|
|
});
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 23:41:48 +02:00
|
|
|
/// Sets the space between each column and row.
|
2025-03-21 03:17:59 +01:00
|
|
|
#[inline]
|
2024-05-29 23:41:48 +02:00
|
|
|
pub const fn spacing(mut self, spacing: u16) -> Self {
|
|
|
|
|
self.column_spacing = spacing;
|
|
|
|
|
self.row_spacing = spacing;
|
|
|
|
|
self
|
|
|
|
|
}
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
impl<Message: 'static + Clone> Widget<Message, crate::Theme, Renderer> for FlexRow<'_, Message> {
|
2023-09-20 16:14:40 +02:00
|
|
|
fn children(&self) -> Vec<Tree> {
|
|
|
|
|
self.children.iter().map(Tree::new).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn diff(&mut self, tree: &mut Tree) {
|
|
|
|
|
tree.diff_children(self.children.as_mut_slice());
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
fn size(&self) -> iced_core::Size<Length> {
|
|
|
|
|
iced_core::Size::new(self.width, Length::Shrink)
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-30 14:01:42 -05:00
|
|
|
fn layout(
|
2026-02-10 15:37:41 -05:00
|
|
|
&mut self,
|
2023-11-30 14:01:42 -05:00
|
|
|
tree: &mut Tree,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
2024-01-30 22:14:00 -05:00
|
|
|
let size = self.size();
|
2023-09-20 16:14:40 +02:00
|
|
|
let limits = limits
|
|
|
|
|
.max_width(self.max_width)
|
2024-01-30 22:14:00 -05:00
|
|
|
.width(size.width)
|
|
|
|
|
.height(size.height);
|
2023-09-20 16:14:40 +02:00
|
|
|
|
|
|
|
|
super::layout::resolve(
|
|
|
|
|
renderer,
|
|
|
|
|
&limits,
|
2026-02-10 15:37:41 -05:00
|
|
|
&mut self.children,
|
2023-09-20 16:14:40 +02:00
|
|
|
self.padding,
|
|
|
|
|
f32::from(self.column_spacing),
|
|
|
|
|
f32::from(self.row_spacing),
|
2024-05-30 12:37:32 +02:00
|
|
|
self.min_item_width,
|
|
|
|
|
self.justify_items,
|
2026-03-11 00:15:14 +01:00
|
|
|
self.align_items,
|
2024-05-29 23:55:50 +02:00
|
|
|
self.justify_content,
|
2023-11-30 14:01:42 -05:00
|
|
|
&mut tree.children,
|
2023-09-20 16:14:40 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn operate(
|
2026-02-10 15:37:41 -05:00
|
|
|
&mut self,
|
2023-09-20 16:14:40 +02:00
|
|
|
tree: &mut Tree,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
renderer: &Renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
operation: &mut dyn Operation<()>,
|
2023-09-20 16:14:40 +02:00
|
|
|
) {
|
2026-02-10 15:37:41 -05:00
|
|
|
operation.traverse(&mut |operation| {
|
2023-09-20 16:14:40 +02:00
|
|
|
self.children
|
2026-02-10 15:37:41 -05:00
|
|
|
.iter_mut()
|
2023-09-20 16:14:40 +02:00
|
|
|
.zip(&mut tree.children)
|
|
|
|
|
.zip(layout.children())
|
2025-03-20 10:10:02 -04:00
|
|
|
.for_each(|((child, state), c_layout)| {
|
2026-02-10 15:37:41 -05:00
|
|
|
child.as_widget_mut().operate(
|
2025-03-20 10:10:02 -04:00
|
|
|
state,
|
|
|
|
|
c_layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
renderer,
|
|
|
|
|
operation,
|
|
|
|
|
);
|
2023-09-20 16:14:40 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 15:37:41 -05:00
|
|
|
fn update(
|
2023-09-20 16:14:40 +02:00
|
|
|
&mut self,
|
|
|
|
|
tree: &mut Tree,
|
2026-02-10 15:37:41 -05:00
|
|
|
event: &Event,
|
2023-09-20 16:14:40 +02:00
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
clipboard: &mut dyn Clipboard,
|
|
|
|
|
shell: &mut Shell<'_, Message>,
|
|
|
|
|
viewport: &Rectangle,
|
2026-02-10 15:37:41 -05:00
|
|
|
) {
|
2026-02-22 22:47:28 -05:00
|
|
|
for ((child, state), c_layout) in self
|
|
|
|
|
.children
|
2023-09-20 16:14:40 +02:00
|
|
|
.iter_mut()
|
|
|
|
|
.zip(&mut tree.children)
|
|
|
|
|
.zip(layout.children())
|
2026-02-22 22:47:28 -05:00
|
|
|
{
|
|
|
|
|
child.as_widget_mut().update(
|
|
|
|
|
state,
|
|
|
|
|
event,
|
|
|
|
|
c_layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
cursor,
|
|
|
|
|
renderer,
|
|
|
|
|
clipboard,
|
|
|
|
|
shell,
|
|
|
|
|
viewport,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &Tree,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
self.children
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(&tree.children)
|
|
|
|
|
.zip(layout.children())
|
2025-03-20 10:10:02 -04:00
|
|
|
.map(|((child, state), c_layout)| {
|
|
|
|
|
child.as_widget().mouse_interaction(
|
|
|
|
|
state,
|
|
|
|
|
c_layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
cursor,
|
|
|
|
|
viewport,
|
|
|
|
|
renderer,
|
|
|
|
|
)
|
2023-09-20 16:14:40 +02:00
|
|
|
})
|
|
|
|
|
.max()
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &Tree,
|
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &renderer::Style,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
) {
|
2025-03-20 10:10:02 -04:00
|
|
|
for ((child, state), c_layout) in self
|
2023-09-20 16:14:40 +02:00
|
|
|
.children
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(&tree.children)
|
|
|
|
|
.zip(layout.children())
|
|
|
|
|
{
|
2025-03-20 10:10:02 -04:00
|
|
|
child.as_widget().draw(
|
|
|
|
|
state,
|
|
|
|
|
renderer,
|
|
|
|
|
theme,
|
|
|
|
|
style,
|
|
|
|
|
c_layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
cursor,
|
|
|
|
|
viewport,
|
|
|
|
|
);
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlay<'b>(
|
|
|
|
|
&'b mut self,
|
|
|
|
|
tree: &'b mut Tree,
|
2026-02-10 15:37:41 -05:00
|
|
|
layout: Layout<'b>,
|
2023-09-20 16:14:40 +02:00
|
|
|
renderer: &Renderer,
|
2026-02-10 15:37:41 -05:00
|
|
|
viewport: &Rectangle,
|
2024-10-16 20:36:46 -04:00
|
|
|
translation: Vector,
|
2024-01-30 22:14:00 -05:00
|
|
|
) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> {
|
2026-02-10 15:37:41 -05:00
|
|
|
overlay::from_children(
|
|
|
|
|
&mut self.children,
|
|
|
|
|
tree,
|
|
|
|
|
layout,
|
|
|
|
|
renderer,
|
|
|
|
|
viewport,
|
|
|
|
|
translation,
|
|
|
|
|
)
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
/// get the a11y nodes for the widget
|
|
|
|
|
fn a11y_nodes(
|
|
|
|
|
&self,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
state: &Tree,
|
|
|
|
|
p: mouse::Cursor,
|
|
|
|
|
) -> iced_accessibility::A11yTree {
|
|
|
|
|
use iced_accessibility::A11yTree;
|
|
|
|
|
A11yTree::join(
|
|
|
|
|
self.children
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(layout.children())
|
|
|
|
|
.zip(state.children.iter())
|
|
|
|
|
.map(|((c, c_layout), state)| c.as_widget().a11y_nodes(c_layout, state, p)),
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-04-01 16:12:45 -04:00
|
|
|
|
|
|
|
|
fn drag_destinations(
|
|
|
|
|
&self,
|
|
|
|
|
state: &Tree,
|
|
|
|
|
layout: Layout<'_>,
|
2024-05-31 19:10:51 -04:00
|
|
|
renderer: &Renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles,
|
2024-04-01 16:12:45 -04:00
|
|
|
) {
|
|
|
|
|
for ((e, layout), state) in self
|
|
|
|
|
.children
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(layout.children())
|
|
|
|
|
.zip(state.children.iter())
|
|
|
|
|
{
|
|
|
|
|
e.as_widget()
|
2024-05-31 19:10:51 -04:00
|
|
|
.drag_destinations(state, layout, renderer, dnd_rectangles);
|
2024-04-01 16:12:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-20 16:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message: 'static + Clone> From<FlexRow<'a, Message>> for Element<'a, Message> {
|
|
|
|
|
fn from(flex_row: FlexRow<'a, Message>) -> Self {
|
|
|
|
|
Self::new(flex_row)
|
|
|
|
|
}
|
|
|
|
|
}
|