gpui-rhai 0.2.1

A Rhai-authored declarative UI runtime for GPUI
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Native pointer hot lane for the public controlled `Draggable` component.

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

use gpui::{
    AnyElement, App, Bounds, CursorStyle, DispatchPhase, Element, ElementId, FocusHandle,
    GlobalElementId, Hitbox, HitboxBehavior, InspectorElementId, InteractiveElement, IntoElement,
    KeyDownEvent, LayoutId, MouseButton, MouseDownEvent, ParentElement, Pixels, Style, Styled,
    Window, div, relative, size,
};

use crate::{
    ComponentStateSchema, EventSchema, ObjectField, PrimitiveContext, PrimitiveDescriptor,
    PrimitiveHandler, PrimitiveId, PrimitiveInstance, PrimitiveProps, PrimitiveTheme, SignalKind,
    SignalValue, UiValue, ValueSchema,
};

const MAX_POSITION: f64 = 1_000_000.0;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DragAxes {
    Both,
    Horizontal,
    Vertical,
}

#[derive(Clone)]
struct DragConfig {
    id: String,
    source_x: f64,
    source_y: f64,
    axes: DragAxes,
    contain: bool,
    threshold: f64,
    snap_x: Option<f64>,
    snap_y: Option<f64>,
    keyboard_step: f64,
    disabled: bool,
    boundary_ref: crate::ElementRef,
    object_ref: crate::ElementRef,
    handle_ref: Option<crate::ElementRef>,
    x_signal: crate::NativeSignal,
    y_signal: crate::NativeSignal,
    focus: Option<FocusHandle>,
}

#[derive(Clone, Debug, PartialEq)]
struct DragSourceIdentity {
    x: f64,
    y: f64,
    x_signal: crate::SignalId,
    y_signal: crate::SignalId,
    disabled: bool,
}

#[derive(Clone)]
struct DragState(Rc<RefCell<DragSourceIdentity>>);

impl DragState {
    fn new(config: &DragConfig) -> Self {
        Self(Rc::new(RefCell::new(source_identity(config))))
    }
}

fn source_identity(config: &DragConfig) -> DragSourceIdentity {
    DragSourceIdentity {
        x: config.source_x,
        y: config.source_y,
        x_signal: config.x_signal.id().clone(),
        y_signal: config.y_signal.id().clone(),
        disabled: config.disabled,
    }
}

struct DragPrepaint {
    hitbox: Hitbox,
}

struct DragHandleElement {
    config: DragConfig,
    context: PrimitiveContext,
}

impl IntoElement for DragHandleElement {
    type Element = Self;

    fn into_element(self) -> Self::Element {
        self
    }
}

impl Element for DragHandleElement {
    type RequestLayoutState = ();
    type PrepaintState = DragPrepaint;

    fn id(&self) -> Option<ElementId> {
        Some(ElementId::Name(self.config.id.clone().into()))
    }

    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, ()) {
        (
            window.request_layout(
                Style {
                    size: size(relative(1.0).into(), relative(1.0).into()),
                    ..Style::default()
                },
                None,
                cx,
            ),
            (),
        )
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        (): &mut (),
        window: &mut Window,
        cx: &mut App,
    ) -> DragPrepaint {
        let state = window
            .use_state(cx, |_, _| DragState::new(&self.config))
            .read(cx)
            .clone();
        let changed = {
            let next = source_identity(&self.config);
            let mut source = state.0.borrow_mut();
            let changed = *source != next;
            *source = next;
            changed
        };
        let owner = self.context.interaction_owner(&self.config.id);
        if changed && !self.context.cancel_interaction(&owner, window, cx) {
            clear_preview(&self.context, &self.config, cx);
        }
        DragPrepaint {
            hitbox: window.insert_hitbox(bounds, HitboxBehavior::Normal),
        }
    }

    fn paint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        (): &mut (),
        prepaint: &mut DragPrepaint,
        window: &mut Window,
        _: &mut App,
    ) {
        let owner = self.context.interaction_owner(&self.config.id);
        self.context.present_interaction(owner);
        if !self.config.disabled {
            window.set_cursor_style(CursorStyle::OpenHand, &prepaint.hitbox);
        }
        register_pointer_down(prepaint, &self.config, &self.context, window);
    }
}

