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
use std::sync::atomic::{AtomicU32, Ordering};

use crate::{
  layout::layout_kind::{FlexParams, FrameConstraints},
  node::{
    background_color::BackgroundColor,
    border::{Border, BorderRadius, Borders, ThemedBorderRadius},
    border_size_value::BorderSizeValue,
    box_shadow::BoxShadowValue,
    cursor::CursorIcon,
    dimension::Dimension,
    interaction_state::InteractionState,
    outline::Outline,
    padding::Padding,
    radius_value::RadiusValue,
    spacing_value::SpacingValue,
    text_color::TextColor,
  },
};

#[derive(Clone, Default)]
pub struct Style {
  pub(crate) color: Option<BackgroundColor>,
  pub(crate) border_radius: Option<ThemedBorderRadius>,
  pub(crate) border: Option<Borders>,
  pub(crate) box_shadow: Option<Box<BoxShadowValue>>,
  pub(crate) outline: Option<Box<Outline>>,
  pub(crate) foreground: Option<TextColor>,
  pub(crate) opacity: Option<f32>,
  pub(crate) cursor: Option<CursorIcon>,
  pub(crate) frame: Option<FrameConstraints>,
  pub(crate) padding: Option<Padding>,
  pub(crate) flex: Option<FlexParams>,
}

#[derive(Clone, Default)]
pub(crate) struct StateStyles {
  pub(crate) hovered: Option<Style>,
  pub(crate) active: Option<Style>,
  pub(crate) focused: Option<Style>,
  pub(crate) focus_visible: Option<Style>,
  /// While files from the OS are dragged over the node.
  pub(crate) drag_over: Option<Style>,
  /// While the node is disabled. Paint only: it is no source of layout.
  pub(crate) disabled: Option<Style>,
  /// While the interaction group the node is in is under the pointer or holds the focus
  /// (`Node::interaction_group`). Paint only: it is no source of layout.
  pub(crate) within: Option<Style>,
  /// While the interaction group the node is in is under the pointer. Paint only, laid over `within`.
  pub(crate) within_hovered: Option<Style>,
  /// While the interaction group the node is in is held down. Paint only, laid over `within_hovered`.
  pub(crate) within_pressed: Option<Style>,
  layout_sources: LayoutStyleSources,
}

// Style merging replaces each layout field as a whole. Cache its winning
// state rather than copying paint properties or allocating their shadows.
// The low byte holds interaction bits; the other bytes select layout fields.
// The empty StateStyles is shared by LazyBox, so this cache must be Sync.
#[derive(Default)]
struct LayoutStyleSources(AtomicU32);

impl Clone for LayoutStyleSources {
  fn clone(&self) -> Self {
    Self(AtomicU32::new(self.0.load(Ordering::Relaxed)))
  }
}

#[cfg(test)]
thread_local! {
  pub(crate) static STYLE_OPERATIONS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) };
}

impl Style {
  pub fn new() -> Self {
    #[cfg(test)]
    STYLE_OPERATIONS.with(|counts| counts.set((counts.get().0 + 1, counts.get().1)));
    Self::default()
  }

  pub fn background(mut self, color: impl Into<BackgroundColor>) -> Self {
    self.color = Some(color.into());
    self
  }

  pub fn width(mut self, width: impl Into<Dimension>) -> Self {
    self.frame_mut().width = Some(width.into());
    self
  }

  pub fn height(mut self, height: impl Into<Dimension>) -> Self {
    self.frame_mut().height = Some(height.into());
    self
  }

  pub fn size(mut self, width: impl Into<Dimension>, height: impl Into<Dimension>) -> Self {
    let frame = self.frame_mut();
    frame.width = Some(width.into());
    frame.height = Some(height.into());
    self
  }

  pub fn min_width(mut self, width: impl Into<Dimension>) -> Self {
    self.frame_mut().min_width = Some(width.into());
    self
  }

  pub fn max_width(mut self, width: impl Into<Dimension>) -> Self {
    self.frame_mut().max_width = Some(width.into());
    self
  }

  pub fn min_height(mut self, height: impl Into<Dimension>) -> Self {
    self.frame_mut().min_height = Some(height.into());
    self
  }

  pub fn max_height(mut self, height: impl Into<Dimension>) -> Self {
    self.frame_mut().max_height = Some(height.into());
    self
  }

  pub fn min_size(mut self, width: impl Into<Dimension>, height: impl Into<Dimension>) -> Self {
    let frame = self.frame_mut();
    frame.min_width = Some(width.into());
    frame.min_height = Some(height.into());
    self
  }

  pub fn max_size(mut self, width: impl Into<Dimension>, height: impl Into<Dimension>) -> Self {
    let frame = self.frame_mut();
    frame.max_width = Some(width.into());
    frame.max_height = Some(height.into());
    self
  }

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

  pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
    let padding = padding.into();
    if let Some(existing) = &mut self.padding {
      existing.merge_from(&padding);
    } else {
      self.padding = Some(padding);
    }
    self
  }

  pub fn padding_custom(mut self, padding: Padding) -> Self {
    self.padding = Some(padding);
    self
  }

  pub fn padding_horizontal(self, value: impl Into<SpacingValue>) -> Self {
    self.padding(Padding::horizontal(value.into()))
  }

  pub fn padding_vertical(self, value: impl Into<SpacingValue>) -> Self {
    self.padding(Padding::vertical(value.into()))
  }

  pub fn padding_left(mut self, value: impl Into<SpacingValue>) -> Self {
    self.padding = Some(self.padding.unwrap_or_default().left(value.into()));
    self
  }

  pub fn padding_right(mut self, value: impl Into<SpacingValue>) -> Self {
    self.padding = Some(self.padding.unwrap_or_default().right(value.into()));
    self
  }

  pub fn padding_top(mut self, value: impl Into<SpacingValue>) -> Self {
    self.padding = Some(self.padding.unwrap_or_default().top(value.into()));
    self
  }

  pub fn padding_bottom(mut self, value: impl Into<SpacingValue>) -> Self {
    self.padding = Some(self.padding.unwrap_or_default().bottom(value.into()));
    self
  }

  pub fn flex(mut self, factor: f32) -> Self {
    self.flex = Some(FlexParams::grow(factor));
    self
  }

  pub fn flex_shrink(mut self, factor: f32) -> Self {
    self.flex = Some(FlexParams {
      grow: 0.0,
      shrink: factor,
      basis: None,
    });
    self
  }

  pub fn flex_full(mut self, grow: f32, shrink: f32, basis: Option<f32>) -> Self {
    self.flex = Some(FlexParams { grow, shrink, basis });
    self
  }

  pub fn corner_radius(mut self, radius: impl Into<RadiusValue>) -> Self {
    self.border_radius = Some(ThemedBorderRadius::all(radius));
    self
  }

  pub fn corner_radius_custom(mut self, radius: BorderRadius) -> Self {
    self.border_radius = Some(radius.into());
    self
  }

  pub fn corner_radius_top_left(mut self, radius: impl Into<RadiusValue>) -> Self {
    self
      .border_radius
      .get_or_insert_with(ThemedBorderRadius::default)
      .top_left = radius.into();
    self
  }

  pub fn corner_radius_top_right(mut self, radius: impl Into<RadiusValue>) -> Self {
    self
      .border_radius
      .get_or_insert_with(ThemedBorderRadius::default)
      .top_right = radius.into();
    self
  }

  pub fn corner_radius_bottom_right(mut self, radius: impl Into<RadiusValue>) -> Self {
    self
      .border_radius
      .get_or_insert_with(ThemedBorderRadius::default)
      .bottom_right = radius.into();
    self
  }

  pub fn corner_radius_bottom_left(mut self, radius: impl Into<RadiusValue>) -> Self {
    self
      .border_radius
      .get_or_insert_with(ThemedBorderRadius::default)
      .bottom_left = radius.into();
    self
  }

  pub fn rounded(mut self, radius: impl Into<RadiusValue>) -> Self {
    self.border_radius = Some(ThemedBorderRadius::all(radius));
    self
  }

  pub fn border_inside(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
    self.border = Some(Borders::all(Border::inside(width, color)));
    self
  }

  pub fn border_outside(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
    self.border = Some(Borders::all(Border::outside(width, color)));
    self
  }

  pub fn border_center(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
    self.border = Some(Borders::all(Border::center(width, color)));
    self
  }

  pub fn border(mut self, border: Border) -> Self {
    self.border = Some(Borders::all(border));
    self
  }

  pub fn border_custom(mut self, border: Borders) -> Self {
    self.border = Some(border);
    self
  }

  pub fn border_top(mut self, border: Border) -> Self {
    self.border.get_or_insert_with(Borders::default).top = Some(border);
    self
  }

  pub fn border_right(mut self, border: Border) -> Self {
    self.border.get_or_insert_with(Borders::default).right = Some(border);
    self
  }

  pub fn border_bottom(mut self, border: Border) -> Self {
    self.border.get_or_insert_with(Borders::default).bottom = Some(border);
    self
  }

  pub fn border_left(mut self, border: Border) -> Self {
    self.border.get_or_insert_with(Borders::default).left = Some(border);
    self
  }

  /// The box shadow while this style applies, for example a higher
  /// [`ShadowStyle`](crate::app::theme::ShadowStyle) on hover.
  /// [`BoxShadowValue::none`] removes the element's own shadow.
  pub fn box_shadow(mut self, shadow: impl Into<BoxShadowValue>) -> Self {
    self.box_shadow = Some(Box::new(shadow.into()));
    self
  }

  /// The outline while this style applies: a focus ring in a focus-visible
  /// style. [`Outline::none`] removes the element's own outline.
  pub fn outline(mut self, outline: Outline) -> Self {
    self.outline = Some(Box::new(outline));
    self
  }

  /// The color of the text in the element and under it while this style
  /// applies; see `Node::foreground`.
  pub fn foreground(mut self, color: impl Into<TextColor>) -> Self {
    self.foreground = Some(color.into());
    self
  }

  /// The opacity of the element and its subtree while this style applies.
  pub fn opacity(mut self, value: f32) -> Self {
    self.opacity = Some(value);
    self
  }

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

  pub(crate) fn merge_from(&mut self, other: &Style) {
    #[cfg(test)]
    STYLE_OPERATIONS.with(|counts| counts.set((counts.get().0, counts.get().1 + 1)));
    if other.color.is_some() {
      self.color = other.color.clone();
    }
    if other.border_radius.is_some() {
      self.border_radius = other.border_radius;
    }
    if other.border.is_some() {
      self.border = other.border.clone();
    }
    if other.box_shadow.is_some() {
      self.box_shadow = other.box_shadow.clone();
    }
    if other.outline.is_some() {
      self.outline = other.outline.clone();
    }
    if other.foreground.is_some() {
      self.foreground = other.foreground.clone();
    }
    if other.opacity.is_some() {
      self.opacity = other.opacity;
    }
    if other.cursor.is_some() {
      self.cursor = other.cursor;
    }
    if other.frame.is_some() {
      self.frame = other.frame;
    }
    if other.padding.is_some() {
      self.padding = other.padding;
    }
    if other.flex.is_some() {
      self.flex = other.flex;
    }
  }

  pub(crate) fn affects_layout(&self) -> bool {
    self.frame.is_some() || self.padding.is_some() || self.flex.is_some()
  }

  fn frame_mut(&mut self) -> &mut FrameConstraints {
    self.frame.get_or_insert_with(FrameConstraints::default)
  }
}

