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

@ -1,20 +1,44 @@
use std::time::Duration;
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;
const DELAY: Duration = Duration::from_millis(500);
#[derive(Clone, Debug)]
pub struct TextTooltip {
title: SharedString,
visible: bool,
}
impl TextTooltip {
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>> {
cx.build_view(|cx| TextTooltip::new(str))
pub fn build_view(str: SharedString, cx: &mut WindowContext) -> View<Self> {
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 {
let theme = cx.theme();
div()
.when(!self.visible, |this| this.invisible())
.bg(theme.colors().background)
.rounded(px(8.))
.border()
.font("Zed Sans")
.border_color(theme.colors().border)
.text_color(theme.colors().text)
.pl_2()