Skip to content

Commit e54ec7c

Browse files
emilkhacknus
authored andcommitted
When debugging widget rects on hover, show width and height (emilk#4762)
As requested by @gavrelina ![image](https://github.com/emilk/egui/assets/1148717/15d9600a-ae2e-43dd-981b-c690f9b1bdf1)
1 parent b809764 commit e54ec7c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

crates/egui/src/painter.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,14 @@ impl Painter {
290290
let galley = self.layout_no_wrap(text.to_string(), FontId::monospace(12.0), color);
291291
let rect = anchor.anchor_size(pos, galley.size());
292292
let frame_rect = rect.expand(2.0);
293-
self.add(Shape::rect_filled(
294-
frame_rect,
295-
0.0,
296-
Color32::from_black_alpha(150),
297-
));
293+
294+
let is_text_bright = color.is_additive() || epaint::Rgba::from(color).intensity() > 0.5;
295+
let bg_color = if is_text_bright {
296+
Color32::from_black_alpha(150)
297+
} else {
298+
Color32::from_white_alpha(150)
299+
};
300+
self.add(Shape::rect_filled(frame_rect, 0.0, bg_color));
298301
self.galley(rect.min, galley, color);
299302
frame_rect
300303
}

crates/egui/src/ui.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,13 +2621,32 @@ fn register_rect(ui: &Ui, rect: Rect) {
26212621

26222622
// Paint rectangle around widget:
26232623
{
2624+
// Print width and height:
2625+
let text_color = if ui.visuals().dark_mode {
2626+
Color32::WHITE
2627+
} else {
2628+
Color32::BLACK
2629+
};
2630+
painter.debug_text(
2631+
rect.left_center() + 2.0 * Vec2::LEFT,
2632+
Align2::RIGHT_CENTER,
2633+
text_color,
2634+
format!("H: {:.1}", rect.height()),
2635+
);
2636+
painter.debug_text(
2637+
rect.center_top(),
2638+
Align2::CENTER_BOTTOM,
2639+
text_color,
2640+
format!("W: {:.1}", rect.width()),
2641+
);
2642+
2643+
// Paint rect:
26242644
let rect_fg_color = if is_clicking {
26252645
Color32::WHITE
26262646
} else {
26272647
Color32::LIGHT_BLUE
26282648
};
26292649
let rect_bg_color = Color32::BLUE.gamma_multiply(0.5);
2630-
26312650
painter.rect(rect, 0.0, rect_bg_color, (1.0, rect_fg_color));
26322651
}
26332652

@@ -2655,7 +2674,7 @@ fn register_rect(ui: &Ui, rect: Rect) {
26552674
let screen_rect = ui.ctx().screen_rect();
26562675
let y = if galley.size().y <= rect.top() {
26572676
// Above
2658-
rect.top() - galley.size().y
2677+
rect.top() - galley.size().y - 16.0
26592678
} else {
26602679
// Below
26612680
rect.bottom()

0 commit comments

Comments
 (0)