day-pieces 0.6.1

Declarative app development API using native UI toolkits
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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The split (docs/split.md): two panes with a divider the user drags, side by side or
//! stacked, the first pane's share bound to an app-owned `Signal<f64>`. Where `Cap::Split` is
//! `Native` the panes are the toolkit's own splitter (an `NSSplitView`, a `GtkPaned`, a
//! `QSplitter`); everywhere else the piece lays the panes out itself and draws the divider,
//! which it also drags.

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use day_core::*;
use day_geometry::{Proposal, Rect};
use day_reactive::{AnyBinding, Binding, Signal, bind_seeded};
use day_spec::props::{SplitAxis, SplitPaneProps, SplitPatch, SplitProps};
use day_spec::{Cap, Color, Cursor, DragPhase, Event, Shape, Size, Support, kinds};

use crate::*;

/// The least length either pane may be dragged to, in points (default).
pub const SPLIT_MIN_PANE: f64 = 80.0;
/// The composed divider's grab width, in points: a hairline to look at, this much to hit.
const HANDLE: f64 = 9.0;

/// Two panes and the divider between them, bound to the app (docs/split.md).
///
/// ```ignore
/// let share = Signal::new(0.5);
/// let stacked = Signal::new(false);
/// split(request_form(), response_view())
///     .axis(move || if stacked.get() { SplitAxis::Vertical } else { SplitAxis::Horizontal })
///     .fraction(share)
/// ```
pub struct Split {
    first: AnyPiece,
    second: AnyPiece,
    axis: Reactive<SplitAxis>,
    fraction: Option<AnyBinding<f64>>,
    min_pane: f64,
}

/// Build a [`Split`]: `first` beside (or above) `second`, divided at half until dragged.
pub fn split<A: Piece, B: Piece>(first: A, second: B) -> Split {
    Split {
        first: AnyPiece::new(first),
        second: AnyPiece::new(second),
        axis: Reactive::Const(SplitAxis::Horizontal),
        fraction: None,
        min_pane: SPLIT_MIN_PANE,
    }
}

#[day_macros::builder(SplitBuilder)]
impl Split {
    /// Side by side ([`SplitAxis::Horizontal`], the default) or stacked; a signal or a closure
    /// turns the split live, keeping the share.
    pub fn axis<M>(mut self, axis: impl IntoReactive<SplitAxis, M>) -> Self {
        self.axis = axis.into_reactive();
        self
    }
    /// The first pane's share of the length, `0.0..=1.0`, two-way: a dragged divider writes
    /// it, writing it moves the divider. Unbound, the split keeps a share of its own from 0.5.
    pub fn fraction(mut self, fraction: impl Binding<f64>) -> Self {
        self.fraction = Some(AnyBinding::new(fraction));
        self
    }
    /// The least length either pane may be dragged to (default [`SPLIT_MIN_PANE`]).
    pub fn min_pane(mut self, points: f64) -> Self {
        self.min_pane = points.max(0.0);
        self
    }
}

impl Piece for Split {
    fn build(self, cx: &mut BuildCx) -> RNode {
        if day_core::capability(Cap::Split) == Support::Native {
            build_native(self, cx)
        } else {
            build_composed(self, cx)
        }
    }
}

/// A share kept inside `0..1` with both panes at least `min` of a `total`.
fn clamp_share(share: f64, total: f64, min: f64) -> f64 {
    if total <= 0.0 || 2.0 * min >= total {
        return share.clamp(0.0, 1.0);
    }
    let lo = min / total;
    share.clamp(lo, 1.0 - lo)
}

// ---------------------------------------------------------------------------
// Native: the toolkit's own splitter (`Cap::Split == Native`)
// ---------------------------------------------------------------------------

fn build_native(split: Split, cx: &mut BuildCx) -> RNode {
    let Split {
        first,
        second,
        axis,
        fraction,
        min_pane,
    } = split;
    let fraction = fraction.unwrap_or_else(|| AnyBinding::new(Signal::new(0.5)));
    let initial_axis = axis.get_untracked();
    let initial = fraction.get_untracked().clamp(0.0, 1.0);
    let sizes: Rc<RefCell<std::collections::HashMap<RNode, Size>>> = Rc::default();
    let stacked = Rc::new(Cell::new(initial_axis == SplitAxis::Vertical));
    let share = Rc::new(Cell::new(initial));
    let node = cx.native(
        kinds::SPLIT,
        &SplitProps {
            axis: initial_axis,
            fraction: initial,
            min_pane,
        },
        Rc::new(SplitLayout {
            sizes: sizes.clone(),
            stacked: stacked.clone(),
            fraction: share.clone(),
        }),
        Flex {
            grow_w: true,
            grow_h: true,
            ..Default::default()
        },
        Boundary::Yes,
    );

    // The two panes, first then second (`SplitPaneProps::second`); frames native-owned,
    // reported like nav pages and inspector panes.
    let first_pane = pane(node, false, &sizes);
    let mut fcx = BuildCx::new(first_pane);
    let _ = first.build(&mut fcx);
    let second_pane = pane(node, true, &sizes);
    let mut scx = BuildCx::new(second_pane);
    let _ = second.build(&mut scx);

    // Axis → native, as a targeted patch; the layout cell keeps the fallback split in step.
    {
        let stacked = stacked.clone();
        bind_seeded(
            initial_axis,
            move || axis.get(),
            move |a: &SplitAxis| {
                stacked.set(*a == SplitAxis::Vertical);
                with_tree(|t| {
                    t.patch(node, Box::new(SplitPatch::Axis(*a)), false);
                    t.mark_needs_measure(node);
                    t.mark_layout_dirty();
                    t.layout_if_needed();
                });
            },
        );
    }
    // Share → native. `native_share` is what the splitter last reported, so a report written
    // into the signal is not patched straight back (the echo guard every bound control has).
    let native_share: Rc<Cell<Option<f64>>> = Rc::new(Cell::new(None));
    {
        let share = share.clone();
        let native_share = native_share.clone();
        bind_seeded(
            initial,
            move || fraction.get(),
            move |f: &f64| {
                let f = f.clamp(0.0, 1.0);
                share.set(f);
                if native_share.get().is_some_and(|n| (n - f).abs() < 1e-6) {
                    return;
                }
                with_tree(|t| {
                    t.patch(node, Box::new(SplitPatch::Fraction(f)), false);
                    t.mark_needs_measure(node);
                    t.mark_layout_dirty();
                    t.layout_if_needed();
                });
            },
        );
    }
    // Native divider → signal.
    cx.on(node, move |ev| {
        if let Event::ValueChanged(f) = ev {
            let f = f.clamp(0.0, 1.0);
            native_share.set(Some(f));
            if (fraction.get_untracked() - f).abs() > 1e-6 {
                fraction.set(f);
            }
        }
    });
    node
}

/// One native-owned pane container under the split host, with the same `FrameChanged`
/// wiring as an inspector pane: the backend reports each pane's real frame, and Day lays the
/// pane's content out inside it.
fn pane(
    host: RNode,
    second: bool,
    sizes: &Rc<RefCell<std::collections::HashMap<RNode, Size>>>,
) -> RNode {
    let mut cx = BuildCx::new(host);
    let pane = cx.native(
        kinds::SPLIT_PANE,
        &SplitPaneProps { second },
        Rc::new(PassThrough),
        Flex::default(),
        Boundary::Yes,
    );
    let sizes = sizes.clone();
    cx.on(pane, move |ev| {
        if let Event::FrameChanged(sz) = ev {
            let changed = sizes.borrow().get(&pane) != Some(sz);
            if changed {
                sizes.borrow_mut().insert(pane, *sz);
                with_tree(|t| {
                    t.mark_needs_measure(pane);
                    t.mark_layout_dirty();
                    t.layout_if_needed();
                });
            }
        }
    });
    pane
}

// ---------------------------------------------------------------------------
// Composed: Day's own layout, a drawn divider, a drag (every other backend)
// ---------------------------------------------------------------------------

/// The composed split's state, shared by its layout, its divider and its drag.
struct ComposedState {
    stacked: Cell<bool>,
    share: Cell<f64>,
    min_pane: f64,
    /// The length along the axis at the last placement: what a drag's translation is a share of.
    length: Cell<f64>,
}

struct ComposedLayout {
    state: Rc<ComposedState>,
}

