woocraft 0.4.5

GPUI components lib for Woocraft design system.
Documentation
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
use std::{cell::Cell, rc::Rc};

use gpui::{
  AnyElement, App, Axis, Corners, ElementId, Hsla, InteractiveElement as _, IntoElement,
  ParentElement as _, RenderOnce, StatefulInteractiveElement as _, StyleRefinement, Styled, Window,
  div, prelude::FluentBuilder as _, px,
};

use crate::{
  ActiveTheme, Button, ButtonVariant, ButtonVariants as _, Disableable, Icon, IconLabel, Input,
  Label, Selectable, Sizable, Size, StyleSized as _, StyledExt, h_flex,
};

type WidgetGroupClickHandler = Box<dyn Fn(&Vec<usize>, &mut Window, &mut App) + 'static>;

pub enum WidgetGroupChild {
  Button(Box<Button>),
  Input(Box<Input>),
  Element(AnyElement),
}

impl WidgetGroupChild {
  fn is_selected(&self) -> bool {
    match self {
      Self::Button(button) => button.is_selected(),
      Self::Input(input) => input.is_selected(),
      Self::Element(_) => false,
    }
  }
}

impl From<Button> for WidgetGroupChild {
  fn from(value: Button) -> Self {
    Self::Button(Box::new(value))
  }
}

impl From<Input> for WidgetGroupChild {
  fn from(value: Input) -> Self {
    Self::Input(Box::new(value))
  }
}

impl From<AnyElement> for WidgetGroupChild {
  fn from(value: AnyElement) -> Self {
    Self::Element(value)
  }
}

impl From<Icon> for WidgetGroupChild {
  fn from(value: Icon) -> Self {
    Self::Element(value.into_any_element())
  }
}

impl From<IconLabel> for WidgetGroupChild {
  fn from(value: IconLabel) -> Self {
    Self::Element(value.into_any_element())
  }
}

impl From<Label> for WidgetGroupChild {
  fn from(value: Label) -> Self {
    Self::Element(value.into_any_element())
  }
}

#[derive(IntoElement)]
pub struct WidgetGroup {
  id: ElementId,
  style: StyleRefinement,
  children: Vec<WidgetGroupChild>,
  multiple: bool,
  disabled: bool,
  layout: Axis,
  compact: bool,
  outline: bool,
  variant: Option<ButtonVariant>,
  size: Option<Size>,
  on_click: Option<WidgetGroupClickHandler>,
}

impl WidgetGroup {
  pub fn new(id: impl Into<ElementId>) -> Self {
    Self {
      id: id.into(),
      style: StyleRefinement::default(),
      children: Vec::new(),
      multiple: false,
      disabled: false,
      layout: Axis::Horizontal,
      compact: false,
      outline: false,
      variant: None,
      size: None,
      on_click: None,
    }
  }

  pub fn child(mut self, child: impl Into<WidgetGroupChild>) -> Self {
    self.children.push(child.into());
    self
  }

  pub fn children(mut self, children: impl IntoIterator<Item = WidgetGroupChild>) -> Self {
    self.children.extend(children);
    self
  }

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

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

  pub fn compact(mut self) -> Self {
    self.compact = true;
    self
  }

  pub fn outline(mut self) -> Self {
    self.outline = true;
    self
  }

  pub fn on_click(
    mut self, handler: impl Fn(&Vec<usize>, &mut Window, &mut App) + 'static,
  ) -> Self {
    self.on_click = Some(Box::new(handler));
    self
  }

  fn corners_for(ix: usize, len: usize, layout: Axis) -> Corners<bool> {
    let is_first = ix == 0;
    let is_last = ix + 1 == len;
    if len == 1 {
      return Corners::all(true);
    }

    if matches!(layout, Axis::Horizontal) {
      Corners {
        top_left: is_first,
        top_right: is_last,
        bottom_left: is_first,
        bottom_right: is_last,
      }
    } else {
      Corners {
        top_left: is_first,
        top_right: is_first,
        bottom_left: is_last,
        bottom_right: is_last,
      }
    }
  }

