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
//! Form module.
//!
//! This public module implements the Liora form and form-item layout primitives. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.
use gpui::{AnyElement, App, Component, Pixels, SharedString, Window, div, prelude::*, px};
use liora_core::Config;
/// Fluent native GPUI component for rendering Liora form.
pub struct Form {
_label_width: Option<Pixels>,
inline: bool,
children: Vec<AnyElement>,
}
impl Form {
/// Creates `Form` with default theme-driven styling and no optional callbacks attached.
pub fn new() -> Self {
Self {
_label_width: None,
inline: false,
children: Vec::new(),
}
}
/// Sets the reserved label column width.
pub fn label_width(mut self, width: impl Into<Pixels>) -> Self {
self._label_width = Some(width.into());
self
}
/// Renders the code block with inline metrics instead of a block frame.
pub fn inline(mut self, inline: bool) -> Self {
self.inline = inline;
self
}
/// Adds a child element to the component body.
pub fn child(mut self, child: impl IntoElement) -> Self {
self.children.push(child.into_any_element());
self
}
}
impl IntoElement for Form {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
impl RenderOnce for Form {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.flex()
.when(self.inline, |s| s.flex_row().gap_4().flex_wrap())
.when(!self.inline, |s| s.flex_col().gap_4())
.children(self.children)
}
}
/// Data model used by form item rendering.
pub struct FormItem {
label: Option<SharedString>,
label_width: Option<Pixels>,
required: bool,
error: Option<SharedString>,
content: Option<AnyElement>,
}
impl FormItem {
/// Creates `FormItem` with default theme-driven styling and no optional callbacks attached.
pub fn new() -> Self {
Self {
label: None,
label_width: None,
required: false,
error: None,
content: None,
}
}
/// Returns the stable user-facing label for this value.
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
/// Sets the reserved label column width.
pub fn label_width(mut self, width: impl Into<Pixels>) -> Self {
self.label_width = Some(width.into());
self
}
/// Sets the required value used by the component.
pub fn required(mut self, r: bool) -> Self {
self.required = r;
self
}
/// Sets the error value used by the component.
pub fn error(mut self, e: impl Into<SharedString>) -> Self {
self.error = Some(e.into());
self
}
/// Adds a child element to the component body.
pub fn child(mut self, child: impl IntoElement) -> Self {
self.content = Some(child.into_any_element());
self
}
}
impl IntoElement for FormItem {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
impl RenderOnce for FormItem {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = &cx.global::<Config>().theme;
div()
.flex()
.flex_col()
.gap_1()
.child(div().flex().flex_row().items_center().gap_1().when_some(
self.label,
|this, label| {
this.child(
div()
.flex()
.flex_row()
.items_center()
.gap_1()
.when_some(self.label_width, |s, w| s.w(w))
.child(
div()
.text_size(px(theme.font_size.md))
.text_color(theme.neutral.text_1)
.child(label),
)
.when(self.required, |this| {
this.child(div().text_color(theme.danger.base).child("*"))
}),
)
},
))
.when_some(self.content, |this, content| this.child(content))
.when_some(self.error, |this, error| {
this.child(
div()
.text_size(px(theme.font_size.sm))
.text_color(theme.danger.base)
.child(error),
)
})
}
}