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
use crate::{
    AsyncWindowContext, Bounds, Pixels, PlatformInputHandler, View, ViewContext, WindowContext,
};
use std::ops::Range;

/// Implement this trait to allow views to handle textual input when implementing an editor, field, etc.
///
/// Once your view `V` implements this trait, you can use it to construct an [ElementInputHandler<V>].
/// This input handler can then be assigned during paint by calling [WindowContext::handle_input].
pub trait InputHandler: 'static + Sized {
    fn text_for_range(&mut self, range: Range<usize>, cx: &mut ViewContext<Self>)
        -> Option<String>;
    fn selected_text_range(&mut self, cx: &mut ViewContext<Self>) -> Option<Range<usize>>;
    fn marked_text_range(&self, cx: &mut ViewContext<Self>) -> Option<Range<usize>>;
    fn unmark_text(&mut self, cx: &mut ViewContext<Self>);
    fn replace_text_in_range(
        &mut self,
        range: Option<Range<usize>>,
        text: &str,
        cx: &mut ViewContext<Self>,
    );
    fn replace_and_mark_text_in_range(
        &mut self,
        range: Option<Range<usize>>,
        new_text: &str,
        new_selected_range: Option<Range<usize>>,
        cx: &mut ViewContext<Self>,
    );
    fn bounds_for_range(
        &mut self,
        range_utf16: Range<usize>,
        element_bounds: Bounds<Pixels>,
        cx: &mut ViewContext<Self>,
    ) -> Option<Bounds<Pixels>>;
}

/// The canonical implementation of `PlatformInputHandler`. Call `WindowContext::handle_input`
/// with an instance during your element's paint.
pub struct ElementInputHandler<V> {
    view: View<V>,
    element_bounds: Bounds<Pixels>,
    cx: AsyncWindowContext,
}

impl<V: 'static> ElementInputHandler<V> {
    /// Used in [Element::paint] with the element's bounds and a view context for its
    /// containing view.
    pub fn new(element_bounds: Bounds<Pixels>, view: View<V>, cx: &mut WindowContext) -> Self {
        ElementInputHandler {
            view,
            element_bounds,
            cx: cx.to_async(),
        }
    }
}

impl<V: InputHandler> PlatformInputHandler for ElementInputHandler<V> {
    fn selected_text_range(&mut self) -> Option<Range<usize>> {
        self.view
            .update(&mut self.cx, |view, cx| view.selected_text_range(cx))
            .ok()
            .flatten()
    }

    fn marked_text_range(&mut self) -> Option<Range<usize>> {
        self.view
            .update(&mut self.cx, |view, cx| view.marked_text_range(cx))
            .ok()
            .flatten()
    }

    fn text_for_range(&mut self, range_utf16: Range<usize>) -> Option<String> {
        self.view
            .update(&mut self.cx, |view, cx| {
                view.text_for_range(range_utf16, cx)
            })
            .ok()
            .flatten()
    }

    fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str) {
        self.view
            .update(&mut self.cx, |view, cx| {
                view.replace_text_in_range(replacement_range, text, cx)
            })
            .ok();
    }

    fn replace_and_mark_text_in_range(
        &mut self,
        range_utf16: Option<Range<usize>>,
        new_text: &str,
        new_selected_range: Option<Range<usize>>,
    ) {
        self.view
            .update(&mut self.cx, |view, cx| {
                view.replace_and_mark_text_in_range(range_utf16, new_text, new_selected_range, cx)
            })
            .ok();
    }

    fn unmark_text(&mut self) {
        self.view
            .update(&mut self.cx, |view, cx| view.unmark_text(cx))
            .ok();
    }

    fn bounds_for_range(&mut self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> {
        self.view
            .update(&mut self.cx, |view, cx| {
                view.bounds_for_range(range_utf16, self.element_bounds, cx)
            })
            .ok()
            .flatten()
    }
}