impl StateStyles {
  pub(crate) fn invalidate_layout_style(&mut self) {
    self.layout_sources.0.store(0, Ordering::Relaxed);
  }

  fn style_at(&self, index: u8) -> Option<&Style> {
    match index {
      1 => self.focused.as_ref(),
      2 => self.focus_visible.as_ref(),
      3 => self.hovered.as_ref(),
      4 => self.active.as_ref(),
      5 => self.drag_over.as_ref(),
      _ => None,
    }
  }

  fn layout_sources(&self, state: &InteractionState) -> [u8; 3] {
    if !self.affects_layout() {
      return [0; 3];
    }
    let states = state.layout_states();
    let state_bits = states
      .iter()
      .enumerate()
      .fold(0, |bits, (index, active)| bits | ((*active as u32) << index));
    let cached = self.layout_sources.0.load(Ordering::Relaxed);
    if cached & 0xff == state_bits {
      return [(cached >> 8) as u8, (cached >> 16) as u8, (cached >> 24) as u8];
    }
    let mut fields = [0; 3];
    for (index, active) in states.into_iter().enumerate() {
      if active && let Some(style) = self.style_at(index as u8 + 1) {
        for (field, present) in [style.frame.is_some(), style.padding.is_some(), style.flex.is_some()]
          .into_iter()
          .enumerate()
        {
          if present {
            fields[field] = index as u8 + 1;
          }
        }
      }
    }
    let sources = state_bits | ((fields[0] as u32) << 8) | ((fields[1] as u32) << 16) | ((fields[2] as u32) << 24);
    self.layout_sources.0.store(sources, Ordering::Relaxed);
    fields
  }

  pub(crate) fn layout_frame(&self, state: &InteractionState) -> Option<FrameConstraints> {
    self
      .style_at(self.layout_sources(state)[0])
      .and_then(|style| style.frame)
  }

  pub(crate) fn layout_padding(&self, state: &InteractionState) -> Option<Padding> {
    self
      .style_at(self.layout_sources(state)[1])
      .and_then(|style| style.padding)
  }

  pub(crate) fn layout_flex(&self, state: &InteractionState) -> Option<FlexParams> {
    self
      .style_at(self.layout_sources(state)[2])
      .and_then(|style| style.flex)
  }

  pub(crate) fn affects_layout(&self) -> bool {
    self.hovered.as_ref().is_some_and(Style::affects_layout)
      || self.active.as_ref().is_some_and(Style::affects_layout)
      || self.focused.as_ref().is_some_and(Style::affects_layout)
      || self.focus_visible.as_ref().is_some_and(Style::affects_layout)
      || self.drag_over.as_ref().is_some_and(Style::affects_layout)
  }
}