Skip to main content

guise/layout/
center.rs

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