Skip to main content

guise/layout/
center.rs

1//! `Center` — centers its content on both axes.
2
3use crate::devtools::Probed;
4use gpui::prelude::*;
5use gpui::{div, AnyElement, App, IntoElement, Window};
6
7/// A flex container that centers its children.
8#[derive(IntoElement)]
9pub struct Center {
10    children: Vec<AnyElement>,
11    inline: bool,
12}
13
14impl Center {
15    pub fn new() -> Self {
16        Center {
17            children: Vec::new(),
18            inline: false,
19        }
20    }
21
22    /// Lay out inline (shrink to content) instead of filling the parent.
23    pub fn inline(mut self, inline: bool) -> Self {
24        self.inline = inline;
25        self
26    }
27}
28
29impl Default for Center {
30    fn default() -> Self {
31        Center::new()
32    }
33}
34
35impl ParentElement for Center {
36    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
37        self.children.extend(elements);
38    }
39}
40
41impl RenderOnce for Center {
42    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
43        let mut base = div().flex().items_center().justify_center();
44        if !self.inline {
45            base = base.size_full();
46        }
47        base.children(self.children).probe("Center")
48    }
49}