| 1 | use crate::{App, Bounds, Context, Entity, InputHandler, Pixels, UTF16Selection, Window}; |
| 2 | use std::ops::Range; |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | pub trait EntityInputHandler: 'static + Sized { |
| 11 | |
| 12 | fn text_for_range( |
| 13 | &mut self, |
| 14 | range: Range<usize>, |
| 15 | adjusted_range: &mut Option<Range<usize>>, |
| 16 | window: &mut Window, |
| 17 | cx: &mut Context<Self>, |
| 18 | ) -> Option<String>; |
| 19 | |
| 20 | |
| 21 | fn selected_text_range( |
| 22 | &mut self, |
| 23 | ignore_disabled_input: bool, |
| 24 | window: &mut Window, |
| 25 | cx: &mut Context<Self>, |
| 26 | ) -> Option<UTF16Selection>; |
| 27 | |
| 28 | |
| 29 | fn marked_text_range( |
| 30 | &self, |
| 31 | window: &mut Window, |
| 32 | cx: &mut Context<Self>, |
| 33 | ) -> Option<Range<usize>>; |
| 34 | |
| 35 | |
| 36 | fn unmark_text(&mut self, window: &mut Window, cx: &mut Context<Self>); |
| 37 | |
| 38 | |
| 39 | fn replace_text_in_range( |
| 40 | &mut self, |
| 41 | range: Option<Range<usize>>, |
| 42 | text: &str, |
| 43 | window: &mut Window, |
| 44 | cx: &mut Context<Self>, |
| 45 | ); |
| 46 | |
| 47 | |
| 48 | fn replace_and_mark_text_in_range( |
| 49 | &mut self, |
| 50 | range: Option<Range<usize>>, |
| 51 | new_text: &str, |
| 52 | new_selected_range: Option<Range<usize>>, |
| 53 | window: &mut Window, |
| 54 | cx: &mut Context<Self>, |
| 55 | ); |
| 56 | |
| 57 | |
| 58 | fn bounds_for_range( |
| 59 | &mut self, |
| 60 | range_utf16: Range<usize>, |
| 61 | element_bounds: Bounds<Pixels>, |
| 62 | window: &mut Window, |
| 63 | cx: &mut Context<Self>, |
| 64 | ) -> Option<Bounds<Pixels>>; |
| 65 | |
| 66 | |
| 67 | fn character_index_for_point( |
| 68 | &mut self, |
| 69 | point: crate::Point<Pixels>, |
| 70 | window: &mut Window, |
| 71 | cx: &mut Context<Self>, |
| 72 | ) -> Option<usize>; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | pub struct ElementInputHandler<V> { |
| 78 | view: Entity<V>, |
| 79 | element_bounds: Bounds<Pixels>, |
| 80 | } |
| 81 | |
| 82 | impl<V: 'static> ElementInputHandler<V> { |
| 83 | |
| 84 | |
| 85 | |
| 86 | pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self { |
| 87 | ElementInputHandler { |
| 88 | view, |
| 89 | element_bounds, |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> { |
| 95 | fn selected_text_range( |
| 96 | &mut self, |
| 97 | ignore_disabled_input: bool, |
| 98 | window: &mut Window, |
| 99 | cx: &mut App, |
| 100 | ) -> Option<UTF16Selection> { |
| 101 | self.view.update(cx, |view, cx| { |
| 102 | view.selected_text_range(ignore_disabled_input, window, cx) |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>> { |
| 107 | self.view |
| 108 | .update(cx, |view, cx| view.marked_text_range(window, cx)) |
| 109 | } |
| 110 | |
| 111 | fn text_for_range( |
| 112 | &mut self, |
| 113 | range_utf16: Range<usize>, |
| 114 | adjusted_range: &mut Option<Range<usize>>, |
| 115 | window: &mut Window, |
| 116 | cx: &mut App, |
| 117 | ) -> Option<String> { |
| 118 | self.view.update(cx, |view, cx| { |
| 119 | view.text_for_range(range_utf16, adjusted_range, window, cx) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | fn replace_text_in_range( |
| 124 | &mut self, |
| 125 | replacement_range: Option<Range<usize>>, |
| 126 | text: &str, |
| 127 | window: &mut Window, |
| 128 | cx: &mut App, |
| 129 | ) { |
| 130 | self.view.update(cx, |view, cx| { |
| 131 | view.replace_text_in_range(replacement_range, text, window, cx) |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | fn replace_and_mark_text_in_range( |
| 136 | &mut self, |
| 137 | range_utf16: Option<Range<usize>>, |
| 138 | new_text: &str, |
| 139 | new_selected_range: Option<Range<usize>>, |
| 140 | window: &mut Window, |
| 141 | cx: &mut App, |
| 142 | ) { |
| 143 | self.view.update(cx, |view, cx| { |
| 144 | view.replace_and_mark_text_in_range( |
| 145 | range_utf16, |
| 146 | new_text, |
| 147 | new_selected_range, |
| 148 | window, |
| 149 | cx, |
| 150 | ) |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | fn unmark_text(&mut self, window: &mut Window, cx: &mut App) { |
| 155 | self.view |
| 156 | .update(cx, |view, cx| view.unmark_text(window, cx)); |
| 157 | } |
| 158 | |
| 159 | fn bounds_for_range( |
| 160 | &mut self, |
| 161 | range_utf16: Range<usize>, |
| 162 | window: &mut Window, |
| 163 | cx: &mut App, |
| 164 | ) -> Option<Bounds<Pixels>> { |
| 165 | self.view.update(cx, |view, cx| { |
| 166 | view.bounds_for_range(range_utf16, self.element_bounds, window, cx) |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | fn character_index_for_point( |
| 171 | &mut self, |
| 172 | point: crate::Point<Pixels>, |
| 173 | window: &mut Window, |
| 174 | cx: &mut App, |
| 175 | ) -> Option<usize> { |
| 176 | self.view.update(cx, |view, cx| { |
| 177 | view.character_index_for_point(point, window, cx) |
| 178 | }) |
| 179 | } |
| 180 | } |