fn register_pointer_down(
    prepaint: &DragPrepaint,
    config: &DragConfig,
    context: &PrimitiveContext,
    window: &mut Window,
) {
    let hitbox = prepaint.hitbox.clone();
    let config = config.clone();
    let context = context.clone();
    let view = window.current_view();
    window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
        if phase != DispatchPhase::Bubble
            || event.button != MouseButton::Left
            || config.disabled
            || !hitbox.is_hovered(window)
        {
            return;
        }
        let eligible_ref = config.handle_ref.as_ref().unwrap_or(&config.object_ref);
        let Some(eligible) = context.element_bounds(eligible_ref, cx) else {
            return;
        };
        if !contains(eligible, event.position) {
            return;
        }
        let (Some(boundary), Some(object)) = (
            context.element_bounds(&config.boundary_ref, cx),
            context.element_bounds(&config.object_ref, cx),
        ) else {
            return;
        };
        if config.contain
            && (config.source_x < 0.0
                || config.source_y < 0.0
                || config.source_x + object.width > boundary.width + 0.5
                || config.source_y + object.height > boundary.height + 0.5)
        {
            return;
        }
        if let Some(focus) = config.focus.as_ref() {
            focus.focus(window, cx);
        }
        let boundary_size = (boundary.width, boundary.height);
        let object_size = (object.width, object.height);
        let update_config = config.clone();
        let update_context = context.clone();
        let update =
            move |gesture: crate::interaction::GestureUpdate, _: &mut Window, cx: &mut App| {
                let Some(boundary) = update_context.element_bounds(&update_config.boundary_ref, cx)
                else {
                    return crate::interaction::InteractionFlow::Cancel;
                };
                if (boundary.width - boundary_size.0).abs() > 0.5
                    || (boundary.height - boundary_size.1).abs() > 0.5
                {
                    return crate::interaction::InteractionFlow::Cancel;
                }
                if gesture.moved() {
                    let next =
                        drag_position(&update_config, gesture.delta(), boundary_size, object_size);
                    write_preview(&update_context, &update_config, Some(next), cx);
                }
                crate::interaction::InteractionFlow::Continue
            };
        let finish_config = config.clone();
        let finish_context = context.clone();
        let finish =
            move |gesture: crate::interaction::GestureUpdate, window: &mut Window, cx: &mut App| {
                clear_preview(&finish_context, &finish_config, cx);
                if gesture.moved() {
                    let next =
                        drag_position(&finish_config, gesture.delta(), boundary_size, object_size);
                    if position_changed(&finish_config, next) {
                        finish_context.propose("move", position_value(next), window, cx);
                    }
                }
            };
        let cancel_config = config.clone();
        let cancel_context = context.clone();
        let cancel = move |_: &mut Window, cx: &mut App| {
            clear_preview(&cancel_context, &cancel_config, cx);
        };
        let owner = context.interaction_owner(&config.id);
        context.begin_interaction(
            crate::interaction::NativeGesture::new(
                owner,
                event.position,
                view,
                update,
                finish,
                cancel,
            )
            .with_threshold(config.threshold),
            window,
            cx,
        );
        cx.stop_propagation();
    });
}

fn contains(bounds: crate::GeometryBounds, point: gpui::Point<Pixels>) -> bool {
    let x = f64::from(point.x);
    let y = f64::from(point.y);
    x >= bounds.x && x <= bounds.x + bounds.width && y >= bounds.y && y <= bounds.y + bounds.height
}

fn drag_position(
    config: &DragConfig,
    delta: (f64, f64),
    boundary: (f64, f64),
    object: (f64, f64),
) -> (f64, f64) {
    moved_position(config, delta, boundary, object, |source, delta, step| {
        snap(source + delta, step)
    })
}

/// The position after an arrow key. A key always reaches at least the next grid line in
/// its direction, so a snap step larger than the key step still moves the object.
fn key_position(
    config: &DragConfig,
    delta: (f64, f64),
    boundary: (f64, f64),
    object: (f64, f64),
) -> (f64, f64) {
    moved_position(config, delta, boundary, object, snap_toward)
}

