guise-ui 1.5.3

A component library for gpui, Zed's GPU-accelerated UI framework: a themed palette, sizing tokens, 130+ composable components, a reactive state layer, and an in-app Safari-style inspector.
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Turning a gpui [`StyleRefinement`] into something that reads like CSS.
//!
//! Safari's Styles sidebar is a list of declarations and its Computed sidebar
//! is a box-model diagram; both want `property: value` pairs, and gpui stores
//! style as a refinement of typed `Option` fields. This module is the
//! translation, kept pure so it can be tested without a window.
//!
//! Only *set* fields are emitted. That is the useful behaviour and also the
//! honest one: a refinement records what a component actually asked for, and
//! inventing defaults for the rest would show style the element does not have.

use gpui::{
  AbsoluteLength, DefiniteLength, Edges, EdgesRefinement, Fill, Hsla, Length, Pixels, SharedString,
  StyleRefinement,
};

/// One `property: value` line in the Styles sidebar.
#[derive(Debug, Clone, PartialEq)]
pub struct Declaration {
  pub property: SharedString,
  pub value: SharedString,
  /// Set when the value names a color, so the row can paint a swatch.
  pub color: Option<Hsla>,
}

impl Declaration {
  fn new(property: &'static str, value: impl Into<SharedString>) -> Self {
    Declaration {
      property: SharedString::new_static(property),
      value: value.into(),
      color: None,
    }
  }

  fn colored(property: &'static str, color: Hsla) -> Self {
    Declaration {
      property: SharedString::new_static(property),
      value: hex(color).into(),
      color: Some(color),
    }
  }
}

/// `#rrggbb`, or `#rrggbbaa` when the color is not fully opaque — the notation
/// Safari's color swatches label themselves with.
pub fn hex(color: Hsla) -> String {
  let rgba = gpui::Rgba::from(color);
  let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
  if rgba.a >= 1.0 {
    format!(
      "#{:02x}{:02x}{:02x}",
      byte(rgba.r),
      byte(rgba.g),
      byte(rgba.b)
    )
  } else {
    format!(
      "#{:02x}{:02x}{:02x}{:02x}",
      byte(rgba.r),
      byte(rgba.g),
      byte(rgba.b),
      byte(rgba.a)
    )
  }
}

