mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
Add UiStack::has_class and StyleArgs::has_class
A `StyleProvider` reads a class either off the widget or off the `Ui`s it sits in — the equivalent of a descendant selector. Give it one call that covers both, next to the classes it queries. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use core::{any::Any, iter::FusedIterator};
|
use core::{any::Any, iter::FusedIterator};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::widget_style::Classes;
|
use crate::widget_style::{ClassName, Classes, HasClasses as _};
|
||||||
use epaint::Color32;
|
use epaint::Color32;
|
||||||
|
|
||||||
use crate::{Direction, Frame, Id, Rect};
|
use crate::{Direction, Frame, Id, Rect};
|
||||||
@@ -290,6 +290,13 @@ impl UiStack {
|
|||||||
pub fn contained_in(&self, kind: UiKind) -> bool {
|
pub fn contained_in(&self, kind: UiKind) -> bool {
|
||||||
self.iter().any(|frame| frame.kind() == Some(kind))
|
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<ClassName>) -> bool {
|
||||||
|
let class = class.into();
|
||||||
|
self.iter()
|
||||||
|
.any(|node| node.classes.has_class(class.clone()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -149,3 +149,15 @@ pub struct StyleArgs<'a> {
|
|||||||
pub style: &'a Style,
|
pub style: &'a Style,
|
||||||
pub ctx: &'a Context,
|
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<ClassName>) -> bool {
|
||||||
|
let class = class.into();
|
||||||
|
self.classes.has_class(class.clone()) || self.stack.has_class(class)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user