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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Scrollable and selectable label

use super::{ScrollBar, ScrollMsg};
use kas::event::components::{TextInput, TextInputAction};
use kas::event::{Command, CursorIcon, FocusSource, Scroll, ScrollDelta};
use kas::geom::Vec2;
use kas::prelude::*;
use kas::text::format::{EditableText, FormattableText};
use kas::text::{SelectionHelper, Text};
use kas::theme::TextClass;

impl_scope! {
    /// A static text label supporting scrolling and selection
    ///
    /// Line-wrapping is enabled; default alignment is derived from the script
    /// (usually top-left).
    #[derive(Clone, Default, Debug)]
    #[widget{
        cursor_icon = CursorIcon::Text;
    }]
    pub struct ScrollLabel<T: FormattableText + 'static> {
        core: widget_core!(),
        view_offset: Offset,
        text: Text<T>,
        text_size: Size,
        selection: SelectionHelper,
        has_sel_focus: bool,
        input_handler: TextInput,
        #[widget]
        bar: ScrollBar<kas::dir::Down>,
    }

    impl Layout for Self {
        fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
            let class = TextClass::LabelScroll;
            let mut rules = sizer.text_rules(&mut self.text, class, axis);
            let _ = self.bar.size_rules(sizer.re(), axis);
            if axis.is_vertical() {
                rules.reduce_min_to(sizer.line_height(class) * 4);
            }
            rules
        }

        fn set_rect(&mut self, cx: &mut ConfigCx, mut rect: Rect) {
            self.core.rect = rect;
            cx.text_set_size(&mut self.text, TextClass::LabelScroll, rect.size, None);
            self.text_size = Vec2::from(self.text.bounding_box().unwrap().1).cast_ceil();

            let max_offset = self.max_scroll_offset();
            self.view_offset = self.view_offset.min(max_offset);

            let w = cx.size_cx().scroll_bar_width().min(rect.size.0);
            rect.pos.0 += rect.size.0 - w;
            rect.size.0 = w;
            self.bar.set_rect(cx, rect);
            let _ = self.bar.set_limits(max_offset.1, rect.size.1);
            self.bar.set_value(cx, self.view_offset.1);
        }

        fn find_id(&mut self, coord: Coord) -> Option<Id> {
            if !self.rect().contains(coord) {
                return None;
            }

            self.bar.find_id(coord).or_else(|| Some(self.id()))
        }

        fn draw(&mut self, mut draw: DrawCx) {
            let class = TextClass::LabelScroll;
            let rect = Rect::new(self.rect().pos, self.text_size);
            draw.with_clip_region(self.rect(), self.view_offset, |mut draw| {
                if self.selection.is_empty() {
                    draw.text(rect, &self.text, class);
                } else {
                    // TODO(opt): we could cache the selection rectangles here to make
                    // drawing more efficient (self.text.highlight_lines(range) output).
                    // The same applies to the edit marker below.
                    draw.text_selected(rect, &self.text, self.selection.range(), class);
                }
            });
            draw.with_pass(|mut draw| {
                draw.recurse(&mut self.bar);
            });
        }
    }

    impl Self {
        /// Construct an `ScrollLabel` with the given inital `text`
        #[inline]
        pub fn new(text: T) -> Self {
            ScrollLabel {
                core: Default::default(),
                view_offset: Default::default(),
                text: Text::new(text),
                text_size: Size::ZERO,
                selection: SelectionHelper::new(0, 0),
                has_sel_focus: false,
                input_handler: Default::default(),
                bar: ScrollBar::new().with_invisible(true),
            }
        }

        /// Set text in an existing `Label`
        ///
        /// Note: this must not be called before fonts have been initialised
        /// (usually done by the theme when the main loop starts).
        pub fn set_text(&mut self, text: T) -> Action {
            self.text
                .set_and_try_prepare(text)
                .expect("invalid font_id");

            self.text_size = Vec2::from(self.text.bounding_box().unwrap().1).cast_ceil();
            let max_offset = self.max_scroll_offset();
            let _ = self.bar.set_limits(max_offset.1, self.rect().size.1);
            self.view_offset = self.view_offset.min(max_offset);

            self.selection.set_max_len(self.text.str_len());

            Action::REDRAW
        }

        fn set_edit_pos_from_coord(&mut self, cx: &mut EventCx, coord: Coord) {
            let rel_pos = (coord - self.rect().pos + self.view_offset).cast();
            if let Ok(pos) = self.text.text_index_nearest(rel_pos) {
                if pos != self.selection.edit_pos() {
                    self.selection.set_edit_pos(pos);
                    self.set_view_offset_from_edit_pos(cx, pos);
                    self.bar.set_value(cx, self.view_offset.1);
                    cx.redraw(self);
                }
            }
        }

        fn set_primary(&self, cx: &mut EventCx) {
            if self.has_sel_focus && !self.selection.is_empty() && cx.has_primary() {
                let range = self.selection.range();
                cx.set_primary(String::from(&self.text.as_str()[range]));
            }
        }

        // Pan by given delta.
        fn pan_delta(&mut self, cx: &mut EventCx, mut delta: Offset) -> IsUsed {
            let new_offset = (self.view_offset - delta)
                .min(self.max_scroll_offset())
                .max(Offset::ZERO);
            if new_offset != self.view_offset {
                delta -= self.view_offset - new_offset;
                self.set_offset(cx, new_offset);
            }

            cx.set_scroll(if delta == Offset::ZERO {
                Scroll::Scrolled
            } else {
                Scroll::Offset(delta)
            });
            Used
        }

        /// Update view_offset from edit_pos
        ///
        /// This method is mostly identical to its counterpart in `EditField`.
        fn set_view_offset_from_edit_pos(&mut self, cx: &mut EventCx, edit_pos: usize) {
            if let Some(marker) = self
                .text
                .text_glyph_pos(edit_pos)
                .ok()
                .and_then(|mut m| m.next_back())
            {
                let bounds = Vec2::from(self.text.env().bounds);
                let min_x = marker.pos.0 - bounds.0;
                let min_y = marker.pos.1 - marker.descent - bounds.1;
                let max_x = marker.pos.0;
                let max_y = marker.pos.1 - marker.ascent;
                let min = Offset(min_x.cast_ceil(), min_y.cast_ceil());
                let max = Offset(max_x.cast_floor(), max_y.cast_floor());

                let max = max.min(self.max_scroll_offset());

                let new_offset = self.view_offset.max(min).min(max);
                if new_offset != self.view_offset {
                    self.view_offset = new_offset;
                    cx.set_scroll(Scroll::Scrolled);
                }
            }
        }

        /// Set offset, updating the scroll bar
        fn set_offset(&mut self, cx: &mut EventState, offset: Offset) {
            self.view_offset = offset;
            // unnecessary: cx.redraw(self);
            self.bar.set_value(cx, offset.1);
        }
    }

    impl HasStr for Self {
        fn get_str(&self) -> &str {
            self.text.as_str()
        }
    }

    impl HasString for Self
    where
        T: EditableText,
    {
        fn set_string(&mut self, string: String) -> Action {
            self.text.set_string(string);
            let _ = self.text.try_prepare();
            Action::REDRAW
        }
    }

    impl Events for Self {
        type Data = ();

        fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
            match event {
                Event::Command(cmd, _) => match cmd {
                    Command::Escape | Command::Deselect if !self.selection.is_empty() => {
                        self.selection.set_empty();
                        cx.redraw(self);
                        Used
                    }
                    Command::SelectAll => {
                        self.selection.set_sel_pos(0);
                        self.selection.set_edit_pos(self.text.str_len());
                        self.set_primary(cx);
                        cx.redraw(self);
                        Used
                    }
                    Command::Cut | Command::Copy => {
                        let range = self.selection.range();
                        cx.set_clipboard((self.text.as_str()[range]).to_string());
                        Used
                    }
                    // TODO: scroll by command
                    _ => Unused,
                },
                Event::SelFocus(source) => {
                    self.has_sel_focus = true;
                    if source == FocusSource::Pointer {
                        self.set_primary(cx);
                    }
                    Used
                }
                Event::LostSelFocus => {
                    self.has_sel_focus = false;
                    self.selection.set_empty();
                    cx.redraw(self);
                    Used
                }
                Event::Scroll(delta) => {
                    let delta2 = match delta {
                        ScrollDelta::LineDelta(x, y) => cx.config().scroll_distance((x, y)),
                        ScrollDelta::PixelDelta(coord) => coord,
                    };
                    self.pan_delta(cx, delta2)
                }
                event => match self.input_handler.handle(cx, self.id(), event) {
                    TextInputAction::None => Used,
                    TextInputAction::Unused => Unused,
                    TextInputAction::Pan(delta) => self.pan_delta(cx, delta),
                    TextInputAction::Focus { coord, action } => {
                        if let Some(coord) = coord {
                            self.set_edit_pos_from_coord(cx, coord);
                        }
                        self.selection.action(&self.text, action);

                        if self.has_sel_focus {
                            self.set_primary(cx);
                        } else {
                            cx.request_sel_focus(self.id(), FocusSource::Pointer);
                        }
                        Used
                    }
                },
            }
        }

        fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
            if let Some(ScrollMsg(y)) = cx.try_pop() {
                let y = y.clamp(0, self.max_scroll_offset().1);
                self.view_offset.1 = y;
                cx.redraw(self);
            }
        }
    }

    impl Scrollable for Self {
        fn scroll_axes(&self, size: Size) -> (bool, bool) {
            let max = self.max_scroll_offset();
            (max.0 > size.0, max.1 > size.1)
        }

        fn max_scroll_offset(&self) -> Offset {
            let text_size = Offset::conv(self.text_size);
            let self_size = Offset::conv(self.rect().size);
            (text_size - self_size).max(Offset::ZERO)
        }

        fn scroll_offset(&self) -> Offset {
            self.view_offset
        }

        fn set_scroll_offset(&mut self, cx: &mut EventCx, offset: Offset) -> Offset {
            let new_offset = offset.min(self.max_scroll_offset()).max(Offset::ZERO);
            if new_offset != self.view_offset {
                self.set_offset(cx, new_offset);
                // No widget moves so do not need to report Action::REGION_MOVED
            }
            new_offset
        }
    }
}