impl Layout for ComposedLayout {
    fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        // Greedy, like the native split: the panes take whatever the parent proposes.
        Size::new(p.width.unwrap_or(480.0), p.height.unwrap_or(640.0))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        // Children: 0 first, 1 the divider, 2 second.
        let (w, h) = (bounds.size.width, bounds.size.height);
        let stacked = self.state.stacked.get();
        let length = if stacked { h } else { w };
        self.state.length.set(length);
        let usable = (length - HANDLE).max(0.0);
        let share = clamp_share(self.state.share.get(), usable, self.state.min_pane);
        let a = (usable * share).round();
        let b = (usable - a).max(0.0);
        let rects = if stacked {
            [
                Rect::new(0.0, 0.0, w, a),
                Rect::new(0.0, a, w, HANDLE),
                Rect::new(0.0, a + HANDLE, w, b),
            ]
        } else {
            [
                Rect::new(0.0, 0.0, a, h),
                Rect::new(a, 0.0, HANDLE, h),
                Rect::new(a + HANDLE, 0.0, b, h),
            ]
        };
        for (child, rect) in children.iter().zip(rects) {
            cx.place_child(*child, rect);
        }
    }
}

fn build_composed(split: Split, cx: &mut BuildCx) -> RNode {
    let Split {
        first,
        second,
        axis,
        fraction,
        min_pane,
    } = split;
    let fraction = fraction.unwrap_or_else(|| AnyBinding::new(Signal::new(0.5)));
    let initial_axis = axis.get_untracked();
    let state = Rc::new(ComposedState {
        stacked: Cell::new(initial_axis == SplitAxis::Vertical),
        share: Cell::new(fraction.get_untracked().clamp(0.0, 1.0)),
        min_pane,
        length: Cell::new(0.0),
    });
    let node = cx.layout_only(
        Rc::new(ComposedLayout {
            state: state.clone(),
        }),
        Flex {
            grow_w: true,
            grow_h: true,
            ..Default::default()
        },
        Boundary::No,
    );
    let relayout = move || {
        with_tree(|t| {
            t.mark_needs_measure(node);
            t.mark_layout_dirty();
        });
    };
    // The divider: a hairline in the middle of its grab width, drawn by Day, dragged by Day.
    // The drag moves the share by the pointer's travel as a fraction of the split's length.
    let drag_axis = axis.clone();
    let cursor_axis = axis.clone();
    let draw_axis = axis.clone();
    let drag_state = state.clone();
    let drag_start = Rc::new(Cell::new(0.0f64));
    let divider = canvas(move |d, size| {
        let stacked = draw_axis.get() == SplitAxis::Vertical;
        let line = if stacked {
            Rect::new(0.0, (size.height / 2.0).floor(), size.width, 1.0)
        } else {
            Rect::new((size.width / 2.0).floor(), 0.0, 1.0, size.height)
        };
        d.fill(Shape::Rect(line), Color::rgba(0.5, 0.5, 0.5, 0.45));
    })
    .cursor(move || {
        if cursor_axis.get() == SplitAxis::Vertical {
            Cursor::RowResize
        } else {
            Cursor::ColResize
        }
    })
    .on_drag(move |drag| {
        let stacked = drag_axis.get_untracked() == SplitAxis::Vertical;
        match drag.phase {
            DragPhase::Began => drag_start.set(drag_state.share.get()),
            DragPhase::Changed | DragPhase::Ended => {
                let usable = (drag_state.length.get() - HANDLE).max(1.0);
                let travel = if stacked {
                    drag.translation.y
                } else {
                    drag.translation.x
                };
                let next = clamp_share(
                    drag_start.get() + travel / usable,
                    usable,
                    drag_state.min_pane,
                );
                if (fraction.get_untracked() - next).abs() > 1e-6 {
                    fraction.set(next);
                }
            }
        }
    })
    .id(day_core::id!(day_split_divider));
    cx.under(node, |cx| {
        let _ = first.build(cx);
        let _ = divider.build(cx);
        let _ = second.build(cx);
    });
    // Axis and share → the layout, which re-places the three children.
    {
        let state = state.clone();
        bind_seeded(
            initial_axis,
            move || axis.get(),
            move |a: &SplitAxis| {
                state.stacked.set(*a == SplitAxis::Vertical);
                relayout();
            },
        );
    }
    bind_seeded(
        state.share.get(),
        move || fraction.get(),
        move |f: &f64| {
            state.share.set(f.clamp(0.0, 1.0));
            relayout();
        },
    );
    node
}

#[cfg(feature = "conformance")]
pub(crate) mod conformance {
    use day_core::conformance::{Case, Drive, FrameExpect};
    use day_reactive::Signal;
    use day_spec::kinds;
    use day_spec::props::SplitAxis;

    use crate::*;

