crabshell-ui 0.5.0

Widgets, styles, layout, text and text editing, focus, hit testing and invalidation for Crabshell
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
//! [`Slider`], a handle dragged along a track, made of a [`Button`] and
//! boxes that share the track's free space by `grow`, so the handle sits
//! at its fraction of the way with no size worked out by hand.

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

use crabshell_reactive::{Memo, Signal, untrack};

use crate::cx::Cx;
use crate::input::{Drag, DragPhase, PIXELS_PER_STEP, Wheel};
use crate::keyboard::{Key, KeyEvent};
use crate::prop::{IntoProp, Prop};
use crate::scroll::Built;
use crate::style::Style;
use crate::widgets::{Align, Button, Column, Common, Element, Row, Stack, Widget};

/// A shared style property, or the empty style when unset.
type StyleFn = Rc<dyn Fn() -> Style>;

/// A value picked by dragging a handle along a track, as Qt's `Slider`.
/// The slider writes the value back to its signal, and setting the signal
/// moves the handle.
///
/// A press anywhere on the slider moves the value there and drags it from
/// there. The wheel moves it one step a notch, or a twentieth of the range
/// when it has no step, and a touchpad in proportion; so do the arrow keys
/// while it has the keyboard focus, which Tab gives it. The value written
/// is always within the range and on a step, and only a change is
/// written. A value set from outside the range shows at that end, and NaN
/// at the start.
///
/// It draws nothing until styled: `style` sets the track's box, 120 by 4
/// logical pixels unless it sets a size, `fill_style` the part from the
/// start to the handle, and `handle_style` the handle, 16 by 16 unless it
/// sets a size. The slider is as large as the track, or the handle where
/// that is larger.
///
/// ```
/// use crabshell_ui::{Color, Cx, Slider, Style};
///
/// const TRACK: Style = Style::new().width(200.0).height(4.0).radius(2.0)
///     .background(Color::rgb(0x45, 0x47, 0x5a));
/// const FILL: Style = Style::new().radius(2.0).background(Color::rgb(0x89, 0xb4, 0xfa));
/// const HANDLE: Style = Style::new().radius(8.0).background(Color::rgb(0xcd, 0xd6, 0xf4));
///
/// fn volume(cx: &Cx) -> Slider {
///     let volume = cx.signal(50.0);
///     Slider::new(volume).range(0.0, 100.0).step(5.0)
///         .style(TRACK).fill_style(FILL).handle_style(HANDLE)
/// }
/// ```
pub struct Slider {
  props: SliderProps,
  common: Common,
}

struct SliderProps {
  value: Signal<f32>,
  min: Option<Prop<f32>>,
  max: Option<Prop<f32>>,
  step: Option<Prop<f32>>,
  vertical: Option<Prop<bool>>,
  enabled: Option<Prop<bool>>,
  style: Option<Prop<Style>>,
  fill_style: Option<Prop<Style>>,
  handle_style: Option<Prop<Style>>,
  dragging: Option<Signal<bool>>,
}

/// The track's size when its style sets none, along and across.
const TRACK: (f32, f32) = (120.0, 4.0);

/// The handle's size when its style sets none.
const HANDLE: f32 = 16.0;

/// The parts of the range the wheel and the keys move a slider without a
/// step.
const PARTS: f32 = 20.0;

impl Slider {
  /// A slider showing `value` and writing it back as it is dragged.
  pub fn new(value: Signal<f32>) -> Slider {
    Slider {
      props: SliderProps {
        value,
        min: None,
        max: None,
        step: None,
        vertical: None,
        enabled: None,
        style: None,
        fill_style: None,
        handle_style: None,
        dragging: None,
      },
      common: Common::default(),
    }
  }

  /// Sets the values at the start and the end, 0 and 1 by default. A range
  /// whose end isn't above its start holds only the start.
  pub fn range(mut self, min: impl IntoProp<f32>, max: impl IntoProp<f32>) -> Slider {
    self.props.min = Some(min.into_prop());
    self.props.max = Some(max.into_prop());
    self
  }

  /// Snaps the value to whole steps from the start of the range. 0, the
  /// default, or a step that isn't a number above 0, slides freely.
  pub fn step(mut self, step: impl IntoProp<f32>) -> Slider {
    self.props.step = Some(step.into_prop());
    self
  }

  /// Makes the slider run from the bottom up instead of left to right.
  pub fn vertical(mut self, vertical: impl IntoProp<bool>) -> Slider {
    self.props.vertical = Some(vertical.into_prop());
    self
  }

  /// Turns the slider on or off, as [`Button::enabled`]: off, nothing
  /// moves it and Tab skips it.
  pub fn enabled(mut self, enabled: impl IntoProp<bool>) -> Slider {
    self.props.enabled = Some(enabled.into_prop());
    self
  }

  /// Sets the track's style, its size included.
  pub fn style(mut self, style: impl IntoProp<Style>) -> Slider {
    self.props.style = Some(style.into_prop());
    self
  }

