mtk-rs 0.1.0-beta.2

Muse Toolkit
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
use std::marker::PhantomData;

use crate::debugger::SourceLocation;
use crate::style::{Rect, Style, TextStyle};
use crate::text::{TextRenderInfo, TextSpan};
use crate::ui::event::EventResult;
use crate::ui::{Event, View};
use crate::{Context, Node};

/// Geometric bounding box information for an interactive text span.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct SpanGeometry {
    /// Bounding rectangle in window/screen coordinates.
    pub rect: Rect,
    /// Bounding rectangle in local coordinates relative to the rich text widget.
    pub local_rect: Rect,
}

impl std::ops::Deref for SpanGeometry {
    type Target = Rect;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.rect
    }
}

/// A widget that displays styled rich text with syntax-highlighted spans and interactive hover/click callbacks.
pub struct RichText<Msg, Id = ()> {
    pub(crate) text: String,
    pub(crate) spans: Vec<TextSpan<Id>>,
    pub(crate) style: Option<Style>,
    pub(crate) text_style: Option<TextStyle>,
    pub(crate) on_span_click: Option<Box<dyn Fn(Id, SpanGeometry) -> Option<Msg>>>,
    pub(crate) on_span_hover: Option<Box<dyn Fn(Id, bool, SpanGeometry) -> Option<Msg>>>,
    pub(crate) source_loc: Option<SourceLocation>,
    _marker: PhantomData<Msg>,
}

/// Creates a new `RichText` widget displaying styled text with support for sub-range styling and span interactivity.
#[track_caller]
pub fn rich_text<S: Into<String>, Msg>(text: S) -> RichText<Msg, ()> {
    RichText {
        text: text.into(),
        spans: Vec::new(),
        style: None,
        text_style: None,
        on_span_click: None,
        on_span_hover: None,
        source_loc: Some(SourceLocation::here("RichText")),
        _marker: PhantomData,
    }
}

impl<Msg> RichText<Msg, ()> {
    /// Sets the list of styled spans over the text buffer, transitioning to an identified span model if IDs are provided.
    pub fn spans<Id: Clone + PartialEq + 'static>(
        self,
        spans: Vec<TextSpan<Id>>,
    ) -> RichText<Msg, Id> {
        RichText {
            text: self.text,
            spans,
            style: self.style,
            text_style: self.text_style,
            on_span_click: None,
            on_span_hover: None,
            source_loc: self.source_loc,
            _marker: PhantomData,
        }
    }
}

impl<Msg, Id: Clone + PartialEq + 'static> RichText<Msg, Id> {
    /// Replaces the list of styled spans with a new set of the same identifier type.
    pub fn set_spans(mut self, spans: Vec<TextSpan<Id>>) -> Self {
        self.spans = spans;
        self
    }

    /// Adds a single styled span over the text buffer.
    pub fn span(mut self, span: TextSpan<Id>) -> Self {
        self.spans.push(span);
        self
    }

    /// Sets the box layout and container style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = Some(style);
        self
    }

    /// Sets the base text style (font size, font family, line height, wrap, etc.).
    pub fn text_style(mut self, style: TextStyle) -> Self {
        self.text_style = Some(style);
        self
    }

    /// Attaches a callback triggered when an identified span is clicked, passing the span ID and geometric `SpanGeometry`.
    pub fn on_span_click<F>(mut self, handler: F) -> Self
    where
        F: Fn(Id, SpanGeometry) -> Option<Msg> + 'static,
    {
        self.on_span_click = Some(Box::new(handler));
        self
    }

    /// Attaches a callback triggered when the mouse enters or leaves an identified span, passing the span ID, hover status, and geometric `SpanGeometry`.
    pub fn on_span_hover<F>(mut self, handler: F) -> Self
    where
        F: Fn(Id, bool, SpanGeometry) -> Option<Msg> + 'static,
    {
        self.on_span_hover = Some(Box::new(handler));
        self
    }
}

pub struct RichTextElement<Id> {
    pub(crate) node: Node,
    pub(crate) current_text: String,
    pub(crate) current_spans: Vec<TextSpan<Id>>,
    pub(crate) hovered_span: Option<Id>,
    pub(crate) hovered_span_geom: Option<SpanGeometry>,
}

