fret-ui 0.1.0

Mechanism-layer UI engine for Fret with tree, layout, focus, routing, and interaction contracts.
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
use fret_core::{
    Axis, Color, Corners, DrawOrder, Edges, Event, MouseButton, Paint, Point, Px, Rect, Size,
};
use fret_runtime::Model;

use crate::resize_handle::ResizeHandle;
use crate::widget::{EventCx, LayoutCx, PaintCx, Widget};
use crate::{Invalidation, UiHost};

#[derive(Debug, Clone, Copy)]
struct DragState {
    grab_offset: f32,
}

fn split_log(_args: std::fmt::Arguments<'_>) {
    #[cfg(not(target_arch = "wasm32"))]
    {
        use std::{
            io::Write,
            sync::{Mutex, OnceLock},
        };

        if !crate::runtime_config::ui_runtime_config().resizable_split_log {
            return;
        }

        static LOG_FILE: OnceLock<Mutex<Option<std::fs::File>>> = OnceLock::new();
        let file = LOG_FILE.get_or_init(|| {
            let _ = std::fs::create_dir_all("target");
            let path = std::path::Path::new("target").join("fret-resizable-split.log");
            let file = std::fs::OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&path);

            let file = match file {
                Ok(mut file) => {
                    let _ = writeln!(
                        file,
                        "[session] pid={} time={:?}",
                        std::process::id(),
                        std::time::SystemTime::now()
                    );
                    Some(file)
                }
                Err(err) => {
                    if crate::strict_runtime::strict_runtime_enabled() {
                        panic!(
                            "open resizable split log failed: path={:?} err={err:?}",
                            path
                        );
                    }

                    tracing::warn!(
                        ?err,
                        path = ?path,
                        "open resizable split log failed; disabling split log output for this process"
                    );
                    None
                }
            };

            Mutex::new(file)
        });

        let Ok(mut file) = file.lock() else {
            return;
        };
        let Some(file) = file.as_mut() else {
            return;
        };
        let _ = writeln!(file, "{}", _args);
    }
}

/// A simple two-panel resizable split primitive.
///
/// This is a runtime substrate widget: it owns pointer capture, cursor affordances, and the split
/// fraction clamping rules. Higher-level panel groups and editor docking policies should live in
/// component/app layers.
pub struct ResizableSplit {
    axis: Axis,
    fraction: Model<f32>,
    min_px: Px,
    hit_thickness: Px,
    paint_device_px: f32,
    dragging: Option<DragState>,
    last_bounds: Rect,
    last_handle_rect: Rect,
}

impl ResizableSplit {
    pub fn new(axis: Axis, fraction: Model<f32>) -> Self {
        Self {
            axis,
            fraction,
            min_px: Px(120.0),
            hit_thickness: Px(6.0),
            paint_device_px: 1.0,
            dragging: None,
            last_bounds: Rect::default(),
            last_handle_rect: Rect::default(),
        }
    }

    pub fn with_min_px(mut self, min_px: Px) -> Self {
        self.min_px = min_px;
        self
    }

    pub fn with_hit_thickness(mut self, thickness: Px) -> Self {
        self.hit_thickness = thickness;
        self
    }

    pub fn with_paint_device_px(mut self, px: f32) -> Self {
        self.paint_device_px = px;
        self
    }

    fn axis_len(bounds: Rect, axis: Axis) -> f32 {
        match axis {
            Axis::Horizontal => bounds.size.width.0,
            Axis::Vertical => bounds.size.height.0,
        }
    }

    fn axis_origin(bounds: Rect, axis: Axis) -> f32 {
        match axis {
            Axis::Horizontal => bounds.origin.x.0,
            Axis::Vertical => bounds.origin.y.0,
        }
    }

    fn axis_pos(pos: Point, axis: Axis) -> f32 {
        match axis {
            Axis::Horizontal => pos.x.0,
            Axis::Vertical => pos.y.0,
        }
    }

    fn clamp_fraction(&self, fraction: f32, bounds: Rect) -> f32 {
        let gap = self.hit_thickness.0.max(0.0);
        let axis_len = Self::axis_len(bounds, self.axis);
        let avail = (axis_len - gap).max(0.0);
        if avail <= 0.0 {
            return 0.5;
        }

        let min = self.min_px.0.max(0.0);
        if avail <= min * 2.0 {
            return 0.5;
        }

        let left = (avail * fraction.clamp(0.0, 1.0)).clamp(min, (avail - min).max(min));
        (left / avail).clamp(0.0, 1.0)
    }

    fn compute_layout(&self, bounds: Rect, fraction: f32) -> (Rect, Rect, Rect, f32) {
        let gap = self.hit_thickness.0.max(0.0);
        let axis_len = Self::axis_len(bounds, self.axis);
        let avail = (axis_len - gap).max(0.0);

        let f = self.clamp_fraction(fraction, bounds);
        let left_len = avail * f;
        let right_len = (avail - left_len).max(0.0);

        match self.axis {
            Axis::Horizontal => {
                let rect_a = Rect::new(bounds.origin, Size::new(Px(left_len), bounds.size.height));
                let handle_rect = Rect::new(
                    Point::new(Px(bounds.origin.x.0 + left_len), bounds.origin.y),
                    Size::new(Px(gap), bounds.size.height),
                );
                let rect_b = Rect::new(
                    Point::new(Px(bounds.origin.x.0 + left_len + gap), bounds.origin.y),
                    Size::new(Px(right_len), bounds.size.height),
                );
                let center = bounds.origin.x.0 + left_len + gap * 0.5;
                (rect_a, rect_b, handle_rect, center)
            }
            Axis::Vertical => {
                let rect_a = Rect::new(bounds.origin, Size::new(bounds.size.width, Px(left_len)));
                let handle_rect = Rect::new(
                    Point::new(bounds.origin.x, Px(bounds.origin.y.0 + left_len)),
                    Size::new(bounds.size.width, Px(gap)),
                );
                let rect_b = Rect::new(
                    Point::new(bounds.origin.x, Px(bounds.origin.y.0 + left_len + gap)),
                    Size::new(bounds.size.width, Px(right_len)),
                );
                let center = bounds.origin.y.0 + left_len + gap * 0.5;
                (rect_a, rect_b, handle_rect, center)
            }
        }
    }
}

impl<H: UiHost> Widget<H> for ResizableSplit {
    fn event(&mut self, cx: &mut EventCx<'_, H>, event: &Event) {
        let Event::Pointer(pe) = event else {
            return;
        };

        self.last_bounds = cx.bounds;

        let fraction = match cx.app.models().try_get_copied(&self.fraction) {
            Ok(Some(v)) => v,
            Ok(None) => {
                #[cfg(debug_assertions)]
                tracing::warn!(
                    model_id = ?self.fraction.id(),
                    "resizable_split: fraction model not found"
                );
                0.5
            }
            Err(err) => {
                #[cfg(debug_assertions)]
                tracing::warn!(
                    ?err,
                    model_id = ?self.fraction.id(),
                    "resizable_split: failed to read fraction model"
                );
                0.5
            }
        };
        let (_a, _b, handle_rect, handle_center) = self.compute_layout(cx.bounds, fraction);
        self.last_handle_rect = handle_rect;

        match pe {
            fret_core::PointerEvent::Move { position, .. } => {
                if let Some(drag) = self.dragging {
                    let gap = self.hit_thickness.0.max(0.0);
                    let axis_len = Self::axis_len(cx.bounds, self.axis);
                    let avail = (axis_len - gap).max(0.0);
                    if avail <= 0.0 {
                        return;
                    }

                    let min = self.min_px.0.max(0.0);
                    if avail <= min * 2.0 {
                        return;
                    }

                    let origin = Self::axis_origin(cx.bounds, self.axis);
                    let pos = Self::axis_pos(*position, self.axis);
                    let center = pos - drag.grab_offset;
                    let left = (center - origin - gap * 0.5).clamp(min, (avail - min).max(min));
                    let next = (left / avail).clamp(0.0, 1.0);

                    let _ = cx.app.models_mut().update(&self.fraction, |v| {
                        *v = next;
                    });

                    split_log(format_args!(
                        "move dragging=true node={:?} captured={:?} pos={:?} bounds={:?} center={:.2} next={:.4}",
                        cx.node, cx.captured, position, cx.bounds, handle_center, next
                    ));

                    cx.set_cursor_icon(
                        ResizeHandle {
                            axis: self.axis,
                            hit_thickness: self.hit_thickness,
                            paint_device_px: self.paint_device_px,
                        }
                        .cursor_icon(),
                    );
                    cx.invalidate_self(Invalidation::Layout);
                    cx.invalidate_self(Invalidation::Paint);
                    cx.request_redraw();
                    cx.stop_propagation();
                    return;
                }

                if self.last_handle_rect.contains(*position) {
                    cx.set_cursor_icon(
                        ResizeHandle {
                            axis: self.axis,
                            hit_thickness: self.hit_thickness,
                            paint_device_px: self.paint_device_px,
                        }
                        .cursor_icon(),
                    );
                    split_log(format_args!(
                        "move hover=true node={:?} pos={:?} handle={:?} bounds={:?}",
                        cx.node, position, self.last_handle_rect, self.last_bounds
                    ));
                }
            }
            fret_core::PointerEvent::Down {
                position, button, ..
            } => {
                if *button != MouseButton::Left {
                    return;
                }
                if !self.last_handle_rect.contains(*position) {
                    split_log(format_args!(
                        "down hit=false node={:?} pos={:?} handle={:?} bounds={:?}",
                        cx.node, position, self.last_handle_rect, self.last_bounds
                    ));
                    return;
                }

                cx.capture_pointer(cx.node);
                self.dragging = Some(DragState {
                    grab_offset: Self::axis_pos(*position, self.axis) - handle_center,
                });

                split_log(format_args!(
                    "down dragging=true node={:?} pos={:?} handle={:?} bounds={:?}",
                    cx.node, position, self.last_handle_rect, self.last_bounds
                ));

                cx.stop_propagation();
            }
            fret_core::PointerEvent::Up { button, .. } => {
                if *button != MouseButton::Left {
                    return;
                }
                if self.dragging.is_none() {
                    return;
                }

                cx.release_pointer_capture();
                self.dragging = None;
                cx.invalidate_self(Invalidation::Paint);
                cx.request_redraw();
                cx.stop_propagation();
            }
            _ => {}
        }
    }

