egui api changes

Guide for egui api changes

egui API Changes and Common Errors

Purpose

This document catalogs common egui API changes and compilation errors encountered when upgrading or working with egui 0.28.x.

Tooltip API Changes

show_tooltip_text Signature Change

Problem: The egui::show_tooltip_text function signature changed in egui 0.28, causing compilation errors when using the old 3-argument pattern.
Old Code (egui 0.27 and earlier):
egui::show_tooltip_text(ui.ctx(), egui::Id::new("my_tooltip"), |ui| {
    ui.label("Tooltip content");
});
Error in egui 0.28:
error[E0061]: this function takes 4 arguments but 3 arguments were supplied
   --> src/systems/ui/skills_ui.rs:121:9
    |
121 |         egui::show_tooltip_text(ui.ctx(), egui::Id::new(minified_name), |ui| {
    |         ^^^^^^^^^^^^^^^^^^^^^^^ unexpected argument #3
    |
note: function defined here
   --> .cargo/registry/src/.../egui-0.28.1/src/containers/popup.rs:300:8
    |
300 | pub fn show_tooltip_text(
    |        ^^^^^^^^^^^^^^^^^
New Code (egui 0.28):
// Option 1: Use response.on_hover_ui (RECOMMENDED)
let response = ui.button("Hover me");
if response.hovered() {
    response.on_hover_ui(|ui| {
        ui.label("Tooltip content");
    });
}

// Option 2: Use show_tooltip_text with all 4 arguments
egui::show_tooltip_text(
    ui.ctx(),
    LayerId::new(Order::Tooltip, egui::Id::new("my_tooltip")),
    egui::Id::new("my_tooltip"),
    "Tooltip text"
);
Recommendation: Use response.on_hover_ui() for custom tooltips. It's more idiomatic and handles positioning automatically.

Color32 API Changes

Color32::black() Removed

Problem: Color32::black() is not available in egui 0.28.
Old Code:
.stroke(egui::Stroke::new(1.0, egui::Color32::black()))
Error:
error[E0599]: no function or associated item named `black` found for struct `Color32`
  --> src/systems/ui/skills_ui.rs:96:55
   |
96 | ...::Color32::black()))
   |               ^^^^^ function or associated item not found in `Color32`
New Code:
// Use Color32::from_rgb for black
.stroke(egui::Stroke::new(1.0, egui::Color32::from_rgb(0, 0, 0)))

// Or use the BLACK constant
.stroke(egui::Stroke::new(1.0, egui::Color32::BLACK))
Available Color32 Constants:
egui::Color32::BLACK
egui::Color32::WHITE
egui::Color32::RED
egui::Color32::GREEN
egui::Color32::BLUE
egui::Color32::YELLOW
egui::Color32::TRANSPARENT
// ... and more

Type Alias Restrictions

Cannot Derive on Type Aliases

Problem: Attempting to use #[derive] on a type alias causes a compilation error.
Old Code:
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, Resource)]
pub use crate::systems::combat::Style as AttackStyle;
Error:
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
   --> src/systems/ui/mod.rs:194:1
    |
194 | #[derive(Default, PartialEq, Eq, Clone, Copy, Debug, Resource)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not applicable here
195 | pub use crate::systems::combat::Style as AttackStyle;
    | ----------------------------------------------------- not a `struct`, `enum` or `union`
New Code:
// Remove #[derive] from type alias
pub use crate::systems::combat::Style as AttackStyle;

// Ensure the original type has the necessary derives
// In src/systems/combat/mod.rs:
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Style {
    #[default]
    Accurate,
    Aggressive,
    Defensive,
    Controlled,
    Longrange,
}
Explanation: Type aliases inherit traits from the original type. Add derives to the source type, not the alias.

Common Patterns

Creating Custom Tooltips

Pattern:
let response = egui::Frame::none()
    .fill(egui::Color32::from_rgb(45, 40, 35))
    .stroke(egui::Stroke::new(1.0, egui::Color32::from_rgb(0, 0, 0)))
    .show(ui, |ui| {
        // Widget content
        ui.label("Content");
    }).response;

// Add tooltip
if response.hovered() {
    response.on_hover_ui(|ui| {
        ui.heading("Tooltip Title");
        ui.separator();
        ui.label("Tooltip details");
    });
}

Compact Grid Layout

Pattern:
egui::Grid::new("my_grid")
    .spacing(egui::vec2(4.0, 4.0))  // Tight spacing
    .show(ui, |ui| {
        for (i, item) in items.iter().enumerate() {
            render_item(ui, item);
            
            if (i + 1) % 3 == 0 {  // 3 columns
                ui.end_row();
            }
        }
    });

Fixed-Size Cells

Pattern:
let cell_size = egui::vec2(62.0, 30.0);

egui::Frame::none()
    .show(ui, |ui| {
        ui.set_min_size(cell_size);  // Force minimum size
        ui.horizontal(|ui| {
            // Cell content
        });
    });

Migration Checklist

When upgrading egui or encountering API errors:
  • Replace show_tooltip_text with response.on_hover_ui
  • Replace Color32::black() with Color32::from_rgb(0, 0, 0) or Color32::BLACK
  • Remove #[derive] from type aliases
  • Check for deprecated methods in egui changelog
  • Test all UI interactions after upgrade
  • Update tooltip implementations
  • Verify color constants are available

References