allui/layout/
hstack.rs

1//! HStack - Horizontal stack layout.
2
3use gpui::{div, px, App, IntoElement, ParentElement, RenderOnce, Styled, Window};
4
5use crate::alignment::VerticalAlignment;
6use crate::modifier::Modifier;
7
8/// A view that arranges its children in a horizontal line.
9///
10/// By default, HStack centers its children vertically (matching SwiftUI).
11///
12/// # Example
13///
14/// ```rust,ignore
15/// HStack::new()
16///     .spacing(8.0)
17///     .child(Image::system_name("star"))
18///     .child(Text::new("Favorites"))
19///     .child(Spacer::new())
20///     .child(Text::new("12"))
21/// ```
22#[derive(IntoElement)]
23pub struct HStack {
24    spacing: f32,
25    alignment: VerticalAlignment,
26    children: Vec<gpui::AnyElement>,
27}
28
29impl HStack {
30    /// Create a new horizontal stack.
31    pub fn new() -> Self {
32        Self {
33            spacing: 8.0,
34            alignment: VerticalAlignment::Center, // SwiftUI default
35            children: Vec::new(),
36        }
37    }
38
39    /// Set the spacing between children.
40    pub fn spacing(mut self, spacing: f32) -> Self {
41        self.spacing = spacing;
42        self
43    }
44
45    /// Set the vertical alignment of children.
46    pub fn alignment(mut self, alignment: VerticalAlignment) -> Self {
47        self.alignment = alignment;
48        self
49    }
50
51    impl_child_methods!();
52}
53
54impl Default for HStack {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl Modifier for HStack {}
61
62impl RenderOnce for HStack {
63    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
64        let container = div().flex().flex_row().gap(px(self.spacing));
65        self.alignment
66            .apply_as_items(container)
67            .children(self.children)
68    }
69}