iced_widget/
space.rs

1//! Distribute content vertically.
2use crate::core;
3use crate::core::layout;
4use crate::core::mouse;
5use crate::core::renderer;
6use crate::core::widget::Tree;
7use crate::core::{Element, Layout, Length, Rectangle, Size, Widget};
8
9/// An amount of empty space.
10///
11/// It can be useful if you want to fill some space with nothing.
12#[derive(Debug)]
13pub struct Space {
14    width: Length,
15    height: Length,
16}
17
18impl Space {
19    /// Creates an amount of empty [`Space`] with the given width and height.
20    pub fn new(width: impl Into<Length>, height: impl Into<Length>) -> Self {
21        Space {
22            width: width.into(),
23            height: height.into(),
24        }
25    }
26
27    /// Creates an amount of horizontal [`Space`].
28    pub fn with_width(width: impl Into<Length>) -> Self {
29        Space {
30            width: width.into(),
31            height: Length::Shrink,
32        }
33    }
34
35    /// Creates an amount of vertical [`Space`].
36    pub fn with_height(height: impl Into<Length>) -> Self {
37        Space {
38            width: Length::Shrink,
39            height: height.into(),
40        }
41    }
42
43    /// Sets the width of the [`Space`].
44    pub fn width(mut self, width: impl Into<Length>) -> Self {
45        self.width = width.into();
46        self
47    }
48
49    /// Sets the height of the [`Space`].
50    pub fn height(mut self, height: impl Into<Length>) -> Self {
51        self.height = height.into();
52        self
53    }
54}
55
56impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Space
57where
58    Renderer: core::Renderer,
59{
60    fn size(&self) -> Size<Length> {
61        Size {
62            width: self.width,
63            height: self.height,
64        }
65    }
66
67    fn layout(
68        &self,
69        _tree: &mut Tree,
70        _renderer: &Renderer,
71        limits: &layout::Limits,
72    ) -> layout::Node {
73        layout::atomic(limits, self.width, self.height)
74    }
75
76    fn draw(
77        &self,
78        _state: &Tree,
79        _renderer: &mut Renderer,
80        _theme: &Theme,
81        _style: &renderer::Style,
82        _layout: Layout<'_>,
83        _cursor: mouse::Cursor,
84        _viewport: &Rectangle,
85    ) {
86    }
87}
88
89impl<'a, Message, Theme, Renderer> From<Space>
90    for Element<'a, Message, Theme, Renderer>
91where
92    Renderer: core::Renderer,
93    Message: 'a,
94{
95    fn from(space: Space) -> Element<'a, Message, Theme, Renderer> {
96        Element::new(space)
97    }
98}