fn moved_position(
    config: &DragConfig,
    delta: (f64, f64),
    boundary: (f64, f64),
    object: (f64, f64),
    axis: impl Fn(f64, f64, Option<f64>) -> f64,
) -> (f64, f64) {
    let mut x = config.source_x;
    let mut y = config.source_y;
    if matches!(config.axes, DragAxes::Both | DragAxes::Horizontal) {
        x = axis(x, delta.0, config.snap_x);
    }
    if matches!(config.axes, DragAxes::Both | DragAxes::Vertical) {
        y = axis(y, delta.1, config.snap_y);
    }
    if config.contain {
        x = x.clamp(0.0, (boundary.0 - object.0).max(0.0));
        y = y.clamp(0.0, (boundary.1 - object.1).max(0.0));
    }
    (x, y)
}

fn snap(value: f64, step: Option<f64>) -> f64 {
    step.map_or(value, |step| (value / step).round() * step)
}

fn snap_toward(source: f64, delta: f64, step: Option<f64>) -> f64 {
    let next = snap(source + delta, step);
    match step {
        Some(step) if delta > 0.0 && next <= source => (source / step).floor().mul_add(step, step),
        Some(step) if delta < 0.0 && next >= source => (source / step).ceil().mul_add(step, -step),
        _ => next,
    }
}

fn write_preview(
    context: &PrimitiveContext,
    config: &DragConfig,
    position: Option<(f64, f64)>,
    cx: &mut App,
) {
    let _ = context.write_signals(
        [
            (
                config.x_signal.clone(),
                SignalValue::OptionalFloat(position.map(|position| position.0 - config.source_x)),
            ),
            (
                config.y_signal.clone(),
                SignalValue::OptionalFloat(position.map(|position| position.1 - config.source_y)),
            ),
        ],
        cx,
    );
}

fn clear_preview(context: &PrimitiveContext, config: &DragConfig, cx: &mut App) {
    write_preview(context, config, None, cx);
}

fn position_value(position: (f64, f64)) -> UiValue {
    UiValue::Map(BTreeMap::from([
        ("x".to_owned(), UiValue::Float(position.0)),
        ("y".to_owned(), UiValue::Float(position.1)),
    ]))
}

fn position_changed(config: &DragConfig, position: (f64, f64)) -> bool {
    (position.0 - config.source_x).abs() > f64::EPSILON
        || (position.1 - config.source_y).abs() > f64::EPSILON
}

#[derive(Default)]
pub struct DraggablePrimitiveHandler;

impl PrimitiveHandler for DraggablePrimitiveHandler {
    fn uses_primary_focus(&self) -> bool {
        true
    }

    fn render(
        &mut self,
        instance: &PrimitiveInstance,
        context: &PrimitiveContext,
        _: &PrimitiveTheme,
        _: &mut Window,
        _: &mut App,
    ) -> Result<AnyElement, String> {
        let config = parse_config(&instance.node.props, instance.focus_handle().cloned())?;
        let key_config = config.clone();
        let key_context = context.clone();
        let mut root = div().size_full();
        if let Some(focus) = config.focus.as_ref() {
            root = root.track_focus(&focus.clone().tab_stop(!config.disabled));
        }
        Ok(root
            .on_key_down(move |event: &KeyDownEvent, window, cx: &mut App| {
                if key_config.disabled {
                    return;
                }
                let multiplier = if event.keystroke.modifiers.shift {
                    4.0
                } else {
                    1.0
                };
                let step = key_config.keyboard_step * multiplier;
                let delta = match event.keystroke.key.as_str() {
                    "left" => (-step, 0.0),
                    "right" => (step, 0.0),
                    "up" => (0.0, -step),
                    "down" => (0.0, step),
                    _ => return,
                };
                let (Some(boundary), Some(object)) = (
                    key_context.element_bounds(&key_config.boundary_ref, cx),
                    key_context.element_bounds(&key_config.object_ref, cx),
                ) else {
                    return;
                };
                let next = key_position(
                    &key_config,
                    delta,
                    (boundary.width, boundary.height),
                    (object.width, object.height),
                );
                if position_changed(&key_config, next) {
                    key_context.propose("move", position_value(next), window, cx);
                }
                cx.stop_propagation();
            })
            .child(DragHandleElement {
                config,
                context: context.clone(),
            })
            .into_any_element())
    }
}