/// Recover the color behind a [`Fill`].
///
/// `Background::solid` is crate-private in gpui and `Fill::color` hands back
/// the same opaque `Background`, so its `Debug` output is the only public view
/// of the value. Parsing it is unlovely, but it is contained here, it is
/// covered by tests, and the failure mode is a missing swatch rather than a
/// wrong one.
pub fn fill_color(fill: &Fill) -> Option<Hsla> {
  let background = fill.color()?;
  let text = format!("{background:?}");
  if !text.starts_with("Solid(") {
    return None;
  }
  let floats: Vec<f32> = text
    .split(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-'))
    .filter(|part| !part.is_empty())
    .filter_map(|part| part.parse::<f32>().ok())
    .collect();
  match floats.as_slice() {
    [h, s, l, a, ..] => Some(Hsla {
      h: *h,
      s: *s,
      l: *l,
      a: *a,
    }),
    _ => None,
  }
}

fn absolute(length: AbsoluteLength) -> String {
  match length {
    AbsoluteLength::Pixels(px) => format!("{}px", trim(f32::from(px))),
    AbsoluteLength::Rems(rems) => format!("{}rem", trim(rems.0)),
  }
}

fn definite(length: DefiniteLength) -> String {
  match length {
    DefiniteLength::Absolute(absolute_length) => absolute(absolute_length),
    DefiniteLength::Fraction(fraction) => format!("{}%", trim(fraction * 100.0)),
  }
}

fn length(length: Length) -> String {
  match length {
    Length::Definite(definite_length) => definite(definite_length),
    Length::Auto => "auto".to_string(),
  }
}

/// Drop the trailing `.0` so `12px` does not read as `12.0px`.
fn trim(value: f32) -> String {
  if (value - value.round()).abs() < f32::EPSILON {
    format!("{}", value.round() as i64)
  } else {
    format!("{value:.2}")
      .trim_end_matches('0')
      .trim_end_matches('.')
      .to_string()
  }
}

/// Emit `prefix`, `prefix-top`, … for a set of edges, collapsing to the
/// shorthand when every side agrees — the same rule a CSS formatter uses.
fn edges<T: Copy + PartialEq>(
  out: &mut Vec<Declaration>,
  shorthand: &'static str,
  sides: [(&'static str, Option<T>); 4],
  format: impl Fn(T) -> String,
) {
  let values: Vec<T> = sides.iter().filter_map(|(_, value)| *value).collect();
  if values.len() == 4 && values.windows(2).all(|pair| pair[0] == pair[1]) {
    out.push(Declaration::new(shorthand, format(values[0])));
    return;
  }
  for (property, value) in sides {
    if let Some(value) = value {
      out.push(Declaration::new(property, format(value)));
    }
  }
}

/// Every declaration the refinement actually sets, in CSS authoring order:
/// layout, then box, then flex, then visual, then text.
pub fn declarations(style: &StyleRefinement) -> Vec<Declaration> {
  let mut out = Vec::new();

  if let Some(display) = style.display {
    out.push(Declaration::new(
      "display",
      format!("{display:?}").to_lowercase(),
    ));
  }
  if let Some(position) = style.position {
    out.push(Declaration::new(
      "position",
      format!("{position:?}").to_lowercase(),
    ));
  }
  if let Some(visibility) = style.visibility {
    out.push(Declaration::new(
      "visibility",
      format!("{visibility:?}").to_lowercase(),
    ));
  }
  if let Some(overflow) = style.overflow.x {
    out.push(Declaration::new(
      "overflow-x",
      format!("{overflow:?}").to_lowercase(),
    ));
  }
  if let Some(overflow) = style.overflow.y {
    out.push(Declaration::new(
      "overflow-y",
      format!("{overflow:?}").to_lowercase(),
    ));
  }

  edges(
    &mut out,
    "inset",
    [
      ("top", style.inset.top),
      ("right", style.inset.right),
      ("bottom", style.inset.bottom),
      ("left", style.inset.left),
    ],
    length,
  );

  if let Some(width) = style.size.width {
    out.push(Declaration::new("width", length(width)));
  }
  if let Some(height) = style.size.height {
    out.push(Declaration::new("height", length(height)));
  }
  if let Some(width) = style.min_size.width {
    out.push(Declaration::new("min-width", length(width)));
  }
  if let Some(height) = style.min_size.height {
    out.push(Declaration::new("min-height", length(height)));
  }
  if let Some(width) = style.max_size.width {
    out.push(Declaration::new("max-width", length(width)));
  }
  if let Some(height) = style.max_size.height {
    out.push(Declaration::new("max-height", length(height)));
  }
  if let Some(ratio) = style.aspect_ratio {
    out.push(Declaration::new("aspect-ratio", trim(ratio)));
  }

  edges(
    &mut out,
    "margin",
    [
      ("margin-top", style.margin.top),
      ("margin-right", style.margin.right),
      ("margin-bottom", style.margin.bottom),
      ("margin-left", style.margin.left),
    ],
    length,
  );
  edges(
    &mut out,
    "padding",
    [
      ("padding-top", style.padding.top),
      ("padding-right", style.padding.right),
      ("padding-bottom", style.padding.bottom),
      ("padding-left", style.padding.left),
    ],
    definite,
  );
  edges(
    &mut out,
    "border-width",
    [
      ("border-top-width", style.border_widths.top),
      ("border-right-width", style.border_widths.right),
      ("border-bottom-width", style.border_widths.bottom),
      ("border-left-width", style.border_widths.left),
    ],
    absolute,
  );

  if let Some(direction) = style.flex_direction {
    out.push(Declaration::new(
      "flex-direction",
      match format!("{direction:?}").as_str() {
        "Row" => "row".to_string(),
        "Column" => "column".to_string(),
        "RowReverse" => "row-reverse".to_string(),
        "ColumnReverse" => "column-reverse".to_string(),
        other => other.to_lowercase(),
      },
    ));
  }
  if let Some(wrap) = style.flex_wrap {
    out.push(Declaration::new("flex-wrap", kebab(&format!("{wrap:?}"))));
  }
  if let Some(align) = style.align_items {
    out.push(Declaration::new(
      "align-items",
      kebab(&format!("{align:?}")),
    ));
  }
  if let Some(align) = style.align_self {
    out.push(Declaration::new("align-self", kebab(&format!("{align:?}"))));
  }
  if let Some(align) = style.align_content {
    out.push(Declaration::new(
      "align-content",
      kebab(&format!("{align:?}")),
    ));
  }
  if let Some(justify) = style.justify_content {
    out.push(Declaration::new(
      "justify-content",
      kebab(&format!("{justify:?}")),
    ));
  }
  if let Some(basis) = style.flex_basis {
    out.push(Declaration::new("flex-basis", length(basis)));
  }
  if let Some(grow) = style.flex_grow {
    out.push(Declaration::new("flex-grow", trim(grow)));
  }
  if let Some(shrink) = style.flex_shrink {
    out.push(Declaration::new("flex-shrink", trim(shrink)));
  }
  if let Some(gap) = style.gap.width {
    out.push(Declaration::new("column-gap", definite(gap)));
  }
  if let Some(gap) = style.gap.height {
    out.push(Declaration::new("row-gap", definite(gap)));
  }

  if let Some(fill) = &style.background {
    match fill_color(fill) {
      Some(color) => out.push(Declaration::colored("background-color", color)),
      None => out.push(Declaration::new("background", format!("{fill:?}"))),
    }
  }
  if let Some(color) = style.border_color {
    out.push(Declaration::colored("border-color", color));
  }
  if let Some(style_) = style.border_style {
    out.push(Declaration::new(
      "border-style",
      format!("{style_:?}").to_lowercase(),
    ));
  }

  let radii = [
    ("border-top-left-radius", style.corner_radii.top_left),
    ("border-top-right-radius", style.corner_radii.top_right),
    (
      "border-bottom-right-radius",
      style.corner_radii.bottom_right,
    ),
    ("border-bottom-left-radius", style.corner_radii.bottom_left),
  ];
  edges(&mut out, "border-radius", radii, absolute);

  if let Some(opacity) = style.opacity {
    out.push(Declaration::new("opacity", trim(opacity)));
  }
  if !style
    .box_shadow
    .as_ref()
    .is_none_or(|shadows| shadows.is_empty())
  {
    let count = style.box_shadow.as_ref().map_or(0, |shadows| shadows.len());
    out.push(Declaration::new(
      "box-shadow",
      if count == 1 {
        "1 shadow".to_string()
      } else {
        format!("{count} shadows")
      },
    ));
  }

  if let Some(text) = &style.text {
    if let Some(color) = text.color {
      out.push(Declaration::colored("color", color));
    }
    if let Some(family) = &text.font_family {
      out.push(Declaration::new("font-family", family.clone()));
    }
    if let Some(size) = text.font_size {
      out.push(Declaration::new("font-size", absolute(size)));
    }
    if let Some(weight) = text.font_weight {
      out.push(Declaration::new("font-weight", trim(weight.0)));
    }
    if let Some(font_style) = text.font_style {
      out.push(Declaration::new(
        "font-style",
        format!("{font_style:?}").to_lowercase(),
      ));
    }
    if let Some(height) = text.line_height {
      out.push(Declaration::new("line-height", definite(height)));
    }
    if let Some(align) = text.text_align {
      out.push(Declaration::new("text-align", kebab(&format!("{align:?}"))));
    }
    if let Some(white_space) = text.white_space {
      out.push(Declaration::new(
        "white-space",
        kebab(&format!("{white_space:?}")),
      ));
    }
    if let Some(color) = text.background_color {
      out.push(Declaration::colored("background-color (text)", color));
    }
  }

  out
}

/// `SpaceBetween` -> `space-between`.
fn kebab(variant: &str) -> String {
  let mut out = String::with_capacity(variant.len() + 4);
  for (index, ch) in variant.chars().enumerate() {
    if ch.is_uppercase() {
      if index > 0 {
        out.push('-');
      }
      out.extend(ch.to_lowercase());
    } else {
      out.push(ch);
    }
  }
  out
}

/// The four nested boxes of the Computed sidebar's diagram. Every value is in
/// pixels, already resolved against the rem size, so the diagram can label
/// itself with numbers rather than units.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct BoxModel {
  pub margin: Edges<f32>,
  pub border: Edges<f32>,
  pub padding: Edges<f32>,
  /// The laid-out size, which is the border box gpui measured.
  pub width: f32,
  pub height: f32,
}

impl BoxModel {
  /// The content box: the laid-out size less border and padding.
  pub fn content(&self) -> (f32, f32) {
    let width =
      self.width - self.border.left - self.border.right - self.padding.left - self.padding.right;
    let height =
      self.height - self.border.top - self.border.bottom - self.padding.top - self.padding.bottom;
    (width.max(0.0), height.max(0.0))
  }
}

fn edge_pixels<T: Copy + std::fmt::Debug + Default + PartialEq>(
  edges: &EdgesRefinement<T>,
  resolve: impl Fn(T) -> f32,
) -> Edges<f32> {
  Edges {
    top: edges.top.map(&resolve).unwrap_or(0.0),
    right: edges.right.map(&resolve).unwrap_or(0.0),
    bottom: edges.bottom.map(&resolve).unwrap_or(0.0),
    left: edges.left.map(&resolve).unwrap_or(0.0),
  }
}

/// Resolve the box model for an element, given the bounds it was laid out at.
///
/// `rem_size` is what turns rem-based style into the pixel numbers the diagram
/// prints; percentage values have no parent to resolve against here and read
/// as zero, exactly as Safari shows them when it cannot compute one.
pub fn box_model(style: &StyleRefinement, size: gpui::Size<Pixels>, rem_size: Pixels) -> BoxModel {
  let absolute_px = |value: AbsoluteLength| f32::from(value.to_pixels(rem_size));
  let definite_px = |value: DefiniteLength| match value {
    DefiniteLength::Absolute(absolute) => absolute_px(absolute),
    DefiniteLength::Fraction(_) => 0.0,
  };
  let length_px = |value: Length| match value {
    Length::Definite(definite) => definite_px(definite),
    Length::Auto => 0.0,
  };

  BoxModel {
    margin: edge_pixels(&style.margin, length_px),
    border: edge_pixels(&style.border_widths, absolute_px),
    padding: edge_pixels(&style.padding, definite_px),
    width: f32::from(size.width),
    height: f32::from(size.height),
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use gpui::{px, rems, Styled};

  fn style_of(build: impl FnOnce(gpui::Div) -> gpui::Div) -> StyleRefinement {
    let mut div = build(gpui::div());
    div.style().clone()
  }

  fn find<'a>(declarations: &'a [Declaration], property: &str) -> Option<&'a Declaration> {
    declarations
      .iter()
      .find(|d| d.property.as_ref() == property)
  }

  #[test]
  fn only_set_fields_are_emitted() {
    let declarations = declarations(&style_of(|d| d.w(px(120.0))));
    assert_eq!(declarations.len(), 1);
    assert_eq!(declarations[0].property.as_ref(), "width");
    assert_eq!(declarations[0].value.as_ref(), "120px");
  }

  #[test]
  fn uniform_edges_collapse_to_the_shorthand() {
    let declarations = declarations(&style_of(|d| d.p(px(8.0))));
    assert_eq!(
      find(&declarations, "padding").unwrap().value.as_ref(),
      "8px"
    );
    assert!(find(&declarations, "padding-top").is_none());
  }

  #[test]
  fn mixed_edges_stay_long_hand() {
    let declarations = declarations(&style_of(|d| d.pt(px(4.0)).pb(px(10.0))));
    assert!(find(&declarations, "padding").is_none());
    assert_eq!(
      find(&declarations, "padding-top").unwrap().value.as_ref(),
      "4px"
    );
    assert_eq!(
      find(&declarations, "padding-bottom")
        .unwrap()
        .value
        .as_ref(),
      "10px"
    );
  }

  #[test]
  fn rems_keep_their_unit() {
    let declarations = declarations(&style_of(|d| d.w(rems(2.0))));
    assert_eq!(find(&declarations, "width").unwrap().value.as_ref(), "2rem");
  }

  #[test]
  fn fractions_render_as_percentages() {
    let declarations = declarations(&style_of(|d| d.w_1_2()));
    assert_eq!(find(&declarations, "width").unwrap().value.as_ref(), "50%");
  }

  #[test]
  fn variants_are_kebab_cased() {
    let declarations = declarations(&style_of(|d| d.flex().justify_between().items_center()));
    assert_eq!(
      find(&declarations, "justify-content")
        .unwrap()
        .value
        .as_ref(),
      "space-between"
    );
    assert_eq!(
      find(&declarations, "align-items").unwrap().value.as_ref(),
      "center"
    );
    assert_eq!(
      find(&declarations, "display").unwrap().value.as_ref(),
      "flex"
    );
  }

  #[test]
  fn a_background_yields_a_swatch_color() {
    let blue = gpui::hsla(0.6, 0.5, 0.5, 1.0);
    let declarations = declarations(&style_of(|d| d.bg(blue)));
    let background = find(&declarations, "background-color").unwrap();

    let color = background
      .color
      .expect("a solid fill should recover its color");
    assert!((color.h - blue.h).abs() < 0.01);
    assert!((color.s - blue.s).abs() < 0.01);
    assert!((color.l - blue.l).abs() < 0.01);
    assert!((color.a - blue.a).abs() < 0.01);
  }

  #[test]
  fn a_translucent_color_keeps_its_alpha_byte() {
    assert_eq!(hex(gpui::hsla(0.0, 0.0, 0.0, 1.0)), "#000000");
    assert_eq!(hex(gpui::hsla(0.0, 0.0, 1.0, 1.0)), "#ffffff");
    assert_eq!(hex(gpui::hsla(0.0, 0.0, 0.0, 0.5)), "#00000080");
  }

  #[test]
  fn the_box_model_resolves_rems_against_the_rem_size() {
    let style = style_of(|d| d.p(rems(1.0)).border_2().m(px(6.0)));
    let model = box_model(&style, gpui::size(px(100.0), px(50.0)), px(16.0));

    assert_eq!(model.padding.top, 16.0);
    assert_eq!(model.border.left, 2.0);
    assert_eq!(model.margin.bottom, 6.0);
    assert_eq!(model.content(), (100.0 - 32.0 - 4.0, 50.0 - 32.0 - 4.0));
  }

  #[test]
  fn a_content_box_never_goes_negative() {
    let style = style_of(|d| d.p(px(80.0)));
    let model = box_model(&style, gpui::size(px(20.0), px(20.0)), px(16.0));
    assert_eq!(model.content(), (0.0, 0.0));
  }

  #[test]
  fn pixel_values_lose_their_trailing_zero() {
    assert_eq!(trim(12.0), "12");
    assert_eq!(trim(12.5), "12.5");
    assert_eq!(trim(0.25), "0.25");
  }
}