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
//! A toggle: a button that stays on.

use std::sync::Arc;

use super::{
  button::ButtonSize,
  names,
  part_style::Length,
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
  shared::{fading, glyph, one_line, pressable},
  theme::{Themed, current_theme},
};
use crate::{
  app::theme::{ControlSize, Density, IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
  core::Signal,
  node::Element,
};

/// The parts of a toggle a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TogglePart {
  /// The toggle itself: what is pressed.
  Root,
  /// Its text.
  Label,
  /// Its icon: its side, and a color of its own.
  Icon,
}

names!(TogglePart {
  Root => "root",
  Label => "label",
  Icon => "icon",
});

/// The states of a toggle a recipe styles, besides what the pointer and the
/// keyboard do to it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ToggleState {
  /// It is on.
  On,
  /// It takes no input.
  Disabled,
  /// It is an icon alone, as wide as it is high.
  IconOnly,
}

names!(ToggleState {
  On => "on",
  Disabled => "disabled",
  IconOnly => "icon_only",
});

/// The sizes of a toggle: a button's.
pub type ToggleSize = ButtonSize;

/// What builds a toggle's nodes: a theme's in place of the default one.
pub type ToggleRenderer = Arc<dyn Fn(ToggleView) -> Element + Send + Sync>;

/// A toggle: a button that stays on until it is pressed again. Bold in a
/// text editor, a filter that holds, a panel that is shown.
///
/// ```
/// use lurq::ui::{Toggle, icons};
///
/// let bold = Toggle::new("Bold").icon(icons::BOLD).on(true).on_change(|on| println!("bold: {on}"));
/// // An icon alone: its label is still what it is called.
/// let italic = Toggle::new("Italic").icon(icons::ITALIC).icon_only(true);
/// ```
///
/// A toggle does not keep whether it is on: the app says so with
/// [`Toggle::on`], and is told of a press with [`Toggle::on_change`]. Its
/// sizes are a button's ([`ButtonSize`]).
pub struct Toggle {
  label: Arc<str>,
  size: Name,
  icon: Option<Name>,
  leading: Option<Element>,
  icon_only: bool,
  on: bool,
  disabled: bool,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
  focuses: Option<crate::core::ElementRef>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl Toggle {
  pub fn new(label: &str) -> Self {
    Self {
      label: Arc::from(label),
      size: ButtonSize::default().into(),
      icon: None,
      leading: None,
      icon_only: false,
      on: false,
      disabled: false,
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      on_change: Vec::new(),
      focuses: None,
      id: None,
      key: None,
    }
  }

  /// What a press of the toggle gives the focus to, in place of taking it itself: the text a toggle of an
  /// editor's bar works on, which goes on being typed in. The toggle is still a stop of Tab.
  pub fn focuses(mut self, target: &crate::core::ElementRef) -> Self {
    self.focuses = Some(target.clone());
    self
  }

  /// A [`ToggleSize`], or a size the theme's recipe names.
  pub fn size(mut self, size: impl Into<Name>) -> Self {
    self.size = size.into();
    self
  }

  /// An icon before the label, by its name in the theme's set.
  pub fn icon(mut self, name: impl Into<Name>) -> Self {
    self.icon = Some(name.into());
    self
  }

  /// An element before the label, where an icon by name is not enough: the dot of a recording. It stands where
  /// the icon would, before one that is given too.
  pub fn leading(mut self, element: impl Into<Element>) -> Self {
    self.leading = Some(element.into());
    self
  }

  /// Whether the toggle is its icon alone: a square as wide as it is high.
  pub fn icon_only(mut self, icon_only: bool) -> Self {
    self.icon_only = icon_only;
    self
  }

  /// Whether the toggle is on.
  pub fn on(mut self, on: bool) -> Self {
    self.on = on;
    self
  }

  /// On while `on` is true, which a press turns. The handlers of
  /// [`on_change`](Self::on_change) are called after the signal is set.
  pub fn bind(self, on: Signal<bool>) -> Self {
    let set = on.clone();
    self.on(on.get()).on_change(move |next| set.set(next))
  }

  /// Whether the toggle takes no input.
  pub fn disabled(mut self, disabled: bool) -> Self {
    self.disabled = disabled;
    self
  }

  /// Puts the toggle in a state the theme's recipe names, or takes it out.
  pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
    let state = state.into();
    self.states.retain(|held| *held != state);
    if on {
      self.states.push(state);
    }
    self
  }

