use std::collections::HashSet;
use std::rc::Rc;
use gpui_kit::base::{Root, RootPlugin};
use gpui_kit::{
App, Bounds, Context, DivInspectorState, Empty, Global, InspectorElementId,
InspectorElementPath, IntoElement, Pixels, PlatformInput, Render, ScrollDelta, ScrollEvent,
ScrollSource, TouchPhase, Window, point, px, rems,
};
const LAYER_DELTA: f32 = 36.;
const MAX_LAYERS: usize = 8;
const INSPECTOR_WIDTH_REM: f32 = 30.;
const COVER_SLACK: f32 = 1.;
const KIT_CRATES: [&str; 2] = ["gpui-component-", "gpui-base-"];
#[derive(Default)]
struct PassThrough(HashSet<Rc<InspectorElementPath>>);
impl Global for PassThrough {}
pub(super) fn register(cx: &mut App) {
cx.set_global(PassThrough::default());
Root::register_plugin::<SkipWindowLayers>(cx, |_, _| SkipWindowLayers);
}
struct SkipWindowLayers;
impl Render for SkipWindowLayers {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
impl RootPlugin for SkipWindowLayers {
fn prepare(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if !window.is_inspector_picking(cx) {
return;
}
let Some(inspector) = super::inspector_of(window, cx) else {
return;
};
for _ in 0..MAX_LAYERS {
let on_layer = inspector
.read(cx)
.active_element_id()
.is_some_and(|id| is_layer(id, cx));
if !on_layer {
return;
}
window.dispatch_event(one_layer_down(window), cx);
}
}
}
fn one_layer_down(window: &Window) -> PlatformInput {
PlatformInput::Scroll(ScrollEvent {
position: window.mouse_position(),
delta: ScrollDelta::Pixels(point(px(0.), px(LAYER_DELTA))),
modifiers: window.modifiers(),
touch_phase: TouchPhase::Moved,
source: ScrollSource::Pointer,
})
}
fn is_layer(id: &InspectorElementId, cx: &App) -> bool {
is_root_container(id.path.source_location.file())
|| cx
.try_global::<PassThrough>()
.is_some_and(|seen| seen.0.contains(&id.path))
}
pub(super) fn learn(
id: &InspectorElementId,
state: &DivInspectorState,
window: &Window,
cx: &mut App,
) -> bool {
let layer = is_kit_source(id.path.source_location.file())
&& state.base_style.background.is_none()
&& covers_window(state.bounds, window);
if layer {
cx.default_global::<PassThrough>().0.insert(id.path.clone());
}
layer || is_layer(id, cx)
}
fn covers_window(bounds: Bounds<Pixels>, window: &Window) -> bool {
let viewport = window.viewport_size();
let width = viewport.width - rems(INSPECTOR_WIDTH_REM).to_pixels(window.rem_size());
let slack = px(COVER_SLACK);
bounds.origin.x <= slack
&& bounds.origin.y <= slack
&& bounds.size.width + slack >= width
&& bounds.size.height + slack >= viewport.height
}
fn kit_relative(file: &str) -> Option<&str> {
if let Some((_, relative)) = file.rsplit_once("vendor/gpui-base/") {
return Some(relative);
}
KIT_CRATES.iter().find_map(|name| {
let (_, rest) = file.rsplit_once(name)?;
let (version, path) = rest.split_once('/')?;
version
.as_bytes()
.first()
.is_some_and(u8::is_ascii_digit)
.then_some(path)
})
}
fn is_kit_source(file: &str) -> bool {
kit_relative(file).is_some()
}
pub(super) fn is_root_container(file: &str) -> bool {
kit_relative(file) == Some("src/root.rs")
}