fn parse_config(props: &PrimitiveProps, focus: Option<FocusHandle>) -> Result<DragConfig, String> {
    let source_x = required_number(props, "x")?;
    let source_y = required_number(props, "y")?;
    if source_x.abs() > MAX_POSITION || source_y.abs() > MAX_POSITION {
        return Err("draggable position exceeds the supported range".to_owned());
    }
    let axes = match props.string("axes") {
        None | Some("both") => DragAxes::Both,
        Some("horizontal") => DragAxes::Horizontal,
        Some("vertical") => DragAxes::Vertical,
        Some(_) => return Err("draggable axes must be both, horizontal, or vertical".to_owned()),
    };
    let threshold = props.number("threshold").unwrap_or(4.0);
    let keyboard_step = props.number("keyboard_step").unwrap_or(8.0);
    let snap_x = optional_positive_number(props, "snap_x")?;
    let snap_y = optional_positive_number(props, "snap_y")?;
    if !threshold.is_finite()
        || !(0.0..=64.0).contains(&threshold)
        || !keyboard_step.is_finite()
        || !(0.0..=512.0).contains(&keyboard_step)
        || keyboard_step == 0.0
    {
        return Err("draggable threshold or keyboard_step is invalid".to_owned());
    }
    let x_signal = optional_float_signal(props, "x_signal")?;
    let y_signal = optional_float_signal(props, "y_signal")?;
    Ok(DragConfig {
        id: format!(
            "gpui-rhai-draggable:{}:{}",
            x_signal.id().component(),
            x_signal.id().key()
        ),
        source_x,
        source_y,
        axes,
        contain: props.boolean("contain").unwrap_or(true),
        threshold,
        snap_x,
        snap_y,
        keyboard_step,
        disabled: props.boolean("disabled").unwrap_or(false),
        boundary_ref: props
            .element_ref("boundary_ref")
            .cloned()
            .ok_or_else(|| "draggable requires boundary_ref".to_owned())?,
        object_ref: props
            .element_ref("object_ref")
            .cloned()
            .ok_or_else(|| "draggable requires object_ref".to_owned())?,
        handle_ref: props.element_ref("handle_ref").cloned(),
        x_signal,
        y_signal,
        focus,
    })
}

fn required_number(props: &PrimitiveProps, name: &str) -> Result<f64, String> {
    props
        .number(name)
        .filter(|value| value.is_finite())
        .ok_or_else(|| format!("draggable requires finite numeric {name}"))
}

fn optional_positive_number(props: &PrimitiveProps, name: &str) -> Result<Option<f64>, String> {
    match props.data(name) {
        None | Some(UiValue::Null) => Ok(None),
        Some(_) => props
            .number(name)
            .filter(|value| value.is_finite() && *value > 0.0)
            .map(Some)
            .ok_or_else(|| format!("draggable {name} must be a positive finite number")),
    }
}

fn optional_float_signal(
    props: &PrimitiveProps,
    name: &str,
) -> Result<crate::NativeSignal, String> {
    let signal = props
        .signal(name)
        .cloned()
        .ok_or_else(|| format!("draggable requires signal {name}"))?;
    if signal.id().kind() != SignalKind::OptionalFloat {
        return Err(format!("draggable {name} must be optional_float"));
    }
    Ok(signal)
}

fn position_bound_schema() -> ValueSchema {
    ValueSchema::bounded_number(Some(-MAX_POSITION), Some(MAX_POSITION))
}

fn position_schema() -> ValueSchema {
    ValueSchema::object(BTreeMap::from([
        ("x".to_owned(), ObjectField::required(ValueSchema::number())),
        ("y".to_owned(), ObjectField::required(ValueSchema::number())),
    ]))
}

