use crate::{ AnyWindowHandle, AppCell, Bounds, Context, Pixels, PlatformInputHandler, View, ViewContext, WindowContext, }; use std::{ops::Range, rc::Weak}; pub struct WindowInputHandler { pub cx: Weak, pub input_handler: Box, pub window: AnyWindowHandle, pub element_bounds: Bounds, } pub trait InputHandlerView { fn text_for_range(&self, range: Range, cx: &mut WindowContext) -> Option; fn selected_text_range(&self, cx: &mut WindowContext) -> Option>; fn marked_text_range(&self, cx: &mut WindowContext) -> Option>; fn unmark_text(&self, cx: &mut WindowContext); fn replace_text_in_range( &self, range: Option>, text: &str, cx: &mut WindowContext, ); fn replace_and_mark_text_in_range( &self, range: Option>, new_text: &str, new_selected_range: Option>, cx: &mut WindowContext, ); fn bounds_for_range( &self, range_utf16: std::ops::Range, element_bounds: crate::Bounds, cx: &mut WindowContext, ) -> Option>; } pub trait InputHandler: Sized { fn text_for_range(&self, range: Range, cx: &mut ViewContext) -> Option; fn selected_text_range(&self, cx: &mut ViewContext) -> Option>; fn marked_text_range(&self, cx: &mut ViewContext) -> Option>; fn unmark_text(&mut self, cx: &mut ViewContext); fn replace_text_in_range( &mut self, range: Option>, text: &str, cx: &mut ViewContext, ); fn replace_and_mark_text_in_range( &mut self, range: Option>, new_text: &str, new_selected_range: Option>, cx: &mut ViewContext, ); fn bounds_for_range( &mut self, range_utf16: std::ops::Range, element_bounds: crate::Bounds, cx: &mut ViewContext, ) -> Option>; } impl InputHandlerView for View { fn text_for_range(&self, range: Range, cx: &mut WindowContext) -> Option { self.update(cx, |this, cx| this.text_for_range(range, cx)) } fn selected_text_range(&self, cx: &mut WindowContext) -> Option> { self.update(cx, |this, cx| this.selected_text_range(cx)) } fn marked_text_range(&self, cx: &mut WindowContext) -> Option> { self.update(cx, |this, cx| this.marked_text_range(cx)) } fn unmark_text(&self, cx: &mut WindowContext) { self.update(cx, |this, cx| this.unmark_text(cx)) } fn replace_text_in_range( &self, range: Option>, text: &str, cx: &mut WindowContext, ) { self.update(cx, |this, cx| this.replace_text_in_range(range, text, cx)) } fn replace_and_mark_text_in_range( &self, range: Option>, new_text: &str, new_selected_range: Option>, cx: &mut WindowContext, ) { self.update(cx, |this, cx| { this.replace_and_mark_text_in_range(range, new_text, new_selected_range, cx) }) } fn bounds_for_range( &self, range_utf16: std::ops::Range, element_bounds: crate::Bounds, cx: &mut WindowContext, ) -> Option> { self.update(cx, |this, cx| { this.bounds_for_range(range_utf16, element_bounds, cx) }) } } impl PlatformInputHandler for WindowInputHandler { fn selected_text_range(&self) -> Option> { self.update(|handler, cx| handler.selected_text_range(cx)) .flatten() } fn marked_text_range(&self) -> Option> { self.update(|handler, cx| handler.marked_text_range(cx)) .flatten() } fn text_for_range(&self, range_utf16: Range) -> Option { self.update(|handler, cx| handler.text_for_range(range_utf16, cx)) .flatten() } fn replace_text_in_range(&mut self, replacement_range: Option>, text: &str) { self.update(|handler, cx| handler.replace_text_in_range(replacement_range, text, cx)); } fn replace_and_mark_text_in_range( &mut self, range_utf16: Option>, new_text: &str, new_selected_range: Option>, ) { self.update(|handler, cx| { handler.replace_and_mark_text_in_range(range_utf16, new_text, new_selected_range, cx) }); } fn unmark_text(&mut self) { self.update(|handler, cx| handler.unmark_text(cx)); } fn bounds_for_range(&self, range_utf16: Range) -> Option> { self.update(|handler, cx| handler.bounds_for_range(range_utf16, self.element_bounds, cx)) .flatten() } } impl WindowInputHandler { fn update( &self, f: impl FnOnce(&dyn InputHandlerView, &mut WindowContext) -> R, ) -> Option { let cx = self.cx.upgrade()?; let mut cx = cx.borrow_mut(); cx.update_window(self.window, |_, cx| f(&*self.input_handler, cx)) .ok() } }