audit tooltip

This commit is contained in:
2026-11-08 09:02:49 +07:00
parent 65cae95851
commit eb2273f87e
6 changed files with 289 additions and 37 deletions
+39 -2
View File
@@ -1,9 +1,10 @@
use gpui::prelude::FluentBuilder as _;
use gpui::{
App, BoxShadow, Div, ElementId, FocusHandle, Hsla, IntoElement, Refineable as _,
StatefulInteractiveElement, StyleRefinement, Styled, Window, div, px,
App, Bounds, BoxShadow, Div, ElementId, FocusHandle, Global, Hsla, IntoElement, Pixels,
Refineable as _, StatefulInteractiveElement, StyleRefinement, Styled, Window, div, px,
};
use gpui_native_theme::ActiveTheme as _;
use std::collections::HashMap;
/// A row whose children center on the cross axis.
pub fn h_flex() -> Div {
@@ -35,6 +36,42 @@ pub fn focus_handle(id: &ElementId, window: &mut Window, cx: &mut App) -> FocusH
.clone()
}
/// The painted bounds of elements that tooltips anchor to, keyed by element id.
#[derive(Clone, Default)]
struct AnchorBoundsRegistry(HashMap<ElementId, Bounds<Pixels>>);
impl Global for AnchorBoundsRegistry {}
impl AnchorBoundsRegistry {
fn record(&mut self, id: ElementId, bounds: Bounds<Pixels>) {
self.0.insert(id, bounds);
}
fn get(&self, id: &ElementId) -> Option<Bounds<Pixels>> {
self.0.get(id).copied()
}
}
/// The last bounds an element painted at.
pub fn anchor_bounds(id: &ElementId, cx: &mut App) -> Option<Bounds<Pixels>> {
cx.try_global::<AnchorBoundsRegistry>()
.and_then(|registry| registry.get(id))
}
/// A canvas prepaint closure recording the element's painted bounds.
pub fn record_anchor_bounds(
id: ElementId,
) -> impl Fn(Bounds<Pixels>, &mut Window, &mut App) + 'static {
move |bounds, _window, cx| {
let mut registry = cx
.try_global::<AnchorBoundsRegistry>()
.cloned()
.unwrap_or_default();
registry.record(id.clone(), bounds);
cx.set_global(registry);
}
}
/// Returns a focus ring shadow with the given color at half ink.
pub fn focus_ring(color: Hsla) -> Vec<BoxShadow> {
vec![BoxShadow::new(px(0.), px(0.), color.alpha(0.5)).spread_radius(px(3.))]