Register text input handlers via new element hook
Provide element bounds to the input handler's `bounds_for_rect` method. Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
parent
d273fa6dd0
commit
1a37d9edc6
7 changed files with 160 additions and 66 deletions
|
@ -1,65 +1,142 @@
|
|||
use crate::{AnyWindowHandle, AppCell, Context, PlatformInputHandler, ViewContext, WeakView};
|
||||
use crate::{
|
||||
AnyWindowHandle, AppCell, Bounds, Context, Pixels, PlatformInputHandler, View, ViewContext,
|
||||
WindowContext,
|
||||
};
|
||||
use std::{ops::Range, rc::Weak};
|
||||
|
||||
pub struct WindowInputHandler<V>
|
||||
where
|
||||
V: InputHandler,
|
||||
{
|
||||
pub struct WindowInputHandler {
|
||||
pub cx: Weak<AppCell>,
|
||||
pub input_handler: Box<dyn InputHandlerView>,
|
||||
pub window: AnyWindowHandle,
|
||||
pub handler: WeakView<V>,
|
||||
pub element_bounds: Bounds<Pixels>,
|
||||
}
|
||||
|
||||
impl<V: InputHandler + 'static> PlatformInputHandler for WindowInputHandler<V> {
|
||||
fn selected_text_range(&self) -> Option<std::ops::Range<usize>> {
|
||||
self.update(|view, cx| view.selected_text_range(cx))
|
||||
.flatten()
|
||||
pub trait InputHandlerView {
|
||||
fn text_for_range(&self, range: Range<usize>, cx: &mut WindowContext) -> Option<String>;
|
||||
fn selected_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>>;
|
||||
fn marked_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>>;
|
||||
fn unmark_text(&self, cx: &mut WindowContext);
|
||||
fn replace_text_in_range(
|
||||
&self,
|
||||
range: Option<Range<usize>>,
|
||||
text: &str,
|
||||
cx: &mut WindowContext,
|
||||
);
|
||||
fn replace_and_mark_text_in_range(
|
||||
&self,
|
||||
range: Option<Range<usize>>,
|
||||
new_text: &str,
|
||||
new_selected_range: Option<Range<usize>>,
|
||||
cx: &mut WindowContext,
|
||||
);
|
||||
fn bounds_for_range(
|
||||
&self,
|
||||
range_utf16: std::ops::Range<usize>,
|
||||
element_bounds: crate::Bounds<Pixels>,
|
||||
cx: &mut WindowContext,
|
||||
) -> Option<crate::Bounds<Pixels>>;
|
||||
}
|
||||
|
||||
impl<V: InputHandler + 'static> InputHandlerView for View<V> {
|
||||
fn text_for_range(&self, range: Range<usize>, cx: &mut WindowContext) -> Option<String> {
|
||||
self.update(cx, |this, cx| this.text_for_range(range, cx))
|
||||
}
|
||||
|
||||
fn marked_text_range(&self) -> Option<std::ops::Range<usize>> {
|
||||
self.update(|view, cx| view.marked_text_range(cx)).flatten()
|
||||
fn selected_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>> {
|
||||
self.update(cx, |this, cx| this.selected_text_range(cx))
|
||||
}
|
||||
|
||||
fn text_for_range(&self, range_utf16: std::ops::Range<usize>) -> Option<String> {
|
||||
self.update(|view, cx| view.text_for_range(range_utf16, cx))
|
||||
.flatten()
|
||||
fn marked_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>> {
|
||||
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(
|
||||
&mut self,
|
||||
replacement_range: Option<std::ops::Range<usize>>,
|
||||
&self,
|
||||
range: Option<Range<usize>>,
|
||||
text: &str,
|
||||
cx: &mut WindowContext,
|
||||
) {
|
||||
self.update(|view, cx| view.replace_text_in_range(replacement_range, text, cx));
|
||||
self.update(cx, |this, cx| this.replace_text_in_range(range, text, cx))
|
||||
}
|
||||
|
||||
fn replace_and_mark_text_in_range(
|
||||
&self,
|
||||
range: Option<Range<usize>>,
|
||||
new_text: &str,
|
||||
new_selected_range: Option<Range<usize>>,
|
||||
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<usize>,
|
||||
element_bounds: crate::Bounds<Pixels>,
|
||||
cx: &mut WindowContext,
|
||||
) -> Option<crate::Bounds<Pixels>> {
|
||||
self.update(cx, |this, cx| {
|
||||
this.bounds_for_range(range_utf16, element_bounds, cx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl PlatformInputHandler for WindowInputHandler {
|
||||
fn selected_text_range(&self) -> Option<Range<usize>> {
|
||||
self.update(|handler, cx| handler.selected_text_range(cx))
|
||||
.flatten()
|
||||
}
|
||||
|
||||
fn marked_text_range(&self) -> Option<Range<usize>> {
|
||||
self.update(|handler, cx| handler.marked_text_range(cx))
|
||||
.flatten()
|
||||
}
|
||||
|
||||
fn text_for_range(&self, range_utf16: Range<usize>) -> Option<String> {
|
||||
self.update(|handler, cx| handler.text_for_range(range_utf16, cx))
|
||||
.flatten()
|
||||
}
|
||||
|
||||
fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, 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<std::ops::Range<usize>>,
|
||||
range_utf16: Option<Range<usize>>,
|
||||
new_text: &str,
|
||||
new_selected_range: Option<std::ops::Range<usize>>,
|
||||
new_selected_range: Option<Range<usize>>,
|
||||
) {
|
||||
self.update(|view, cx| {
|
||||
view.replace_and_mark_text_in_range(range_utf16, new_text, new_selected_range, cx)
|
||||
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(|view, cx| view.unmark_text(cx));
|
||||
self.update(|handler, cx| handler.unmark_text(cx));
|
||||
}
|
||||
|
||||
fn bounds_for_range(&self, range_utf16: std::ops::Range<usize>) -> Option<crate::Bounds<f32>> {
|
||||
self.update(|view, cx| view.bounds_for_range(range_utf16, cx))
|
||||
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> {
|
||||
self.update(|handler, cx| handler.bounds_for_range(range_utf16, self.element_bounds, cx))
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: InputHandler + 'static> WindowInputHandler<V> {
|
||||
fn update<T>(&self, f: impl FnOnce(&mut V, &mut ViewContext<V>) -> T) -> Option<T> {
|
||||
impl WindowInputHandler {
|
||||
fn update<R>(
|
||||
&self,
|
||||
f: impl FnOnce(&dyn InputHandlerView, &mut WindowContext) -> R,
|
||||
) -> Option<R> {
|
||||
let cx = self.cx.upgrade()?;
|
||||
let mut cx = cx.borrow_mut();
|
||||
cx.update_window(self.window, |_, cx| self.handler.update(cx, f).ok())
|
||||
.ok()?
|
||||
cx.update_window(self.window, |_, cx| f(&*self.input_handler, cx))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,6 +161,7 @@ pub trait InputHandler: Sized {
|
|||
fn bounds_for_range(
|
||||
&self,
|
||||
range_utf16: std::ops::Range<usize>,
|
||||
element_bounds: crate::Bounds<Pixels>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Option<crate::Bounds<f32>>;
|
||||
) -> Option<crate::Bounds<Pixels>>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue