Make platform input handler private

Automatically record the context on non-view input handlers
Simplify the async window context update() method
This commit is contained in:
Mikayla 2024-01-20 06:56:04 -08:00
parent 0858db9ebb
commit 33105486aa
No known key found for this signature in database
17 changed files with 229 additions and 148 deletions

View file

@ -1,11 +1,11 @@
use editor::{Cursor, HighlightedRange, HighlightedRangeLine};
use gpui::{
div, fill, point, px, relative, AnyElement, AsyncWindowContext, AvailableSpace, BorrowWindow,
Bounds, DispatchPhase, Element, ElementId, FocusHandle, Font, FontStyle, FontWeight,
HighlightStyle, Hsla, InteractiveBounds, InteractiveElement, InteractiveElementState,
div, fill, point, px, relative, AnyElement, AvailableSpace, BorrowWindow, Bounds,
DispatchPhase, Element, ElementId, FocusHandle, Font, FontStyle, FontWeight, HighlightStyle,
Hsla, InputHandler, InteractiveBounds, InteractiveElement, InteractiveElementState,
Interactivity, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton,
MouseMoveEvent, Pixels, PlatformInputHandler, Point, ShapedLine, StatefulInteractiveElement,
Styled, TextRun, TextStyle, TextSystem, UnderlineStyle, WeakView, WhiteSpace, WindowContext,
MouseMoveEvent, Pixels, Point, ShapedLine, StatefulInteractiveElement, Styled, TextRun,
TextStyle, TextSystem, UnderlineStyle, WeakView, WhiteSpace, WindowContext,
};
use itertools::Itertools;
use language::CursorShape;
@ -749,7 +749,6 @@ impl Element for TerminalElement {
let origin = bounds.origin + Point::new(layout.gutter, px(0.));
let terminal_input_handler = TerminalInputHandler {
cx: cx.to_async(),
terminal: self.terminal.clone(),
cursor_bounds: layout
.cursor
@ -838,37 +837,35 @@ impl IntoElement for TerminalElement {
}
struct TerminalInputHandler {
cx: AsyncWindowContext,
terminal: Model<Terminal>,
workspace: WeakView<Workspace>,
cursor_bounds: Option<Bounds<Pixels>>,
}
impl PlatformInputHandler for TerminalInputHandler {
fn selected_text_range(&mut self) -> Option<std::ops::Range<usize>> {
self.cx
.update(|_, cx| {
if self
.terminal
.read(cx)
.last_content
.mode
.contains(TermMode::ALT_SCREEN)
{
None
} else {
Some(0..0)
}
})
.ok()
.flatten()
impl InputHandler for TerminalInputHandler {
fn selected_text_range(&mut self, cx: &mut WindowContext) -> Option<std::ops::Range<usize>> {
if self
.terminal
.read(cx)
.last_content
.mode
.contains(TermMode::ALT_SCREEN)
{
None
} else {
Some(0..0)
}
}
fn marked_text_range(&mut self) -> Option<std::ops::Range<usize>> {
fn marked_text_range(&mut self, _: &mut WindowContext) -> Option<std::ops::Range<usize>> {
None
}
fn text_for_range(&mut self, _: std::ops::Range<usize>) -> Option<String> {
fn text_for_range(
&mut self,
_: std::ops::Range<usize>,
_: &mut WindowContext,
) -> Option<String> {
None
}
@ -876,19 +873,16 @@ impl PlatformInputHandler for TerminalInputHandler {
&mut self,
_replacement_range: Option<std::ops::Range<usize>>,
text: &str,
cx: &mut WindowContext,
) {
self.cx
.update(|_, cx| {
self.terminal.update(cx, |terminal, _| {
terminal.input(text.into());
});
self.terminal.update(cx, |terminal, _| {
terminal.input(text.into());
});
self.workspace
.update(cx, |this, cx| {
let telemetry = this.project().read(cx).client().telemetry().clone();
telemetry.log_edit_event("terminal");
})
.ok();
self.workspace
.update(cx, |this, cx| {
let telemetry = this.project().read(cx).client().telemetry().clone();
telemetry.log_edit_event("terminal");
})
.ok();
}
@ -898,12 +892,17 @@ impl PlatformInputHandler for TerminalInputHandler {
_range_utf16: Option<std::ops::Range<usize>>,
_new_text: &str,
_new_selected_range: Option<std::ops::Range<usize>>,
_: &mut WindowContext,
) {
}
fn unmark_text(&mut self) {}
fn unmark_text(&mut self, _: &mut WindowContext) {}
fn bounds_for_range(&mut self, _range_utf16: std::ops::Range<usize>) -> Option<Bounds<Pixels>> {
fn bounds_for_range(
&mut self,
_range_utf16: std::ops::Range<usize>,
_: &mut WindowContext,
) -> Option<Bounds<Pixels>> {
self.cursor_bounds
}
}