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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use gpui::{
    div, px, AnyElement, ClickEvent, Div, RenderOnce, Stateful, StatefulInteractiveElement,
};
use smallvec::SmallVec;
use std::rc::Rc;

use crate::{
    disclosure_control, h_stack, v_stack, Avatar, Icon, IconElement, IconSize, Label, Toggle,
};
use crate::{prelude::*, GraphicSlot};

#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub enum ListItemVariant {
    /// The list item extends to the far left and right of the list.
    FullWidth,
    #[default]
    Inset,
}

pub enum ListHeaderMeta {
    // TODO: These should be IconButtons
    Tools(Vec<Icon>),
    // TODO: This should be a button
    Button(Label),
    Text(Label),
}

#[derive(RenderOnce)]
pub struct ListHeader {
    label: SharedString,
    left_icon: Option<Icon>,
    meta: Option<ListHeaderMeta>,
    variant: ListItemVariant,
    toggle: Toggle,
}

impl Component for ListHeader {
    type Rendered = Div;

    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
        let disclosure_control = disclosure_control(self.toggle);

        let meta = match self.meta {
            Some(ListHeaderMeta::Tools(icons)) => div().child(
                h_stack()
                    .gap_2()
                    .items_center()
                    .children(icons.into_iter().map(|i| {
                        IconElement::new(i)
                            .color(Color::Muted)
                            .size(IconSize::Small)
                    })),
            ),
            Some(ListHeaderMeta::Button(label)) => div().child(label),
            Some(ListHeaderMeta::Text(label)) => div().child(label),
            None => div(),
        };

        h_stack()
            .w_full()
            .bg(cx.theme().colors().surface_background)
            .relative()
            .child(
                div()
                    .h_5()
                    .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
                    .flex()
                    .flex_1()
                    .items_center()
                    .justify_between()
                    .w_full()
                    .gap_1()
                    .child(
                        h_stack()
                            .gap_1()
                            .child(
                                div()
                                    .flex()
                                    .gap_1()
                                    .items_center()
                                    .children(self.left_icon.map(|i| {
                                        IconElement::new(i)
                                            .color(Color::Muted)
                                            .size(IconSize::Small)
                                    }))
                                    .child(Label::new(self.label.clone()).color(Color::Muted)),
                            )
                            .child(disclosure_control),
                    )
                    .child(meta),
            )
    }
}

impl ListHeader {
    pub fn new(label: impl Into<SharedString>) -> Self {
        Self {
            label: label.into(),
            left_icon: None,
            meta: None,
            variant: ListItemVariant::default(),
            toggle: Toggle::NotToggleable,
        }
    }

    pub fn toggle(mut self, toggle: Toggle) -> Self {
        self.toggle = toggle;
        self
    }

    pub fn left_icon(mut self, left_icon: Option<Icon>) -> Self {
        self.left_icon = left_icon;
        self
    }

    pub fn meta(mut self, meta: Option<ListHeaderMeta>) -> Self {
        self.meta = meta;
        self
    }

    // before_ship!("delete")
    // fn render<V: 'static>(self,  cx: &mut WindowContext) -> impl Element<V> {
    //     let disclosure_control = disclosure_control(self.toggle);

    //     let meta = match self.meta {
    //         Some(ListHeaderMeta::Tools(icons)) => div().child(
    //             h_stack()
    //                 .gap_2()
    //                 .items_center()
    //                 .children(icons.into_iter().map(|i| {
    //                     IconElement::new(i)
    //                         .color(TextColor::Muted)
    //                         .size(IconSize::Small)
    //                 })),
    //         ),
    //         Some(ListHeaderMeta::Button(label)) => div().child(label),
    //         Some(ListHeaderMeta::Text(label)) => div().child(label),
    //         None => div(),
    //     };

