bezel-ui 0.2.23

SwiftUI-flavored components for gpui — popovers, menus, buttons, toggles, and loaders
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
//! [`ColorPicker`]: a saturation/value field, a hue strip, an optional alpha
//! strip and a hex field, drawn in gpui on every platform.
//!
//! The picker is the body of a card, not the popover around it. It emits
//! [`ColorPickerEvent::Changed`] on every drag step and on every hex entry
//! that parses; [`ColorPicker::set_color`] moves it without emitting.
//!
//! ```ignore
//! let picker = cx.new(|cx| ColorPicker::new(color, true, cx));
//! cx.subscribe(&picker, |view, _, event, cx| match event {
//!     ColorPickerEvent::Changed(color) => view.set_tint(*color, cx),
//! })
//! .detach();
//! ```

use std::cell::Cell;
use std::rc::Rc;

use gpui::{
    Axis, Bounds, Context, DragMoveEvent, Entity, EntityId, EventEmitter, Hsla, MouseButton,
    MouseDownEvent, Pixels, Point, Rgba, Subscription, Window, canvas, div, linear_color_stop,
    linear_gradient, prelude::*, px,
};
use theme::{TextStyle, Theme, Typeset};

use crate::input::{FieldEvent, TextField};
use crate::{stack, widgets};

const FIELD_HEIGHT: f32 = 96.0;
const STRIP_HEIGHT: f32 = 12.0;
const KNOB: f32 = 12.0;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ColorPickerEvent {
    Changed(Hsla),
}

/// Hue, saturation, value and alpha, each in `0..=1`. Hue is kept apart from
/// the colour so a grey keeps the hue it was dragged at.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Hsva {
    h: f32,
    s: f32,
    v: f32,
    a: f32,
}

impl Hsva {
    fn from_hsla(color: Hsla) -> Self {
        let Rgba { r, g, b, a } = color.to_rgb();
        let max = r.max(g).max(b);
        let delta = max - r.min(g).min(b);
        let h = if delta <= f32::EPSILON {
            color.h
        } else if max == r {
            ((g - b) / delta).rem_euclid(6.0) / 6.0
        } else if max == g {
            ((b - r) / delta + 2.0) / 6.0
        } else {
            ((r - g) / delta + 4.0) / 6.0
        };
        let s = if max <= f32::EPSILON {
            0.0
        } else {
            delta / max
        };
        Self { h, s, v: max, a }
    }

    fn to_hsla(self) -> Hsla {
        let Self { h, s, v, a } = self;
        let sector = (h.rem_euclid(1.0)) * 6.0;
        let chroma = v * s;
        let x = chroma * (1.0 - (sector.rem_euclid(2.0) - 1.0).abs());
        let (r, g, b) = match sector as u32 {
            0 => (chroma, x, 0.0),
            1 => (x, chroma, 0.0),
            2 => (0.0, chroma, x),
            3 => (0.0, x, chroma),
            4 => (x, 0.0, chroma),
            _ => (chroma, 0.0, x),
        };
        let m = v - chroma;
        Rgba {
            r: r + m,
            g: g + m,
            b: b + m,
            a,
        }
        .into()
    }
}

/// `#rrggbb`, or `#rrggbbaa` when `alpha` is set.
pub fn to_hex(color: Hsla, alpha: bool) -> String {
    let Rgba { r, g, b, a } = color.to_rgb();
    let byte = |value: f32| (value.clamp(0.0, 1.0) * 255.0).round() as u8;
    match alpha {
        true => format!(
            "#{:02x}{:02x}{:02x}{:02x}",
            byte(r),
            byte(g),
            byte(b),
            byte(a)
        ),
        false => format!("#{:02x}{:02x}{:02x}", byte(r), byte(g), byte(b)),
    }
}

