sinomo-ui-widgets 0.3.0

Built-in widget library for SUI.
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
use super::*;

pub struct Label {
    text: String,
    single_line: bool,
    text_reader: Option<Box<dyn Fn() -> String>>,
    text_source: Option<Arc<dyn Observable<String>>>,
    semantic_name: Option<String>,
    style: TextStyle,
    style_reader: Option<Box<dyn Fn() -> TextStyle>>,
    font_size_override: Option<f32>,
    line_height_override: Option<f32>,
    color_override: Option<Color>,
    color_reader: Option<Box<dyn Fn() -> Color>>,
    layout: Option<PersistentTextLayout>,
    selection_scope: Option<SelectionScope>,
    clipboard_behavior: SelectionClipboardBehavior,
    selection: TextSelection,
    dragging_selection: Option<u64>,
}

impl Label {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            single_line: false,
            text_reader: None,
            text_source: None,
            semantic_name: None,
            style: DefaultTheme::default().body_text_style(),
            style_reader: None,
            font_size_override: None,
            line_height_override: None,
            color_override: None,
            color_reader: None,
            layout: None,
            selection_scope: None,
            clipboard_behavior: SelectionClipboardBehavior::AppManaged,
            selection: TextSelection::new(TextCursor::new(0), TextCursor::new(0)),
            dragging_selection: None,
        }
    }

    /// Keep the label on one line, clipping overflow while retaining its full semantics.
    pub fn single_line(mut self) -> Self {
        self.single_line = true;
        self
    }

    pub fn dynamic<F>(fallback: impl Into<String>, reader: F) -> Self
    where
        F: Fn() -> String + 'static,
    {
        Self::new(fallback).text_when(reader)
    }

    pub fn text_when<F>(mut self, reader: F) -> Self
    where
        F: Fn() -> String + 'static,
    {
        self.text_reader = Some(Box::new(reader));
        self.text_source = None;
        self
    }

    /// Bind label text to an observable value.
    ///
    /// Changes automatically invalidate text measurement, paint, and
    /// semantics for this retained label instance.
    pub fn text_from<O>(mut self, source: O) -> Self
    where
        O: Observable<String> + 'static,
    {
        self.text = source.get();
        self.text_reader = None;
        self.text_source = Some(Arc::new(source));
        self
    }

    pub fn semantic_name(mut self, name: impl Into<String>) -> Self {
        self.semantic_name = Some(name.into());
        self
    }

    pub fn selectable(mut self, selection_scope: SelectionScope) -> Self {
        self.selection_scope = Some(selection_scope);
        self
    }

    pub fn clipboard_behavior(mut self, behavior: SelectionClipboardBehavior) -> Self {
        self.clipboard_behavior = behavior;
        self
    }

    pub fn copy_to_clipboard(self, enabled: bool) -> Self {
        self.clipboard_behavior(if enabled {
            SelectionClipboardBehavior::WidgetManaged
        } else {
            SelectionClipboardBehavior::AppManaged
        })
    }

    pub fn selection_scope(&self) -> Option<&SelectionScope> {
        self.selection_scope.as_ref()
    }

    pub fn theme(mut self, theme: DefaultTheme) -> Self {
        self.style = theme.body_text_style();
        self.clear_style_overrides();
        self
    }

    pub fn text(&self) -> &str {
        &self.text
    }

    pub fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        self.text_reader = None;
        self.text_source = None;
    }

    pub fn color(mut self, color: Color) -> Self {
        self.color_override = Some(color);
        self.color_reader = None;
        self
    }

    pub fn color_when<F>(mut self, color: F) -> Self
    where
        F: Fn() -> Color + 'static,
    {
        self.color_override = None;
        self.color_reader = Some(Box::new(color));
        self
    }

    pub fn font_size(mut self, font_size: f32) -> Self {
        self.font_size_override = Some(font_size.max(1.0));
        self
    }

    pub fn line_height(mut self, line_height: f32) -> Self {
        self.line_height_override = Some(line_height.max(1.0));
        self
    }

    pub fn style(mut self, style: TextStyle) -> Self {
        self.style = style;
        self.clear_style_overrides();
        self
    }

    /// Resolve the complete text style each time the label is measured or painted.
    ///
    /// As with [`Self::style`], this replaces earlier whole-style and per-property
    /// builders. Later `font_size`, `line_height`, `color`, or `color_when` calls
    /// layer their respective property over the dynamically resolved style.
    pub fn style_when<F>(mut self, style: F) -> Self
    where
        F: Fn() -> TextStyle + 'static,
    {
        self.clear_style_overrides();
        self.style_reader = Some(Box::new(style));
        self
    }

    fn current_text(&self) -> String {
        self.text_source
            .as_ref()
            .map(|source| source.get())
            .or_else(|| self.text_reader.as_ref().map(|reader| reader()))
            .unwrap_or_else(|| self.text.clone())
    }

    fn observed_text(&self, ctx: &MeasureCtx) -> String {
        self.text_source
            .as_ref()
            .map(|source| ctx.observe_with(source.as_ref(), InvalidationKind::Text))
            .unwrap_or_else(|| self.current_text())
    }

    pub(super) fn resolved_style(&self) -> TextStyle {
        let mut style = self
            .style_reader
            .as_ref()
            .map(|reader| reader())
            .unwrap_or_else(|| self.style.clone());
        if let Some(font_size) = self.font_size_override {
            style.font_size = font_size;
        }
        if let Some(line_height) = self.line_height_override {
            style.line_height = line_height;
        }
        if let Some(color) = self.color_override {
            style.color = color;
        }
        if let Some(color_reader) = &self.color_reader {
            style.color = color_reader();
        }
        style
    }

    fn clear_style_overrides(&mut self) {
        self.style_reader = None;
        self.font_size_override = None;
        self.line_height_override = None;
        self.color_override = None;
        self.color_reader = None;
    }

    fn has_explicit_line_break(text: &str) -> bool {
        text.bytes().any(|byte| matches!(byte, b'\n' | b'\r'))
    }

    fn owner_id(widget_id: WidgetId) -> SelectionOwnerId {
        SelectionOwnerId::from(widget_id)
    }

    fn selected_range(&self, text_len: usize) -> std::ops::Range<usize> {
        selection_range(&self.selection, text_len)
    }

    fn active_selection_range(
        &self,
        widget_id: WidgetId,
        text_len: usize,
    ) -> Option<std::ops::Range<usize>> {
        let owner = Self::owner_id(widget_id);
        let range = self.selected_range(text_len);
        self.selection_scope
            .as_ref()
            .is_some_and(|scope| scope.has_owner_selection(owner))
            .then_some(range)
            .filter(|range| !range.is_empty())
    }

    fn sync_selection_scope(&self, ctx: &mut EventCtx, text: &str) {
        let Some(scope) = &self.selection_scope else {
            return;
        };
        let owner = Self::owner_id(ctx.widget_id());
        let range = self.selected_range(text.len());
        let selected = text.get(range.clone()).unwrap_or("").to_string();
        let change = scope.replace_text(owner, owner, range, text.len(), selected);
        request_selection_change(ctx, change);
    }

    fn label_layout_origin_for_event(&self, bounds: Rect, layout: &PersistentTextLayout) -> Point {
        let measurement = layout.measurement();
        let height = self
            .resolved_style()
            .line_height
            .max(measurement.height)
            .min(bounds.height());
        Point::new(
            bounds.x() - measurement.bounds.x(),
            bounds.y() + ((bounds.height() - height).max(0.0) * 0.5),
        )
    }

    fn hit_test_offset(&self, bounds: Rect, position: Point, text_len: usize) -> usize {
        self.layout
            .as_ref()
            .map(|layout| {
                let origin = self.label_layout_origin_for_event(bounds, layout);
                layout
                    .hit_test_point(Point::new(position.x - origin.x, position.y - origin.y))
                    .utf8_offset
                    .min(text_len)
            })
            .unwrap_or(text_len)
    }

    fn set_selection(&mut self, anchor: usize, focus: usize, text_len: usize) {
        self.selection = TextSelection::new(
            TextCursor::new(anchor.min(text_len)),
            TextCursor::new(focus.min(text_len)),
        );
    }

    fn handles_implicit_clipboard(&self) -> bool {
        self.clipboard_behavior.is_widget_managed()
    }

    fn copy_selection(&self, ctx: &mut EventCtx) -> bool {
        let text = self.current_text();
        let range = self.selected_range(text.len());
        let Some(selected) = text.get(range).filter(|selected| !selected.is_empty()) else {
            return false;
        };
        ctx.set_clipboard_text(selected);
        true
    }
}