/// Build the native controlled-position interaction schema.
///
/// # Panics
///
/// Panics only if the static built-in primitive ID becomes invalid.
#[must_use]
#[allow(clippy::too_many_lines)] // One declarative list of documented props and events.
pub fn draggable_primitive_descriptor() -> PrimitiveDescriptor {
    let optional_step =
        || ObjectField::optional(ValueSchema::optional(ValueSchema::positive_number()));
    PrimitiveDescriptor {
        id: PrimitiveId::parse("gpui_rhai.draggable").expect("static primitive ID"),
        export: "DraggablePrimitive".to_owned(),
        props: BTreeMap::from([
            (
                "x".to_owned(),
                ObjectField::required(position_bound_schema()).with_doc(
                    "Controlled left edge of the object in `boundary_ref`'s local logical pixels.",
                ),
            ),
            (
                "y".to_owned(),
                ObjectField::required(position_bound_schema()).with_doc(
                    "Controlled top edge of the object in `boundary_ref`'s local logical pixels.",
                ),
            ),
            (
                "axes".to_owned(),
                ObjectField::optional(ValueSchema::String {
                    allowed: vec![
                        "both".to_owned(),
                        "horizontal".to_owned(),
                        "vertical".to_owned(),
                    ],
                })
                .with_default(UiValue::String("both".to_owned()))
                .with_doc("Directions the object moves in: `both`, `horizontal` or `vertical`."),
            ),
            (
                "contain".to_owned(),
                ObjectField::optional(ValueSchema::Bool)
                    .with_default(UiValue::Bool(true))
                    .with_doc(
                        "Keeps the object inside `boundary_ref`; presses are ignored while the controlled position lies outside it.",
                    ),
            ),
            (
                "threshold".to_owned(),
                ObjectField::optional(ValueSchema::bounded_number(Some(0.0), Some(64.0))).with_doc(
                    "Pointer movement in logical pixels before a press becomes a drag; defaults to 4.",
                ),
            ),
            (
                "snap_x".to_owned(),
                optional_step().with_doc(
                    "Grid step in logical pixels the left edge rounds to, or `()` to move freely.",
                ),
            ),
            (
                "snap_y".to_owned(),
                optional_step().with_doc(
                    "Grid step in logical pixels the top edge rounds to, or `()` to move freely.",
                ),
            ),
            (
                "keyboard_step".to_owned(),
                ObjectField::optional(ValueSchema::Number {
                    min: None,
                    max: Some(512.0),
                    exclusive_min: Some(0.0),
                    exclusive_max: None,
                })
                .with_doc(
                    "Logical pixels one arrow-key press moves the object, up to 512; Shift multiplies it by four; with a larger snap step a press moves to the next grid line; defaults to 8.",
                ),
            ),
            (
                "disabled".to_owned(),
                ObjectField::optional(ValueSchema::Bool)
                    .with_default(UiValue::Bool(false))
                    .with_doc(
                        "Ignores presses and arrow keys and removes the object from the tab order.",
                    ),
            ),
            (
                "boundary_ref".to_owned(),
                ObjectField::required(ValueSchema::Ref).with_doc(
                    "Ref to the container the position is local to; resizing it during a drag cancels the drag.",
                ),
            ),
            (
                "object_ref".to_owned(),
                ObjectField::required(ValueSchema::Ref).with_doc(
                    "Ref to the moved object; its size bounds `contain`, and without `handle_ref` a press on it starts a drag.",
                ),
            ),
            (
                "handle_ref".to_owned(),
                ObjectField::optional(ValueSchema::optional(ValueSchema::Ref)).with_doc(
                    "Ref to a drag handle inside the object; when set, only presses inside it start a drag.",
                ),
            ),
            (
                "x_signal".to_owned(),
                ObjectField::required(ValueSchema::Signal).with_doc(
                    "Optional-float signal that receives the previewed horizontal offset from `x` during a drag, or `()` when idle.",
                ),
            ),
            (
                "y_signal".to_owned(),
                ObjectField::required(ValueSchema::Signal).with_doc(
                    "Optional-float signal that receives the previewed vertical offset from `y` during a drag, or `()` when idle.",
                ),
            ),
            (
                "on_move".to_owned(),
                ObjectField::optional(ValueSchema::optional(ValueSchema::Callback)).with_doc(
                    "Called with the proposed `{x, y}` when a drag ends or an arrow key is pressed.",
                ),
            ),
        ]),
        events: BTreeMap::from([(
            "move".to_owned(),
            EventSchema {
                doc: Some(
                    "Emitted once when a drag ends or an arrow key moves the object; the payload is the next `{x, y}`."
                        .to_owned(),
                ),
                payload: position_schema(),
            },
        )]),
        state: ComponentStateSchema::default(),
        lifecycle: false,
        effect: None,
    }
}

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

    #[test]
    fn the_schema_rejects_what_the_handler_rejects() {
        let props = draggable_primitive_descriptor().props;
        let accepts = |name: &str, value: f64| {
            props[name]
                .schema
                .validate_ui_value(&UiValue::Float(value))
                .is_ok()
        };
        assert!(accepts("x", -MAX_POSITION) && !accepts("x", 2_000_000.0));
        assert!(!accepts("y", -2_000_000.0));
        assert!(accepts("keyboard_step", 512.0) && !accepts("keyboard_step", 513.0));
        assert!(!accepts("keyboard_step", 0.0) && !accepts("snap_x", -1.0));
    }

    fn config(axes: DragAxes) -> DragConfig {
        let component = crate::ComponentInstancePath::root("Draggable", "card");
        DragConfig {
            id: "card".to_owned(),
            source_x: 20.0,
            source_y: 30.0,
            axes,
            contain: true,
            threshold: 4.0,
            snap_x: Some(10.0),
            snap_y: None,
            keyboard_step: 8.0,
            disabled: false,
            boundary_ref: crate::ElementRef::new(
                crate::ElementRefId::new(component.clone(), "boundary").unwrap(),
            ),
            object_ref: crate::ElementRef::new(
                crate::ElementRefId::new(component.clone(), "object").unwrap(),
            ),
            handle_ref: None,
            x_signal: crate::NativeSignal::new(
                crate::SignalId::new(component.clone(), "x", SignalKind::OptionalFloat).unwrap(),
            ),
            y_signal: crate::NativeSignal::new(
                crate::SignalId::new(component, "y", SignalKind::OptionalFloat).unwrap(),
            ),
            focus: None,
        }
    }

    #[test]
    fn drag_axes_snap_and_containment_are_one_pure_policy() {
        assert_eq!(
            drag_position(
                &config(DragAxes::Both),
                (17.0, 50.0),
                (100.0, 90.0),
                (30.0, 20.0)
            ),
            (40.0, 70.0)
        );
        assert_eq!(
            drag_position(
                &config(DragAxes::Horizontal),
                (99.0, 50.0),
                (100.0, 90.0),
                (30.0, 20.0)
            ),
            (70.0, 30.0)
        );
    }

    #[test]
    fn arrow_keys_reach_the_next_grid_line_when_the_snap_step_is_larger() {
        let mut config = config(DragAxes::Horizontal);
        config.source_x = 0.0;
        config.snap_x = Some(50.0);
        let key = |config: &DragConfig, delta| {
            key_position(config, (delta, 0.0), (400.0, 90.0), (30.0, 20.0)).0
        };
        assert!((key(&config, 8.0) - 50.0).abs() < f64::EPSILON);
        assert!(key(&config, -8.0).abs() < f64::EPSILON);
        config.source_x = 50.0;
        assert!((key(&config, 8.0) - 100.0).abs() < f64::EPSILON);
        assert!(key(&config, -8.0).abs() < f64::EPSILON);
        config.source_x = 30.0;
        assert!((key(&config, 8.0) - 50.0).abs() < f64::EPSILON);
        assert!(key(&config, -8.0).abs() < f64::EPSILON);
        // A key step past a grid line rounds as a drag does.
        config.snap_x = Some(10.0);
        config.source_x = 20.0;
        assert!((key(&config, 32.0) - 50.0).abs() < f64::EPSILON);
    }
}