    //     h_stack()
    //         .w_full()
    //         .bg(cx.theme().colors().surface_background)
    //         // TODO: Add focus state
    //         // .when(self.state == InteractionState::Focused, |this| {
    //         //     this.border()
    //         //         .border_color(cx.theme().colors().border_focused)
    //         // })
    //         .relative()
    //         .child(
    //             div()
    //                 .h_5()
    //                 .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
    //                 .flex()
    //                 .flex_1()
    //                 .items_center()
    //                 .justify_between()
    //                 .w_full()
    //                 .gap_1()
    //                 .child(
    //                     h_stack()
    //                         .gap_1()
    //                         .child(
    //                             div()
    //                                 .flex()
    //                                 .gap_1()
    //                                 .items_center()
    //                                 .children(self.left_icon.map(|i| {
    //                                     IconElement::new(i)
    //                                         .color(TextColor::Muted)
    //                                         .size(IconSize::Small)
    //                                 }))
    //                                 .child(Label::new(self.label.clone()).color(TextColor::Muted)),
    //                         )
    //                         .child(disclosure_control),
    //                 )
    //                 .child(meta),
    //         )
    // }
}

#[derive(RenderOnce, Clone)]
pub struct ListSubHeader {
    label: SharedString,
    left_icon: Option<Icon>,
    variant: ListItemVariant,
}

impl ListSubHeader {
    pub fn new(label: impl Into<SharedString>) -> Self {
        Self {
            label: label.into(),
            left_icon: None,
            variant: ListItemVariant::default(),
        }
    }

    pub fn left_icon(mut self, left_icon: Option<Icon>) -> Self {
        self.left_icon = left_icon;
        self
    }
}

impl Component for ListSubHeader {
    type Rendered = Div;

    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
        h_stack().flex_1().w_full().relative().py_1().child(
            div()
                .h_6()
                .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
                .flex()
                .flex_1()
                .w_full()
                .gap_1()
                .items_center()
                .justify_between()
                .child(
                    div()
                        .flex()
                        .gap_1()
                        .items_center()
                        .children(self.left_icon.map(|i| {
                            IconElement::new(i)
                                .color(Color::Muted)
                                .size(IconSize::Small)
                        }))
                        .child(Label::new(self.label.clone()).color(Color::Muted)),
                ),
        )
    }
}

#[derive(Default, PartialEq, Copy, Clone)]
pub enum ListEntrySize {
    #[default]
    Small,
    Medium,
}

#[derive(RenderOnce)]
pub struct ListItem {
    id: ElementId,
    disabled: bool,
    // TODO: Reintroduce this
    // disclosure_control_style: DisclosureControlVisibility,
    indent_level: u32,
    label: Label,
    left_slot: Option<GraphicSlot>,
    overflow: OverflowStyle,
    size: ListEntrySize,
    toggle: Toggle,
    variant: ListItemVariant,
    on_click: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
}

impl Clone for ListItem {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            disabled: self.disabled,
            indent_level: self.indent_level,
            label: self.label.clone(),
            left_slot: self.left_slot.clone(),
            overflow: self.overflow,
            size: self.size,
            toggle: self.toggle,
            variant: self.variant,
            on_click: self.on_click.clone(),
        }
    }
}

impl ListItem {
    pub fn new(id: impl Into<ElementId>, label: Label) -> Self {
        Self {
            id: id.into(),
            disabled: false,
            indent_level: 0,
            label,
            left_slot: None,
            overflow: OverflowStyle::Hidden,
            size: ListEntrySize::default(),
            toggle: Toggle::NotToggleable,
            variant: ListItemVariant::default(),
            on_click: Default::default(),
        }
    }

    pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
        self.on_click = Some(Rc::new(handler));
        self
    }

    pub fn variant(mut self, variant: ListItemVariant) -> Self {
        self.variant = variant;
        self
    }

    pub fn indent_level(mut self, indent_level: u32) -> Self {
        self.indent_level = indent_level;
        self
    }

    pub fn toggle(mut self, toggle: Toggle) -> Self {
        self.toggle = toggle;
        self
    }

    pub fn left_content(mut self, left_content: GraphicSlot) -> Self {
        self.left_slot = Some(left_content);
        self
    }

    pub fn left_icon(mut self, left_icon: Icon) -> Self {
        self.left_slot = Some(GraphicSlot::Icon(left_icon));
        self
    }

    pub fn left_avatar(mut self, left_avatar: impl Into<SharedString>) -> Self {
        self.left_slot = Some(GraphicSlot::Avatar(left_avatar.into()));
        self
    }

    pub fn size(mut self, size: ListEntrySize) -> Self {
        self.size = size;
        self
    }
}