impl Widget for Label {
    fn supports_output_reuse(&self) -> bool {
        self.text_reader.is_none()
            && self.style_reader.is_none()
            && self.color_reader.is_none()
            && self.selection_scope.is_none()
    }
    fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
        if self.selection_scope.is_none() {
            return;
        }

        match event {
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Down
                    && pointer.button == Some(PointerButton::Primary)
                    && ctx.phase() != sui_runtime::EventPhase::Capture
                    && ctx.bounds().contains(pointer.position) =>
            {
                let text = self.current_text();
                let offset = self.hit_test_offset(ctx.bounds(), pointer.position, text.len());
                let anchor = if pointer.modifiers.shift {
                    self.selection.anchor.utf8_offset
                } else {
                    offset
                };
                self.set_selection(anchor, offset, text.len());
                self.dragging_selection = Some(pointer.pointer_id);
                self.sync_selection_scope(ctx, &text);
                ctx.request_focus();
                ctx.request_pointer_capture(pointer.pointer_id);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Move
                    && self.dragging_selection == Some(pointer.pointer_id)
                    && pointer.buttons.contains(PointerButton::Primary) =>
            {
                let text = self.current_text();
                let anchor = self.selection.anchor.utf8_offset;
                let focus = self.hit_test_offset(ctx.bounds(), pointer.position, text.len());
                self.set_selection(anchor, focus, text.len());
                self.sync_selection_scope(ctx, &text);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Up
                    && self.dragging_selection == Some(pointer.pointer_id) =>
            {
                self.dragging_selection = None;
                ctx.release_pointer_capture(pointer.pointer_id);
                ctx.set_handled();
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Cancel
                    && self.dragging_selection == Some(pointer.pointer_id) =>
            {
                self.dragging_selection = None;
                ctx.release_pointer_capture(pointer.pointer_id);
                ctx.set_handled();
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Down
                    && pointer.button == Some(PointerButton::Secondary)
                    && ctx.phase() != EventPhase::Capture
                    && ctx.bounds().contains(pointer.position) =>
            {
                // Preserve the active selection while handing the same press
                // to a wrapping ContextMenu. Its activation can route a
                // TextCommand back to this label after focus moves to the menu.
                ctx.request_focus();
                ctx.request_paint();
                ctx.request_semantics();
            }
            Event::Keyboard(key)
                if key.state == KeyState::Pressed
                    && ctx.is_focused()
                    && (key.modifiers.control || key.modifiers.meta)
                    && matches!(key.key.as_str(), "a" | "A") =>
            {
                let text = self.current_text();
                self.set_selection(0, text.len(), text.len());
                self.sync_selection_scope(ctx, &text);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Keyboard(key)
                if key.state == KeyState::Pressed
                    && ctx.is_focused()
                    && (key.modifiers.control || key.modifiers.meta)
                    && matches!(key.key.as_str(), "c" | "C")
                    && self.handles_implicit_clipboard() =>
            {
                if self.copy_selection(ctx) {
                    ctx.set_handled();
                }
            }
            Event::Keyboard(key)
                if key.state == KeyState::Pressed && ctx.is_focused() && key.key == "Escape" =>
            {
                if let Some(scope) = &self.selection_scope {
                    let change = scope.clear_owner(Self::owner_id(ctx.widget_id()));
                    request_selection_change(ctx, change);
                }
                self.set_selection(0, 0, 0);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Semantics(semantics) if semantics.target == ctx.widget_id() => {
                match &semantics.action {
                    SemanticsActionRequest::SetSelection(selection) => {
                        let text = self.current_text();
                        self.set_selection(selection.start, selection.end, text.len());
                        self.sync_selection_scope(ctx, &text);
                        ctx.request_paint();
                        ctx.request_semantics();
                        ctx.set_handled();
                    }
                    SemanticsActionRequest::Copy
                        if self.handles_implicit_clipboard() && self.copy_selection(ctx) =>
                    {
                        ctx.set_handled();
                    }
                    _ => {}
                }
            }
            _ => {}
        }
    }

    fn command(&mut self, ctx: &mut EventCtx, command: &Command<'_>) {
        let Some(command) = TextCommand::from_command(command) else {
            return;
        };
        match command {
            TextCommand::SelectAll => {
                let text = self.current_text();
                self.set_selection(0, text.len(), text.len());
                self.sync_selection_scope(ctx, &text);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            TextCommand::Copy => {
                if self.copy_selection(ctx) {
                    ctx.set_handled();
                }
            }
            TextCommand::Cut | TextCommand::Paste => {}
        }
    }

    fn intrinsic_size(
        &mut self,
        ctx: &mut MeasureCtx,
        axis: Axis,
        available_cross: f32,
    ) -> IntrinsicSize {
        if axis == Axis::Horizontal {
            let text = self.observed_text(ctx);
            let style = self.resolved_style();
            let natural = measure_text_size(ctx, &text, &style).width;
            let minimum = text
                .split_whitespace()
                .map(|segment| measure_text_size(ctx, segment, &style).width)
                .fold(0.0_f32, f32::max);
            return IntrinsicSize::new(minimum, natural);
        }

        let width = if available_cross.is_finite() {
            available_cross.max(0.0)
        } else {
            f32::INFINITY
        };
        let size = self.measure(
            ctx,
            Constraints::new(Size::ZERO, Size::new(width, f32::INFINITY)),
        );
        IntrinsicSize::fixed(size.height)
    }

    fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
        let text = self.observed_text(ctx);
        let style = self.resolved_style();
        let natural_measurement = measure_text_size(ctx, &text, &style);
        if self.single_line {
            self.layout = None;
            return constraints.clamp(Size::new(natural_measurement.width, style.line_height));
        }
        let max_width = constraints.max.width;
        let wraps_to_constraint = max_width.is_finite() && natural_measurement.width > max_width;
        let needs_layout = self.selection_scope.is_some()
            || wraps_to_constraint
            || Self::has_explicit_line_break(&text);
        let mut measured_width = if wraps_to_constraint {
            max_width.max(0.0)
        } else {
            natural_measurement.width
        };
        let mut measurement = natural_measurement;

        if needs_layout {
            let layout_width = if max_width.is_finite() {
                measured_width.min(max_width).max(1.0)
            } else {
                measured_width.max(1.0)
            };
            measurement = ctx
                .layout()
                .measure_document_size(
                    sui_text::TextLayoutRequest::new(sui_text::TextDocument::from_plain_text(
                        text.clone(),
                        style.clone(),
                    ))
                    .with_box_size(Size::new(layout_width, 1.0)),
                )
                .unwrap_or(measurement);
            if !wraps_to_constraint {
                measured_width = if max_width.is_finite() {
                    measurement.width.min(max_width).max(0.0)
                } else {
                    measurement.width.max(0.0)
                };
            }
            self.layout = ctx
                .layout()
                .shape_text_persistent(
                    self.layout.as_ref().map(|layout| layout.handle()),
                    text,
                    Size::new(
                        measured_width.max(1.0),
                        measurement.height.max(style.line_height).max(1.0),
                    ),
                    style.clone(),
                )
                .ok();
        } else {
            self.layout = None;
        }
        constraints.clamp(Size::new(
            measured_width,
            measurement.height.max(style.line_height),
        ))
    }

    fn paint(&self, ctx: &mut PaintCtx) {
        let text = self.current_text();
        let style = self.resolved_style();
        if self.single_line {
            let text = text.replace(['\r', '\n'], " ");
            ctx.push_clip_rect(ctx.bounds());
            paint_single_line_aligned_text(
                ctx,
                ctx.bounds(),
                &text,
                &style,
                style.line_height,
                0.0,
            );
            ctx.pop_clip();
            return;
        }
        if let Some(layout) = &self.layout {
            let layout_bounds = layout.measurement().bounds;
            let mut layout_rect = aligned_text_rect_for_layout_with_mode(
                ctx,
                ctx.bounds(),
                layout.layout(),
                style.line_height,
                0.0,
                HorizontalTextAlignmentMode::Optical,
            );
            if layout.lines().len() > 1 {
                let block_height = style
                    .line_height
                    .max(layout.measurement().height)
                    .min(ctx.bounds().height());
                layout_rect = Rect::new(
                    layout_rect.x(),
                    ctx.bounds().y() + ((ctx.bounds().height() - block_height).max(0.0) * 0.5),
                    layout_rect.width(),
                    block_height,
                );
            }
            let origin = Point::new(layout_rect.x() - layout_bounds.x(), layout_rect.y());
            if let Some(range) = self.active_selection_range(ctx.widget_id(), text.len()) {
                let theme = DefaultTheme::default();
                for rect in layout.selection_rects(range) {
                    ctx.fill_rect(rect.translate(origin.to_vector()), theme.palette.selection);
                }
            }
            ctx.draw_persistent_text_layout(origin, layout);
        } else {
            paint_aligned_text(ctx, ctx.bounds(), &text, &style, style.line_height, 0.0);
        }
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        let text = self.current_text();
        let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
        node.name = Some(self.semantic_name.clone().unwrap_or_else(|| text.clone()));
        if self.semantic_name.is_some() {
            node.value = Some(SemanticsValue::Text(text));
        }
        if self.selection_scope.is_some() {
            node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetSelection];
            if self.handles_implicit_clipboard() {
                node.actions.push(SemanticsAction::Copy);
            }
            node.state.focused = ctx.is_focused();
        }
        ctx.push(node);
    }

    fn accepts_focus(&self) -> bool {
        self.selection_scope.is_some()
    }

    fn focus_changed(&mut self, ctx: &mut EventCtx, _focused: bool) {
        ctx.request_paint();
        ctx.request_semantics();
    }
}

pub struct Link {
    theme: Box<DefaultTheme>,
    theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
    label: String,
    label_reader: Option<Box<dyn Fn() -> String>>,
    url: String,
    url_reader: Option<Box<dyn Fn() -> String>>,
    semantic_name: Option<String>,
    text_style: Option<TextStyle>,
    enabled: bool,
    enabled_reader: Option<Box<dyn Fn() -> bool>>,
    hovered: bool,
    pressed: bool,
    measurement: Option<TextMeasurement>,
    on_open: Option<Box<dyn FnMut(&str)>>,
    on_open_with_ctx: Option<Box<dyn FnMut(&mut EventCtx, &str)>>,
}

impl Link {
    pub fn new(label: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            theme: Box::new(DefaultTheme::default()),
            theme_reader: None,
            label: label.into(),
            label_reader: None,
            url: url.into(),
            url_reader: None,
            semantic_name: None,
            text_style: None,
            enabled: true,
            enabled_reader: None,
            hovered: false,
            pressed: false,
            measurement: None,
            on_open: None,
            on_open_with_ctx: None,
        }
    }

    pub fn url(url: impl Into<String>) -> Self {
        let url = url.into();
        Self::new(url.clone(), url)
    }

    pub fn label_when<F>(mut self, reader: F) -> Self
    where
        F: Fn() -> String + 'static,
    {
        self.label_reader = Some(Box::new(reader));
        self
    }

    pub fn url_when<F>(mut self, reader: F) -> Self
    where
        F: Fn() -> String + 'static,
    {
        self.url_reader = Some(Box::new(reader));
        self
    }

    pub fn semantic_name(mut self, name: impl Into<String>) -> Self {
        self.semantic_name = Some(name.into());
        self
    }

    pub fn theme(mut self, theme: DefaultTheme) -> Self {
        self.theme = Box::new(theme);
        self.theme_reader = None;
        self
    }

    pub fn theme_when<F>(mut self, theme: F) -> Self
    where
        F: Fn() -> DefaultTheme + 'static,
    {
        self.theme_reader = Some(Box::new(theme));
        self
    }

    pub fn text_style(mut self, text_style: TextStyle) -> Self {
        self.text_style = Some(text_style);
        self
    }

    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self.enabled_reader = None;
        self
    }

    pub fn enabled_when<F>(mut self, enabled: F) -> Self
    where
        F: Fn() -> bool + 'static,
    {
        self.enabled_reader = Some(Box::new(enabled));
        self
    }

    pub fn on_open<F>(mut self, on_open: F) -> Self
    where
        F: FnMut(&str) + 'static,
    {
        self.on_open = Some(Box::new(on_open));
        self
    }

    pub fn on_open_with_ctx<F>(mut self, on_open: F) -> Self
    where
        F: FnMut(&mut EventCtx, &str) + 'static,
    {
        self.on_open_with_ctx = Some(Box::new(on_open));
        self
    }

    fn current_label(&self) -> String {
        single_line_text(
            self.label_reader
                .as_ref()
                .map(|reader| reader())
                .unwrap_or_else(|| self.label.clone()),
        )
    }

    fn current_url(&self) -> String {
        single_line_text(
            self.url_reader
                .as_ref()
                .map(|reader| reader())
                .unwrap_or_else(|| self.url.clone()),
        )
        .trim()
        .to_string()
    }

    fn is_enabled(&self) -> bool {
        self.enabled_reader
            .as_ref()
            .map(|enabled| enabled())
            .unwrap_or(self.enabled)
    }

    fn resolved_theme(&self) -> DefaultTheme {
        self.theme_reader
            .as_ref()
            .map(|theme| theme())
            .unwrap_or(*self.theme)
    }

    fn resolved_text_style(&self, color: Color) -> TextStyle {
        let mut style = self
            .text_style
            .clone()
            .unwrap_or_else(|| self.resolved_theme().body_text_style());
        style.color = color;
        style
    }

    fn resolved_color(&self, theme: &DefaultTheme) -> Color {
        if !self.is_enabled() {
            return theme
                .palette
                .placeholder
                .with_alpha(theme.interaction.disabled_content_opacity);
        }
        if self.pressed {
            theme.palette.accent_pressed
        } else if self.hovered {
            theme.palette.accent_hover
        } else {
            theme.palette.accent
        }
    }

    fn is_visible_parts(label: &str, url: &str) -> bool {
        !label.trim().is_empty() && !url.trim().is_empty()
    }

    fn is_visible(&self) -> bool {
        Self::is_visible_parts(&self.current_label(), &self.current_url())
    }

    fn can_activate_parts(&self, label: &str, url: &str) -> bool {
        self.is_enabled() && Self::is_visible_parts(label, url)
    }

    fn activate(&mut self, ctx: &mut EventCtx) {
        let label = self.current_label();
        let url = self.current_url();
        if !self.can_activate_parts(&label, &url) {
            return;
        }
        if let Some(on_open) = &mut self.on_open {
            on_open(&url);
        }
        if let Some(on_open) = &mut self.on_open_with_ctx {
            on_open(ctx, &url);
        }
    }

    fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
        if self.hovered != hovered {
            self.hovered = hovered;
            ctx.request_paint();
            ctx.request_semantics();
        }
    }

    fn reset_interaction(&mut self, ctx: &mut EventCtx) {
        if self.hovered || self.pressed {
            self.hovered = false;
            self.pressed = false;
            ctx.request_paint();
            ctx.request_semantics();
        }
    }
}

impl Widget for Link {
    fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
        if !self.is_visible() || !self.is_enabled() {
            self.reset_interaction(ctx);
            return;
        }

