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
//! Segmented module.
//!
//! This public module implements the Liora segmented control for compact mutually exclusive choices. 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 crate::gpui_compat::element_id;
use crate::motion::pop_in;
use gpui::{App, Context, IntoElement, Render, SharedString, Window, div, prelude::*, px};
use liora_core::Config;
/// Fluent native GPUI component for rendering Liora segmented option.
pub struct SegmentedOption {
/// User-facing label rendered for this item.
pub label: SharedString,
/// Machine-readable value represented by this item.
pub value: SharedString,
/// Whether user interaction is disabled for this item.
pub disabled: bool,
}
impl SegmentedOption {
/// Creates `SegmentedOption` initialized from the supplied label, and value.
pub fn new(label: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
value: value.into(),
disabled: false,
}
}
/// Toggles the disabled state and suppresses user interaction when enabled.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// Fluent native GPUI component for rendering Liora segmented.
pub struct Segmented {
id: SharedString,
options: Vec<SegmentedOption>,
value: Option<SharedString>,
block: bool,
on_change: Option<Box<dyn Fn(SharedString, &mut Window, &mut App) + 'static>>,
}
impl Segmented {
/// Creates `Segmented` that renders the supplied options collection.
pub fn new(options: Vec<SegmentedOption>) -> Self {
let first_value = options.first().map(|o| o.value.clone());
Self {
id: liora_core::unique_id("segmented"),
options,
value: first_value,
block: false,
on_change: None,
}
}
/// Assigns a stable element id used by GPUI state, hit testing, and automated interaction tests.
pub fn id(mut self, id: impl Into<SharedString>) -> Self {
self.id = id.into();
self
}
/// Returns the serialized value used by forms, configuration, or persistence.
pub fn value(mut self, val: impl Into<SharedString>) -> Self {
self.value = Some(val.into());
self
}
/// Makes the component occupy the available inline width.
pub fn block(mut self, block: bool) -> Self {
self.block = block;
self
}
/// Registers a callback that runs when change occurs.
pub fn on_change(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Box::new(f));
self
}
/// Updates the stored on change value and keeps the existing component identity.
pub fn set_on_change(&mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) {
self.on_change = Some(Box::new(f));
}
fn select_option(&mut self, value: SharedString, window: &mut Window, cx: &mut Context<Self>) {
if Some(&value) != self.value.as_ref() {
self.value = Some(value.clone());
if let Some(ref on_change) = self.on_change {
(on_change)(value, window, cx);
}
cx.notify();
}
}
}
impl Render for Segmented {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
div()
.flex()
.flex_row()
.items_center()
.p(px(2.0))
.gap(px(2.0))
.bg(theme.neutral.hover)
.rounded(px(theme.radius.md))
.when(self.block, |s| s.w_full())
.children(self.options.iter().enumerate().map(|(i, opt)| {
let is_active = self.value.as_ref() == Some(&opt.value);
let value = opt.value.clone();
let disabled = opt.disabled;
let option = div()
.id(element_id(format!("{}-option-{}", self.id, i)))
.flex()
.items_center()
.justify_center()
.h(px(28.0))
.px_3()
.rounded(px(theme.radius.sm))
.when(self.block, |s| s.flex_1())
.when(is_active, |s| {
s.bg(theme.neutral.card)
.shadow_sm()
.text_color(theme.neutral.text_1)
.font_weight(gpui::FontWeight::BOLD)
})
.when(!is_active && !disabled, |s| {
s.text_color(theme.neutral.text_2).hover(|s| {
s.cursor_pointer()
.bg(theme.neutral.card.opacity(0.6))
.text_color(theme.neutral.text_1)
})
})
.when(disabled, |s| {
s.text_color(theme.neutral.text_3)
.opacity(0.5)
.cursor_not_allowed()
})
.when(!disabled && !is_active, |s| {
s.cursor_pointer().on_click(cx.listener({
let value = value.clone();
move |this, _, window, cx| {
this.select_option(value.clone(), window, cx);
}
}))
})
.child(div().text_sm().child(opt.label.clone()));
if is_active {
pop_in(
element_id(format!("{}-option-motion-{}", self.id, i)),
option,
)
.into_any_element()
} else {
option.into_any_element()
}
}))
}
}
#[cfg(test)]
mod tests {
#[test]
fn segmented_supports_runtime_on_change_binding() {
let source = include_str!("segmented.rs");
assert!(source.contains("pub fn set_on_change"));
assert!(source.contains("on_change: Option"));
}
}