Skip to main content

repose_ui/
selection.rs

1//! Selectable text. `SelectableText` mirrors Compose's `SelectionContainer`
2//! for a single `Text` view: drag to select a byte range, and a highlight
3//! rect is painted on top of the glyphs. Single-line selection is
4//! pixel-accurate; multi-line tracks the range but the highlight is drawn
5//! per-line.
6
7use std::cell::RefCell;
8use std::rc::Rc;
9
10use repose_core::prelude::*;
11use repose_core::{Brush, PointerEvent, Rect, Scene, SceneNode, View};
12
13use crate::Text;
14use crate::textfield::{caret_xy_for_byte, index_for_xy_bytes};
15
16pub fn SelectableText(
17    text: impl Into<String>,
18    font_size_dp: f32,
19    on_selection_change: impl Fn(Option<(usize, usize)>) + 'static,
20) -> View {
21    let text: String = text.into();
22    let text_for_handlers = text.clone();
23    let text_for_paint = text.clone();
24
25    let selection: Rc<RefCell<Option<(usize, usize)>>> =
26        remember_with_key("sel:range", || RefCell::new(None));
27    let anchor: Rc<RefCell<usize>> = remember_with_key("sel:anchor", || RefCell::new(0));
28    let dragging: Rc<RefCell<bool>> = remember_with_key("sel:dragging", || RefCell::new(false));
29    let last_rect: Rc<RefCell<Rect>> =
30        remember_with_key("sel:last_rect", || RefCell::new(Rect::default()));
31
32    let callback = Rc::new(on_selection_change);
33
34    let cb_down = callback.clone();
35    let on_down = {
36        let text = text_for_handlers.clone();
37        let selection = selection.clone();
38        let anchor = anchor.clone();
39        let dragging = dragging.clone();
40        let last_rect = last_rect.clone();
41        move |ev: PointerEvent| {
42            let r = *last_rect.borrow();
43            if r.w <= 0.0 || r.h <= 0.0 {
44                return;
45            }
46            let font_px = dp_to_px(font_size_dp) * text_scale().0;
47            let lx = (ev.position.x - r.x).max(0.0);
48            let ly = (ev.position.y - r.y).max(0.0);
49            let wrap_w = r.w.max(1.0);
50            let byte = index_for_xy_bytes(&text, font_px, wrap_w, lx, ly);
51            *anchor.borrow_mut() = byte;
52            *selection.borrow_mut() = Some((byte, byte));
53            *dragging.borrow_mut() = true;
54            cb_down(Some((byte, byte)));
55        }
56    };
57
58    let cb_move = callback.clone();
59    let on_move = {
60        let text = text_for_handlers.clone();
61        let selection = selection.clone();
62        let anchor = anchor.clone();
63        let dragging = dragging.clone();
64        let last_rect = last_rect.clone();
65        move |ev: PointerEvent| {
66            if !*dragging.borrow() {
67                return;
68            }
69            let r = *last_rect.borrow();
70            if r.w <= 0.0 || r.h <= 0.0 {
71                return;
72            }
73            let font_px = dp_to_px(font_size_dp) * text_scale().0;
74            let lx = (ev.position.x - r.x).max(0.0);
75            let ly = (ev.position.y - r.y).max(0.0);
76            let wrap_w = r.w.max(1.0);
77            let byte = index_for_xy_bytes(&text, font_px, wrap_w, lx, ly);
78            let a = *anchor.borrow();
79            *selection.borrow_mut() = Some((a, byte));
80            cb_move(Some((a, byte)));
81        }
82    };
83
84    let cb_up = callback.clone();
85    let on_up = {
86        let selection = selection.clone();
87        let dragging = dragging.clone();
88        move |_ev: PointerEvent| {
89            *dragging.borrow_mut() = false;
90            cb_up(*selection.borrow());
91        }
92    };
93
94    let painter = {
95        let text = text_for_paint.clone();
96        let selection = selection.clone();
97        let last_rect = last_rect.clone();
98        move |scene: &mut Scene, rect: Rect, _alpha: f32| {
99            *last_rect.borrow_mut() = rect;
100
101            let (s, e) = match *selection.borrow() {
102                Some((a, b)) if a != b => {
103                    if a < b {
104                        (a, b)
105                    } else {
106                        (b, a)
107                    }
108                }
109                _ => return,
110            };
111            if e == 0 || e <= s {
112                return;
113            }
114
115            let font_px = dp_to_px(font_size_dp) * text_scale().0;
116            let wrap_w = rect.w.max(1.0);
117            let (sx, sy, sli) = caret_xy_for_byte(&text, font_px, wrap_w, s);
118            let (ex, ey, eli) = caret_xy_for_byte(&text, font_px, wrap_w, e);
119            let th = theme();
120            let brush = Brush::Solid(th.primary.with_alpha(96));
121            let line_h = font_px * 1.2;
122
123            if sli == eli {
124                let x = sx.min(ex);
125                let w = (ex - sx).abs().max(2.0);
126                scene.nodes.push(SceneNode::Rect {
127                    rect: Rect {
128                        x: rect.x + x,
129                        y: rect.y + sy,
130                        w,
131                        h: line_h,
132                    },
133                    brush,
134                    radius: 0.0,
135                });
136            } else {
137                scene.nodes.push(SceneNode::Rect {
138                    rect: Rect {
139                        x: rect.x + sx,
140                        y: rect.y + sy,
141                        w: (rect.w - sx).max(2.0),
142                        h: line_h,
143                    },
144                    brush: brush,
145                    radius: 0.0,
146                });
147                if eli > sli + 1 {
148                    scene.nodes.push(SceneNode::Rect {
149                        rect: Rect {
150                            x: rect.x,
151                            y: rect.y + (sli as f32 + 1.0) * line_h,
152                            w: rect.w,
153                            h: (eli as f32 - sli as f32 - 1.0) * line_h,
154                        },
155                        brush: brush,
156                        radius: 0.0,
157                    });
158                }
159                scene.nodes.push(SceneNode::Rect {
160                    rect: Rect {
161                        x: rect.x,
162                        y: rect.y + ey,
163                        w: ex.max(2.0),
164                        h: line_h,
165                    },
166                    brush,
167                    radius: 0.0,
168                });
169            }
170        }
171    };
172
173    let mut v = Text(text_for_paint);
174    v.modifier = v
175        .modifier
176        .on_pointer_down(on_down)
177        .on_pointer_move(on_move)
178        .on_pointer_up(on_up)
179        .painter(painter);
180    v
181}