-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathselect.rs
More file actions
147 lines (130 loc) · 4.12 KB
/
select.rs
File metadata and controls
147 lines (130 loc) · 4.12 KB
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
// Select component - Dropdown selection component
// Following Zed's pattern with traits for reusability
use gpui::{Context, Render, SharedString, Window, div, prelude::*};
use super::traits::{Disableable, Selectable};
use crate::styles::colors;
pub struct Select<T> {
id: SharedString,
placeholder: String,
options: Vec<T>,
selected_index: Option<usize>,
disabled: bool,
on_select: Option<Box<dyn Fn(&mut Window, &mut Context<Self>, T) + Send + Sync>>,
}
impl<T> Select<T>
where
T: Clone + ToString + 'static,
{
pub fn new(id: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
placeholder: "Select an option...".to_string(),
options: Vec::new(),
selected_index: None,
disabled: false,
on_select: None,
}
}
pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn options(mut self, options: Vec<T>) -> Self {
self.options = options;
self
}
pub fn selected_index(mut self, index: Option<usize>) -> Self {
self.selected_index = index;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl<T> Selectable for Select<T>
where
T: Clone + ToString + 'static,
{
type Item = T;
fn selected(mut self, item: Self::Item) -> Self {
if let Some(index) = self
.options
.iter()
.position(|opt| opt.to_string() == item.to_string())
{
self.selected_index = Some(index);
}
self
}
fn on_select<F>(mut self, handler: F) -> Self
where
F: Fn(&mut Window, &mut Context<Self>, Self::Item) + Send + Sync + 'static,
{
self.on_select = Some(Box::new(handler));
self
}
}
impl<T> Disableable for Select<T>
where
T: Clone + ToString + 'static,
{
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl<T> Render for Select<T>
where
T: Clone + ToString + 'static,
{
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let selected_text = self
.selected_index
.and_then(|idx| self.options.get(idx))
.map(|item| item.to_string())
.unwrap_or_else(|| self.placeholder.clone());
let disabled = self.disabled;
div()
.id(self.id.clone())
.relative()
.w_full()
.px_3()
.py_2()
.rounded_md()
.bg(colors::background())
.text_color(colors::foreground())
.cursor_pointer()
.when(!disabled, |div| {
div.hover(|div| div.bg(gpui::rgba(0x2A2A2AFF)))
.on_click(cx.listener(move |this, _event, window, cx| {
// For now, just cycle through options on click
// TODO: Implement proper dropdown with overlay
let next_index = this
.selected_index
.map(|idx| (idx + 1) % this.options.len())
.unwrap_or(0);
this.selected_index = Some(next_index);
if let (Some(index), Some(callback)) =
(this.selected_index, &this.on_select)
&& let Some(item) = this.options.get(index).cloned()
{
callback(window, cx, item);
}
cx.notify();
}))
})
.when(disabled, |div| {
div.bg(gpui::rgba(0x1A1A1AFF))
.text_color(gpui::rgba(0x666666FF))
})
.child(
div()
.flex()
.justify_between()
.items_center()
.child(selected_text)
.child("▼"), // Simple dropdown arrow
)
}
}