        match event {
            Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
                self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
            }
            Event::Pointer(pointer) if pointer.kind == PointerEventKind::Enter => {
                self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
            }
            Event::Pointer(_pointer) if _pointer.kind == PointerEventKind::Leave => {
                self.set_hovered(false, ctx);
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Down
                    && pointer.button == Some(PointerButton::Primary) =>
            {
                self.pressed = true;
                self.hovered = true;
                ctx.request_pointer_capture(pointer.pointer_id);
                ctx.request_focus();
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Pointer(pointer)
                if pointer.kind == PointerEventKind::Up
                    && pointer.button == Some(PointerButton::Primary) =>
            {
                let hovered = ctx.bounds().contains(pointer.position);
                let activate = self.pressed && hovered;
                self.pressed = false;
                self.hovered = hovered;
                ctx.release_pointer_capture(pointer.pointer_id);
                if activate {
                    self.activate(ctx);
                }
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
                if self.pressed {
                    self.pressed = false;
                    self.hovered = false;
                    ctx.release_pointer_capture(pointer.pointer_id);
                    ctx.request_paint();
                    ctx.request_semantics();
                    ctx.set_handled();
                }
            }
            Event::Keyboard(key)
                if key.state == KeyState::Pressed
                    && ctx.is_focused()
                    && matches!(key.key.as_str(), "Enter" | " ") =>
            {
                self.activate(ctx);
                ctx.request_paint();
                ctx.request_semantics();
                ctx.set_handled();
            }
            _ => {}
        }
    }

    fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
        let label = self.current_label();
        let url = self.current_url();
        if !Self::is_visible_parts(&label, &url) {
            self.measurement = None;
            return constraints.clamp(Size::ZERO);
        }

        let theme = self.resolved_theme();
        let style = self.resolved_text_style(self.resolved_color(&theme));
        let measurement = measure_text(ctx, &label, &style);
        self.measurement = Some(measurement);
        constraints.clamp(Size::new(
            measurement.width,
            measurement.height.max(style.line_height),
        ))
    }

    fn paint(&self, ctx: &mut PaintCtx) {
        let label = self.current_label();
        let url = self.current_url();
        if !Self::is_visible_parts(&label, &url) {
            return;
        }

        let theme = self.resolved_theme();
        let color = self.resolved_color(&theme);
        let style = self.resolved_text_style(color);
        let bounds = ctx.bounds();
        ctx.push_clip_rect(bounds);
        paint_single_line_aligned_text(ctx, bounds, &label, &style, style.line_height, 0.0);
        ctx.pop_clip();

        let measured_width = self
            .measurement
            .map(|measurement| measurement.width)
            .unwrap_or(bounds.width())
            .min(bounds.width())
            .max(0.0);
        if measured_width > 0.0 {
            let underline_y = bounds.y() + bounds.height() - physical_pixels(ctx, 2.0);
            let mut underline = PathBuilder::new();
            underline
                .move_to(Point::new(bounds.x(), underline_y))
                .line_to(Point::new(bounds.x() + measured_width, underline_y));
            ctx.stroke(
                underline.build(),
                color,
                StrokeStyle::new(physical_pixels(ctx, 1.0)),
            );
        }

        if ctx.is_focused() && self.is_enabled() {
            ctx.stroke_rect(
                bounds.inflate(physical_pixels(ctx, 2.0), physical_pixels(ctx, 1.0)),
                theme.palette.focus_ring,
                StrokeStyle::new(physical_pixels(ctx, 1.0)),
            );
        }
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        let label = self.current_label();
        let url = self.current_url();
        if !Self::is_visible_parts(&label, &url) {
            return;
        }

        let enabled = self.is_enabled();
        let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Link, ctx.bounds());
        node.name = Some(self.semantic_name.clone().unwrap_or(label));
        node.value = Some(SemanticsValue::Text(url));
        node.state.focused = ctx.is_focused();
        node.state.hovered = self.hovered && enabled;
        node.state.disabled = !enabled;
        node.actions = if enabled {
            vec![SemanticsAction::Focus, SemanticsAction::Activate]
        } else {
            Vec::new()
        };
        ctx.push(node);
    }

    fn accepts_focus(&self) -> bool {
        self.is_enabled() && self.is_visible()
    }

    fn focus_changed(&mut self, ctx: &mut EventCtx, _focused: bool) {
        ctx.request_paint();
        ctx.request_semantics();
    }
}