lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
//! The inspector, beside the tree: what the selected node is, what rendered it, its props, contexts, reactive
//! values and attributes in a table of their own each, and its layout and style at the end.

use super::{ComponentsState, DetailTab, Inspected, summary};
use crate::{
  app::{
    component::ComponentInfo,
    ctx::ComponentContextKind,
    devtools::{
      kit::{self, FILL, line, mono, sans, short_tag, short_type_name},
      snapshot::{DevToolsNode, DevToolsNodeKind, DevToolsSnapshot},
    },
    theme::PaletteColor,
  },
  components::{Column, Row, Spacer},
  layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
  node::Element,
  ui::{
    Badge, BadgeSize, BadgeVariant, Button, ButtonSize, ButtonVariant, EmptyState, Icon, Separator, Switch, Tab, Table,
    TableColumn, Tabs, TabsPart, TabsVariant, icons,
  },
};

/// The sizes the board gives the inspector (`pencil/lurq-devtools.pen`, Components · dark, Inspector).
const HEADER_HEIGHT: f32 = 48.0;
const RENDER_INFO_HEIGHT: f32 = 36.0;
const TABS_HEIGHT: f32 = 40.0;
const PADDING: f32 = kit::DETAIL_PADDING;
const ID_WIDTH: f32 = 104.0;
const NAME_WIDTH: f32 = 200.0;
const VALUE_WIDTH: f32 = 200.0;
const FIELD_INDENT: f32 = 16.0;

pub(super) fn panel(snapshot: &DevToolsSnapshot, state: &ComponentsState, inspected: &Inspected) -> Element {
  let path = state.selected_path.get();
  let Some(node) = snapshot.selected_node(&path) else {
    return Column::new()
      .size(FILL, FILL)
      .align_items(Alignment::Center)
      .justify(Justify::Center)
      .child(
        EmptyState::new("Nothing selected")
          .description("Select a node of the tree to see what it is made of.")
          .icon(icons::CURSOR),
      )
      .into();
  };
  Column::new()
    .size(FILL, FILL)
    .child(header(node, &path, state, inspected))
    .child(render_info(snapshot, node, &path))
    .child(
      Row::new()
        .width(FILL)
        .flex(1.0)
        .align_items(Alignment::Stretch)
        .child(detail(node, state))
        .child(summary::panel(node)),
    )
    .into()
}

/// The node's name, what it is, and what is done with it in the inspected window.
fn header(node: &DevToolsNode, path: &[usize], state: &ComponentsState, inspected: &Inspected) -> Row {
  let kind = match node.kind {
    DevToolsNodeKind::Component => "Component",
    DevToolsNodeKind::Element => "Element",
  };
  let (overlaid, overlay_told) = (state.clone(), inspected.clone());
  let (shot, shot_path) = (inspected.on_save_node_screenshot.clone(), path.to_vec());
  Row::new()
    .width(FILL)
    .height(HEADER_HEIGHT)
    .padding_left(PADDING)
    .padding_right(16.0)
    .spacing(10.0)
    .align_items(Alignment::Center)
    .border_bottom(line())
    .child(mono(short_tag(&node.tag), 15.0, FontWeight::SemiBold))
    .child(Badge::new(kind).variant(BadgeVariant::Outline).size(BadgeSize::Sm))
    .child(mono(&format!("NodeId({})", node.id.value()), 12.0, FontWeight::Normal).color(PaletteColor::MutedForeground))
    .child(Spacer::new().flex(1.0))
    .child(
      Row::new()
        .spacing(8.0)
        .align_items(Alignment::Center)
        .child(sans("Overlay", 12.0, FontWeight::Normal).color(PaletteColor::MutedForeground))
        .child(
          Switch::new("Overlay")
            .label_hidden(true)
            .on(state.overlay.get())
            .on_change(move |on| overlaid.set_overlay(on, &overlay_told))
            .id("devtools-overlay"),
        ),
    )
    .child(Separator::vertical().length(16.0))
    .child(
      Button::new("Screenshot")
        .variant(ButtonVariant::Outline)
        .size(ButtonSize::Xs)
        .icon(icons::IMAGE)
        .disabled(shot.is_none())
        .on_click(move |_| {
          if let Some(save) = &shot {
            save(shot_path.clone());
          }
        })
        .id("devtools-screenshot"),
    )
}

