1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

Make has_class take &str

This commit is contained in:
Lucas Meurer
2026-08-31 16:33:24 +02:00
parent e0db1f2705
commit 059f3879c4
4 changed files with 16 additions and 17 deletions

View File

@@ -38,7 +38,7 @@ impl StyleProvider<ButtonStyle> for DefaultStyle {
let spacing = &style.spacing;
let mut widget_visuals = *style.visuals.widgets.state(*state);
if classes.has_class(Button::CLASS_SELECTED) {
if classes.has_class(&Button::CLASS_SELECTED) {
let visuals = &style.visuals;
widget_visuals.weak_bg_fill = visuals.selection.bg_fill;
widget_visuals.bg_fill = visuals.selection.bg_fill;
@@ -48,7 +48,7 @@ impl StyleProvider<ButtonStyle> for DefaultStyle {
let mut inner_margin: Margin = spacing.button_padding.into();
// A small button as high as regular text
if classes.has_class(Button::CLASS_SMALL) {
if classes.has_class(&Button::CLASS_SMALL) {
inner_margin.top = 0;
inner_margin.bottom = 0;
}
@@ -63,13 +63,13 @@ impl StyleProvider<ButtonStyle> for DefaultStyle {
// Ensure changing expansion and stroke don't affect layout:
.expand_in_place(widget_visuals.expansion);
let has_frame = classes.has_class(Button::CLASS_FRAME)
|| (!classes.has_class(Button::CLASS_NO_FRAME) && style.visuals.button_frame);
let has_frame = classes.has_class(&Button::CLASS_FRAME)
|| (!classes.has_class(&Button::CLASS_NO_FRAME) && style.visuals.button_frame);
let frame = if !has_frame {
// No frame at all: the button takes up no more room than its contents.
Frame::new()
} else if classes.has_class(Button::CLASS_HIDE_FRAME_WHEN_INACTIVE)
} else if classes.has_class(&Button::CLASS_HIDE_FRAME_WHEN_INACTIVE)
&& *state == WidgetState::Inactive
{
// Hide the frame, but keep its spacing
@@ -79,7 +79,7 @@ impl StyleProvider<ButtonStyle> for DefaultStyle {
};
ButtonStyle {
min_size: if classes.has_class(Button::CLASS_SMALL) {
min_size: if classes.has_class(&Button::CLASS_SMALL) {
Vec2::ZERO
} else {
Vec2::new(0.0, spacing.interact_size.y)

View File

@@ -1,7 +1,7 @@
use core::{any::Any, iter::FusedIterator};
use std::sync::Arc;
use crate::widget_style::{ClassName, Classes, HasClasses as _};
use crate::widget_style::{Classes, HasClasses as _};
use epaint::Color32;
use crate::{Direction, Frame, Id, Rect};
@@ -292,10 +292,8 @@ impl UiStack {
}
/// 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()))
pub fn has_class(&self, class: &str) -> bool {
self.iter().any(|node| node.classes.has_class(class))
}
}

View File

@@ -289,8 +289,11 @@ pub trait HasClasses {
/// True if the class is present.
#[inline]
fn has_class(&self, class: impl Into<ClassName>) -> bool {
self.classes().classes.contains(&class.into())
fn has_class(&self, class: &str) -> bool {
self.classes()
.classes
.iter()
.any(|existing| existing.as_str() == class)
}
/// The list of class.

View File

@@ -15,7 +15,6 @@ pub mod class {
}
use core::fmt::Debug;
use epaint::{Color32, FontId, Stroke, Vec2};
use crate::{
@@ -158,8 +157,7 @@ impl StyleArgs<'_> {
/// 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)
pub fn has_class(&self, class: &str) -> bool {
self.classes.has_class(class) || self.stack.has_class(class)
}
}