Advertisement
loloof64

StackLayout try in Rust Iced framework

Jan 30th, 2023
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.34 KB | None | 0 0
  1. /*
  2. Using code from https://docs.rs/iced_native/0.7.0/src/iced_native/widget/column.rs.html
  3. */
  4.  
  5. use iced::{Element, Length, Size};
  6. use iced_native::{Widget, widget::Tree};
  7.  
  8. #[macro_export]
  9. macro_rules! stack {
  10.     () => (
  11.         StackLayout::new()
  12.     );
  13.     ($($x:expr),+ $(,)?) => (
  14.         StackLayout::with_children(vec![$(Element::from($x)),+])
  15.     );
  16. }
  17.  
  18. pub struct StackLayout<'a, Message, Renderer> {
  19.    width: Length,
  20.    height: Length,
  21.    children: Vec<Element<'a, Message, Renderer>>
  22. }
  23.  
  24. impl<'a, Message, Renderer> StackLayout<'a, Message, Renderer> {
  25.     pub fn new() -> Self {
  26.         Self::with_children(Vec::new())
  27.     }
  28.  
  29.     pub fn with_children(children: Vec<Element<'a, Message, Renderer>>) -> Self {
  30.        Self {
  31.            children,
  32.            width: Length::Shrink,
  33.            height: Length::Shrink,
  34.         }
  35.    }
  36.  
  37.    pub fn width(mut self, width: Length) -> Self {
  38.        self.width = width;
  39.        self
  40.    }
  41.  
  42.    pub fn height(mut self, height: Length) -> Self {
  43.        self.height = height;
  44.        self
  45.    }
  46.  
  47.    pub fn push(
  48.        mut self,
  49.        child: impl Into<Element<'a, Message, Renderer>>,
  50.     ) -> Self {
  51.         self.children.push(child.into());
  52.         self
  53.     }
  54. }
  55.  
  56. impl<'a, Message, Renderer> Default for StackLayout<'a, Message, Renderer> {
  57.     fn default() -> Self {
  58.         Self::new()
  59.     }
  60. }
  61.  
  62. impl<'a, Message, Renderer> Widget<Message, Renderer>
  63. for StackLayout<'a, Message, Renderer>
  64. where
  65. Renderer: iced_native::Renderer,
  66. {
  67.     fn children(&self) -> Vec<Tree> {
  68.         self.children.iter().map(Tree::new).collect()
  69.     }
  70.  
  71.     fn width(&self) -> Length {
  72.         self.width
  73.     }
  74.  
  75.     fn height(&self) -> Length {
  76.         self.height
  77.     }
  78.  
  79.     fn layout(
  80.         &self,
  81.         renderer: &Renderer,
  82.         limits: &iced_native::layout::Limits,
  83.     ) -> iced_native::layout::Node {
  84.         if self.children.is_empty() {iced_native::layout::Node::new(Size::ZERO)}
  85.         else {self.children.first().unwrap().as_widget().layout(renderer, limits)}
  86.     }
  87.  
  88.     fn draw(
  89.         &self,
  90.         tree: &iced_native::widget::Tree,
  91.         renderer: &mut Renderer,
  92.         theme: &<Renderer as iced_native::Renderer>::Theme,
  93.         style: &iced_native::renderer::Style,
  94.         layout: iced_native::Layout<'_>,
  95.        cursor_position: iced::Point,
  96.        viewport: &iced::Rectangle,
  97.    ) {
  98.        for ((child, state), layout) in self
  99.            .children
  100.            .iter()
  101.            .zip(&tree.children)
  102.            .zip(layout.children())
  103.        {
  104.            child.as_widget().draw(
  105.                state,
  106.                renderer,
  107.                theme,
  108.                style,
  109.                layout,
  110.                cursor_position,
  111.                viewport,
  112.            );
  113.        }
  114.    }
  115.  
  116.    fn diff(&self, tree: &mut Tree) {
  117.        let mut children_array = vec![];
  118.        self.children.iter().for_each(|elt| children_array.push(elt));
  119.        let children_slice = children_array.as_slice();
  120.        tree.diff_children(children_slice);
  121.    }
  122. }
  123.  
  124. impl<'a, Message, Renderer> From<StackLayout<'a, Message, Renderer>> for Element<'a, Message, Renderer>
  125. where
  126. Message: 'a,
  127. Renderer: iced_native::Renderer + 'a,
  128. {
  129.     fn from(stack: StackLayout<'a, Message, Renderer>) -> Self
  130.    {
  131.        Self::new(stack)
  132.    }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement