From 1a177f7ecd8bc1c526a056cb096bfeaf223c9a50 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 28 Aug 2021 10:28:50 +0200 Subject: [PATCH] Improve area introspection panel --- egui/src/context.rs | 35 +++++++++++++++++++++++++++-------- egui/src/id.rs | 3 ++- egui/src/layers.rs | 21 +++++++++++++++++++++ egui/src/memory.rs | 7 +++++-- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/egui/src/context.rs b/egui/src/context.rs index 472d34909..f96b3735a 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -722,6 +722,7 @@ impl Context { } } + /// Top-most layer at the given position. pub fn layer_id_at(&self, pos: Pos2) -> Option { let resize_grab_radius_side = self.style().interaction.resize_grab_radius_side; self.memory().layer_id_at(pos, resize_grab_radius_side) @@ -823,7 +824,7 @@ impl Context { )) .on_hover_text("Is egui currently listening for text input?"); ui.label(format!( - "keyboard focus widget: {}", + "Keyboard focus widget: {}", self.memory() .interaction .focus @@ -833,6 +834,22 @@ impl Context { .unwrap_or_default() )) .on_hover_text("Is egui currently listening for text input?"); + + let pointer_pos = self + .input() + .pointer + .hover_pos() + .map_or_else(String::new, |pos| format!("{:?}", pos)); + ui.label(format!("Pointer pos: {}", pointer_pos)); + + let top_layer = self + .input() + .pointer + .hover_pos() + .and_then(|pos| self.layer_id_at(pos)) + .map_or_else(String::new, |layer| layer.short_debug_format()); + ui.label(format!("Top layer under mouse: {}", top_layer)); + ui.add_space(16.0); ui.label(format!( @@ -864,7 +881,7 @@ impl Context { ui.horizontal(|ui| { ui.label(format!( - "{} areas (window positions)", + "{} areas (panels, windows, popups, …)", self.memory().areas.count() )); if ui.button("Reset").clicked() { @@ -872,18 +889,20 @@ impl Context { } }); ui.indent("areas", |ui| { + ui.label("Visible areas, ordered back to front."); + ui.label("Hover to highlight"); let layers_ids: Vec = self.memory().areas.order().to_vec(); for layer_id in layers_ids { let area = self.memory().areas.get(layer_id.id).cloned(); if let Some(area) = area { let is_visible = self.memory().areas.is_visible(&layer_id); + if !is_visible { + continue; + } + let text = format!("{} - {:?}", layer_id.short_debug_format(), area.rect(),); + // TODO: `Sense::hover_highlight()` if ui - .label(format!( - "{:?} {:?} {}", - layer_id.order, - area.rect(), - if is_visible { "" } else { "(INVISIBLE)" } - )) + .add(Label::new(text).monospace().sense(Sense::click())) .hovered && is_visible { diff --git a/egui/src/id.rs b/egui/src/id.rs index 6a901992a..ce641fc6f 100644 --- a/egui/src/id.rs +++ b/egui/src/id.rs @@ -57,7 +57,8 @@ impl Id { Id(hasher.finish()) } - pub(crate) fn short_debug_format(&self) -> String { + /// Short and readable summary + pub fn short_debug_format(&self) -> String { format!("{:04X}", self.0 as u16) } diff --git a/egui/src/layers.rs b/egui/src/layers.rs index f4b8bc879..7ba1ff662 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -48,6 +48,18 @@ impl Order { Self::Tooltip => false, } } + + /// Short and readable summary + pub fn short_debug_format(&self) -> &'static str { + match self { + Self::Background => "backg", + Self::PanelResizeLine => "panel", + Self::Middle => "middl", + Self::Foreground => "foreg", + Self::Tooltip => "toolt", + Self::Debug => "debug", + } + } } /// An identifier for a paint layer. @@ -82,6 +94,15 @@ impl LayerId { pub fn allow_interaction(&self) -> bool { self.order.allow_interaction() } + + /// Short and readable summary + pub fn short_debug_format(&self) -> String { + format!( + "{} {}", + self.order.short_debug_format(), + self.id.short_debug_format() + ) + } } /// A unique identifier of a specific [`Shape`] in a [`PaintList`]. diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 7aa0844b3..3bbbedb15 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -291,6 +291,7 @@ impl Memory { self.drag_value.end_frame(input); } + /// Top-most layer at the given position. pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option { self.areas.layer_id_at(pos, resize_interact_radius_side) } @@ -435,7 +436,7 @@ impl Memory { #[cfg_attr(feature = "persistence", serde(default))] pub struct Areas { areas: HashMap, - /// Top is last + /// Back-to-front. Top is last. order: Vec, visible_last_frame: HashSet, visible_current_frame: HashSet, @@ -457,6 +458,7 @@ impl Areas { self.areas.get(&id) } + /// Back-to-front. Top is last. pub(crate) fn order(&self) -> &[LayerId] { &self.order } @@ -469,11 +471,12 @@ impl Areas { } } + /// Top-most layer at the given position. pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option { for layer in self.order.iter().rev() { if self.is_visible(layer) { if let Some(state) = self.areas.get(&layer.id) { - let mut rect = Rect::from_min_size(state.pos, state.size); + let mut rect = state.rect(); if state.interactable { // Allow us to resize by dragging just outside the window: rect = rect.expand(resize_interact_radius_side);