iced_widget/
space.rs

1//! Add some explicit spacing between elements.
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/// Creates a new [`Space`] widget that fills the available
10/// horizontal space.
11///
12/// This can be useful to separate widgets in a [`Row`](crate::Row).
13pub fn horizontal() -> Space {
14    Space::new().width(Length::Fill)
15}
16
17/// Creates a new [`Space`] widget that fills the available
18/// vertical space.
19///
20/// This can be useful to separate widgets in a [`Column`](crate::Column).
21pub fn vertical() -> Space {
22    Space::new().height(Length::Fill)
23}
24
25/// An amount of empty space.
26///
27/// It can be useful if you want to fill some space with nothing.
28#[derive(Debug)]
29pub struct Space {
30    width: Length,
31    height: Length,
32}
33
34impl Space {
35    /// Creates some empty [`Space`] with no size.
36    pub fn new() -> Self {
37        Space {
38            width: Length::Shrink,
39            height: Length::Shrink,
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 Default for Space {
57    fn default() -> Self {
58        Space::new()
59    }
60}
61
62impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Space
63where
64    Renderer: core::Renderer,
65{
66    fn size(&self) -> Size<Length> {
67        Size {
68            width: self.width,
69            height: self.height,
70        }
71    }
72
73    fn layout(
74        &mut self,
75        _tree: &mut Tree,
76        _renderer: &Renderer,
77        limits: &layout::Limits,
78    ) -> layout::Node {
79        layout::atomic(limits, self.width, self.height)
80    }
81
82    fn draw(
83        &self,
84        _state: &Tree,
85        _renderer: &mut Renderer,
86        _theme: &Theme,
87        _style: &renderer::Style,
88        _layout: Layout<'_>,
89        _cursor: mouse::Cursor,
90        _viewport: &Rectangle,
91    ) {
92    }
93}
94
95impl<'a, Message, Theme, Renderer> From<Space>
96    for Element<'a, Message, Theme, Renderer>
97where
98    Renderer: core::Renderer,
99    Message: 'a,
100{
101    fn from(space: Space) -> Element<'a, Message, Theme, Renderer> {
102        Element::new(space)
103    }
104}