From 132d0ec4305e999a47650bbc3e2f2b7ad935911f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 10 Feb 2024 13:20:01 +0100 Subject: [PATCH] Add `Options::debug_paint_interactive_widgets` (#4018) This paints the rects of all interactive widgets on each layer. ![image](https://github.com/emilk/egui/assets/1148717/3584c4fb-06ce-43d4-bd7c-c5baf927ddf1) --- crates/egui/src/context.rs | 21 +++++++++++++++++++++ crates/egui/src/memory.rs | 7 +++++++ 2 files changed, 28 insertions(+) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index f3d2a02cb14..3566042dda6 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -1842,6 +1842,27 @@ impl Context { self.read(|ctx| ctx.plugins.clone()).on_end_frame(self); + if self.options(|o| o.debug_paint_interactive_widgets) { + let rects = self.write(|ctx| ctx.viewport().layer_rects_this_frame.clone()); + for (layer_id, rects) in rects.by_layer { + let painter = Painter::new(self.clone(), layer_id, Rect::EVERYTHING); + for rect in rects { + if rect.sense.interactive() { + let (color, text) = if rect.sense.click && rect.sense.drag { + (Color32::from_rgb(0x88, 0, 0x88), "click+drag") + } else if rect.sense.click { + (Color32::from_rgb(0x88, 0, 0), "click") + } else if rect.sense.drag { + (Color32::from_rgb(0, 0, 0x88), "drag") + } else { + (Color32::from_rgb(0, 0, 0x88), "hover") + }; + painter.debug_rect(rect.rect, color, text); + } + } + } + } + self.write(|ctx| ctx.end_frame()) } } diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index d179d5bbcb7..8b969dc4bb5 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -214,6 +214,9 @@ pub struct Options { /// /// By default this is `true` in debug builds. pub warn_on_id_clash: bool, + + /// If true, paint all interactive widgets in the order they were added to each layer. + pub debug_paint_interactive_widgets: bool, } impl Default for Options { @@ -227,6 +230,7 @@ impl Default for Options { screen_reader: false, preload_font_glyphs: true, warn_on_id_clash: cfg!(debug_assertions), + debug_paint_interactive_widgets: false, } } } @@ -243,6 +247,7 @@ impl Options { screen_reader: _, // needs to come from the integration preload_font_glyphs: _, warn_on_id_clash, + debug_paint_interactive_widgets, } = self; use crate::Widget as _; @@ -261,6 +266,8 @@ impl Options { ); ui.checkbox(warn_on_id_clash, "Warn if two widgets have the same Id"); + + ui.checkbox(debug_paint_interactive_widgets, "Debug interactive widgets"); }); use crate::containers::*;