mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 20:01:33 +00:00
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
|
@ -9752,8 +9752,9 @@ impl InputHandler for Editor {
|
|||
fn bounds_for_range(
|
||||
&self,
|
||||
range_utf16: Range<usize>,
|
||||
element_bounds: gpui::Bounds<Pixels>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Option<gpui::Bounds<f32>> {
|
||||
) -> Option<gpui::Bounds<Pixels>> {
|
||||
// todo!()
|
||||
// See how we did it before: `rect_for_range`
|
||||
None
|
||||
|
|
|
@ -17,10 +17,11 @@ use collections::{BTreeMap, HashMap};
|
|||
use gpui::{
|
||||
black, hsla, point, px, relative, size, transparent_black, Action, AnyElement,
|
||||
BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase,
|
||||
Edges, Element, ElementId, Entity, GlobalElementId, Hsla, KeyDownEvent, KeyListener, KeyMatch,
|
||||
Line, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels,
|
||||
ScrollWheelEvent, ShapedGlyph, Size, Style, TextRun, TextStyle, TextSystem, ViewContext,
|
||||
WindowContext,
|
||||
Edges, Element, ElementId, Entity, FocusHandle, GlobalElementId, Hsla, InputHandler,
|
||||
InputHandlerView, KeyDownEvent, KeyListener, KeyMatch, Line, LineLayout, Modifiers,
|
||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, ScrollWheelEvent,
|
||||
ShapedGlyph, Size, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext,
|
||||
WrappedLineLayout,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use language::language_settings::ShowWhitespaceSetting;
|
||||
|
@ -2502,10 +2503,6 @@ impl Element<Editor> for EditorElement {
|
|||
size: layout.text_size,
|
||||
};
|
||||
|
||||
if editor.focus_handle.is_focused(cx) {
|
||||
cx.handle_text_input();
|
||||
}
|
||||
|
||||
cx.with_content_mask(ContentMask { bounds }, |cx| {
|
||||
self.paint_mouse_listeners(
|
||||
bounds,
|
||||
|
@ -2521,6 +2518,14 @@ impl Element<Editor> for EditorElement {
|
|||
self.paint_text(text_bounds, &layout, editor, cx);
|
||||
});
|
||||
}
|
||||
|
||||
fn handle_text_input<'a>(
|
||||
&self,
|
||||
editor: &'a mut Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> Option<(Box<dyn InputHandlerView>, &'a FocusHandle)> {
|
||||
Some((Box::new(cx.view()), &editor.focus_handle))
|
||||
}
|
||||
}
|
||||
|
||||
// impl EditorElement {
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, ViewContext};
|
||||
use crate::{
|
||||
BorrowWindow, Bounds, ElementId, FocusHandle, InputHandlerView, LayoutId, Pixels, ViewContext,
|
||||
WindowInputHandler,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
pub(crate) use smallvec::SmallVec;
|
||||
use std::{any::Any, mem};
|
||||
|
@ -31,6 +34,14 @@ pub trait Element<V: 'static> {
|
|||
element_state: &mut Self::ElementState,
|
||||
cx: &mut ViewContext<V>,
|
||||
);
|
||||
|
||||
fn handle_text_input<'a>(
|
||||
&self,
|
||||
_view_state: &'a mut V,
|
||||
_cx: &mut ViewContext<V>,
|
||||
) -> Option<(Box<dyn InputHandlerView>, &'a FocusHandle)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
|
@ -154,6 +165,18 @@ where
|
|||
mut frame_state,
|
||||
} => {
|
||||
let bounds = cx.layout_bounds(layout_id);
|
||||
if let Some((input_handler, focus_handle)) =
|
||||
self.element.handle_text_input(view_state, cx)
|
||||
{
|
||||
if focus_handle.is_focused(cx) {
|
||||
cx.window.requested_input_handler = Some(Box::new(WindowInputHandler {
|
||||
cx: cx.app.this.clone(),
|
||||
window: cx.window_handle(),
|
||||
input_handler,
|
||||
element_bounds: bounds,
|
||||
}));
|
||||
}
|
||||
}
|
||||
if let Some(id) = self.element.id() {
|
||||
cx.with_element_state(id, |element_state, cx| {
|
||||
let mut element_state = element_state.unwrap();
|
||||
|
|
|
@ -305,7 +305,7 @@ pub trait PlatformInputHandler {
|
|||
new_selected_range: Option<Range<usize>>,
|
||||
);
|
||||
fn unmark_text(&mut self);
|
||||
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<f32>>;
|
||||
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -1484,10 +1484,10 @@ extern "C" fn first_rect_for_character_range(
|
|||
|bounds| {
|
||||
NSRect::new(
|
||||
NSPoint::new(
|
||||
frame.origin.x + bounds.origin.x as f64,
|
||||
frame.origin.y + frame.size.height - bounds.origin.y as f64,
|
||||
frame.origin.x + bounds.origin.x.0 as f64,
|
||||
frame.origin.y + frame.size.height - bounds.origin.y.0 as f64,
|
||||
),
|
||||
NSSize::new(bounds.size.width as f64, bounds.size.height as f64),
|
||||
NSSize::new(bounds.size.width.0 as f64, bounds.size.height.0 as f64),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
|
|
@ -2,14 +2,14 @@ use crate::{
|
|||
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
||||
Bounds, BoxShadow, Context, Corners, CursorStyle, DevicePixels, DispatchContext, DisplayId,
|
||||
Edges, Effect, Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId,
|
||||
GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, InputHandler, IsZero, KeyListener,
|
||||
KeyMatch, KeyMatcher, Keystroke, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite,
|
||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
|
||||
PlatformDisplay, PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptLevel,
|
||||
Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels,
|
||||
SceneBuilder, Shadow, SharedString, Size, Style, SubscriberSet, Subscription,
|
||||
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView,
|
||||
WindowBounds, WindowInputHandler, WindowOptions, SUBPIXEL_VARIANTS,
|
||||
GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch,
|
||||
KeyMatcher, Keystroke, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
|
||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay,
|
||||
PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render,
|
||||
RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow,
|
||||
SharedString, Size, Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline,
|
||||
UnderlineStyle, View, VisualContext, WeakView, WindowBounds, WindowInputHandler, WindowOptions,
|
||||
SUBPIXEL_VARIANTS,
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use collections::HashMap;
|
||||
|
@ -212,7 +212,7 @@ pub struct Window {
|
|||
default_prevented: bool,
|
||||
mouse_position: Point<Pixels>,
|
||||
requested_cursor_style: Option<CursorStyle>,
|
||||
requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
|
||||
pub(crate) requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
|
||||
scale_factor: f32,
|
||||
bounds: WindowBounds,
|
||||
bounds_observers: SubscriberSet<(), AnyObserver>,
|
||||
|
@ -2168,19 +2168,6 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<V> ViewContext<'_, V>
|
||||
where
|
||||
V: InputHandler + 'static,
|
||||
{
|
||||
pub fn handle_text_input(&mut self) {
|
||||
self.window.requested_input_handler = Some(Box::new(WindowInputHandler {
|
||||
cx: self.app.this.clone(),
|
||||
window: self.window_handle(),
|
||||
handler: self.view().downgrade(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> ViewContext<'_, V>
|
||||
where
|
||||
V: EventEmitter,
|
||||
|
|
|
@ -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…
Reference in a new issue