Skip to main content

gpui_component/dialog/
footer.rs

1use gpui::{
2    AnyElement, App, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
3    StatefulInteractiveElement, StyleRefinement, Styled, Window, div, relative,
4};
5
6use gpui_base::LogicalStyledExt as _;
7
8use crate::{
9    ActiveTheme as _, StyledExt as _,
10    button::Button,
11    dialog::{Confirm, DialogDispatchAnchor},
12    h_flex,
13};
14
15/// Footer section of a dialog, typically contains action buttons.
16///
17/// # Examples
18///
19/// ```ignore
20/// DialogFooter::new()
21///     .child(DialogClose::new().trigger(|button| button.label("Cancel")))
22///     .child(Button::new("confirm").label("Confirm"))
23/// ```
24#[derive(IntoElement)]
25pub struct DialogFooter {
26    style: StyleRefinement,
27    children: Vec<AnyElement>,
28}
29
30impl DialogFooter {
31    pub fn new() -> Self {
32        Self {
33            style: StyleRefinement::default(),
34            children: Vec::new(),
35        }
36    }
37}
38
39impl ParentElement for DialogFooter {
40    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
41        self.children.extend(elements);
42    }
43}
44
45impl Styled for DialogFooter {
46    fn style(&mut self) -> &mut StyleRefinement {
47        &mut self.style
48    }
49}
50
51impl RenderOnce for DialogFooter {
52    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
53        // Mirrored in RTL: the primary action stays on the end edge.
54        h_flex()
55            .flex_row_dir(cx)
56            .gap_2()
57            .justify_flex_end()
58            .line_height(relative(1.25))
59            .rounded_b(cx.theme().radius_lg)
60            .refine_style(&self.style)
61            .children(self.children)
62    }
63}
64
65pub trait DialogFooterButton {
66    fn is_cancel(&self) -> bool {
67        false
68    }
69
70    fn is_action(&self) -> bool {
71        false
72    }
73}
74#[derive(IntoElement)]
75pub struct DialogClose {
76    base: gpui_base::DialogClose,
77}
78
79impl DialogClose {
80    pub fn new() -> Self {
81        Self {
82            base: gpui_base::DialogClose::new(),
83        }
84    }
85
86    /// Styles a close button whose accessibility and activation are owned by Base.
87    pub fn trigger<E: IntoElement>(mut self, build: impl FnOnce(Button) -> E) -> Self {
88        self.base = self
89            .base
90            .trigger(|button| build(Button::new("close").with_base(button)));
91        self
92    }
93}
94
95impl ParentElement for DialogClose {
96    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
97        self.base.extend(elements);
98    }
99}
100
101impl RenderOnce for DialogClose {
102    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
103        div().size_full().child(self.base)
104    }
105}
106
107#[derive(IntoElement)]
108pub struct DialogAction {
109    children: Vec<AnyElement>,
110}
111
112impl DialogAction {
113    pub fn new() -> Self {
114        Self {
115            children: Vec::new(),
116        }
117    }
118}
119
120impl ParentElement for DialogAction {
121    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
122        self.children.extend(elements);
123    }
124}
125
126impl RenderOnce for DialogAction {
127    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
128        let anchor = DialogDispatchAnchor::new("dialog-action-anchor", window, cx);
129        div()
130            .size_full()
131            .id("dialog-action")
132            .child(anchor.element())
133            .on_click(move |_, window, cx| {
134                anchor.dispatch(&Confirm { secondary: false }, window, cx)
135            })
136            .children(self.children)
137    }
138}