Implement Container widget
Remove `align_self` and `justify_content` methods
This commit is contained in:
parent
bfe19193b9
commit
ceb02f4a36
25 changed files with 310 additions and 205 deletions
|
|
@ -17,7 +17,7 @@
|
|||
// limitations under the License.
|
||||
use crate::{
|
||||
layout::{Limits, Node},
|
||||
Element, Size,
|
||||
Align, Element, Size,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -56,6 +56,7 @@ pub fn resolve<Message, Renderer>(
|
|||
limits: &Limits,
|
||||
padding: f32,
|
||||
spacing: f32,
|
||||
align_items: Align,
|
||||
children: &[Element<'_, Message, Renderer>],
|
||||
) -> Node
|
||||
where
|
||||
|
|
@ -66,6 +67,7 @@ where
|
|||
let mut total_non_fill =
|
||||
spacing as f32 * (children.len() as i32 - 1).max(0) as f32;
|
||||
let mut fill_sum = 0;
|
||||
let mut cross = axis.cross(limits.min());
|
||||
|
||||
let mut nodes: Vec<Node> = Vec::with_capacity(children.len());
|
||||
nodes.resize(children.len(), Node::default());
|
||||
|
|
@ -81,8 +83,10 @@ where
|
|||
let child_limits = Limits::new(Size::ZERO, limits.max());
|
||||
|
||||
let layout = child.layout(renderer, &child_limits);
|
||||
let size = layout.size();
|
||||
|
||||
total_non_fill += axis.main(layout.size());
|
||||
total_non_fill += axis.main(size);
|
||||
cross = cross.max(axis.cross(size));
|
||||
|
||||
nodes[i] = layout;
|
||||
} else {
|
||||
|
|
@ -120,13 +124,13 @@ where
|
|||
);
|
||||
|
||||
let layout = child.layout(renderer, &child_limits);
|
||||
cross = cross.max(axis.cross(layout.size()));
|
||||
|
||||
nodes[i] = layout;
|
||||
}
|
||||
}
|
||||
|
||||
let mut main = padding;
|
||||
let mut cross = axis.cross(limits.min());
|
||||
|
||||
for (i, node) in nodes.iter_mut().enumerate() {
|
||||
if i > 0 {
|
||||
|
|
@ -138,10 +142,18 @@ where
|
|||
node.bounds.x = x;
|
||||
node.bounds.y = y;
|
||||
|
||||
match axis {
|
||||
Axis::Horizontal => {
|
||||
node.align(Align::Start, align_items, Size::new(0.0, cross));
|
||||
}
|
||||
Axis::Vertical => {
|
||||
node.align(align_items, Align::Start, Size::new(cross, 0.0));
|
||||
}
|
||||
}
|
||||
|
||||
let size = node.size();
|
||||
|
||||
main += axis.main(size);
|
||||
cross = cross.max(axis.cross(size));
|
||||
}
|
||||
|
||||
let (width, height) = axis.pack(main, cross);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,14 @@ impl Limits {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn min(&self) -> Size {
|
||||
self.min
|
||||
}
|
||||
|
||||
pub fn max(&self) -> Size {
|
||||
self.max
|
||||
}
|
||||
|
||||
pub fn width(mut self, width: Length) -> Limits {
|
||||
match width {
|
||||
Length::Shrink => {
|
||||
|
|
@ -85,27 +93,6 @@ impl Limits {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn resolve(&self, intrinsic_size: Size) -> Size {
|
||||
Size::new(
|
||||
intrinsic_size
|
||||
.width
|
||||
.min(self.max.width)
|
||||
.max(self.fill.width),
|
||||
intrinsic_size
|
||||
.height
|
||||
.min(self.max.height)
|
||||
.max(self.fill.height),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn min(&self) -> Size {
|
||||
self.min
|
||||
}
|
||||
|
||||
pub fn max(&self) -> Size {
|
||||
self.max
|
||||
}
|
||||
|
||||
pub fn pad(&self, padding: f32) -> Limits {
|
||||
self.shrink(Size::new(padding * 2.0, padding * 2.0))
|
||||
}
|
||||
|
|
@ -128,4 +115,25 @@ impl Limits {
|
|||
|
||||
Limits { min, max, fill }
|
||||
}
|
||||
|
||||
pub fn loose(&self) -> Limits {
|
||||
Limits {
|
||||
min: Size::ZERO,
|
||||
max: self.max,
|
||||
fill: self.fill,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(&self, intrinsic_size: Size) -> Size {
|
||||
Size::new(
|
||||
intrinsic_size
|
||||
.width
|
||||
.min(self.max.width)
|
||||
.max(self.fill.width),
|
||||
intrinsic_size
|
||||
.height
|
||||
.min(self.max.height)
|
||||
.max(self.fill.height),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{Rectangle, Size};
|
||||
use crate::{Align, Rectangle, Size};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Node {
|
||||
|
|
@ -34,4 +34,27 @@ impl Node {
|
|||
pub fn children(&self) -> &[Node] {
|
||||
&self.children
|
||||
}
|
||||
|
||||
pub fn align(
|
||||
&mut self,
|
||||
horizontal_alignment: Align,
|
||||
vertical_alignment: Align,
|
||||
space: Size,
|
||||
) {
|
||||
match horizontal_alignment {
|
||||
Align::Start => {}
|
||||
Align::Center => {
|
||||
self.bounds.x += (space.width - self.bounds.width) / 2.0;
|
||||
}
|
||||
Align::End => {}
|
||||
}
|
||||
|
||||
match vertical_alignment {
|
||||
Align::Start => {}
|
||||
Align::Center => {
|
||||
self.bounds.y += (space.height - self.bounds.height) / 2.0;
|
||||
}
|
||||
Align::End => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -211,7 +211,7 @@ mod size;
|
|||
mod user_interface;
|
||||
|
||||
pub use iced_core::{
|
||||
Align, Background, Color, Justify, Length, Point, Rectangle, Vector,
|
||||
Align, Background, Color, Length, Point, Rectangle, Vector,
|
||||
};
|
||||
|
||||
pub use element::Element;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ pub mod slider;
|
|||
pub mod text;
|
||||
pub mod text_input;
|
||||
|
||||
mod container;
|
||||
|
||||
#[doc(no_inline)]
|
||||
pub use button::Button;
|
||||
#[doc(no_inline)]
|
||||
|
|
@ -38,6 +40,8 @@ pub use checkbox::Checkbox;
|
|||
#[doc(no_inline)]
|
||||
pub use column::Column;
|
||||
#[doc(no_inline)]
|
||||
pub use container::Container;
|
||||
#[doc(no_inline)]
|
||||
pub use image::Image;
|
||||
#[doc(no_inline)]
|
||||
pub use radio::Radio;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
//! [`Class`]: enum.Class.html
|
||||
|
||||
use crate::input::{mouse, ButtonState};
|
||||
use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget};
|
||||
use crate::{layout, Element, Event, Hasher, Layout, Point, Widget};
|
||||
use std::hash::Hash;
|
||||
|
||||
pub use iced_core::button::State;
|
||||
|
|
@ -78,7 +78,6 @@ where
|
|||
|
||||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
self.width.hash(state);
|
||||
self.align_self.hash(state);
|
||||
self.content.hash_layout(state);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
use std::hash::Hash;
|
||||
|
||||
use crate::{
|
||||
layout, Element, Event, Hasher, Layout, Length, Point, Size, Widget,
|
||||
};
|
||||
use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget};
|
||||
|
||||
/// A container that distributes its contents vertically.
|
||||
pub type Column<'a, Message, Renderer> =
|
||||
|
|
@ -34,6 +32,7 @@ where
|
|||
&limits,
|
||||
self.padding as f32,
|
||||
self.spacing as f32,
|
||||
self.align_items,
|
||||
&self.children,
|
||||
)
|
||||
}
|
||||
|
|
@ -74,9 +73,7 @@ where
|
|||
self.height.hash(state);
|
||||
self.max_width.hash(state);
|
||||
self.max_height.hash(state);
|
||||
self.align_self.hash(state);
|
||||
self.align_items.hash(state);
|
||||
self.justify_content.hash(state);
|
||||
self.spacing.hash(state);
|
||||
|
||||
for child in &self.children {
|
||||
|
|
|
|||
93
native/src/widget/container.rs
Normal file
93
native/src/widget/container.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
use std::hash::Hash;
|
||||
|
||||
use crate::{
|
||||
layout, Element, Event, Hasher, Layout, Length, Point, Size, Widget,
|
||||
};
|
||||
|
||||
/// A container that distributes its contents vertically.
|
||||
pub type Container<'a, Message, Renderer> =
|
||||
iced_core::Container<Element<'a, Message, Renderer>>;
|
||||
|
||||
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||
for Container<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: crate::Renderer,
|
||||
{
|
||||
fn width(&self) -> Length {
|
||||
self.width
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
renderer: &Renderer,
|
||||
limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
let limits = limits
|
||||
.loose()
|
||||
.max_width(self.max_width)
|
||||
.max_height(self.max_height)
|
||||
.width(self.width)
|
||||
.height(self.height);
|
||||
|
||||
let mut content = self.content.layout(renderer, &limits);
|
||||
let size = limits.resolve(content.size());
|
||||
|
||||
content.align(self.horizontal_alignment, self.vertical_alignment, size);
|
||||
|
||||
layout::Node::with_children(size, vec![content])
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
messages: &mut Vec<Message>,
|
||||
renderer: &Renderer,
|
||||
) {
|
||||
self.content.widget.on_event(
|
||||
event,
|
||||
layout.children().next().unwrap(),
|
||||
cursor_position,
|
||||
messages,
|
||||
renderer,
|
||||
)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
renderer: &mut Renderer,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
) -> Renderer::Output {
|
||||
self.content.draw(
|
||||
renderer,
|
||||
layout.children().next().unwrap(),
|
||||
cursor_position,
|
||||
)
|
||||
}
|
||||
|
||||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
0.hash(state);
|
||||
self.width.hash(state);
|
||||
self.height.hash(state);
|
||||
self.max_width.hash(state);
|
||||
self.max_height.hash(state);
|
||||
self.padding.hash(state);
|
||||
|
||||
self.content.hash_layout(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> From<Container<'a, Message, Renderer>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: 'a + crate::Renderer,
|
||||
Message: 'static,
|
||||
{
|
||||
fn from(
|
||||
column: Container<'a, Message, Renderer>,
|
||||
) -> Element<'a, Message, Renderer> {
|
||||
Element::new(column)
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,6 @@ where
|
|||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
self.width.hash(state);
|
||||
self.height.hash(state);
|
||||
self.align_self.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ where
|
|||
&limits,
|
||||
self.padding as f32,
|
||||
self.spacing as f32,
|
||||
self.align_items,
|
||||
&self.children,
|
||||
)
|
||||
}
|
||||
|
|
@ -72,9 +73,7 @@ where
|
|||
self.height.hash(state);
|
||||
self.max_width.hash(state);
|
||||
self.max_height.hash(state);
|
||||
self.align_self.hash(state);
|
||||
self.align_items.hash(state);
|
||||
self.justify_content.hash(state);
|
||||
self.spacing.hash(state);
|
||||
self.spacing.hash(state);
|
||||
|
||||
|
|
|
|||
|
|
@ -168,7 +168,6 @@ where
|
|||
|
||||
self.height.hash(state);
|
||||
self.max_height.hash(state);
|
||||
self.align_self.hash(state);
|
||||
|
||||
self.content.hash_layout(state)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue