repose-platform 0.16.0

Platform runners (winit Desktop, Android and Web)
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
use crate::*;
use repose_core::input::{PointerButton, PointerEvent, PointerEventKind, PointerId, PointerKind};
use repose_core::locals::dp_to_px;
use repose_ui::TextFieldState;
use repose_ui::textfield::{
    TF_FONT_DP, TF_PADDING_X_DP, caret_xy_for_byte, index_for_x_bytes, index_for_xy_bytes,
    measure_text,
};

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

/// Find the top-most hit region index under `pos` (reverse iteration).
pub(crate) fn top_hit_index(frame: &Frame, pos: Vec2) -> Option<usize> {
    frame
        .hit_regions
        .iter()
        .enumerate()
        .rev()
        .find(|(_, h)| h.rect.contains(pos))
        .map(|(i, _)| i)
}

pub(crate) fn hit_index_by_id(frame: &Frame, id: u64) -> Option<usize> {
    frame.hit_regions.iter().position(|h| h.id == id)
}

pub(crate) fn tf_key_of(frame: &Frame, visual_id: u64) -> u64 {
    if let Some(i) = hit_index_by_id(frame, visual_id) {
        let hr = &frame.hit_regions[i];
        return hr.tf_state_key.unwrap_or(hr.id);
    }
    visual_id
}

pub(crate) fn pe_mouse(event: PointerEventKind, pos: Vec2, mods: Modifiers) -> PointerEvent {
    PointerEvent {
        id: PointerId(0),
        kind: PointerKind::Mouse,
        event,
        position: pos,
        pressure: 1.0,
        modifiers: mods,
    }
}

pub(crate) fn pe_touch(event: PointerEventKind, pos: Vec2, mods: Modifiers) -> PointerEvent {
    PointerEvent {
        id: PointerId(0),
        kind: PointerKind::Touch,
        event,
        position: pos,
        pressure: 1.0,
        modifiers: mods,
    }
}

pub(crate) fn pe_down_primary(kind: PointerKind, pos: Vec2, mods: Modifiers) -> PointerEvent {
    PointerEvent {
        id: PointerId(0),
        kind,
        event: PointerEventKind::Down(PointerButton::Primary),
        position: pos,
        pressure: 1.0,
        modifiers: mods,
    }
}

pub(crate) fn pe_up_primary(kind: PointerKind, pos: Vec2, mods: Modifiers) -> PointerEvent {
    PointerEvent {
        id: PointerId(0),
        kind,
        event: PointerEventKind::Up(PointerButton::Primary),
        position: pos,
        pressure: 1.0,
        modifiers: mods,
    }
}

pub(crate) fn map_key(key: winit::keyboard::PhysicalKey) -> repose_core::input::Key {
    use repose_core::input::Key;
    use winit::keyboard::{KeyCode, PhysicalKey};

    match key {
        PhysicalKey::Code(KeyCode::Enter) => Key::Enter,
        PhysicalKey::Code(KeyCode::Tab) => Key::Tab,
        PhysicalKey::Code(KeyCode::Backspace) => Key::Backspace,
        PhysicalKey::Code(KeyCode::Delete) => Key::Delete,
        PhysicalKey::Code(KeyCode::Escape) => Key::Escape,
        PhysicalKey::Code(KeyCode::ArrowLeft) => Key::ArrowLeft,
        PhysicalKey::Code(KeyCode::ArrowRight) => Key::ArrowRight,
        PhysicalKey::Code(KeyCode::ArrowUp) => Key::ArrowUp,
        PhysicalKey::Code(KeyCode::ArrowDown) => Key::ArrowDown,
        PhysicalKey::Code(KeyCode::Home) => Key::Home,
        PhysicalKey::Code(KeyCode::End) => Key::End,
        PhysicalKey::Code(KeyCode::PageUp) => Key::PageUp,
        PhysicalKey::Code(KeyCode::PageDown) => Key::PageDown,
        PhysicalKey::Code(KeyCode::Space) => Key::Space,
        PhysicalKey::Code(KeyCode::KeyA) => Key::Character('a'),
        PhysicalKey::Code(KeyCode::KeyB) => Key::Character('b'),
        PhysicalKey::Code(KeyCode::KeyC) => Key::Character('c'),
        PhysicalKey::Code(KeyCode::KeyD) => Key::Character('d'),
        PhysicalKey::Code(KeyCode::KeyE) => Key::Character('e'),
        PhysicalKey::Code(KeyCode::KeyF) => Key::Character('f'),
        PhysicalKey::Code(KeyCode::KeyG) => Key::Character('g'),
        PhysicalKey::Code(KeyCode::KeyH) => Key::Character('h'),
        PhysicalKey::Code(KeyCode::KeyI) => Key::Character('i'),
        PhysicalKey::Code(KeyCode::KeyJ) => Key::Character('j'),
        PhysicalKey::Code(KeyCode::KeyK) => Key::Character('k'),
        PhysicalKey::Code(KeyCode::KeyL) => Key::Character('l'),
        PhysicalKey::Code(KeyCode::KeyM) => Key::Character('m'),
        PhysicalKey::Code(KeyCode::KeyN) => Key::Character('n'),
        PhysicalKey::Code(KeyCode::KeyO) => Key::Character('o'),
        PhysicalKey::Code(KeyCode::KeyP) => Key::Character('p'),
        PhysicalKey::Code(KeyCode::KeyQ) => Key::Character('q'),
        PhysicalKey::Code(KeyCode::KeyR) => Key::Character('r'),
        PhysicalKey::Code(KeyCode::KeyS) => Key::Character('s'),
        PhysicalKey::Code(KeyCode::KeyT) => Key::Character('t'),
        PhysicalKey::Code(KeyCode::KeyU) => Key::Character('u'),
        PhysicalKey::Code(KeyCode::KeyV) => Key::Character('v'),
        PhysicalKey::Code(KeyCode::KeyW) => Key::Character('w'),
        PhysicalKey::Code(KeyCode::KeyX) => Key::Character('x'),
        PhysicalKey::Code(KeyCode::KeyY) => Key::Character('y'),
        PhysicalKey::Code(KeyCode::KeyZ) => Key::Character('z'),
        PhysicalKey::Code(KeyCode::Digit0) => Key::Character('0'),
        PhysicalKey::Code(KeyCode::Digit1) => Key::Character('1'),
        PhysicalKey::Code(KeyCode::Digit2) => Key::Character('2'),
        PhysicalKey::Code(KeyCode::Digit3) => Key::Character('3'),
        PhysicalKey::Code(KeyCode::Digit4) => Key::Character('4'),
        PhysicalKey::Code(KeyCode::Digit5) => Key::Character('5'),
        PhysicalKey::Code(KeyCode::Digit6) => Key::Character('6'),
        PhysicalKey::Code(KeyCode::Digit7) => Key::Character('7'),
        PhysicalKey::Code(KeyCode::Digit8) => Key::Character('8'),
        PhysicalKey::Code(KeyCode::Digit9) => Key::Character('9'),
        PhysicalKey::Code(KeyCode::F1) => Key::F(1),
        PhysicalKey::Code(KeyCode::F2) => Key::F(2),
        PhysicalKey::Code(KeyCode::F3) => Key::F(3),
        PhysicalKey::Code(KeyCode::F4) => Key::F(4),
        PhysicalKey::Code(KeyCode::F5) => Key::F(5),
        PhysicalKey::Code(KeyCode::F6) => Key::F(6),
        PhysicalKey::Code(KeyCode::F7) => Key::F(7),
        PhysicalKey::Code(KeyCode::F8) => Key::F(8),
        PhysicalKey::Code(KeyCode::F9) => Key::F(9),
        PhysicalKey::Code(KeyCode::F10) => Key::F(10),
        PhysicalKey::Code(KeyCode::F11) => Key::F(11),
        PhysicalKey::Code(KeyCode::F12) => Key::F(12),
        _ => Key::Unknown,
    }
}

pub(crate) fn tf_ensure_caret_visible(state: &mut TextFieldState, is_multiline: bool) {
    let font_px = dp_to_px(TF_FONT_DP) * repose_core::locals::text_scale().0;
    let wrap_width = state.inner_width;

    if is_multiline {
        let (cx, cy, _) = caret_xy_for_byte(&state.text, font_px, wrap_width, state.caret_index());
        let iw = state.inner_width;
        let ih = state.inner_height;
        state.ensure_caret_visible_xy(cx, cy, iw, ih, dp_to_px(2.0));
    } else {
        let m = measure_text(&state.text, font_px, None);
        let caret_x_px = m.positions.get(state.caret_index()).copied().unwrap_or(0.0);
        state.ensure_caret_visible(caret_x_px, wrap_width, dp_to_px(2.0));
    }
}

/// Place caret in textfield at pointer position and begin drag selection.
/// Handles both single-line and multiline textfields.
/// `pos_px`: absolute pointer position in pixels
/// `scale`: display scale factor
/// `shift`: whether shift key is held (extends selection)
pub(crate) fn tf_place_caret_at_pointer(
    state: &mut TextFieldState,
    hit_rect: Rect,
    is_multiline: bool,
    pos_px: (f32, f32),
    scale: f32,
    shift: bool,
) {
    let padding_px = TF_PADDING_X_DP * scale;
    let inner_x_px = hit_rect.x + padding_px;
    let inner_y_px = hit_rect.y + 8.0 * scale;
    let content_x_px = (pos_px.0 - inner_x_px + state.scroll_offset).max(0.0);
    let content_y_px = (pos_px.1 - inner_y_px + state.scroll_offset_y).max(0.0);
    let font_px = dp_to_px(TF_FONT_DP) * repose_core::locals::text_scale().0;

    let idx = if is_multiline {
        index_for_xy_bytes(
            &state.text,
            font_px,
            hit_rect.w - 2.0 * padding_px,
            content_x_px,
            content_y_px,
        )
    } else {
        index_for_x_bytes(&state.text, font_px, content_x_px)
    };
    state.begin_drag(idx, shift);

    // Ensure caret visible
    let caret_idx = state.caret_index();
    let wrap_w = hit_rect.w - 2.0 * padding_px;
    if is_multiline {
        let (cx, cy, _) = caret_xy_for_byte(&state.text, font_px, wrap_w, caret_idx);
        let iw = state.inner_width;
        let ih = state.inner_height;
        state.ensure_caret_visible_xy(cx, cy, iw, ih, 2.0 * scale);
    } else {
        let m = measure_text(&state.text, font_px, None);
        let cx = m.positions.get(caret_idx).copied().unwrap_or(0.0);
        state.ensure_caret_visible(cx, wrap_w, 2.0 * scale);
    }
}

pub(crate) fn touch_slop_px(scale: f32) -> f32 {
    6.0 * scale
}

pub(crate) fn focus_next_in_chain(chain: &[u64], current: Option<u64>, shift: bool) -> Option<u64> {
    if chain.is_empty() {
        return None;
    }

    let next = if let Some(cur) = current {
        if let Some(idx) = chain.iter().position(|&id| id == cur) {
            if shift {
                if idx == 0 {
                    chain[chain.len() - 1]
                } else {
                    chain[idx - 1]
                }
            } else {
                chain[(idx + 1) % chain.len()]
            }
        } else {
            chain[0]
        }
    } else {
        chain[0]
    };

    Some(next)
}

/// Find the next focusable element in a given spatial direction.
///
/// Uses the rects from `hit_regions` to determine which element is "next" in the
/// given direction from the currently focused element. Implements a simple weighted
/// closest-element heuristic.
pub(crate) fn focus_in_direction(
    chain: &[u64],
    hit_regions: &[HitRegion],
    current: Option<u64>,
    dir: FocusDirection,
) -> Option<u64> {
    if chain.is_empty() {
        return None;
    }

    let current_rect = current.and_then(|id| {
        hit_regions.iter().find(|h| h.id == id).map(|h| h.rect)
    });

    let current_center = current_rect.map(|r| ((r.x + r.w / 2.0), (r.y + r.h / 2.0)));

    // For Next/Previous, use tab-order navigation
    match dir {
        FocusDirection::Next | FocusDirection::Previous => {
            return focus_next_in_chain(chain, current, dir == FocusDirection::Previous);
        }
        _ => {}
    }

    let (cx, cy) = match current_center {
        Some(c) => c,
        None => return chain.first().copied(),
    };

    let mut best: Option<(u64, f32)> = None;

    for &id in chain {
        if Some(id) == current {
            continue;
        }
        let Some(hr) = hit_regions.iter().find(|h| h.id == id) else {
            continue;
        };
        let r = hr.rect;
        let other_cx = r.x + r.w / 2.0;
        let other_cy = r.y + r.h / 2.0;
        let dx = other_cx - cx;
        let dy = other_cy - cy;

        let in_direction = match dir {
            FocusDirection::Left => dx < 0.0 && dy.abs() <= r.h.max(1.0),
            FocusDirection::Right => dx > 0.0 && dy.abs() <= r.h.max(1.0),
            FocusDirection::Up => dy < 0.0 && dx.abs() <= r.w.max(1.0),
            FocusDirection::Down => dy > 0.0 && dx.abs() <= r.w.max(1.0),
            _ => false,
        };

        if !in_direction {
            continue;
        }

        let dist = dx * dx + dy * dy;
        let weight = dist / (r.w * r.h + 1.0).max(1.0);

        match best {
            Some((_, best_weight)) if weight >= best_weight => {}
            _ => best = Some((id, weight)),
        }
    }

    best.map(|(id, _)| id)
}