impl Component for ListItem {
    type Rendered = Stateful<Div>;

    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
        let left_content = match self.left_slot.clone() {
            Some(GraphicSlot::Icon(i)) => Some(
                h_stack().child(
                    IconElement::new(i)
                        .size(IconSize::Small)
                        .color(Color::Muted),
                ),
            ),
            Some(GraphicSlot::Avatar(src)) => Some(h_stack().child(Avatar::new(src))),
            Some(GraphicSlot::PublicActor(src)) => Some(h_stack().child(Avatar::new(src))),
            None => None,
        };

        let sized_item = match self.size {
            ListEntrySize::Small => div().h_6(),
            ListEntrySize::Medium => div().h_7(),
        };
        div()
            .id(self.id)
            .relative()
            .hover(|mut style| {
                style.background = Some(cx.theme().colors().editor_background.into());
                style
            })
            .on_click({
                let on_click = self.on_click.clone();
                move |event, cx| {
                    if let Some(on_click) = &on_click {
                        (on_click)(event, cx)
                    }
                }
            })
            .bg(cx.theme().colors().surface_background)
            // TODO: Add focus state
            // .when(self.state == InteractionState::Focused, |this| {
            //     this.border()
            //         .border_color(cx.theme().colors().border_focused)
            // })
            .child(
                sized_item
                    .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
                    // .ml(rems(0.75 * self.indent_level as f32))
                    .children((0..self.indent_level).map(|_| {
                        div()
                            .w(px(4.))
                            .h_full()
                            .flex()
                            .justify_center()
                            .group_hover("", |style| style.bg(cx.theme().colors().border_focused))
                            .child(
                                h_stack()
                                    .child(div().w_px().h_full())
                                    .child(div().w_px().h_full().bg(cx.theme().colors().border)),
                            )
                    }))
                    .flex()
                    .gap_1()
                    .items_center()
                    .relative()
                    .child(disclosure_control(self.toggle))
                    .children(left_content)
                    .child(self.label),
            )
    }
}

#[derive(RenderOnce, Clone)]
pub struct ListSeparator;

impl ListSeparator {
    pub fn new() -> Self {
        Self
    }
}

impl Component for ListSeparator {
    type Rendered = Div;

    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
        div().h_px().w_full().bg(cx.theme().colors().border_variant)
    }
}

#[derive(RenderOnce)]
pub struct List {
    /// Message to display when the list is empty
    /// Defaults to "No items"
    empty_message: SharedString,
    header: Option<ListHeader>,
    toggle: Toggle,
    children: SmallVec<[AnyElement; 2]>,
}

impl Component for List {
    type Rendered = Div;

    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
        let list_content = match (self.children.is_empty(), self.toggle) {
            (false, _) => div().children(self.children),
            (true, Toggle::Toggled(false)) => div(),
            (true, _) => div().child(Label::new(self.empty_message.clone()).color(Color::Muted)),
        };

        v_stack()
            .w_full()
            .py_1()
            .children(self.header.map(|header| header))
            .child(list_content)
    }
}

impl List {
    pub fn new() -> Self {
        Self {
            empty_message: "No items".into(),
            header: None,
            toggle: Toggle::NotToggleable,
            children: SmallVec::new(),
        }
    }

    pub fn empty_message(mut self, empty_message: impl Into<SharedString>) -> Self {
        self.empty_message = empty_message.into();
        self
    }

    pub fn header(mut self, header: ListHeader) -> Self {
        self.header = Some(header);
        self
    }

    pub fn toggle(mut self, toggle: Toggle) -> Self {
        self.toggle = toggle;
        self
    }
}

impl ParentElement for List {
    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
        &mut self.children
    }
}