/// Six or eight hex digits, with or without a leading `#`.
pub fn parse_hex(text: &str) -> Option<Hsla> {
    let hex = text.trim();
    let hex = hex.strip_prefix('#').unwrap_or(hex);
    if !matches!(hex.len(), 6 | 8) || !hex.is_ascii() {
        return None;
    }
    let byte = |at: usize| u8::from_str_radix(&hex[at..at + 2], 16).ok();
    let alpha = match hex.len() {
        8 => byte(6)?,
        _ => 255,
    };
    let channel = |value: u8| value as f32 / 255.0;
    Some(
        Rgba {
            r: channel(byte(0)?),
            g: channel(byte(2)?),
            b: channel(byte(4)?),
            a: channel(alpha),
        }
        .into(),
    )
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Area {
    Field,
    Hue,
    Alpha,
}

/// The drag payload. Carries the picker's entity so two pickers on screen do
/// not both follow one drag.
struct PickerDrag(EntityId, Area);

pub struct ColorPicker {
    hsva: Hsva,
    alpha: bool,
    hex: Entity<TextField>,
    bounds: [Rc<Cell<Bounds<Pixels>>>; 3],
    _hex: Subscription,
}

impl EventEmitter<ColorPickerEvent> for ColorPicker {}

impl ColorPicker {
    /// `alpha` shows the alpha strip and writes the hex with eight digits.
    /// Without it, every emitted colour is opaque.
    pub fn new(color: Hsla, alpha: bool, cx: &mut Context<Self>) -> Self {
        let mut hsva = Hsva::from_hsla(color);
        if !alpha {
            hsva.a = 1.0;
        }
        let text = to_hex(hsva.to_hsla(), alpha);
        let hex = cx.new(|cx| {
            let mut field = TextField::new(cx).with_placeholder(match alpha {
                true => "#rrggbbaa",
                false => "#rrggbb",
            });
            field.set_content(text, cx);
            field
        });
        let subscription = cx.subscribe(&hex, |this, field, event, cx| {
            if !matches!(event, FieldEvent::Changed(_)) {
                return;
            }
            // The picker's own writes come back here too, and change nothing.
            let Some(color) = parse_hex(field.read(cx).content())
                .filter(|color| to_hex(*color, this.alpha) != to_hex(this.color(), this.alpha))
            else {
                return;
            };
            let mut hsva = Hsva::from_hsla(color);
            if !this.alpha {
                hsva.a = 1.0;
            }
            hsva.h = match hsva.s <= f32::EPSILON {
                true => this.hsva.h,
                false => hsva.h,
            };
            this.hsva = hsva;
            cx.emit(ColorPickerEvent::Changed(hsva.to_hsla()));
            cx.notify();
        });
        Self {
            hsva,
            alpha,
            hex,
            bounds: Default::default(),
            _hex: subscription,
        }
    }

    pub fn color(&self) -> Hsla {
        self.hsva.to_hsla()
    }

    /// Move to `color` without emitting. A colour equal to the current one
    /// keeps the current hue.
    pub fn set_color(&mut self, color: Hsla, cx: &mut Context<Self>) {
        if to_hex(color, self.alpha) == to_hex(self.color(), self.alpha) {
            return;
        }
        let mut hsva = Hsva::from_hsla(color);
        if !self.alpha {
            hsva.a = 1.0;
        }
        self.hsva = hsva;
        self.sync_hex(cx);
        cx.notify();
    }

    fn sync_hex(&mut self, cx: &mut Context<Self>) {
        let text = to_hex(self.color(), self.alpha);
        self.hex.update(cx, |field, cx| field.set_content(text, cx));
    }

    fn point(&mut self, area: Area, position: Point<Pixels>, cx: &mut Context<Self>) {
        let mut bounds = self.bounds[area as usize].get();
        if area != Area::Field {
            // A strip's knob travels inside it, so its centre runs half a knob
            // in from either end.
            bounds.origin.x += px(KNOB / 2.0);
            bounds.size.width -= px(KNOB);
        }
        let x = widgets::axis_fraction(position, bounds, Axis::Horizontal, 0.0);
        match area {
            Area::Field => {
                self.hsva.s = x;
                self.hsva.v = 1.0 - widgets::axis_fraction(position, bounds, Axis::Vertical, 0.0);
            }
            Area::Hue => self.hsva.h = x,
            Area::Alpha => self.hsva.a = x,
        }
        self.sync_hex(cx);
        cx.emit(ColorPickerEvent::Changed(self.color()));
        cx.notify();
    }

    /// One draggable area: records its bounds each paint, jumps on press and
    /// follows the drag.
    fn area(
        &self,
        area: Area,
        body: gpui::Div,
        cx: &mut Context<Self>,
    ) -> gpui::Stateful<gpui::Div> {
        let entity = cx.entity_id();
        let bounds = self.bounds[area as usize].clone();
        body.id(("color-picker", area as usize))
            .relative()
            .cursor_pointer()
            .child(
                canvas(move |rect, _, _| bounds.set(rect), |_, _, _, _| {})
                    .absolute()
                    .size_full(),
            )
            .on_mouse_down(
                MouseButton::Left,
                cx.listener(move |this, event: &MouseDownEvent, _, cx| {
                    this.point(area, event.position, cx)
                }),
            )
            .on_drag(PickerDrag(entity, area), |_, _, _, cx| {
                cx.new(|_| gpui::Empty)
            })
            .on_drag_move(
                cx.listener(move |this, event: &DragMoveEvent<PickerDrag>, _, cx| {
                    let PickerDrag(owner, dragged) = *event.drag(cx);
                    if owner == entity && dragged == area {
                        this.point(area, event.event.position, cx);
                    }
                }),
            )
    }
}

fn knob_face() -> gpui::Div {
    div()
        .absolute()
        .size(px(KNOB))
        .rounded_full()
        .border_2()
        .border_color(gpui::white())
        .shadow_sm()
}

/// The field's knob, centred on the point it marks.
fn field_knob(left: f32, top: f32) -> gpui::Div {
    knob_face()
        .left(gpui::relative(left))
        .top(gpui::relative(top))
        .ml(px(-KNOB / 2.0))
        .mt(px(-KNOB / 2.0))
}

/// A strip's knob, kept inside the strip at either end.
fn strip_knob(at: f32) -> gpui::Div {
    div()
        .absolute()
        .top_0()
        .bottom_0()
        .left_0()
        .right(px(KNOB))
        .child(knob_face().top_0().left(gpui::relative(at)))
}

fn strip() -> gpui::Div {
    div().w_full().h(px(STRIP_HEIGHT)).rounded_full().flex()
}

impl Render for ColorPicker {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = Theme::of(cx).clone();
        let Hsva { h, s, v, a } = self.hsva;
        let pure = Hsva {
            h,
            s: 1.0,
            v: 1.0,
            a: 1.0,
        }
        .to_hsla();
        let opaque = Hsva {
            a: 1.0,
            ..self.hsva
        }
        .to_hsla();

        // Square: rounded layers each antialias their own corner, and the
        // lower ones show through the black at the bottom.
        let field = div()
            .w_full()
            .h(px(FIELD_HEIGHT))
            .bg(linear_gradient(
                90.0,
                linear_color_stop(gpui::white(), 0.0),
                linear_color_stop(pure, 1.0),
            ))
            .child(div().absolute().size_full().bg(linear_gradient(
                180.0,
                linear_color_stop(gpui::black().opacity(0.0), 0.0),
                linear_color_stop(gpui::black(), 1.0),
            )))
            .child(field_knob(s, 1.0 - v));

        // gpui gradients take two stops, so the hue runs as six segments.
        let hue_at = |at: f32| {
            Hsva {
                h: at,
                s: 1.0,
                v: 1.0,
                a: 1.0,
            }
            .to_hsla()
        };
        let hue = strip()
            .children((0..6).map(|segment| {
                let from = segment as f32 / 6.0;
                div()
                    .flex_1()
                    .h_full()
                    .when(segment == 0, |div| div.rounded_l_full())
                    .when(segment == 5, |div| div.rounded_r_full())
                    .bg(linear_gradient(
                        90.0,
                        linear_color_stop(hue_at(from), 0.0),
                        linear_color_stop(hue_at(from + 1.0 / 6.0), 1.0),
                    ))
            }))
            .child(strip_knob(h));

        let alpha = self.alpha.then(|| {
            let strip = strip()
                .bg(theme.ink(0.12))
                .child(
                    div()
                        .absolute()
                        .size_full()
                        .rounded_full()
                        .bg(linear_gradient(
                            90.0,
                            linear_color_stop(opaque.opacity(0.0), 0.0),
                            linear_color_stop(opaque, 1.0),
                        )),
                )
                .child(strip_knob(a));
            self.area(Area::Alpha, strip, cx)
        });

        stack::column()
            .w_full()
            .gap(px(8.0))
            .child(self.area(Area::Field, field, cx))
            .child(self.area(Area::Hue, hue, cx))
            .children(alpha)
            .child(
                stack::row()
                    .gap(px(8.0))
                    .items_center()
                    .child(
                        div()
                            .flex_none()
                            .size(px(20.0))
                            .rounded_full()
                            .border_1()
                            .border_color(theme.ink(0.25))
                            .bg(self.color()),
                    )
                    .child(
                        div()
                            .flex_1()
                            .min_w_0()
                            .text_style(TextStyle::Callout)
                            .font_family(theme.font_mono.clone())
                            .child(self.hex.clone()),
                    ),
            )
    }
}