fn compute_span_geometry(
    text: &str,
    text_style: &TextStyle,
    inner_w: f32,
    inner_h: f32,
    computed: &crate::Computed,
    padding: &crate::style::Edges,
    range: std::ops::Range<usize>,
    text_cx: &crate::text::SharedTextContext,
    spans: &[TextSpan<()>],
) -> SpanGeometry {
    let local_rects =
        crate::text::get_range_geometry(text, text_style, inner_w, inner_h, range, text_cx, spans);

    let (min_x, min_y, max_x, max_y) = local_rects.iter().fold(
        (
            f32::INFINITY,
            f32::INFINITY,
            f32::NEG_INFINITY,
            f32::NEG_INFINITY,
        ),
        |(mx, my, mx2, my2), r| {
            (
                mx.min(r[0]),
                my.min(r[1]),
                mx2.max(r[0] + r[2]),
                my2.max(r[1] + r[3]),
            )
        },
    );

    let local_rect = if min_x <= max_x {
        Rect {
            x: min_x + padding.left,
            y: min_y + padding.top,
            w: max_x - min_x,
            h: max_y - min_y,
        }
    } else {
        Rect::default()
    };

    let screen_rect = Rect {
        x: computed.x + local_rect.x,
        y: computed.y + local_rect.y,
        w: local_rect.w,
        h: local_rect.h,
    };

    SpanGeometry {
        rect: screen_rect,
        local_rect,
    }
}

impl<State, Msg: 'static, Id: Clone + PartialEq + 'static> View<State> for RichText<Msg, Id> {
    type Message = Msg;
    type Element = RichTextElement<Id>;

    fn build(&self, ctx: &mut Context) -> Self::Element {
        let node = ctx.create_node();
        let untyped_spans: Vec<TextSpan<()>> = self.spans.iter().map(|s| s.to_untyped()).collect();
        let base_style = self.text_style.clone().unwrap_or_default();
        let render_info = TextRenderInfo {
            style: base_style,
            cursor: None,
            selection: None,
            preedit_range: None,
            spans: untyped_spans,
        };
        node.set_text_with_userdata(ctx, &self.text, render_info);

        if let Some(ref style) = self.style {
            style.apply_to_node(ctx, node);
        }

        RichTextElement {
            node,
            current_text: self.text.clone(),
            current_spans: self.spans.clone(),
            hovered_span: None,
            hovered_span_geom: None,
        }
    }

    fn rebuild(&self, _prev: &Self, ctx: &mut Context, element: &mut Self::Element) {
        let text_changed = element.current_text != self.text;
        let spans_changed = element.current_spans != self.spans;

        if text_changed {
            element.current_text = self.text.clone();
        }

        if spans_changed || self.text_style.is_some() || text_changed {
            let untyped_spans: Vec<TextSpan<()>> =
                self.spans.iter().map(|s| s.to_untyped()).collect();
            let base_style = self.text_style.clone().unwrap_or_else(|| {
                element
                    .node
                    .get_text_userdata::<TextRenderInfo>(ctx)
                    .map(|i| i.style.clone())
                    .unwrap_or_default()
            });

            let render_info = TextRenderInfo {
                style: base_style,
                cursor: None,
                selection: None,
                preedit_range: None,
                spans: untyped_spans,
            };
            element
                .node
                .set_text_with_userdata(ctx, &element.current_text, render_info);
            element.current_spans = self.spans.clone();
        }

        if text_changed || spans_changed {
            element.node.set_dirty(ctx);
        }

        if let Some(ref style) = self.style {
            style.apply_to_node(ctx, element.node);
        }
    }

    fn teardown(&self, _ctx: &mut Context, _element: &mut Self::Element) {}

    fn get_node(&self, element: &Self::Element) -> Node {
        element.node
    }