  /// Called on a press, with what the toggle is to be: on if it was off.
  pub fn on_change(mut self, handler: impl Fn(bool) + Send + Sync + 'static) -> Self {
    self.on_change.push(Arc::new(handler));
    self
  }

  /// Styles one part of this toggle over what the theme gives it.
  pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
    self.rules = self.rules.base(part, style);
    self
  }

  /// Lays rules over the theme's recipe for this toggle: what the component
  /// it is in says of the toggle in this place.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// Draws the toggle as it is under the pointer, held down or focused from
  /// the keyboard, with nothing pointing at it: for a gallery or a test.
  pub fn force(mut self, interaction: Option<Interaction>) -> Self {
    self.forced = interaction;
    self
  }

  pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
    self.id = Some(id.into());
    self
  }

  pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
    self.key = Some(key.into());
    self
  }

  /// What an item of a menu that stands for this toggle takes from it, checked
  /// while the toggle is on: a toolbar moves a toggle that does not fit into
  /// its menu with this. Choosing it turns the toggle, as a press would.
  pub(crate) fn command(&self) -> super::toolbar::ToolbarCommand {
    let (on, handlers) = (self.on, self.on_change.clone());
    super::toolbar::ToolbarCommand {
      label: self.label.clone(),
      icon: self.icon,
      icon_only: self.icon_only,
      disabled: self.disabled,
      checked: Some(on),
      run: Arc::new(move || {
        for handler in &handlers {
          handler(!on);
        }
      }),
      id: self.id.clone(),
      key: None,
    }
  }

  /// Builds the toggle in `theme`. Converting a toggle into an element does
  /// this in the theme of what is being rendered.
  pub fn build(mut self, theme: &Theme) -> Element {
    // An icon with an empty label is an icon alone: no gap and no padding for a label.
    self.icon_only = self.icon_only || self.label.is_empty() && self.icon.is_some();
    let case = Case {
      variant: None,
      size: Some(self.size),
      density: None,
      breakpoint: super::current_breakpoint(),
      states: self.states.clone(),
    }
    .state(ToggleState::On, self.on)
    .state(ToggleState::Disabled, self.disabled)
    .state(ToggleState::IconOnly, self.icon_only);
    let view = ToggleView {
      label: self.label,
      size: self.size,
      icon: self.icon,
      leading: self.leading,
      icon_only: self.icon_only,
      on: self.on,
      disabled: self.disabled,
      recipe: theme
        .resolve::<Toggle>(&case, Some(&self.rules))
        .owned_by(self.id.as_ref()),
      forced: self.forced,
      on_change: self.on_change,
      focuses: self.focuses,
      id: self.id,
      key: self.key,
    };
    match theme.renderer::<Toggle>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }
}

impl From<Toggle> for Element {
  fn from(toggle: Toggle) -> Self {
    toggle.build(&current_theme())
  }
}

/// A toggle as a renderer sees it: what it was given, the style of its
/// parts, and its behavior ready to be dressed.
pub struct ToggleView {
  pub label: Arc<str>,
  pub size: Name,
  /// The name of its icon, if it has one.
  pub icon: Option<Name>,
  /// The element it was given to stand before its label.
  pub leading: Option<Element>,
  pub icon_only: bool,
  pub on: bool,
  pub disabled: bool,
  /// The style of each part of this toggle.
  pub recipe: ResolvedRecipe,
  forced: Option<Interaction>,
  on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
  focuses: Option<crate::core::ElementRef>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl ToggleView {
  /// The style of a part, as this toggle is to be drawn.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part).forced(self.forced)
  }

  pub fn theme(&self) -> &Theme {
    self.recipe.theme()
  }

