diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index 61375b580..812a8d92c 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -7,6 +7,7 @@ use emath::GuiRounding as _; use crate::{ Align2, Context, Id, InnerResponse, LayerId, Layout, NumExt as _, Order, Pos2, Rect, Response, Sense, Ui, UiBuilder, UiKind, UiStackInfo, Vec2, WidgetRect, WidgetWithState, emath, pos2, + widget_style::{Classes, HasClasses}, }; /// State of an [`Area`] that is persisted between frames. @@ -122,12 +123,23 @@ pub struct Area { fade_in: bool, layout: Layout, sizing_pass: bool, + classes: Classes, } impl WidgetWithState for Area { type State = AreaState; } +impl HasClasses for Area { + fn classes(&self) -> &Classes { + &self.classes + } + + fn classes_mut(&mut self) -> &mut Classes { + &mut self.classes + } +} + impl Area { /// The `id` must be globally unique. pub fn new(id: Id) -> Self { @@ -149,6 +161,7 @@ impl Area { fade_in: true, layout: Layout::default(), sizing_pass: false, + classes: Classes::default(), } } @@ -400,6 +413,7 @@ pub(crate) struct Prepared { fade_in: bool, layout: Layout, + classes: Classes, } impl Area { @@ -434,6 +448,7 @@ impl Area { fade_in, layout, sizing_pass: force_sizing_pass, + classes, } = self; let constrain_rect = constrain_rect.unwrap_or_else(|| ctx.content_rect()); @@ -581,6 +596,7 @@ impl Area { sizing_pass, fade_in, layout, + classes, } } } @@ -612,6 +628,7 @@ impl Prepared { let max_rect = self.state.rect(); let mut ui_builder = UiBuilder::new() + .with_classes(core::mem::take(&mut self.classes)) .ui_stack_info(self.info.take().unwrap_or_default()) .layer_id(self.layer_id) .max_rect(max_rect) diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index beee9f900..c1b150a37 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -7,6 +7,7 @@ use crate::{ Sense, Ui, UiKind, UiStackInfo, containers::menu::{MenuConfig, MenuState, menu_style}, style::StyleModifier, + widget_style::{HasClasses as _, MENU_CLASS}, }; /// What should we anchor the popup to? @@ -594,6 +595,7 @@ impl<'a> Popup<'a> { let (pivot, anchor) = best_align.pivot_pos(&anchor_rect, gap); let mut area = Area::new(id) + .with_class_if(MENU_CLASS, kind == PopupKind::Menu) .order(kind.order()) .pivot(pivot) .fixed_pos(anchor) diff --git a/crates/egui/src/widget_style/classes.rs b/crates/egui/src/widget_style/classes.rs index 59a9c2d53..f69e3c90a 100644 --- a/crates/egui/src/widget_style/classes.rs +++ b/crates/egui/src/widget_style/classes.rs @@ -13,6 +13,12 @@ pub const SELECTED_CLASS: &str = "selected"; /// The read-only class is present on a [`crate::TextEdit`] whose buffer can't be edited. pub const READ_ONLY_CLASS: &str = "read-only"; +/// The menu class is present on the [`crate::Ui`] of a menu popup, and so on every widget in it. +/// +/// A menu lays its items out itself, so a widget in one is generally styled to fit that layout +/// rather than to stand on its own. +pub const MENU_CLASS: &str = "menu"; + /// A class is a static string identifier. pub type ClassName = Cow<'static, str>; diff --git a/crates/egui/src/widget_style/mod.rs b/crates/egui/src/widget_style/mod.rs index 17dcfbe79..f063cd1ce 100644 --- a/crates/egui/src/widget_style/mod.rs +++ b/crates/egui/src/widget_style/mod.rs @@ -5,7 +5,7 @@ mod classes; pub use self::classes::{ - ClassName, Classes, HasClasses, READ_ONLY_CLASS, ROOT_CLASS, SELECTED_CLASS, + ClassName, Classes, HasClasses, MENU_CLASS, READ_ONLY_CLASS, ROOT_CLASS, SELECTED_CLASS, }; use core::fmt::Debug;