Skip to main content

iced_widget/
space.rs

1//! Add some explicit spacing between elements.
2use crate::core::layout;
3use crate::core::mouse;
4use crate::core::renderer;
5use crate::core::widget::{Meta, Tree};
6use crate::core::{Layout, Length, Rectangle, Size, Widget};
7
8/// Creates a new [`Space`] widget that fills the available
9/// horizontal space.
10///
11/// This can be useful to separate widgets in a [`Row`](crate::Row).
12pub fn horizontal() -> Space {
13    Space::new().width(Length::Fill)
14}
15
16/// Creates a new [`Space`] widget that fills the available
17/// vertical space.
18///
19/// This can be useful to separate widgets in a [`Column`](crate::Column).
20pub fn vertical() -> Space {
21    Space::new().height(Length::Fill)
22}
23
24/// An amount of empty space.
25///
26/// It can be useful if you want to fill some space with nothing.
27#[derive(Debug)]
28pub struct Space {
29    width: Length,
30    height: Length,
31}
32
33impl Space {
34    /// Creates some empty [`Space`] with no size.
35    pub fn new() -> Self {
36        Space {
37            width: Length::Fit,
38            height: Length::Fit,
39        }
40    }
41
42    /// Sets the width of the [`Space`].
43    pub fn width(mut self, width: impl Into<Length>) -> Self {
44        self.width = width.into();
45        self
46    }
47
48    /// Sets the height of the [`Space`].
49    pub fn height(mut self, height: impl Into<Length>) -> Self {
50        self.height = height.into();
51        self
52    }
53}
54
55impl Default for Space {
56    fn default() -> Self {
57        Space::new()
58    }
59}
60
61impl Meta for Space {}
62
63impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Space {
64    fn size(&self) -> Size<Length> {
65        Size {
66            width: self.width,
67            height: self.height,
68        }
69    }
70
71    fn layout(&mut self, tree: &mut Tree, _renderer: &Renderer, limits: &layout::Limits) {
72        tree.size = layout::atomic(limits, self.width, self.height);
73    }
74
75    fn draw(
76        &self,
77        _tree: &Tree,
78        _renderer: &mut Renderer,
79        _theme: &Theme,
80        _style: &renderer::Style,
81        _layout: Layout,
82        _cursor: mouse::Cursor,
83        _viewport: &Rectangle,
84    ) {
85    }
86}