Tooltips in mouse event handler & fix executor timer

Co-Authored-By: Conrad Irwin <conrad@zed.dev>
This commit is contained in:
Julia 2023-11-03 18:02:58 -04:00
parent f97046b86f
commit 3834e26f71
6 changed files with 67 additions and 59 deletions

View file

@ -20,6 +20,7 @@ fn generate_dispatch_bindings() {
.header("src/platform/mac/dispatch.h") .header("src/platform/mac/dispatch.h")
.allowlist_var("_dispatch_main_q") .allowlist_var("_dispatch_main_q")
.allowlist_var("DISPATCH_QUEUE_PRIORITY_DEFAULT") .allowlist_var("DISPATCH_QUEUE_PRIORITY_DEFAULT")
.allowlist_var("DISPATCH_TIME_NOW")
.allowlist_function("dispatch_get_global_queue") .allowlist_function("dispatch_get_global_queue")
.allowlist_function("dispatch_async_f") .allowlist_function("dispatch_async_f")
.allowlist_function("dispatch_after_f") .allowlist_function("dispatch_after_f")

View file

@ -17,7 +17,6 @@ use std::{
ops::Deref, ops::Deref,
path::PathBuf, path::PathBuf,
sync::Arc, sync::Arc,
time::{Duration, Instant},
}; };
const DRAG_THRESHOLD: f64 = 2.; const DRAG_THRESHOLD: f64 = 2.;
@ -602,13 +601,14 @@ pub trait ElementInteraction<V: 'static>: 'static {
if let Some(hover_listener) = stateful.hover_listener.take() { if let Some(hover_listener) = stateful.hover_listener.take() {
let was_hovered = element_state.hover_state.clone(); let was_hovered = element_state.hover_state.clone();
let has_mouse_down = element_state.pending_mouse_down.lock().is_some(); let has_mouse_down = element_state.pending_mouse_down.clone();
cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| { cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
if phase != DispatchPhase::Bubble { if phase != DispatchPhase::Bubble {
return; return;
} }
let is_hovered = bounds.contains_point(&event.position) && !has_mouse_down; let is_hovered =
bounds.contains_point(&event.position) && has_mouse_down.lock().is_none();
let mut was_hovered = was_hovered.lock(); let mut was_hovered = was_hovered.lock();
if is_hovered != was_hovered.clone() { if is_hovered != was_hovered.clone() {
@ -620,33 +620,30 @@ pub trait ElementInteraction<V: 'static>: 'static {
}); });
} }
// if we're hovered: if let Some(tooltip_builder) = stateful.tooltip_builder.take() {
// if no timer, start timer let tooltip_view = element_state.tooltip_view.clone();
// if timer hits 1s, call tooltip_builder() let pending_mouse_down = element_state.pending_mouse_down.clone();
//
if let Some(tooltip_builder) = &stateful.tooltip_builder { cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
let mut active_tooltip = element_state.active_tooltip.lock(); if phase != DispatchPhase::Bubble {
let is_hovered = bounds.contains_point(&cx.mouse_position()) return;
&& !element_state.pending_mouse_down.lock().is_some(); }
if is_hovered { let is_hovered = bounds.contains_point(&event.position)
if let Some(active_tooltip) = active_tooltip { && pending_mouse_down.lock().is_none();
active_tooltip.view = Some(tooltip_builder(cx)) let mut tooltip_view = tooltip_view.lock();
if is_hovered {
if tooltip_view.is_none() {
*tooltip_view = Some(tooltip_builder(view_state, cx));
}
} else { } else {
*active_tooltip = Some(ActiveTooltip { tooltip_view.take();
hover_start: Instant::now(),
view: None,
});
} }
} else { });
active_tooltip.take();
}
if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() { if let Some(active_tooltip) = element_state.tooltip_view.lock().as_ref() {
if *element_state.hover_state.lock() { cx.active_tooltip = Some(active_tooltip.clone());
cx.active_tooltip = Some(active_tooltip.clone());
}
} }
} }
@ -837,12 +834,7 @@ pub struct InteractiveElementState {
hover_state: Arc<Mutex<bool>>, hover_state: Arc<Mutex<bool>>,
pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>, pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>,
scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>, scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>,
active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>, tooltip_view: Arc<Mutex<Option<AnyView>>>,
}
pub struct ActiveTooltip {
hover_start: Instant,
view: Option<AnyView>,
} }
impl InteractiveElementState { impl InteractiveElementState {
@ -1194,7 +1186,7 @@ pub(crate) type DragListener<V> =
pub(crate) type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>; pub(crate) type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>;
pub(crate) type TooltipBuilder<V> = Arc<dyn Fn(&mut ViewContext<V>) -> AnyView + 'static>; pub(crate) type TooltipBuilder<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>;
pub type KeyListener<V> = Box< pub type KeyListener<V> = Box<
dyn Fn( dyn Fn(

View file

@ -11,11 +11,7 @@ use objc::{
}; };
use parking::{Parker, Unparker}; use parking::{Parker, Unparker};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{ffi::c_void, sync::Arc, time::Duration};
ffi::c_void,
sync::Arc,
time::{Duration, SystemTime},
};
include!(concat!(env!("OUT_DIR"), "/dispatch_sys.rs")); include!(concat!(env!("OUT_DIR"), "/dispatch_sys.rs"));
@ -62,16 +58,10 @@ impl PlatformDispatcher for MacDispatcher {
} }
fn dispatch_after(&self, duration: Duration, runnable: Runnable) { fn dispatch_after(&self, duration: Duration, runnable: Runnable) {
let now = SystemTime::now();
let after_duration = now
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64
+ duration.as_nanos() as u64;
unsafe { unsafe {
let queue = let queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT.try_into().unwrap(), 0); dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT.try_into().unwrap(), 0);
let when = dispatch_time(0, after_duration as i64); let when = dispatch_time(DISPATCH_TIME_NOW as u64, duration.as_nanos() as i64);
dispatch_after_f( dispatch_after_f(
when, when,
queue, queue,

View file

@ -989,11 +989,14 @@ impl<'a> WindowContext<'a> {
}); });
} else if let Some(active_tooltip) = self.app.active_tooltip.take() { } else if let Some(active_tooltip) = self.app.active_tooltip.take() {
self.stack(1, |cx| { self.stack(1, |cx| {
cx.with_element_offset(Some(cx.mouse_position()), |cx| { cx.with_element_offset(
let available_space = Some(cx.mouse_position() + Point::new(px(8.0), px(8.0))),
size(AvailableSpace::MinContent, AvailableSpace::MinContent); |cx| {
active_tooltip.draw(available_space, cx); let available_space =
}); size(AvailableSpace::MinContent, AvailableSpace::MinContent);
active_tooltip.draw(available_space, cx);
},
);
}); });
} }