  /// The toggle's behavior with the style of its root: the element that is
  /// pressed, focused and disabled, with nothing in it yet.
  pub fn root(&self) -> crate::components::Button {
    let mut root = pressable(&self.part(TogglePart::Root), self.disabled);
    // As a button: in a room narrower than it, it gives way down to its padding and its icon, and its label is cut.
    if !self.icon_only {
      root = root
        .flex_shrink(1.0)
        .shrink_limit(crate::layout::layout_kind::ShrinkLimit::Content)
        .max_width(crate::node::dimension::Dimension::Pct(100.0));
    }
    let (handlers, next) = (self.on_change.clone(), !self.on);
    if !handlers.is_empty() {
      root = root.on_click(move |_| {
        for handler in &handlers {
          handler(next);
        }
      });
    }
    if self.icon_only && !self.label.is_empty() {
      root = root.describe("a11y_name", self.label.clone());
    }
    if let Some(target) = &self.focuses {
      root = root.focuses(target);
    }
    if let Some(id) = &self.id {
      root = root.id(id.clone());
    }
    if let Some(key) = &self.key {
      root = root.key(key.clone());
    }
    root
  }

  /// How the default renderer builds a toggle: the root, with its icon and its label.
  pub fn render(mut self) -> Element {
    let mut root = self.root();
    if let Some(leading) = self.leading.take() {
      root = root.child(leading);
    }
    if let Some(name) = self.icon {
      root = root.child(glyph(&self.part(TogglePart::Icon), name, false));
    }
    if !self.icon_only {
      let part = self.part(TogglePart::Label);
      root = root.child(one_line(&part, &self.label));
    }
    root.into()
  }
}

impl crate::ui::Styled for Toggle {
  type Component = Self;

  fn rules(self, rules: &crate::ui::Recipe) -> Self {
    Self::rules(self, rules)
  }

  fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
    Self::state(self, state, on)
  }
}

impl Themed for Toggle {
  const NAME: &'static str = "toggle";
  type Renderer = ToggleRenderer;

  fn parts() -> crate::ui::Declared {
    TogglePart::declared()
  }

  fn states() -> crate::ui::Declared {
    ToggleState::declared()
  }

  /// The design's toggle (`pencil/lurq-design-system.pen`, Toggle): a
  /// button's height, padded 10, with no fill until the pointer is on it or
  /// it is on, and then the fill of what is picked out.
  fn recipe() -> Recipe {
    use PaletteColor::*;
    use TogglePart::{Icon, Label, Root};
    let picked_out = |s: super::PartStyle| s.background(Highlight).foreground(HighlightForeground);
    let recipe = Recipe::new()
      .base(Root, |s| {
        fading(
          s.height(ControlSize::Md)
            .padding_x(10.0)
            .gap(8.0)
            .radius(RadiusSize::Md)
            .foreground(Foreground),
        )
      })
      .base(Label, |s| s.typography(TypographyStyle::Button))
      .base(Icon, |s| s.square(IconSize::Md))
      .hovered(Root, picked_out)
      .pressed(Root, |s| s.veil(StatePressed))
      .focus_visible(Root, |s| s.focus_ring())
      .size(ButtonSize::Xs, Root, |s| s.height(Density::Compact).padding_x(8.0))
      .size(ButtonSize::Xs, Label, |s| s.font_size(12.0))
      .size(ButtonSize::Sm, Root, |s| s.height(ControlSize::Sm).padding_x(8.0))
      .size(ButtonSize::Lg, Root, |s| s.height(ControlSize::Lg).padding_x(12.0))
      .size(ButtonSize::Xl, Root, |s| s.height(Density::Comfortable).padding_x(14.0))
      .size(ButtonSize::Xl, Label, |s| s.font_size(15.0))
      .state(ToggleState::On, Root, picked_out)
      // A toggle that is on has its fill already: under the pointer it is a step darker.
      .when(Selector::new().state(ToggleState::On).hovered(), Root, |s| {
        s.background(SecondaryHover)
      })
      .state(ToggleState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
      .state(ToggleState::IconOnly, Root, |s| s.padding_x(0.0).width(ControlSize::Md));
    let sized = |recipe: Recipe, size: ButtonSize, icon: Length, side: Length| {
      recipe.size(size, Icon, |s| s.square(icon)).when(
        Selector::new().size(size).state(ToggleState::IconOnly),
        Root,
        |s| s.width(side),
      )
    };
    let recipe = sized(recipe, ButtonSize::Xs, IconSize::Sm.into(), Density::Compact.into());
    let recipe = sized(recipe, ButtonSize::Sm, IconSize::Md.into(), ControlSize::Sm.into());
    let recipe = sized(recipe, ButtonSize::Lg, Length::Px(18.0), ControlSize::Lg.into());
    sized(recipe, ButtonSize::Xl, IconSize::Lg.into(), Density::Comfortable.into())
  }
}