  fn corner_pixels(corners: Corners<bool>, radius: gpui::Pixels) -> Corners<gpui::Pixels> {
    Corners {
      top_left: if corners.top_left { radius } else { px(0.) },
      top_right: if corners.top_right { radius } else { px(0.) },
      bottom_left: if corners.bottom_left { radius } else { px(0.) },
      bottom_right: if corners.bottom_right { radius } else { px(0.) },
    }
  }

  fn divider_color(
    left_active: Option<Hsla>, right_active: Option<Hsla>, disabled: bool, cx: &App,
  ) -> Hsla {
    if let Some(color) = right_active {
      return color;
    }

    if let Some(color) = left_active {
      return color;
    }

    let mut base = cx.theme().input;
    if disabled {
      base.a *= 0.6;
    }
    base
  }

  fn button_active_border_color(
    variant: ButtonVariant, outline: bool, disabled: bool, cx: &App,
  ) -> Hsla {
    let theme = cx.theme();
    let transparent = Hsla::transparent_black();

    let base_border = match variant {
      ButtonVariant::Primary => theme.primary,
      ButtonVariant::Success => theme.success,
      ButtonVariant::Warning => theme.warning,
      ButtonVariant::Info => theme.ring,
      ButtonVariant::Default => theme.border,
      ButtonVariant::Link | ButtonVariant::Flat => transparent,
      ButtonVariant::Danger => theme.danger,
    };

    let mut color = if matches!(variant, ButtonVariant::Flat | ButtonVariant::Link) {
      base_border
    } else if outline {
      if variant == ButtonVariant::Default {
        theme.foreground
      } else {
        base_border
      }
    } else {
      base_border
    };

    if disabled {
      color.a *= 0.6;
    }

    color
  }

  fn input_active_border_color(disabled: bool, cx: &App) -> Hsla {
    let mut color = cx.theme().ring;
    if disabled {
      color.a *= 0.6;
    }
    color
  }

  fn active_border_color(
    child: &WidgetGroupChild, variant: ButtonVariant, outline: bool, disabled: bool,
    window: &Window, cx: &App,
  ) -> Option<Hsla> {
    match child {
      WidgetGroupChild::Button(button) if button.is_selected() => Some(
        Self::button_active_border_color(variant, outline, disabled, cx),
      ),
      WidgetGroupChild::Input(input) if input.is_active(window, cx) => {
        Some(Self::input_active_border_color(disabled, cx))
      }
      _ => None,
    }
  }

  fn divider(layout: Axis, color: Hsla, size: Size) -> AnyElement {
    match layout {
      Axis::Horizontal => div()
        .w(px(1.))
        .h(size.component_height())
        .bg(color)
        .ml(px(-1.))
        .into_any_element(),
      Axis::Vertical => div()
        .h(px(1.))
        .w_full()
        .bg(color)
        .mt(px(-1.))
        .into_any_element(),
    }
  }
}

impl_disableable!(WidgetGroup);

impl Sizable for WidgetGroup {
  fn with_size(mut self, size: impl Into<Size>) -> Self {
    self.size = Some(size.into());
    self
  }
}

impl_styled!(WidgetGroup);

impl crate::ButtonVariants for WidgetGroup {
  fn with_variant(mut self, variant: ButtonVariant) -> Self {
    self.variant = Some(variant);
    self
  }
}

impl RenderOnce for WidgetGroup {
  fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
    let effective_variant = self.variant.unwrap_or_default();
    let active_border_colors = self
      .children
      .iter()
      .map(|child| {
        Self::active_border_color(
          child,
          effective_variant,
          self.outline,
          self.disabled,
          window,
          cx,
        )
      })
      .collect::<Vec<_>>();
    let selected_ixs = self
      .children
      .iter()
      .enumerate()
      .filter_map(|(ix, child)| child.is_selected().then_some(ix))
      .collect::<Vec<_>>();
    let clicked_ix = Rc::new(Cell::new(None));

