diff options
| author | 2019-11-11 05:26:08 +0100 | |
|---|---|---|
| committer | 2019-11-11 05:26:08 +0100 | |
| commit | ceb02f4a36769c488c2525db2fb73f092a6c2706 (patch) | |
| tree | d7a43c6b9444fb3079501e49ca1f80e31f1b4740 /native/src/layout | |
| parent | bfe19193b95e9d1be0694bbc6a96e20a9aefdc09 (diff) | |
| download | iced-ceb02f4a36769c488c2525db2fb73f092a6c2706.tar.gz iced-ceb02f4a36769c488c2525db2fb73f092a6c2706.tar.bz2 iced-ceb02f4a36769c488c2525db2fb73f092a6c2706.zip | |
Implement `Container` widget
Remove `align_self` and `justify_content` methods
Diffstat (limited to '')
| -rw-r--r-- | native/src/layout/flex.rs | 20 | ||||
| -rw-r--r-- | native/src/layout/limits.rs | 50 | ||||
| -rw-r--r-- | native/src/layout/node.rs | 25 | 
3 files changed, 69 insertions, 26 deletions
| diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index ec3e2d00..7a2b0d70 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -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); diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index e5cee5df..af269acd 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -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), +        ) +    }  } diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs index 7537ad3b..9d8ad57d 100644 --- a/native/src/layout/node.rs +++ b/native/src/layout/node.rs @@ -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 => {} +        } +    }  } | 