  /// Sets the style of the part of the track from the start to the
  /// handle's middle. Its size comes from the value.
  pub fn fill_style(mut self, style: impl IntoProp<Style>) -> Slider {
    self.props.fill_style = Some(style.into_prop());
    self
  }

  /// Sets the handle's style, its width and height included.
  pub fn handle_style(mut self, style: impl IntoProp<Style>) -> Slider {
    self.props.handle_style = Some(style.into_prop());
    self
  }

  /// Shares whether the slider is being dragged: `true` from the press
  /// until the release, as Qt's `pressed`. Written only on a change.
  pub fn dragging(mut self, dragging: Signal<bool>) -> Slider {
    self.props.dragging = Some(dragging);
    self
  }

  crate::widgets::visibility_methods!();
}

impl Widget for Slider {
  fn into_element(self: Box<Self>) -> Element {
    let Slider { props, common } = *self;
    let mut once = Some(props);
    // A keyed list of one gives the slider a context for its memos.
    let mut element = Box::new(Stack::new().children_keyed(
      || vec![()],
      |_| (),
      move |cx, _| {
        Built(
          once
            .take()
            .map(|props| Box::new(build(cx, props)).into_element()),
        )
      },
    ))
    .into_element();
    element.name = "Slider";
    element.common = common;
    element
  }
}

/// Where a slider is and how it moves, shared by its handlers.
#[derive(Clone, Copy)]
struct Range {
  value: Signal<f32>,
  min: Memo<f32>,
  max: Memo<f32>,
  step: Memo<f32>,
}

impl Range {
  /// The start and end, the end never below the start. A bound that
  /// isn't a number is 0.
  fn bounds(self) -> (f32, f32) {
    let number = |n: f32| if n.is_nan() { 0.0 } else { n };
    let min = number(self.min.get());
    (min, number(self.max.get()).max(min))
  }

  /// How far along the value is, from 0 to 1.
  fn fraction(self) -> f32 {
    let (min, max) = self.bounds();
    let value = self.value.get();
    if max > min && !value.is_nan() {
      ((value - min) / (max - min)).clamp(0.0, 1.0)
    } else {
      0.0
    }
  }

  /// One step of the wheel or the keys.
  fn unit(self) -> f32 {
    let (min, max) = self.bounds();
    self.snap().unwrap_or((max - min) / PARTS)
  }

  /// The step, unless it slides freely.
  fn snap(self) -> Option<f32> {
    untrack(|| self.step.try_get()).filter(|step| step.is_finite() && *step > 0.0)
  }

  /// The value read now, NaN as the start, outside any binding.
  fn now(self) -> f32 {
    untrack(|| {
      let (min, _) = self.bounds();
      self.value.try_get().filter(|v| !v.is_nan()).unwrap_or(min)
    })
  }

  /// Writes `value`, within the range and on a step, if that changes it.
  fn write(self, value: f32) {
    untrack(|| {
      let (min, max) = self.bounds();
      let mut value = if value.is_nan() { min } else { value };
      if let Some(step) = self.snap() {
        value = min + ((value - min) / step).round() * step;
      }
      let value = value.max(min).min(max);
      if self.value.try_get() != Some(value) {
        self.value.try_set(value);
      }
    });
  }

  /// Writes the value `t` of the way along.
  fn write_fraction(self, t: f32) {
    let (min, max) = untrack(|| self.bounds());
    self.write(min + t.clamp(0.0, 1.0) * (max - min));
  }
}

