From b7e11af52ed1785354b8915d3d2fad7e0588b7d5 Mon Sep 17 00:00:00 2001 From: adrien <221212@umons.ac.be> Date: Tue, 17 Mar 2026 17:39:57 +0100 Subject: [PATCH] rename comment & variable, add const root class for root ui --- crates/egui/src/ui.rs | 18 ++++++++---------- crates/egui/src/widget_style.rs | 27 +++------------------------ examples/styling_engine/src/main.rs | 6 ++++-- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index e7905c87d..096e558bf 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -7,6 +7,7 @@ use emath::GuiRounding as _; use epaint::mutex::RwLock; use crate::containers::menu; +use crate::widget_style::{HasClasses as _, ROOT_CLASS}; use crate::{containers::*, ecolor::*, layout::*, placer::Placer, widgets::*, *}; // ---------------------------------------------------------------------------- @@ -113,7 +114,7 @@ impl Ui { let UiBuilder { id_salt, global_scope: _, - mut ui_stack_info, + ui_stack_info, layer_id, max_rect, layout, @@ -123,7 +124,7 @@ impl Ui { style, sense, accessibility_parent, - classes: modifiers, + classes, } = ui_builder; let layer_id = layer_id.unwrap_or_else(LayerId::background); @@ -139,10 +140,7 @@ impl Ui { let disabled = disabled || invisible; let style = style.unwrap_or_else(|| ctx.global_style()); let sense = sense.unwrap_or_else(Sense::hover); - let modifiers = modifiers.unwrap_or_default(); - - // Temporary use of user tags as proof of concept - ui_stack_info = ui_stack_info.with_tag("root"); + let classes = classes.unwrap_or_default().with_class(ROOT_CLASS); let placer = Placer::new(max_rect, layout); let ui_stack = UiStack { @@ -152,7 +150,7 @@ impl Ui { parent: None, min_rect: placer.min_rect(), max_rect: placer.max_rect(), - classes: modifiers, + classes, }; let mut ui = Ui { @@ -268,7 +266,7 @@ impl Ui { style, sense, accessibility_parent, - classes: modifiers, + classes, } = ui_builder; let mut painter = self.painter.clone(); @@ -286,7 +284,7 @@ impl Ui { let sizing_pass = self.sizing_pass || sizing_pass; let style = style.unwrap_or_else(|| Arc::clone(&self.style)); let sense = sense.unwrap_or_else(Sense::hover); - let modifiers = modifiers.unwrap_or_default(); + let classes = classes.unwrap_or_default(); if sizing_pass { // During the sizing pass we want widgets to use up as little space as possible, @@ -318,7 +316,7 @@ impl Ui { parent: Some(Arc::clone(&self.stack)), min_rect: placer.min_rect(), max_rect: placer.max_rect(), - classes: modifiers, + classes, }; let mut child_ui = Ui { diff --git a/crates/egui/src/widget_style.rs b/crates/egui/src/widget_style.rs index 24add2b6d..eb8a5dbe7 100644 --- a/crates/egui/src/widget_style.rs +++ b/crates/egui/src/widget_style.rs @@ -202,6 +202,8 @@ impl Style { } } +pub const ROOT_CLASS: &str = "root"; + pub type ClassName = Cow<'static, str>; #[derive(Debug, Default, Clone)] @@ -280,31 +282,8 @@ pub trait HasClasses { self } - /// Return true if the modifier is present in the list + /// Return true if the class is present in the list fn has(&self, class: impl Into) -> bool { self.classes().classes.contains(&class.into()) } } - -/// Add a shortcut to add modifiers. The syntax is `add_modifiers`!(Name: (modifier1, modifier2, modifier3,)) for any number of modifiers -#[macro_export] -macro_rules! define_modifiers { - ($trait_name:ident: ($( $name:ident )+),?) => { - - pub trait $trait_name { - $( - fn $name(self) -> Self; - )* - } - - impl $trait_name for T - where - T: HasModifiers, - { - #[inline] - $(fn $name(mut self) -> Self { - self.with_class(stringify!($name)) - })? - } - }; -} diff --git a/examples/styling_engine/src/main.rs b/examples/styling_engine/src/main.rs index eb311fde5..4a2ee6dc7 100644 --- a/examples/styling_engine/src/main.rs +++ b/examples/styling_engine/src/main.rs @@ -49,7 +49,9 @@ fn main() -> eframe::Result { while let Some(p) = parent { text.push(format!( "{}{}class : '{}', kind : {:?}", - " ".repeat((2 * 0_i32.max(i - 1)) as usize), + " ".repeat( + (2 * 0_i32.max(i - 1) + 1.min(i)) as usize + ), if i > 0 { "\\- " } else { "" }, p.classes, p.kind() @@ -58,7 +60,7 @@ fn main() -> eframe::Result { parent = p.parent.as_ref(); } ui.label(format!( - "Current hierarchy :\n{}", + "Current hierarchy (child to root):\n{}", text.join("\n") )); },