Browse Source

Add mouse-over explanation to duplicate ID warning

pull/70/head
Emil Ernerfeldt 4 years ago
parent
commit
91e5295db2
  1. 1
      CHANGELOG.md
  2. 24
      egui/src/context.rs
  3. 6
      egui/src/painter.rs

1
CHANGELOG.md

@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* `ImageButton` - `ui.add(ImageButton::new(...))`.
* `ui.vertical_centered` and `ui.vertical_centered_justified`.
* Mouse-over explanation to duplicate ID warning
### Changed 🔧

24
egui/src/context.rs

@ -341,20 +341,34 @@ impl Context {
/// If the given `Id` is not unique, an error will be printed at the given position.
/// Call this for `Id`:s that need interaction or persistence.
pub(crate) fn register_interaction_id(self: &Arc<Self>, id: Id, new_pos: Pos2) {
if let Some(prev_pos) = self.memory().used_ids.insert(id, new_pos) {
let prev_pos = self.memory().used_ids.insert(id, new_pos);
if let Some(prev_pos) = prev_pos {
if prev_pos.distance(new_pos) < 0.1 {
// Likely same Widget being interacted with twice, which is fine.
return;
}
let show_error = |pos: Pos2, text: String| {
let painter = self.debug_painter();
let rect = painter.error(pos, text);
if let Some(mouse_pos) = self.input.mouse.pos {
if rect.contains(mouse_pos) {
painter.error(
rect.left_bottom() + vec2(2.0, 4.0),
"ID clashes happens when things like Windows or CollpasingHeaders share names,\n\
or when things like ScrollAreas and Resize areas aren't given unique id_source:s.",
);
}
}
};
let id_str = id.short_debug_format();
let painter = self.debug_painter();
if prev_pos.distance(new_pos) < 4.0 {
painter.error(new_pos, format!("Double use of ID {}", id_str));
show_error(new_pos, format!("Double use of ID {}", id_str));
} else {
painter.error(prev_pos, format!("First use of ID {}", id_str));
painter.error(new_pos, format!("Second use of ID {}", id_str));
show_error(prev_pos, format!("First use of ID {}", id_str));
show_error(new_pos, format!("Second use of ID {}", id_str));
}
// TODO: a tooltip explaining this.

6
egui/src/painter.rs

@ -139,18 +139,20 @@ impl Painter {
self.text(rect.min, LEFT_TOP, text.into(), text_style, color);
}
pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) {
pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
let text_style = TextStyle::Monospace;
let font = &self.fonts()[text_style];
let galley = font.layout_multiline(format!("🔥 {}", text), f32::INFINITY);
let rect = anchor_rect(Rect::from_min_size(pos, galley.size), LEFT_TOP);
let frame_rect = rect.expand(2.0);
self.add(PaintCmd::Rect {
rect: rect.expand(2.0),
rect: frame_rect,
corner_radius: 0.0,
fill: Srgba::black_alpha(240),
stroke: Stroke::new(1.0, color::RED),
});
self.galley(rect.min, galley, text_style, color::RED);
frame_rect
}
pub fn debug_arrow(&self, origin: Pos2, dir: Vec2, stroke: Stroke) {

Loading…
Cancel
Save