    fn layout(&mut self, cx: &mut LayoutCx<'_, H>) -> Size {
        self.last_bounds = cx.bounds;

        let fraction = match cx.app.models().try_get_copied(&self.fraction) {
            Ok(Some(v)) => v,
            Ok(None) => {
                #[cfg(debug_assertions)]
                tracing::warn!(
                    model_id = ?self.fraction.id(),
                    "resizable_split: fraction model not found"
                );
                0.5
            }
            Err(err) => {
                #[cfg(debug_assertions)]
                tracing::warn!(
                    ?err,
                    model_id = ?self.fraction.id(),
                    "resizable_split: failed to read fraction model"
                );
                0.5
            }
        };
        let (rect_a, rect_b, handle_rect, _center) = self.compute_layout(cx.bounds, fraction);
        self.last_handle_rect = handle_rect;

        if cx.pass_kind == crate::layout_pass::LayoutPassKind::Final {
            if !cx.children.is_empty() {
                cx.solve_barrier_child_root_if_needed(cx.children[0], rect_a);
            }
            if cx.children.len() >= 2 {
                cx.solve_barrier_child_root_if_needed(cx.children[1], rect_b);
            }
        }

        if !cx.children.is_empty() {
            let _ = cx.layout_in(cx.children[0], rect_a);
        }
        if cx.children.len() >= 2 {
            let _ = cx.layout_in(cx.children[1], rect_b);
        }

        cx.available
    }

    fn paint(&mut self, cx: &mut PaintCx<'_, H>) {
        for &child in cx.children.iter().take(2) {
            if let Some(bounds) = cx.child_bounds(child) {
                cx.paint(child, bounds);
            } else {
                cx.paint(child, cx.bounds);
            }
        }

        // Minimal handle affordance (debug-friendly): a thin quad centered in the handle rect.
        if self.hit_thickness.0 > 0.0 {
            let rect = self.last_handle_rect;
            let thickness = Px((self.paint_device_px.max(1.0)).recip().max(1.0));
            let line = match self.axis {
                Axis::Horizontal => Rect::new(
                    Point::new(
                        Px(rect.origin.x.0 + rect.size.width.0 * 0.5 - thickness.0 * 0.5),
                        rect.origin.y,
                    ),
                    Size::new(thickness, rect.size.height),
                ),
                Axis::Vertical => Rect::new(
                    Point::new(
                        rect.origin.x,
                        Px(rect.origin.y.0 + rect.size.height.0 * 0.5 - thickness.0 * 0.5),
                    ),
                    Size::new(rect.size.width, thickness),
                ),
            };

            cx.scene.push(fret_core::SceneOp::Quad {
                order: DrawOrder(10_000),
                rect: line,
                background: Paint::Solid(Color {
                    r: 1.0,
                    g: 1.0,
                    b: 1.0,
                    a: if self.dragging.is_some() { 0.35 } else { 0.18 },
                })
                .into(),
                border: Edges::all(Px(0.0)),
                border_paint: Paint::Solid(Color::TRANSPARENT).into(),
                corner_radii: Corners::all(Px(0.0)),
            });
        }
    }
}