Add event based drag API to GPUI, continue binding mouse handlers to terminal

This commit is contained in:
Mikayla 2023-12-05 12:07:17 -08:00
parent 8d57d6ca6f
commit ab140ee4c2
No known key found for this signature in database
9 changed files with 295 additions and 224 deletions

1
Cargo.lock generated
View file

@ -9478,6 +9478,7 @@ dependencies = [
"terminal2", "terminal2",
"theme2", "theme2",
"thiserror", "thiserror",
"ui2",
"util", "util",
"workspace2", "workspace2",
] ]

View file

@ -185,7 +185,7 @@ pub struct AppContext {
flushing_effects: bool, flushing_effects: bool,
pending_updates: usize, pending_updates: usize,
pub(crate) actions: Rc<ActionRegistry>, pub(crate) actions: Rc<ActionRegistry>,
pub(crate) active_drag: Option<AnyDrag>, pub(crate) active_drag: Option<AnyDragState>,
pub(crate) active_tooltip: Option<AnyTooltip>, pub(crate) active_tooltip: Option<AnyTooltip>,
pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>, pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>,
pub(crate) frame_consumers: HashMap<DisplayId, Task<()>>, pub(crate) frame_consumers: HashMap<DisplayId, Task<()>>,
@ -1172,6 +1172,34 @@ pub struct AnyDrag {
pub cursor_offset: Point<Pixels>, pub cursor_offset: Point<Pixels>,
} }
pub enum AnyDragState {
EventListener,
AnyDrag(AnyDrag),
}
impl AnyDragState {
pub fn any_drag(&self) -> Option<&AnyDrag> {
match self {
AnyDragState::EventListener => None,
AnyDragState::AnyDrag(any_drag) => Some(any_drag),
}
}
pub fn entity_id(&self) -> Option<EntityId> {
match self {
AnyDragState::EventListener => None,
AnyDragState::AnyDrag(any_drag) => Some(any_drag.view.entity_id()),
}
}
pub fn entity_type(&self) -> Option<TypeId> {
match self {
AnyDragState::EventListener => None,
AnyDragState::AnyDrag(any_drag) => Some(any_drag.view.entity_type()),
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct AnyTooltip { pub(crate) struct AnyTooltip {
pub view: AnyView, pub view: AnyView,

View file

@ -370,39 +370,6 @@ impl<E: Element> DrawableElement<E> {
} }
} }
// impl<V: 'static, E: Element> Element for DrawableElement<V, E> {
// type State = <E::Element as Element>::State;
// fn layout(
// &mut self,
// element_state: Option<Self::State>,
// cx: &mut WindowContext,
// ) -> (LayoutId, Self::State) {
// }
// fn paint(
// self,
// bounds: Bounds<Pixels>,
// element_state: &mut Self::State,
// cx: &mut WindowContext,
// ) {
// todo!()
// }
// }
// impl<V: 'static, E: 'static + Element> RenderOnce for DrawableElement<V, E> {
// type Element = Self;
// fn element_id(&self) -> Option<ElementId> {
// self.element.as_ref()?.element_id()
// }
// fn render_once(self) -> Self::Element {
// self
// }
// }
impl<E> ElementObject for Option<DrawableElement<E>> impl<E> ElementObject for Option<DrawableElement<E>>
where where
E: Element, E: Element,

View file

@ -1,10 +1,10 @@
use crate::{ use crate::{
point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext, point, px, Action, AnyDrag, AnyDragState, AnyElement, AnyTooltip, AnyView, AppContext,
BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle, BorrowAppContext, BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId,
IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent, FocusEvent, FocusHandle, IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, Render, ScrollWheelEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point,
SharedString, Size, StackingOrder, Style, StyleRefinement, Styled, Task, View, Visibility, Render, ScrollWheelEvent, SharedString, Size, StackingOrder, Style, StyleRefinement, Styled,
WindowContext, Task, View, Visibility, WindowContext,
}; };
use collections::HashMap; use collections::HashMap;
use refineable::Refineable; use refineable::Refineable;
@ -415,6 +415,19 @@ pub trait StatefulInteractiveElement: InteractiveElement {
self self
} }
fn on_drag_event(
mut self,
listener: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
) -> Self
where
Self: Sized,
{
self.interactivity()
.drag_event_listeners
.push(Box::new(listener));
self
}
fn on_hover(mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) -> Self fn on_hover(mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) -> Self
where where
Self: Sized, Self: Sized,
@ -559,6 +572,8 @@ pub type KeyDownListener = Box<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowC
pub type KeyUpListener = Box<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + 'static>; pub type KeyUpListener = Box<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + 'static>;
pub type DragEventListener = Box<dyn Fn(&MouseMoveEvent, &mut WindowContext) + 'static>;
pub type ActionListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + 'static>; pub type ActionListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + 'static>;
pub fn div() -> Div { pub fn div() -> Div {
@ -746,6 +761,7 @@ pub struct Interactivity {
pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>, pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>,
pub drop_listeners: SmallVec<[(TypeId, Box<DropListener>); 2]>, pub drop_listeners: SmallVec<[(TypeId, Box<DropListener>); 2]>,
pub click_listeners: SmallVec<[ClickListener; 2]>, pub click_listeners: SmallVec<[ClickListener; 2]>,
pub drag_event_listeners: SmallVec<[DragEventListener; 1]>,
pub drag_listener: Option<DragListener>, pub drag_listener: Option<DragListener>,
pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>, pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>,
pub tooltip_builder: Option<TooltipBuilder>, pub tooltip_builder: Option<TooltipBuilder>,
@ -890,8 +906,11 @@ impl Interactivity {
if phase == DispatchPhase::Bubble if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, &cx) && interactive_bounds.visibly_contains(&event.position, &cx)
{ {
if let Some(drag_state_type) = if let Some(drag_state_type) = cx
cx.active_drag.as_ref().map(|drag| drag.view.entity_type()) .active_drag
.as_ref()
.and_then(|drag| drag.any_drag())
.map(|drag| drag.view.entity_type())
{ {
for (drop_state_type, listener) in &drop_listeners { for (drop_state_type, listener) in &drop_listeners {
if *drop_state_type == drag_state_type { if *drop_state_type == drag_state_type {
@ -899,11 +918,14 @@ impl Interactivity {
.active_drag .active_drag
.take() .take()
.expect("checked for type drag state type above"); .expect("checked for type drag state type above");
let drag = drag.any_drag().expect("checked for any drag above");
listener(drag.view.clone(), cx); listener(drag.view.clone(), cx);
cx.notify(); cx.notify();
cx.stop_propagation(); cx.stop_propagation();
} }
} }
} else {
cx.active_drag = None;
} }
} }
}); });
@ -911,12 +933,16 @@ impl Interactivity {
let click_listeners = mem::take(&mut self.click_listeners); let click_listeners = mem::take(&mut self.click_listeners);
let drag_listener = mem::take(&mut self.drag_listener); let drag_listener = mem::take(&mut self.drag_listener);
let drag_event_listeners = mem::take(&mut self.drag_event_listeners);
if !click_listeners.is_empty() || drag_listener.is_some() { if !click_listeners.is_empty()
|| drag_listener.is_some()
|| !drag_event_listeners.is_empty()
{
let pending_mouse_down = element_state.pending_mouse_down.clone(); let pending_mouse_down = element_state.pending_mouse_down.clone();
let mouse_down = pending_mouse_down.borrow().clone(); let mouse_down = pending_mouse_down.borrow().clone();
if let Some(mouse_down) = mouse_down { if let Some(mouse_down) = mouse_down {
if let Some(drag_listener) = drag_listener { if !drag_event_listeners.is_empty() || drag_listener.is_some() {
let active_state = element_state.clicked_state.clone(); let active_state = element_state.clicked_state.clone();
let interactive_bounds = interactive_bounds.clone(); let interactive_bounds = interactive_bounds.clone();
@ -924,17 +950,29 @@ impl Interactivity {
if cx.active_drag.is_some() { if cx.active_drag.is_some() {
if phase == DispatchPhase::Capture { if phase == DispatchPhase::Capture {
cx.notify(); cx.notify();
} else if interactive_bounds.visibly_contains(&event.position, cx)
&& (event.position - mouse_down.position).magnitude()
> DRAG_THRESHOLD
{
for listener in &drag_event_listeners {
listener(event, cx);
}
} }
} else if phase == DispatchPhase::Bubble } else if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, cx) && interactive_bounds.visibly_contains(&event.position, cx)
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD && (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
{ {
*active_state.borrow_mut() = ElementClickedState::default(); *active_state.borrow_mut() = ElementClickedState::default();
let cursor_offset = event.position - bounds.origin; if let Some(drag_listener) = &drag_listener {
let drag = drag_listener(cursor_offset, cx); let cursor_offset = event.position - bounds.origin;
cx.active_drag = Some(drag); let drag = drag_listener(cursor_offset, cx);
cx.notify(); cx.active_drag = Some(AnyDragState::AnyDrag(drag));
cx.stop_propagation(); cx.notify();
cx.stop_propagation();
}
for listener in &drag_event_listeners {
listener(event, cx);
}
} }
}); });
} }
@ -1197,7 +1235,7 @@ impl Interactivity {
if let Some(drag) = cx.active_drag.take() { if let Some(drag) = cx.active_drag.take() {
for (state_type, group_drag_style) in &self.group_drag_over_styles { for (state_type, group_drag_style) in &self.group_drag_over_styles {
if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) { if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
if *state_type == drag.view.entity_type() if Some(*state_type) == drag.entity_type()
&& group_bounds.contains_point(&mouse_position) && group_bounds.contains_point(&mouse_position)
{ {
style.refine(&group_drag_style.style); style.refine(&group_drag_style.style);
@ -1206,7 +1244,7 @@ impl Interactivity {
} }
for (state_type, drag_over_style) in &self.drag_over_styles { for (state_type, drag_over_style) in &self.drag_over_styles {
if *state_type == drag.view.entity_type() if Some(*state_type) == drag.entity_type()
&& bounds && bounds
.intersect(&cx.content_mask().bounds) .intersect(&cx.content_mask().bounds)
.contains_point(&mouse_position) .contains_point(&mouse_position)
@ -1263,6 +1301,7 @@ impl Default for Interactivity {
action_listeners: SmallVec::new(), action_listeners: SmallVec::new(),
drop_listeners: SmallVec::new(), drop_listeners: SmallVec::new(),
click_listeners: SmallVec::new(), click_listeners: SmallVec::new(),
drag_event_listeners: SmallVec::new(),
drag_listener: None, drag_listener: None,
hover_listener: None, hover_listener: None,
tooltip_builder: None, tooltip_builder: None,

View file

@ -1159,12 +1159,15 @@ impl<'a> WindowContext<'a> {
}); });
if let Some(active_drag) = self.app.active_drag.take() { if let Some(active_drag) = self.app.active_drag.take() {
self.with_z_index(1, |cx| { if let Some(active_drag) = active_drag.any_drag() {
let offset = cx.mouse_position() - active_drag.cursor_offset; self.with_z_index(1, |cx| {
let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent); let offset = cx.mouse_position() - active_drag.cursor_offset;
active_drag.view.draw(offset, available_space, cx); let available_space =
cx.active_drag = Some(active_drag); size(AvailableSpace::MinContent, AvailableSpace::MinContent);
}); active_drag.view.draw(offset, available_space, cx);
});
}
self.active_drag = Some(active_drag);
} else if let Some(active_tooltip) = self.app.active_tooltip.take() { } else if let Some(active_tooltip) = self.app.active_tooltip.take() {
self.with_z_index(1, |cx| { self.with_z_index(1, |cx| {
let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent); let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent);
@ -1240,10 +1243,10 @@ impl<'a> WindowContext<'a> {
FileDropEvent::Entered { position, files } => { FileDropEvent::Entered { position, files } => {
self.window.mouse_position = position; self.window.mouse_position = position;
if self.active_drag.is_none() { if self.active_drag.is_none() {
self.active_drag = Some(AnyDrag { self.active_drag = Some(crate::AnyDragState::AnyDrag(AnyDrag {
view: self.build_view(|_| files).into(), view: self.build_view(|_| files).into(),
cursor_offset: position, cursor_offset: position,
}); }));
} }
InputEvent::MouseDown(MouseDownEvent { InputEvent::MouseDown(MouseDownEvent {
position, position,

View file

@ -1104,7 +1104,12 @@ impl Terminal {
} }
} }
pub fn mouse_drag(&mut self, e: MouseMoveEvent, origin: Point<Pixels>, region: Bounds<Pixels>) { pub fn mouse_drag(
&mut self,
e: &MouseMoveEvent,
origin: Point<Pixels>,
region: Bounds<Pixels>,
) {
let position = e.position - origin; let position = e.position - origin;
self.last_mouse_position = Some(position); self.last_mouse_position = Some(position);
@ -1130,7 +1135,7 @@ impl Terminal {
} }
} }
fn drag_line_delta(&mut self, e: MouseMoveEvent, region: Bounds<Pixels>) -> Option<Pixels> { fn drag_line_delta(&mut self, e: &MouseMoveEvent, region: Bounds<Pixels>) -> Option<Pixels> {
//TODO: Why do these need to be doubled? Probably the same problem that the IME has //TODO: Why do these need to be doubled? Probably the same problem that the IME has
let top = region.origin.y + (self.last_content.size.line_height * 2.); let top = region.origin.y + (self.last_content.size.line_height * 2.);
let bottom = region.lower_left().y - (self.last_content.size.line_height * 2.); let bottom = region.lower_left().y - (self.last_content.size.line_height * 2.);

View file

@ -21,6 +21,7 @@ workspace = { package = "workspace2", path = "../workspace2" }
db = { package = "db2", path = "../db2" } db = { package = "db2", path = "../db2" }
procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "5cd757e5f2eb039ed0c6bb6512223e69d5efc64d", default-features = false } procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "5cd757e5f2eb039ed0c6bb6512223e69d5efc64d", default-features = false }
terminal = { package = "terminal2", path = "../terminal2" } terminal = { package = "terminal2", path = "../terminal2" }
ui = { package = "ui2", path = "../ui2" }
smallvec.workspace = true smallvec.workspace = true
smol.workspace = true smol.workspace = true
mio-extras = "2.0.6" mio-extras = "2.0.6"

View file

@ -1,9 +1,10 @@
use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use editor::{Cursor, HighlightedRange, HighlightedRangeLine};
use gpui::{ use gpui::{
black, point, px, red, relative, transparent_black, AnyElement, Bounds, Element, ElementId, black, div, point, px, red, relative, transparent_black, AnyElement, AvailableSpace, Bounds,
Font, FontStyle, FontWeight, HighlightStyle, Hsla, IntoElement, LayoutId, Pixels, Point, Rgba, Element, ElementId, FocusHandle, Font, FontStyle, FontWeight, HighlightStyle, Hsla,
ShapedLine, Style, TextRun, TextStyle, TextSystem, UnderlineStyle, ViewContext, WeakModel, InteractiveElement, InteractiveElementState, IntoElement, LayoutId, ModelContext, Pixels,
WhiteSpace, WindowContext, Point, Rgba, ShapedLine, Size, StatefulInteractiveElement, Styled, TextRun, TextStyle,
TextSystem, UnderlineStyle, WeakModel, WhiteSpace, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::CursorShape; use language::CursorShape;
@ -20,12 +21,11 @@ use terminal::{
IndexedCell, Terminal, TerminalContent, TerminalSize, IndexedCell, Terminal, TerminalContent, TerminalSize,
}; };
use theme::{ActiveTheme, Theme, ThemeSettings}; use theme::{ActiveTheme, Theme, ThemeSettings};
use ui::Tooltip;
use std::mem; use std::mem;
use std::{fmt::Debug, ops::RangeInclusive}; use std::{fmt::Debug, ops::RangeInclusive};
use crate::TerminalView;
///The information generated during layout that is necessary for painting ///The information generated during layout that is necessary for painting
pub struct LayoutState { pub struct LayoutState {
cells: Vec<LayoutCell>, cells: Vec<LayoutCell>,
@ -146,14 +146,25 @@ impl LayoutRect {
///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection? ///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection?
pub struct TerminalElement { pub struct TerminalElement {
terminal: WeakModel<Terminal>, terminal: WeakModel<Terminal>,
focus: FocusHandle,
focused: bool, focused: bool,
cursor_visible: bool, cursor_visible: bool,
can_navigate_to_selected_word: bool, can_navigate_to_selected_word: bool,
interactivity: gpui::Interactivity,
} }
impl InteractiveElement for TerminalElement {
fn interactivity(&mut self) -> &mut gpui::Interactivity {
&mut self.interactivity
}
}
impl StatefulInteractiveElement for TerminalElement {}
impl TerminalElement { impl TerminalElement {
pub fn new( pub fn new(
terminal: WeakModel<Terminal>, terminal: WeakModel<Terminal>,
focus: FocusHandle,
focused: bool, focused: bool,
cursor_visible: bool, cursor_visible: bool,
can_navigate_to_selected_word: bool, can_navigate_to_selected_word: bool,
@ -161,8 +172,10 @@ impl TerminalElement {
TerminalElement { TerminalElement {
terminal, terminal,
focused, focused,
focus,
cursor_visible, cursor_visible,
can_navigate_to_selected_word, can_navigate_to_selected_word,
interactivity: Default::default(),
} }
} }
@ -365,7 +378,6 @@ impl TerminalElement {
//Setup layout information //Setup layout information
// todo!(Terminal tooltips) // todo!(Terminal tooltips)
// let link_style = settings.theme.editor.link_definition;
// let tooltip_style = settings.theme.tooltip.clone(); // let tooltip_style = settings.theme.tooltip.clone();
let buffer_font_size = settings.buffer_font_size(cx); let buffer_font_size = settings.buffer_font_size(cx);
@ -390,6 +402,20 @@ impl TerminalElement {
let settings = ThemeSettings::get_global(cx); let settings = ThemeSettings::get_global(cx);
let theme = cx.theme().clone(); let theme = cx.theme().clone();
let link_style = HighlightStyle {
color: Some(gpui::blue()),
font_weight: None,
font_style: None,
background_color: None,
underline: Some(UnderlineStyle {
thickness: px(1.0),
color: Some(gpui::red()),
wavy: false,
}),
fade_out: None,
};
let text_style = TextStyle { let text_style = TextStyle {
font_family, font_family,
font_features, font_features,
@ -439,38 +465,19 @@ impl TerminalElement {
let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { let last_hovered_word = terminal_handle.update(cx, |terminal, cx| {
terminal.set_size(dimensions); terminal.set_size(dimensions);
terminal.try_sync(cx); terminal.try_sync(cx);
// if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() { if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() {
// terminal.last_content.last_hovered_word.clone() terminal.last_content.last_hovered_word.clone()
// } else { } else {
None None
// } }
}); });
// let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| { let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| {
// let mut tooltip = Overlay::new( div()
// Empty::new() .size_full()
// .contained() .id("terminal-element")
// .constrained() .tooltip(move |cx| Tooltip::text(hovered_word.word.clone(), cx))
// .with_width(dimensions.width()) });
// .with_height(dimensions.height())
// .with_tooltip::<TerminalElement>(
// hovered_word.id,
// hovered_word.word,
// None,
// tooltip_style,
// cx,
// ),
// )
// .with_position_mode(gpui::OverlayPositionMode::Local)
// .into_any();
// tooltip.layout(
// SizeConstraint::new(Point::zero(), cx.window_size()),
// view_state,
// cx,
// );
// tooltip
// });
let TerminalContent { let TerminalContent {
cells, cells,
@ -498,10 +505,9 @@ impl TerminalElement {
cells, cells,
&text_style, &text_style,
&cx.text_system(), &cx.text_system(),
// todo!(Terminal tooltips) last_hovered_word
last_hovered_word, .as_ref()
// .as_ref() .map(|last_hovered_word| (link_style, &last_hovered_word.word_match)),
// .map(|last_hovered_word| (link_style, &last_hovered_word.word_match)),
cx, cx,
); );
@ -577,92 +583,95 @@ impl TerminalElement {
} }
} }
// todo!() fn generic_button_handler<E>(
// fn generic_button_handler<E>( connection: WeakModel<Terminal>,
// connection: WeakModel<Terminal>, origin: Point<Pixels>,
// origin: Point<Pixels>, focus_handle: FocusHandle,
// f: impl Fn(&mut Terminal, Point<Pixels>, E, &mut ModelContext<Terminal>), f: impl Fn(&mut Terminal, Point<Pixels>, &E, &mut ModelContext<Terminal>),
// ) -> impl Fn(E, &mut TerminalView, &mut EventContext<TerminalView>) { ) -> impl Fn(&E, &mut WindowContext) {
// move |event, _: &mut TerminalView, cx| { move |event, cx| {
// cx.focus_parent(); cx.focus(&focus_handle);
// if let Some(conn_handle) = connection.upgrade() { if let Some(conn_handle) = connection.upgrade() {
// conn_handle.update(cx, |terminal, cx| { conn_handle.update(cx, |terminal, cx| {
// f(terminal, origin, event, cx); f(terminal, origin, event, cx);
// cx.notify(); cx.notify();
// }) })
// } }
// } }
// } }
fn attach_mouse_handlers( fn paint_mouse_listeners(
&self, self,
origin: Point<Pixels>, origin: Point<Pixels>,
visible_bounds: Bounds<Pixels>,
mode: TermMode, mode: TermMode,
cx: &mut ViewContext<TerminalView>, bounds: Bounds<Pixels>,
) { cx: &mut WindowContext,
// todo!() ) -> Self {
// let connection = self.terminal; let focus = self.focus.clone();
let connection = self.terminal.clone();
// let mut region = MouseRegion::new::<Self>(cx.view_id(), 0, visible_bounds); self.on_mouse_down(gpui::MouseButton::Left, {
let connection = connection.clone();
let focus = focus.clone();
move |e, cx| {
cx.focus(&focus);
//todo!(context menu)
// v.context_menu.update(cx, |menu, _cx| menu.delay_cancel());
if let Some(conn_handle) = connection.upgrade() {
conn_handle.update(cx, |terminal, cx| {
terminal.mouse_down(&e, origin);
// // Terminal Emulator controlled behavior: cx.notify();
// region = region })
// // Start selections }
// .on_down(MouseButton::Left, move |event, v: &mut TerminalView, cx| { }
// let terminal_view = cx.handle(); })
// cx.focus(&terminal_view); .on_drag_event({
// v.context_menu.update(cx, |menu, _cx| menu.delay_cancel()); let connection = connection.clone();
// if let Some(conn_handle) = connection.upgrade() { let focus = focus.clone();
// conn_handle.update(cx, |terminal, cx| { move |e, cx| {
// terminal.mouse_down(&event, origin); if focus.is_focused(cx) {
if let Some(conn_handle) = connection.upgrade() {
conn_handle.update(cx, |terminal, cx| {
terminal.mouse_drag(e, origin, bounds);
cx.notify();
})
}
}
}
})
.on_mouse_up(
gpui::MouseButton::Left,
TerminalElement::generic_button_handler(
connection.clone(),
origin,
focus.clone(),
move |terminal, origin, e, cx| {
terminal.mouse_up(&e, origin, cx);
},
),
)
.on_click({
let connection = connection.clone();
move |e, cx| {
if e.down.button == gpui::MouseButton::Right {
let mouse_mode = if let Some(conn_handle) = connection.upgrade() {
conn_handle.update(cx, |terminal, _cx| {
terminal.mouse_mode(e.down.modifiers.shift)
})
} else {
// If we can't get the model handle, probably can't deploy the context menu
true
};
if !mouse_mode {
//todo!(context menu)
// view.deploy_context_menu(e.position, cx);
}
}
}
})
// cx.notify();
// })
// }
// })
// // Update drag selections
// .on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| {
// if event.end {
// return;
// }
// if cx.is_self_focused() {
// if let Some(conn_handle) = connection.upgrade() {
// conn_handle.update(cx, |terminal, cx| {
// terminal.mouse_drag(event, origin);
// cx.notify();
// })
// }
// }
// })
// // Copy on up behavior
// .on_up(
// MouseButton::Left,
// TerminalElement::generic_button_handler(
// connection,
// origin,
// move |terminal, origin, e, cx| {
// terminal.mouse_up(&e, origin, cx);
// },
// ),
// )
// // Context menu
// .on_click(
// MouseButton::Right,
// move |event, view: &mut TerminalView, cx| {
// let mouse_mode = if let Some(conn_handle) = connection.upgrade() {
// conn_handle.update(cx, |terminal, _cx| terminal.mouse_mode(event.shift))
// } else {
// // If we can't get the model handle, probably can't deploy the context menu
// true
// };
// if !mouse_mode {
// view.deploy_context_menu(event.position, cx);
// }
// },
// )
// .on_move(move |event, _: &mut TerminalView, cx| { // .on_move(move |event, _: &mut TerminalView, cx| {
// if cx.is_self_focused() { // if cx.is_self_focused() {
// if let Some(conn_handle) = connection.upgrade() { // if let Some(conn_handle) = connection.upgrade() {
@ -733,71 +742,88 @@ impl TerminalElement {
} }
impl Element for TerminalElement { impl Element for TerminalElement {
type State = (); type State = InteractiveElementState;
fn layout( fn layout(
&mut self, &mut self,
element_state: Option<Self::State>, element_state: Option<Self::State>,
cx: &mut WindowContext<'_>, cx: &mut WindowContext<'_>,
) -> (LayoutId, Self::State) { ) -> (LayoutId, Self::State) {
let mut style = Style::default(); let (layout_id, interactive_state) =
style.size.width = relative(1.).into(); self.interactivity
style.size.height = relative(1.).into(); .layout(element_state, cx, |mut style, cx| {
let layout_id = cx.request_layout(&style, None); style.size.width = relative(1.).into();
style.size.height = relative(1.).into();
let layout_id = cx.request_layout(&style, None);
(layout_id, ()) layout_id
});
(layout_id, interactive_state)
} }
fn paint(self, bounds: Bounds<Pixels>, _: &mut Self::State, cx: &mut WindowContext<'_>) { fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext<'_>) {
let layout = self.compute_layout(bounds, cx); let mut layout = self.compute_layout(bounds, cx);
let theme = cx.theme(); let theme = cx.theme();
cx.paint_quad( cx.paint_quad(
bounds, bounds,
Default::default(), Default::default(),
theme.colors().editor_background, layout.background_color,
Default::default(), Default::default(),
Hsla::default(), Hsla::default(),
); );
let origin = bounds.origin + Point::new(layout.gutter, px(0.)); let origin = bounds.origin + Point::new(layout.gutter, px(0.));
for rect in &layout.rects { let this = self.paint_mouse_listeners(origin, layout.mode, bounds, cx);
rect.paint(origin, &layout, cx);
}
cx.with_z_index(1, |cx| { this.interactivity
for (relative_highlighted_range, color) in layout.relative_highlighted_ranges.iter() { .paint(bounds, bounds.size, state, cx, |_, _, cx| {
if let Some((start_y, highlighted_range_lines)) = for rect in &layout.rects {
to_highlighted_range_lines(relative_highlighted_range, &layout, origin) rect.paint(origin, &layout, cx);
{
let hr = HighlightedRange {
start_y, //Need to change this
line_height: layout.size.line_height,
lines: highlighted_range_lines,
color: color.clone(),
//Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.size.line_height,
};
hr.paint(bounds, cx);
} }
}
});
cx.with_z_index(2, |cx| { cx.with_z_index(1, |cx| {
for cell in &layout.cells { for (relative_highlighted_range, color) in
cell.paint(origin, &layout, bounds, cx); layout.relative_highlighted_ranges.iter()
} {
}); if let Some((start_y, highlighted_range_lines)) =
to_highlighted_range_lines(relative_highlighted_range, &layout, origin)
{
let hr = HighlightedRange {
start_y, //Need to change this
line_height: layout.size.line_height,
lines: highlighted_range_lines,
color: color.clone(),
//Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.size.line_height,
};
hr.paint(bounds, cx);
}
}
});
cx.with_z_index(3, |cx| { cx.with_z_index(2, |cx| {
if let Some(cursor) = &layout.cursor { for cell in &layout.cells {
cursor.paint(origin, cx); cell.paint(origin, &layout, bounds, cx);
} }
}); });
// if let Some(element) = &mut element_state.hyperlink_tooltip { if this.cursor_visible {
// element.paint(origin, visible_bounds, view_state, cx) cx.with_z_index(3, |cx| {
// } if let Some(cursor) = &layout.cursor {
cursor.paint(origin, cx);
}
});
}
if let Some(element) = layout.hyperlink_tooltip.take() {
let width: AvailableSpace = bounds.size.width.into();
let height: AvailableSpace = bounds.size.height.into();
element.draw(origin, Size { width, height }, cx)
}
});
} }
// todo!() remove? // todo!() remove?

View file

@ -555,6 +555,7 @@ impl Render for TerminalView {
.on_action(cx.listener(TerminalView::select_all)) .on_action(cx.listener(TerminalView::select_all))
.child(TerminalElement::new( .child(TerminalElement::new(
terminal_handle, terminal_handle,
self.focus_handle.clone(),
focused, focused,
self.should_show_cursor(focused, cx), self.should_show_cursor(focused, cx),
self.can_navigate_to_selected_word, self.can_navigate_to_selected_word,