/// What is known of how the node came to be drawn.
fn render_info(snapshot: &DevToolsSnapshot, node: &DevToolsNode, path: &[usize]) -> Row {
  let children = match node.children.as_slice() {
    [] => "0".to_owned(),
    [child] => format!("1 ({})", short_tag(&child.tag)),
    children => children.len().to_string(),
  };
  let mut row = Row::new()
    .width(FILL)
    .height(RENDER_INFO_HEIGHT)
    .padding_horizontal(PADDING)
    .padding_bottom(10.0)
    .spacing(28.0)
    .align_items(Alignment::Center)
    .border_bottom(line());
  if let Some(parent) = rendered_by(snapshot, path) {
    row = row.child(stat("Rendered by", short_tag(&parent.tag)));
  }
  // A component says how it rendered; an element is drawn by the render of the component it is in.
  if let Some(render) = node.render {
    row = row
      .child(stat("Render count", &render.count.to_string()))
      .child(stat("Last render", &render_time(render.last)));
  }
  row.child(stat("Children", &children))
}

/// How long a render took, as short as it reads: `2.1ms`, `340µs`.
pub(crate) fn render_time(time: std::time::Duration) -> String {
  let micros = time.as_secs_f64() * 1_000_000.0;
  if micros >= 1_000.0 {
    format!("{:.1}ms", micros / 1_000.0)
  } else {
    format!("{micros:.0}µs")
  }
}

fn stat(label: &str, value: &str) -> Row {
  Row::new()
    .spacing(8.0)
    .align_items(Alignment::Center)
    .child(sans(label, 12.0, FontWeight::Normal).color(PaletteColor::MutedForeground))
    .child(mono(value, 12.0, FontWeight::Medium))
}

/// The component whose render built the node: the nearest component it is in.
pub(crate) fn rendered_by<'a>(snapshot: &'a DevToolsSnapshot, path: &[usize]) -> Option<&'a DevToolsNode> {
  let mut node = snapshot.root.as_ref()?;
  let mut nearest = None;
  for index in path {
    if node.kind == DevToolsNodeKind::Component {
      nearest = Some(node);
    }
    node = node.children.get(*index)?;
  }
  nearest
}

/// The lists of the node, one at a time: a tab for each over its table.
fn detail(node: &DevToolsNode, state: &ComponentsState) -> Column {
  let props = prop_rows(node);
  let contexts = context_rows(node);
  let reactive = reactive_rows(node);
  let attributes = attribute_rows(node);
  let tabs = Tabs::new(
    state.detail.clone(),
    [
      // The struct's own row is not one of its props.
      Tab::new(DetailTab::Props, "Props").count(props.len().saturating_sub(1)),
      Tab::new(DetailTab::Context, "Context").count(contexts.len()),
      Tab::new(DetailTab::Reactive, "Reactive").count(reactive.len()),
      Tab::new(DetailTab::Attributes, "Attributes").count(attributes.len()),
    ],
  )
  .variant(TabsVariant::Line)
  .label("Lists of the selected node")
  .id("devtools-detail")
  .style(TabsPart::List, |s| s.height(TABS_HEIGHT).padding_x(PADDING).gap(24.0))
  .style(TabsPart::Tab, |s| s.height(TABS_HEIGHT).gap(6.0))
  .style(TabsPart::Label, |s| s.font_size(13.0))
  .style(TabsPart::Count, |s| s.font_size(12.0));
  let table = match state.detail.get() {
    DetailTab::Props => props_table(props),
    DetailTab::Context => contexts_table(contexts),
    DetailTab::Reactive => reactive_table(reactive),
    DetailTab::Attributes => attributes_table(attributes),
  };
  Column::new()
    .height(FILL)
    .flex(1.0)
    .child(tabs)
    .child(Column::new().width(FILL).flex(1.0).child(table))
}

/// A table of the inspector as the board draws them, called by what it lists and saying what it has none of.
fn flat<R: Clone + Send + Sync + 'static>(table: Table<R>, label: &str, empty: &str) -> Element {
  kit::flat_table(table.label(label).empty_text(empty)).into()
}

fn value_text(value: Option<&str>) -> Element {
  match value {
    Some(value) => mono(value, 13.0, FontWeight::Medium).into(),
    None => mono("—", 13.0, FontWeight::Normal)
      .color(PaletteColor::MutedForeground)
      .into(),
  }
}