    /// Both panes show, and the bound share moves the divider: writing the signal re-lays
    /// the panes, and the first pane's width follows it.
    #[day_macros::test(day_core)]
    fn split_panes_follow_the_share() -> Case {
        Case::new()
            .proves(kinds::SPLIT)
            .proves(kinds::SPLIT_PANE)
            .page(|| {
                let share = Signal::new(0.5);
                column((
                    label(move || format!("share {:.2}", share.get())).id(day_core::id!(share)),
                    button("Quarter")
                        .action(move || share.set(0.25))
                        .id(day_core::id!(quarter)),
                    split(
                        column((label("First"),)).grow().id(day_core::id!(first)),
                        column((label("Second"),)).grow().id(day_core::id!(second)),
                    )
                    .fraction(share)
                    .width(400.0)
                    .height(200.0)
                    .id(day_core::id!(split)),
                ))
            })
            .drive(|d: Drive| async move {
                d.assert_visible("first").await?;
                d.assert_visible("second").await?;
                d.tap("quarter").await?;
                d.wait_idle().await?;
                d.assert_text("share", "share 0.25").await?;
                // A quarter of the length, less the divider's own width, which differs per
                // toolkit (a hairline on AppKit, a handle on GTK and the composed tier); the
                // pane keeps the split's full height whatever its share.
                d.assert_frame(
                    "first",
                    FrameExpect {
                        width: Some(100.0),
                        height: Some(200.0),
                        tolerance: Some(10.0),
                        ..Default::default()
                    },
                )
                .await
            })
    }

    /// A share that would leave a pane narrower than the minimum is held at the minimum: the
    /// first pane keeps `min_pane` of width whatever the signal says, on the toolkit's own
    /// splitter and on the composed one alike.
    #[day_macros::test(day_core)]
    fn split_min_pane_holds_the_share() -> Case {
        Case::new()
            .proves(kinds::SPLIT)
            .page(|| {
                let share = Signal::new(0.5);
                column((
                    button("Sliver")
                        .action(move || share.set(0.02))
                        .id(day_core::id!(sliver)),
                    split(
                        column((label("First"),)).grow().id(day_core::id!(first)),
                        column((label("Second"),)).grow().id(day_core::id!(second)),
                    )
                    .fraction(share)
                    .min_pane(80.0)
                    .width(400.0)
                    .height(200.0)
                    .id(day_core::id!(split)),
                ))
            })
            .drive(|d: Drive| async move {
                d.tap("sliver").await?;
                d.wait_idle().await?;
                d.assert_frame(
                    "first",
                    FrameExpect {
                        width: Some(80.0),
                        tolerance: Some(10.0),
                        ..Default::default()
                    },
                )
                .await
            })
    }

    /// Turning the split the other way stacks the panes: the second pane moves below the
    /// first instead of beside it.
    #[day_macros::test(day_core)]
    fn split_axis_stacks_the_panes() -> Case {
        Case::new()
            .proves(kinds::SPLIT)
            .page(|| {
                let stacked = Signal::new(false);
                column((
                    button("Stack")
                        .action(move || stacked.set(true))
                        .id(day_core::id!(stack)),
                    // Each pane's content fills its pane, so the pane's extent is what the
                    // frame assertions read; a native pane's Day frame carries its toolkit
                    // size but not its toolkit position.
                    split(
                        column((label("First"),)).grow().id(day_core::id!(first)),
                        column((label("Second"),)).grow().id(day_core::id!(second)),
                    )
                    .axis(move || {
                        if stacked.get() {
                            SplitAxis::Vertical
                        } else {
                            SplitAxis::Horizontal
                        }
                    })
                    .width(400.0)
                    .height(200.0)
                    .id(day_core::id!(split)),
                ))
            })
            .drive(|d: Drive| async move {
                // Side by side: the second pane stands the split's full height.
                d.assert_frame(
                    "second",
                    FrameExpect {
                        height: Some(200.0),
                        ..Default::default()
                    },
                )
                .await?;
                d.tap("stack").await?;
                d.wait_idle().await?;
                // Stacked: it spans the split's full width instead.
                d.assert_frame(
                    "second",
                    FrameExpect {
                        width: Some(400.0),
                        ..Default::default()
                    },
                )
                .await
            })
    }
}

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

    #[test]
    fn a_share_keeps_both_panes_at_the_minimum() {
        assert_eq!(clamp_share(0.5, 400.0, 80.0), 0.5);
        assert_eq!(clamp_share(0.05, 400.0, 80.0), 0.2);
        assert_eq!(clamp_share(0.99, 400.0, 80.0), 0.8);
        // Too short for two minimums: only the 0..1 clamp is left.
        assert_eq!(clamp_share(1.5, 100.0, 80.0), 1.0);
    }
}