diff --git a/crates/egui/src/ui_stack.rs b/crates/egui/src/ui_stack.rs index f6b4865be..59b02db90 100644 --- a/crates/egui/src/ui_stack.rs +++ b/crates/egui/src/ui_stack.rs @@ -1,7 +1,7 @@ use core::{any::Any, iter::FusedIterator}; use std::sync::Arc; -use crate::widget_style::Classes; +use crate::widget_style::{ClassName, Classes, HasClasses as _}; use epaint::Color32; use crate::{Direction, Frame, Id, Rect}; @@ -290,6 +290,13 @@ impl UiStack { pub fn contained_in(&self, kind: UiKind) -> bool { self.iter().any(|frame| frame.kind() == Some(kind)) } + + /// Does this node, or any [`crate::Ui`] up the stack, carry this class? + pub fn has_class(&self, class: impl Into) -> bool { + let class = class.into(); + self.iter() + .any(|node| node.classes.has_class(class.clone())) + } } // ---------------------------------------------------------------------------- diff --git a/crates/egui/src/widget_style/mod.rs b/crates/egui/src/widget_style/mod.rs index 1a28327d9..de446148b 100644 --- a/crates/egui/src/widget_style/mod.rs +++ b/crates/egui/src/widget_style/mod.rs @@ -149,3 +149,15 @@ pub struct StyleArgs<'a> { pub style: &'a Style, pub ctx: &'a Context, } + +impl StyleArgs<'_> { + /// Does the widget or any of its parents contain this class? + /// + /// See also: + /// - [`Classes::has_class`] + /// - [`UiStack::has_class`] + pub fn has_class(&self, class: impl Into) -> bool { + let class = class.into(); + self.classes.has_class(class.clone()) || self.stack.has_class(class) + } +}