fn build(cx: &Cx, props: SliderProps) -> Button {
  let SliderProps {
    value,
    min,
    max,
    step,
    vertical,
    enabled,
    style,
    fill_style,
    handle_style,
    dragging,
  } = props;
  let range = Range {
    value,
    min: cx.memo(shared_or(min, 0.0)),
    max: cx.memo(shared_or(max, 1.0)),
    step: cx.memo(shared_or(step, 0.0)),
  };
  if let Some(dragging) = dragging {
    // Kept under the slider's context, so it drops when the slider goes.
    cx.signal(LetGo(dragging));
  }
  let vertical = cx.memo(shared_or(vertical, false));
  let style = shared_style(style);
  let fill_style = shared_style(fill_style);
  let handle = {
    let handle_style = shared_style(handle_style);
    cx.memo(move || {
      Style::new()
        .width(HANDLE)
        .height(HANDLE)
        .merge(handle_style())
        .shrink(0.0)
    })
  };
  let fraction = cx.memo(move || range.fraction());

  let on_drag = move |_: &Cx, drag: Drag| {
    if let Some(dragging) = dragging {
      let held = drag.phase != DragPhase::End;
      if untrack(|| dragging.try_get()) != Some(held) {
        dragging.try_set(held);
      }
    }
    if drag.phase == DragPhase::End {
      return;
    }
    let vertical = untrack(|| vertical.get());
    let handle = untrack(|| handle.get());
    let (at, length, size) = if vertical {
      (drag.position.y, drag.size.height, handle.height)
    } else {
      (drag.position.x, drag.size.width, handle.width)
    };
    let size = size.unwrap_or(HANDLE).max(0.0);
    // The handle's middle follows the pointer.
    let travel = length - size;
    let t = if travel > 0.0 {
      (at - size / 2.0) / travel
    } else if length > 0.0 {
      at / length
    } else {
      0.0
    };
    range.write_fraction(if vertical { 1.0 - t } else { t });
  };
  // Whole steps of a wheel or touchpad that haven't added up yet.
  let partial = Cell::new(0.0_f32);
  let on_wheel = move |_: &Cx, wheel: Wheel| {
    // Up and right move it up.
    let notches = wheel.x.notches - wheel.y.notches;
    let steps = if notches != 0.0 {
      notches
    } else {
      (wheel.x.pixels - wheel.y.pixels) / PIXELS_PER_STEP
    };
    if !steps.is_finite() {
      return;
    }
    let unit = untrack(|| range.unit());
    if range.snap().is_some() {
      let total = partial.get() + steps;
      let whole = total.trunc();
      partial.set(total - whole);
      if whole != 0.0 {
        range.write(range.now() + whole * unit);
      }
    } else if steps != 0.0 {
      range.write(range.now() + steps * unit);
    }
    if wheel.x.stop || wheel.y.stop {
      partial.set(0.0);
    }
  };
  let on_key = move |_: &Cx, event: &KeyEvent| {
    let steps = match event.key {
      Key::Left | Key::Down => -1.0,
      Key::Right | Key::Up => 1.0,
      _ => return false,
    };
    range.write(range.now() + steps * untrack(|| range.unit()));
    true
  };

  let body = |column: bool| {
    let parts = Parts {
      style: Rc::clone(&style),
      fill_style: Rc::clone(&fill_style),
      handle,
      fraction,
      column,
    };
    parts.area().visible(move || vertical.get() == column)
  };
  let mut button = Button::new(Stack::new().child(body(false)).child(body(true)))
    .on_drag(on_drag)
    .on_wheel(on_wheel)
    .on_key(on_key);
  if let Some(enabled) = enabled {
    button = button.enabled(enabled);
  }
  button
}

/// Writes `false` to a slider's `dragging` signal when the slider goes
/// while held, as no drag `End` comes then.
struct LetGo(Signal<bool>);

impl Drop for LetGo {
  fn drop(&mut self) {
    if untrack(|| self.0.try_get()) == Some(true) {
      self.0.try_set(false);
    }
  }
}

/// What one orientation of a slider is drawn from.
struct Parts {
  style: StyleFn,
  fill_style: StyleFn,
  handle: Memo<Style>,
  fraction: Memo<f32>,
  /// Bottom to top, rather than left to right.
  column: bool,
}

impl Parts {
  /// The track with its fill, and over it the handle, in one cell.
  fn area(self) -> Stack {
    let Parts {
      style,
      fill_style,
      handle,
      fraction,
      column,
    } = self;
    let (along, across) = TRACK;
    let size = if column {
      Style::new().width(across).height(along)
    } else {
      Style::new().width(along).height(across)
    };
    let half = move || {
      let handle = handle.get();
      let size = if column { handle.height } else { handle.width };
      size.unwrap_or(HANDLE).max(0.0) / 2.0
    };
    // The free space past both halves of the handle goes before and after
    // it by the fraction, as it does around the handle below.
    let fill = Stack::new().style(move || fill_style().basis(half()).grow(fraction.get()));
    let rest = Stack::new().style(move || Style::new().basis(half()).grow(1.0 - fraction.get()));
    let track_style = move || {
      size
        .align_self(Align::Center)
        .justify_self(Align::Center)
        .merge(style())
    };
    let before = Stack::new().style(move || Style::new().basis(0.0).grow(fraction.get()));
    let after = Stack::new().style(move || Style::new().basis(0.0).grow(1.0 - fraction.get()));
    let knob = Stack::new().style(move || handle.get());
    if column {
      Stack::new()
        .child(
          Column::new()
            .reverse(true)
            .style(track_style)
            .child(fill)
            .child(rest),
        )
        .child(
          Column::new()
            .reverse(true)
            .align(Align::Center)
            .child(before)
            .child(knob)
            .child(after),
        )
    } else {
      Stack::new()
        .child(Row::new().style(track_style).child(fill).child(rest))
        .child(
          Row::new()
            .align(Align::Center)
            .child(before)
            .child(knob)
            .child(after),
        )
    }
  }
}

/// `prop` as a closure for a memo, or `default` when unset.
fn shared_or<T: Clone + 'static>(prop: Option<Prop<T>>, default: T) -> impl FnMut() -> T + 'static {
  let prop = prop.map(Prop::shared);
  move || prop.as_ref().map_or_else(|| default.clone(), |prop| prop())
}

fn shared_style(prop: Option<Prop<Style>>) -> StyleFn {
  match prop {
    Some(prop) => prop.shared(),
    None => Rc::new(Style::new),
  }
}