chore: audit all components (#2)
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
+41
-4
@@ -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,9 +36,45 @@ pub fn focus_handle(id: &ElementId, window: &mut Window, cx: &mut App) -> FocusH
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Returns a focus ring shadow with the given color.
|
||||
/// 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).spread_radius(px(3.))]
|
||||
vec![BoxShadow::new(px(0.), px(0.), color.alpha(0.5)).spread_radius(px(3.))]
|
||||
}
|
||||
|
||||
/// The next selectable row in the direction, or `None` at the ends.
|
||||
|
||||
Reference in New Issue
Block a user