chore: reduce component overhead (#3)
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
+16
-18
@@ -1,7 +1,8 @@
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, Bounds, BoxShadow, Div, ElementId, FocusHandle, Global, Hsla, IntoElement, Pixels,
|
||||
Refineable as _, StatefulInteractiveElement, StyleRefinement, Styled, Window, div, px,
|
||||
Refineable as _, StatefulInteractiveElement, StyleRefinement, Styled, Window, WindowId, div,
|
||||
px,
|
||||
};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
use std::collections::HashMap;
|
||||
@@ -36,39 +37,36 @@ 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>>);
|
||||
/// The painted bounds of elements that tooltips anchor to, per window and element id.
|
||||
#[derive(Default)]
|
||||
struct AnchorBoundsRegistry(HashMap<(WindowId, ElementId), Bounds<Pixels>>);
|
||||
|
||||
impl Global for AnchorBoundsRegistry {}
|
||||
|
||||
impl AnchorBoundsRegistry {
|
||||
fn record(&mut self, id: ElementId, bounds: Bounds<Pixels>) {
|
||||
self.0.insert(id, bounds);
|
||||
fn record(&mut self, window: WindowId, id: ElementId, bounds: Bounds<Pixels>) {
|
||||
self.0.insert((window, id), bounds);
|
||||
}
|
||||
|
||||
fn get(&self, id: &ElementId) -> Option<Bounds<Pixels>> {
|
||||
self.0.get(id).copied()
|
||||
fn get(&self, window: WindowId, id: &ElementId) -> Option<Bounds<Pixels>> {
|
||||
self.0.get(&(window, id.clone())).copied()
|
||||
}
|
||||
}
|
||||
|
||||
/// The last bounds an element painted at.
|
||||
pub fn anchor_bounds(id: &ElementId, cx: &mut App) -> Option<Bounds<Pixels>> {
|
||||
/// The last bounds an element painted at, in the given window.
|
||||
pub fn anchor_bounds(window: WindowId, id: &ElementId, cx: &App) -> Option<Bounds<Pixels>> {
|
||||
cx.try_global::<AnchorBoundsRegistry>()
|
||||
.and_then(|registry| registry.get(id))
|
||||
.and_then(|registry| registry.get(window, 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);
|
||||
move |bounds, window, cx| {
|
||||
let window_id = window.window_handle().window_id();
|
||||
cx.default_global::<AnchorBoundsRegistry>()
|
||||
.record(window_id, id.clone(), bounds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user