window_input_handler.rs

 1use std::{cell::RefCell, ops::Range, rc::Rc};
 2
 3use pathfinder_geometry::rect::RectF;
 4
 5use crate::{platform::InputHandler, window::WindowContext, AnyView, AnyWindowHandle, AppContext};
 6
 7pub struct WindowInputHandler {
 8    pub app: Rc<RefCell<AppContext>>,
 9    pub window: AnyWindowHandle,
10}
11
12impl WindowInputHandler {
13    fn read_focused_view<T, F>(&self, f: F) -> Option<T>
14    where
15        F: FnOnce(&dyn AnyView, &WindowContext) -> T,
16    {
17        // Input-related application hooks are sometimes called by the OS during
18        // a call to a window-manipulation API, like prompting the user for file
19        // paths. In that case, the AppContext will already be borrowed, so any
20        // InputHandler methods need to fail gracefully.
21        //
22        // See https://github.com/zed-industries/community/issues/444
23        let mut app = self.app.try_borrow_mut().ok()?;
24        self.window.update_optional(&mut *app, |cx| {
25            let view_id = cx.window.focused_view_id?;
26            let view = cx.views.get(&(self.window, view_id))?;
27            let result = f(view.as_ref(), &cx);
28            Some(result)
29        })
30    }
31
32    fn update_focused_view<T, F>(&mut self, f: F) -> Option<T>
33    where
34        F: FnOnce(&mut dyn AnyView, &mut WindowContext, usize) -> T,
35    {
36        let mut app = self.app.try_borrow_mut().ok()?;
37        self.window
38            .update(&mut *app, |cx| {
39                let view_id = cx.window.focused_view_id?;
40                cx.update_any_view(view_id, |view, cx| f(view, cx, view_id))
41            })
42            .flatten()
43    }
44}
45
46impl InputHandler for WindowInputHandler {
47    fn text_for_range(&self, range: Range<usize>) -> Option<String> {
48        self.read_focused_view(|view, cx| view.text_for_range(range.clone(), cx))
49            .flatten()
50    }
51
52    fn selected_text_range(&self) -> Option<Range<usize>> {
53        self.read_focused_view(|view, cx| view.selected_text_range(cx))
54            .flatten()
55    }
56
57    fn replace_text_in_range(&mut self, range: Option<Range<usize>>, text: &str) {
58        self.update_focused_view(|view, cx, view_id| {
59            view.replace_text_in_range(range, text, cx, view_id);
60        });
61    }
62
63    fn marked_text_range(&self) -> Option<Range<usize>> {
64        self.read_focused_view(|view, cx| view.marked_text_range(cx))
65            .flatten()
66    }
67
68    fn unmark_text(&mut self) {
69        self.update_focused_view(|view, cx, view_id| {
70            view.unmark_text(cx, view_id);
71        });
72    }
73
74    fn replace_and_mark_text_in_range(
75        &mut self,
76        range: Option<Range<usize>>,
77        new_text: &str,
78        new_selected_range: Option<Range<usize>>,
79    ) {
80        self.update_focused_view(|view, cx, view_id| {
81            view.replace_and_mark_text_in_range(range, new_text, new_selected_range, cx, view_id);
82        });
83    }
84
85    fn rect_for_range(&self, range_utf16: Range<usize>) -> Option<RectF> {
86        self.window.read_optional_with(&*self.app.borrow(), |cx| {
87            cx.rect_for_text_range(range_utf16)
88        })
89    }
90}