View file

@ -1,20 +1,44 @@
use std::time::Duration;
use gpui2::{ use gpui2::{
div, px, Div, ParentElement, Render, SharedString, Styled, View, ViewContext, VisualContext, div, px, Component, Div, ParentElement, Render, SharedString, Styled, View, ViewContext,
VisualContext, WindowContext,
}; };
use theme2::ActiveTheme; use theme2::ActiveTheme;
const DELAY: Duration = Duration::from_millis(500);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TextTooltip { pub struct TextTooltip {
title: SharedString, title: SharedString,
visible: bool,
} }
impl TextTooltip { impl TextTooltip {
pub fn new(str: SharedString) -> Self { pub fn new(str: SharedString) -> Self {
Self { title: str } Self {
title: str,
visible: false,
}
} }
pub fn build_view<C: VisualContext>(str: SharedString, cx: &mut C) -> C::Result<View<Self>> { pub fn build_view(str: SharedString, cx: &mut WindowContext) -> View<Self> {
cx.build_view(|cx| TextTooltip::new(str)) let view = cx.build_view(|cx| TextTooltip::new(str));
let handle = view.downgrade();
cx.spawn(|mut cx| async move {
cx.background_executor().timer(DELAY).await;
handle
.update(&mut cx, |this, cx| {
this.visible = true;
cx.notify();
})
.ok();
})
.detach();
view
} }
} }
@ -24,9 +48,11 @@ impl Render for TextTooltip {
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let theme = cx.theme(); let theme = cx.theme();
div() div()
.when(!self.visible, |this| this.invisible())
.bg(theme.colors().background) .bg(theme.colors().background)
.rounded(px(8.)) .rounded(px(8.))
.border() .border()
.font("Zed Sans")
.border_color(theme.colors().border) .border_color(theme.colors().border)
.text_color(theme.colors().text) .text_color(theme.colors().text)
.pl_2() .pl_2()

View file

@ -9,8 +9,8 @@ use crate::{
use anyhow::Result; use anyhow::Result;
use collections::{HashMap, HashSet, VecDeque}; use collections::{HashMap, HashSet, VecDeque};
use gpui::{ use gpui::{
AnyView, AppContext, AsyncWindowContext, Component, Div, EntityId, EventEmitter, FocusHandle, AppContext, AsyncWindowContext, Component, Div, EntityId, EventEmitter, FocusHandle, Model,
Model, PromptLevel, Render, Task, View, ViewContext, VisualContext, WeakView, WindowContext, PromptLevel, Render, Task, View, ViewContext, VisualContext, WeakView, WindowContext,
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
use project2::{Project, ProjectEntryId, ProjectPath}; use project2::{Project, ProjectEntryId, ProjectPath};
@ -1398,13 +1398,9 @@ impl Pane {
.group("") .group("")
.id(item.id()) .id(item.id())
.cursor_pointer() .cursor_pointer()
.on_hover(|_, hovered, _| {
dbg!(hovered);
})
.when_some(item.tab_tooltip_text(cx), |div, text| { .when_some(item.tab_tooltip_text(cx), |div, text| {
div.tooltip(move |_, cx| TextTooltip::build_view(text.clone(), cx)) div.tooltip(move |_, cx| TextTooltip::build_view(text.clone(), cx))
}) })
// .tooltip(|pane, cx| cx.build_view(|cx| div().child(title)))
// .on_drag(move |pane, cx| pane.render_tab(ix, item.boxed_clone(), detail, cx)) // .on_drag(move |pane, cx| pane.render_tab(ix, item.boxed_clone(), detail, cx))
// .drag_over::<DraggedTab>(|d| d.bg(cx.theme().colors().element_drop_target)) // .drag_over::<DraggedTab>(|d| d.bg(cx.theme().colors().element_drop_target))
// .on_drop(|_view, state: View<DraggedTab>, cx| { // .on_drop(|_view, state: View<DraggedTab>, cx| {