fn muted(text: &str) -> Element {
  mono(text, 13.0, FontWeight::Normal)
    .color(PaletteColor::MutedForeground)
    .into()
}

/// An icon and a short word after it, in the first column of a list: `#12`, `uses`.
fn marked(icon: &str, text: &str) -> Element {
  Row::new()
    .spacing(10.0)
    .align_items(Alignment::Center)
    .child(Icon::new(icon).size(14.0).color(PaletteColor::MutedForeground))
    .child(mono(text, 13.0, FontWeight::Normal).color(PaletteColor::MutedForeground))
    .into()
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct PropRow {
  /// How deep the field is in the props: 0 for the struct itself.
  depth: usize,
  name: String,
  value: Option<String>,
  type_name: String,
}

/// The props of the node: the struct, and its fields under it, each with the fields it holds.
pub(crate) fn prop_rows(node: &DevToolsNode) -> Vec<PropRow> {
  let Some(props) = &node.props else {
    return Vec::new();
  };
  let mut rows = vec![PropRow {
    depth: 0,
    name: short_type_name(&props.type_name),
    value: None,
    type_name: if props.fields.is_empty() { "()" } else { "struct" }.to_owned(),
  }];
  fn field(info: &ComponentInfo, depth: usize, rows: &mut Vec<PropRow>) {
    rows.push(PropRow {
      depth,
      name: info.name().to_owned(),
      value: info.formatted_value().map(str::to_owned),
      type_name: short_type_name(info.type_name()),
    });
    for child in info.children() {
      field(child, depth + 1, rows);
    }
  }
  for info in &props.fields {
    field(info, 1, &mut rows);
  }
  rows
}

fn props_table(rows: Vec<PropRow>) -> Element {
  let columns = vec![
    TableColumn::new("name", "Name")
      .width(NAME_WIDTH)
      .cell(|row: &PropRow| {
        let weight = if row.depth == 0 {
          FontWeight::Medium
        } else {
          FontWeight::Normal
        };
        Row::new()
          .padding_left(row.depth as f32 * FIELD_INDENT)
          .child(mono(&row.name, 13.0, weight))
          .into()
      }),
    TableColumn::new("value", "Value")
      .width(VALUE_WIDTH)
      .cell(|row: &PropRow| value_text(row.value.as_deref())),
    TableColumn::new("type", "Type").cell(|row: &PropRow| muted(&row.type_name)),
  ];
  flat(Table::new(columns, rows), "Props", "This node takes no props")
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ContextRow {
  relation: &'static str,
  type_name: String,
}

pub(crate) fn context_rows(node: &DevToolsNode) -> Vec<ContextRow> {
  node
    .contexts
    .iter()
    .map(|context| ContextRow {
      relation: match context.kind {
        ComponentContextKind::Provided => "provides",
        ComponentContextKind::Consumed => "uses",
      },
      type_name: short_type_name(&context.type_name),
    })
    .collect()
}

fn contexts_table(rows: Vec<ContextRow>) -> Element {
  let columns = vec![
    TableColumn::new("relation", "Relation")
      .width(ID_WIDTH)
      .cell(|row: &ContextRow| marked(icons::SHARE_NODES, row.relation)),
    TableColumn::new("type", "Type").cell(|row: &ContextRow| value_text(Some(&row.type_name))),
  ];
  flat(
    Table::new(columns, rows),
    "Context",
    "This node provides and uses no context",
  )
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ReactiveRow {
  icon: &'static str,
  id: usize,
  value: Option<String>,
  type_name: String,
}

/// The node's signals, then its memos, then its effects.
pub(crate) fn reactive_rows(node: &DevToolsNode) -> Vec<ReactiveRow> {
  let signals = node.signals.iter().map(|signal| ReactiveRow {
    icon: icons::BOLT,
    id: signal.id,
    value: signal.formatted_value(),
    type_name: format!("Signal<{}>", short_type_name(&signal.type_name)),
  });
  let memos = node.memos.iter().map(|memo| ReactiveRow {
    icon: icons::PULSE,
    id: memo.id,
    value: memo.formatted_value(),
    type_name: format!("Memo<{}>", short_type_name(&memo.type_name)),
  });
  let effects = node.effects.iter().map(|effect| ReactiveRow {
    icon: icons::PLAY_CIRCLE,
    id: effect.id,
    value: None,
    type_name: "Effect".to_owned(),
  });
  signals.chain(memos).chain(effects).collect()
}

fn reactive_table(rows: Vec<ReactiveRow>) -> Element {
  let columns = vec![
    TableColumn::new("id", "ID")
      .width(ID_WIDTH)
      .cell(|row: &ReactiveRow| marked(row.icon, &format!("#{}", row.id))),
    TableColumn::new("value", "Value")
      .width(VALUE_WIDTH)
      .cell(|row: &ReactiveRow| value_text(row.value.as_deref())),
    TableColumn::new("type", "Type").cell(|row: &ReactiveRow| muted(&row.type_name)),
  ];
  flat(
    Table::new(columns, rows),
    "Reactive values",
    "This node has no signals, memos or effects",
  )
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AttributeRow {
  name: String,
  value: String,
}

pub(crate) fn attribute_rows(node: &DevToolsNode) -> Vec<AttributeRow> {
  node
    .attrs
    .iter()
    .map(|(name, value)| AttributeRow {
      name: name.clone(),
      value: format!("{value:?}"),
    })
    .collect()
}

fn attributes_table(rows: Vec<AttributeRow>) -> Element {
  let columns = vec![
    TableColumn::new("name", "Name")
      .width(NAME_WIDTH)
      .cell(|row: &AttributeRow| mono(&row.name, 13.0, FontWeight::Normal).into()),
    TableColumn::new("value", "Value").cell(|row: &AttributeRow| value_text(Some(&row.value))),
  ];
  flat(Table::new(columns, rows), "Attributes", "This node has no attributes")
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::core::IdGenerator;

  #[test]
  fn a_node_is_rendered_by_the_nearest_component_it_is_in() {
    let ids = IdGenerator::new();
    let leaf = DevToolsNode::for_test(ids.next(), "Text", DevToolsNodeKind::Element, vec![]);
    let column = DevToolsNode::for_test(ids.next(), "Column", DevToolsNodeKind::Element, vec![leaf]);
    let card = DevToolsNode::for_test(ids.next(), "demo::Card", DevToolsNodeKind::Component, vec![column]);
    let row = DevToolsNode::for_test(ids.next(), "Row", DevToolsNodeKind::Element, vec![card]);
    let snapshot = DevToolsSnapshot {
      root: Some(DevToolsNode::for_test(
        ids.next(),
        "demo::App",
        DevToolsNodeKind::Component,
        vec![row],
      )),
      frame: Default::default(),
    };
    let by = |path: &[usize]| rendered_by(&snapshot, path).map(|node| short_tag(&node.tag));
    assert_eq!(by(&[]), None, "nothing of the tree rendered its root");
    assert_eq!(by(&[0]), Some("App"));
    assert_eq!(
      by(&[0, 0]),
      Some("App"),
      "a component is rendered by the one it is in, not by itself"
    );
    assert_eq!(by(&[0, 0, 0]), Some("Card"));
    assert_eq!(by(&[0, 0, 0, 0]), Some("Card"));
    assert_eq!(by(&[4]), None, "a path that leads nowhere has no node");
  }

  #[test]
  fn a_render_time_reads_in_the_unit_it_is_of() {
    use std::time::Duration;
    assert_eq!(render_time(Duration::from_micros(2_100)), "2.1ms");
    assert_eq!(render_time(Duration::from_micros(340)), "340µs");
    assert_eq!(render_time(Duration::ZERO), "0µs");
  }

  #[test]
  fn attributes_are_shown_as_they_would_be_written() {
    let mut node = DevToolsNode::for_test(IdGenerator::new().next(), "Row", DevToolsNodeKind::Element, vec![]);
    node.attrs = vec![("id".to_owned(), "app-root".to_owned())];
    assert_eq!(
      attribute_rows(&node),
      [AttributeRow {
        name: "id".to_owned(),
        value: "\"app-root\"".to_owned(),
      }]
    );
  }

  #[test]
  fn a_node_without_props_has_no_row_for_them() {
    let node = DevToolsNode::for_test(IdGenerator::new().next(), "Row", DevToolsNodeKind::Element, vec![]);
    assert!(prop_rows(&node).is_empty());
    assert!(reactive_rows(&node).is_empty());
    assert!(context_rows(&node).is_empty());
  }
}