1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Card component for content containers
//!
//! A flexible card component with optional header, content, and footer sections.
//!
//! # Composition Patterns
//!
//! Cards support two composition patterns:
//!
//! ## Static content (simple)
//! ```ignore
//! Card::new()
//! .header(div().child("Title"))
//! .content(div().child("Body"))
//! ```
//!
//! ## Dynamic content with theme access
//! ```ignore
//! Card::new()
//! .header_with(|theme| {
//! div().text_color(theme.accent).child("Themed Title")
//! })
//! .content_with(|theme| {
//! div().bg(theme.muted).child("Themed Body")
//! })
//! ```
use crate::theme::{Theme, ThemeExt};
use gpui::prelude::*;
use gpui::*;
/// Factory function type for creating elements with theme access
pub type SlotFactory = Box<dyn FnOnce(&Theme) -> AnyElement>;
/// A card container with optional sections
#[derive(IntoElement)]
pub struct Card {
header: Option<AnyElement>,
header_factory: Option<SlotFactory>,
content: Option<AnyElement>,
content_factory: Option<SlotFactory>,
footer: Option<AnyElement>,
footer_factory: Option<SlotFactory>,
/// Custom background color (overrides theme)
background: Option<Rgba>,
/// Custom header background color (overrides theme)
header_background: Option<Rgba>,
/// Custom border color (overrides theme)
border_color: Option<Rgba>,
/// Additional styling
extra_classes: Vec<Box<dyn FnOnce(Div) -> Div>>,
}
impl Card {
/// Create a new empty card
pub fn new() -> Self {
Self {
header: None,
header_factory: None,
content: None,
content_factory: None,
footer: None,
footer_factory: None,
background: None,
header_background: None,
border_color: None,
extra_classes: Vec::new(),
}
}
/// Set the card header with a static element
pub fn header(mut self, element: impl IntoElement) -> Self {
self.header = Some(element.into_any_element());
self
}
/// Set the card header with a factory function that receives the theme
///
/// This allows dynamic content creation with access to theme colors.
///
/// # Example
/// ```ignore
/// Card::new().header_with(|theme| {
/// div()
/// .text_color(theme.accent)
/// .font_weight(FontWeight::BOLD)
/// .child("Themed Header")
/// .into_any_element()
/// })
/// ```
pub fn header_with(mut self, factory: impl FnOnce(&Theme) -> AnyElement + 'static) -> Self {
self.header_factory = Some(Box::new(factory));
self
}
/// Set the card content with a static element
pub fn content(mut self, element: impl IntoElement) -> Self {
self.content = Some(element.into_any_element());
self
}
/// Set the card content with a factory function that receives the theme
///
/// # Example
/// ```ignore
/// Card::new().content_with(|theme| {
/// div()
/// .bg(theme.muted)
/// .p_4()
/// .child("Themed content with background")
/// .into_any_element()
/// })
/// ```
pub fn content_with(mut self, factory: impl FnOnce(&Theme) -> AnyElement + 'static) -> Self {
self.content_factory = Some(Box::new(factory));
self
}
/// Set the card footer with a static element
pub fn footer(mut self, element: impl IntoElement) -> Self {
self.footer = Some(element.into_any_element());
self
}
/// Set the card footer with a factory function that receives the theme
///
/// # Example
/// ```ignore
/// Card::new().footer_with(|theme| {
/// div()
/// .text_color(theme.text_muted)
/// .text_sm()
/// .child("Footer with theme colors")
/// .into_any_element()
/// })
/// ```
pub fn footer_with(mut self, factory: impl FnOnce(&Theme) -> AnyElement + 'static) -> Self {
self.footer_factory = Some(Box::new(factory));
self
}
/// Add custom styling to the card container
pub fn style(mut self, f: impl FnOnce(Div) -> Div + 'static) -> Self {
self.extra_classes.push(Box::new(f));
self
}
/// Set custom background color (overrides theme)
pub fn background(mut self, color: Rgba) -> Self {
self.background = Some(color);
self
}
/// Set custom header background color (overrides theme)
pub fn header_background(mut self, color: Rgba) -> Self {
self.header_background = Some(color);
self
}
/// Set custom border color (overrides theme)
pub fn border(mut self, color: Rgba) -> Self {
self.border_color = Some(color);
self
}
/// Build the card into an element with theme
pub fn build_with_theme(self, theme: &Theme) -> Div {
let bg_color = self.background.unwrap_or(theme.surface);
let border_color = self.border_color.unwrap_or(theme.border);
let header_bg = self.header_background.unwrap_or(theme.muted);
let mut card = div()
.flex()
.flex_col()
.bg(bg_color)
.text_color(theme.text_primary)
.border_1()
.border_color(border_color)
.rounded_lg()
.shadow_md()
.overflow_hidden();
// Apply extra classes
for class_fn in self.extra_classes {
card = class_fn(card);
}
// Header section - factory takes precedence over static element
let header_element = self.header_factory.map(|f| f(theme)).or(self.header);
if let Some(header) = header_element {
card = card.child(
div()
.px_4()
.py_3()
.bg(header_bg)
.text_color(theme.text_primary)
.border_b_1()
.border_color(border_color)
.child(header),
);
}
// Content section - factory takes precedence over static element
let content_element = self.content_factory.map(|f| f(theme)).or(self.content);
if let Some(content) = content_element {
card = card.child(
div()
.px_4()
.py_4()
.text_color(theme.text_secondary)
.child(content),
);
}
// Footer section - factory takes precedence over static element
let footer_element = self.footer_factory.map(|f| f(theme)).or(self.footer);
if let Some(footer) = footer_element {
card = card.child(
div()
.px_4()
.py_3()
.bg(header_bg)
.text_color(theme.text_muted)
.border_t_1()
.border_color(border_color)
.child(footer),
);
}
card
}
}
impl Default for Card {
fn default() -> Self {
Self::new()
}
}
impl RenderOnce for Card {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
self.build_with_theme(&theme)
}
}