    fn handle_event(
        &self,
        element: &mut Self::Element,
        _state: &State,
        event: Event,
        ctx: &mut Context,
    ) -> (EventResult, Option<Self::Message>) {
        match event {
            Event::CursorMoved {
                x, y, hit_nodes, ..
            } => {
                let is_hit = hit_nodes.contains(&element.node);
                if is_hit {
                    if let Some(computed) = element.node.get_computed(ctx) {
                        let constraints = element.node.get_constraints(ctx).unwrap_or_default();
                        let rel_x =
                            x - computed.x - constraints.padding.left + constraints.scroll.x;
                        let rel_y = y - computed.y - constraints.padding.top + constraints.scroll.y;
                        let inner_w =
                            (computed.w - constraints.padding.left - constraints.padding.right)
                                .max(0.0);
                        let inner_h =
                            (computed.h - constraints.padding.top - constraints.padding.bottom)
                                .max(0.0);

                        let info = element.node.get_text_userdata::<TextRenderInfo>(ctx);
                        let text_style = info.map(|i| &i.style).cloned().unwrap_or_default();
                        let spans = info.map(|i| &i.spans[..]).unwrap_or(&[]);

                        let offset = crate::text::hit_test_text(
                            &element.current_text,
                            &text_style,
                            inner_w,
                            inner_h,
                            rel_x,
                            rel_y,
                            &ctx.text_context,
                            spans,
                        );

                        let matched_span = element
                            .current_spans
                            .iter()
                            .find(|s| s.range.contains(&offset));
                        let new_hover_id = matched_span.and_then(|s| s.id.clone());

                        if new_hover_id != element.hovered_span {
                            let old_id = element.hovered_span.take();
                            let old_geom = element.hovered_span_geom.take().unwrap_or_default();

                            let new_geom = matched_span.map(|s| {
                                compute_span_geometry(
                                    &element.current_text,
                                    &text_style,
                                    inner_w,
                                    inner_h,
                                    &computed,
                                    &constraints.padding,
                                    s.range.clone(),
                                    &ctx.text_context,
                                    spans,
                                )
                            });

                            element.hovered_span = new_hover_id.clone();
                            element.hovered_span_geom = new_geom;

                            if let Some(ref on_hover) = self.on_span_hover {
                                if let Some(old_id) = old_id {
                                    if let Some(msg) = on_hover(old_id, false, old_geom) {
                                        return (EventResult::Handled, Some(msg));
                                    }
                                }
                                if let (Some(new_id), Some(geom)) = (new_hover_id, new_geom) {
                                    if let Some(msg) = on_hover(new_id, true, geom) {
                                        return (EventResult::Handled, Some(msg));
                                    }
                                }
                            }
                        }
                    }
                } else if element.hovered_span.is_some() {
                    let old_id = element.hovered_span.take().unwrap();
                    let old_geom = element.hovered_span_geom.take().unwrap_or_default();
                    if let Some(ref on_hover) = self.on_span_hover {
                        if let Some(msg) = on_hover(old_id, false, old_geom) {
                            return (EventResult::Handled, Some(msg));
                        }
                    }
                }
                (EventResult::Ignored, None)
            }
            Event::MouseInput {
                pressed: false,
                hit_nodes,
                ..
            } => {
                if hit_nodes.contains(&element.node) {
                    if let (Some(id), Some(geom)) =
                        (&element.hovered_span, element.hovered_span_geom)
                    {
                        if let Some(ref on_click) = self.on_span_click {
                            if let Some(msg) = on_click(id.clone(), geom) {
                                return (EventResult::Handled, Some(msg));
                            }
                        }
                    }
                }
                (EventResult::Ignored, None)
            }
            _ => (EventResult::Ignored, None),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::clr;

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum Token {
        Keyword,
        FunctionName,
    }

    #[derive(Clone, Debug, PartialEq)]
    enum TestMsg {
        Hovered(Token, bool, SpanGeometry),
        Clicked(Token, SpanGeometry),
    }

    #[test]
    fn test_rich_text_lifecycle_and_spans() {
        let mut ctx = Context::new();
        let widget = rich_text("fn calculate()")
            .spans(vec![
                TextSpan::new(0..2).color(clr!(blue)).id(Token::Keyword),
                TextSpan::new(3..12).bold().id(Token::FunctionName),
            ])
            .on_span_hover(|id, h, geom| Some(TestMsg::Hovered(id, h, geom)))
            .on_span_click(|id, geom| Some(TestMsg::Clicked(id, geom)));

        let mut element = View::<()>::build(&widget, &mut ctx);

        let info = element
            .node
            .get_text_userdata::<TextRenderInfo>(&ctx)
            .unwrap();
        assert_eq!(info.spans.len(), 2);
        assert_eq!(info.spans[0].range, 0..2);
        assert_eq!(info.spans[1].range, 3..12);

        // Rebuild with updated spans
        let updated_widget = rich_text("fn calculate()").spans(vec![
            TextSpan::new(0..2).color(clr!(red)).id(Token::Keyword),
        ]);
        View::<()>::rebuild(&updated_widget, &widget, &mut ctx, &mut element);

        let info_updated = element
            .node
            .get_text_userdata::<TextRenderInfo>(&ctx)
            .unwrap();
        assert_eq!(info_updated.spans.len(), 1);
        assert_eq!(info_updated.spans[0].style.color, Some(clr!(red)));
    }

    #[test]
    fn test_rich_text_range_geometry() {
        let mut ctx = Context::new();
        let widget = rich_text::<_, ()>("pub fn calculate(x: i32) -> bool").spans(vec![
            TextSpan::new(0..3).color(clr!(blue)).id(Token::Keyword),
            TextSpan::new(7..16).bold().id(Token::FunctionName),
        ]);

        let element = View::<()>::build(&widget, &mut ctx);

        // Query geometry of the FunctionName span ("calculate")
        let rects = element.node.get_text_range_geometry(&ctx, 7..16);
        assert!(
            !rects.is_empty(),
            "Must produce bounding rects for token span"
        );
        assert!(rects[0][2] > 0.0, "Width of token must be positive");
        assert!(rects[0][3] > 0.0, "Height of token must be positive");
    }
}