pub(crate) fn is_dnd_target(hit: &HitRegion) -> bool {
    hit.on_drop.is_some()
        || hit.on_drag_enter.is_some()
        || hit.on_drag_over.is_some()
        || hit.on_drag_leave.is_some()
}

pub(crate) fn dnd_target_id_at(frame: &Frame, pos: Vec2) -> Option<u64> {
    frame
        .hit_regions
        .iter()
        .rev()
        .filter(|h| h.rect.contains(pos))
        .find(|h| is_dnd_target(h))
        .map(|h| h.id)
}

/// Dispatch wheel/touch-scroll to the top-most scroll consumer under `pos`.
/// Returns `true` if something consumed the scroll.
pub(crate) fn dispatch_scroll(frame: &Frame, pos: Vec2, delta: Vec2) -> bool {
    for hit in frame
        .hit_regions
        .iter()
        .rev()
        .filter(|h| h.rect.contains(pos))
    {
        if let Some(cb) = &hit.on_scroll {
            let before = delta;
            let leftover = cb(before);
            let consumed_x = (before.x - leftover.x).abs() > 0.001;
            let consumed_y = (before.y - leftover.y).abs() > 0.001;
            if consumed_x || consumed_y {
                return true;
            }
        }
    }
    false
}

/// Shared state for runner-provided "auto root scroll".
#[derive(Default)]
pub(crate) struct RootScrollState {
    pub viewport_h: f32,
    pub content_h: f32,
    pub offset_y: f32,
}

impl RootScrollState {
    #[inline]
    pub fn max_offset(&self) -> f32 {
        (self.content_h - self.viewport_h).max(0.0)
    }
}

/// Wrap any `child` view in a vertical scroll container backed by `RootScrollState`.
/// This is how the runner can provide "overflow-y: auto" semantics for the whole app.
pub(crate) fn wrap_root_scroll(child: View, st: Rc<RefCell<RootScrollState>>) -> View {
    let st_get = st.clone();
    let get_scroll_offset = Some(Rc::new(move || st_get.borrow().offset_y) as Rc<dyn Fn() -> f32>);

    let st_set = st.clone();
    let set_scroll_offset = Some(Rc::new(move |y: f32| {
        let mut s = st_set.borrow_mut();
        s.offset_y = y.clamp(0.0, s.max_offset());
    }) as Rc<dyn Fn(f32)>);

    let st_vp = st.clone();
    let set_viewport_height = Some(Rc::new(move |h: f32| {
        let mut s = st_vp.borrow_mut();
        s.viewport_h = h.max(0.0);
        s.offset_y = s.offset_y.clamp(0.0, s.max_offset());
    }) as Rc<dyn Fn(f32)>);

    let st_ch = st.clone();
    let set_content_height = Some(Rc::new(move |h: f32| {
        let mut s = st_ch.borrow_mut();
        s.content_h = h.max(0.0);
        s.offset_y = s.offset_y.clamp(0.0, s.max_offset());
    }) as Rc<dyn Fn(f32)>);

    let st_scroll = st.clone();
    let on_scroll = Some(Rc::new(move |delta: Vec2| -> Vec2 {
        let mut s = st_scroll.borrow_mut();
        let max_off = s.max_offset();
        if max_off <= 0.5 {
            return delta; // nothing to consume
        }

        let before = s.offset_y;
        let target = (s.offset_y - delta.y).clamp(0.0, max_off);
        s.offset_y = target;

        let consumed = before - target;
        Vec2 {
            x: delta.x,
            y: delta.y - consumed, // leftover
        }
    }) as Rc<dyn Fn(Vec2) -> Vec2>);

    View::new(
        0,
        ViewKind::ScrollV {
            on_scroll,
            set_viewport_height,
            set_content_height,
            get_scroll_offset,
            set_scroll_offset,
            show_scrollbar: true,
        },
    )
    .modifier(Modifier::new().fill_max_size())
    .with_children(vec![child])
}