    let children_len = self.children.len();
    let effective_size = self.size.unwrap_or_default();

    div()
      .id(self.id)
      .flex()
      .when(matches!(self.layout, Axis::Vertical), |this| {
        this
          .flex_col()
          .items_start()
          .when(!self.compact, |this| this.gap_0())
      })
      .when(matches!(self.layout, Axis::Horizontal), |this| {
        this
          .flex_row()
          .items_center()
          .when(!self.compact, |this| this.gap_0())
      })
      .refine_style(&self.style)
      .children(
        self
          .children
          .into_iter()
          .enumerate()
          .flat_map(|(ix, child)| {
            let clicked_ix = clicked_ix.clone();
            let is_first = ix == 0;
            let is_last = ix + 1 == children_len;
            let hide_leading_border = !is_first;
            let hide_trailing_border = !is_last;
            let corners = Self::corners_for(ix, children_len, self.layout);
            let mut elements = Vec::with_capacity(2);

            if ix > 0 {
              let color = Self::divider_color(
                active_border_colors[ix - 1],
                active_border_colors[ix],
                self.disabled,
                cx,
              );
              elements.push(Self::divider(self.layout, color, effective_size));
            }

            let child = match child {
              WidgetGroupChild::Button(button) => button
                .disabled(self.disabled)
                .border_corners(corners)
                .when_some(self.variant, |this, variant| this.with_variant(variant))
                .when_some(self.size, |this, size| this.with_size(size))
                .when(self.outline, |this| this.outline(true))
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.ml(px(0.)).border_l_0(),
                )
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Vertical),
                  |this| this.mt(px(0.)).border_t_0(),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.border_r(px(0.)),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Vertical),
                  |this| this.border_b(px(0.)),
                )
                .when(self.on_click.is_some() && !self.disabled, |this| {
                  this.on_click(move |_, _, _| {
                    clicked_ix.set(Some(ix));
                  })
                })
                .into_any_element(),
              WidgetGroupChild::Input(input) => input
                .disabled(self.disabled)
                .border_corners(corners)
                .with_size(effective_size)
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.ml(px(0.)).border_l_0(),
                )
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Vertical),
                  |this| this.mt(px(0.)).border_t_0(),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.border_r(px(0.)),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Vertical),
                  |this| this.border_b(px(0.)),
                )
                .into_any_element(),
              WidgetGroupChild::Element(element) => h_flex()
                .items_center()
                .component_h(effective_size)
                .px(effective_size.component_px())
                .bg(if self.disabled {
                  cx.theme().muted
                } else {
                  cx.theme().background
                })
                .border_1()
                .border_color(cx.theme().input)
                .corner_radius(Self::corner_pixels(corners, cx.theme().radius))
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.ml(px(0.)).border_l_0(),
                )
                .when(
                  hide_leading_border && matches!(self.layout, Axis::Vertical),
                  |this| this.mt(px(0.)).border_t_0(),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Horizontal),
                  |this| this.border_r(px(0.)),
                )
                .when(
                  hide_trailing_border && matches!(self.layout, Axis::Vertical),
                  |this| this.border_b(px(0.)),
                )
                .child(element)
                .into_any_element(),
            };

            elements.push(child);
            elements
          }),
      )
      .when_some(
        self.on_click.filter(|_| !self.disabled),
        move |this, on_click| {
          this.on_click(move |_, window, cx| {
            let mut next = selected_ixs.clone();
            if let Some(ix) = clicked_ix.get() {
              if self.multiple {
                if let Some(pos) = next.iter().position(|x| *x == ix) {
                  next.remove(pos);
                } else {
                  next.push(ix);
                }
              } else {
                next.clear();
                next.push(ix);
              }
              next.sort_unstable();
            }
            on_click(&next, window, cx);
          })
        },
      )
  }
}