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..4295686ee 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 _, }; /// What should we anchor the popup to? @@ -188,6 +189,12 @@ pub struct Popup<'a> { } impl<'a> Popup<'a> { + /// 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 CLASS_MENU: &'static str = "egui::popup::menu"; + /// Create a new popup pub fn new(id: Id, ctx: Context, anchor: impl Into, layer_id: LayerId) -> Self { Self { @@ -594,6 +601,7 @@ impl<'a> Popup<'a> { let (pivot, anchor) = best_align.pivot_pos(&anchor_rect, gap); let mut area = Area::new(id) + .with_class_if(Self::CLASS_MENU, kind == PopupKind::Menu) .order(kind.order()) .pivot(pivot) .fixed_pos(anchor)