1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00
This commit is contained in:
adrien
2026-01-14 03:26:51 +01:00
parent 8627de6808
commit 87e97fcff9

View File

@@ -202,7 +202,7 @@ impl Style {
pub type WidgetStyleModifier = String; pub type WidgetStyleModifier = String;
/// For now we use [`Vec`] but later we could use [`SmallVec`] for performance /// For now we use Vec for the modifiers but later we could use `SmallVev` for performance
#[derive(Default)] #[derive(Default)]
pub struct StyleModifiers { pub struct StyleModifiers {
modifiers: Vec<WidgetStyleModifier>, modifiers: Vec<WidgetStyleModifier>,
@@ -212,24 +212,29 @@ pub struct StyleModifiers {
impl StyleModifiers { impl StyleModifiers {
/// Add multiples modifiers in one method and return the list for method chaining /// Add multiples modifiers in one method and return the list for method chaining
#[inline]
pub fn with_modifiers(mut self, classes: &[WidgetStyleModifier]) -> Self { pub fn with_modifiers(mut self, classes: &[WidgetStyleModifier]) -> Self {
self.modifiers.append(&mut classes.to_vec()); self.modifiers.append(&mut classes.to_vec());
self self
} }
/// Add a single modifier and return the list for method chaining /// Add a single modifier and return the list for method chaining
#[inline]
pub fn with_modifier(mut self, class: impl Into<WidgetStyleModifier>) -> Self { pub fn with_modifier(mut self, class: impl Into<WidgetStyleModifier>) -> Self {
self.modifiers.push(class.into()); self.modifiers.push(class.into());
self self
} }
/// Add a class to the list if the condition is true /// Add a class to the list if the condition is true
#[inline]
pub fn add_if(&mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) { pub fn add_if(&mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) {
if condition { if condition {
self.modifiers.push(modifier.into()); self.modifiers.push(modifier.into());
} }
} }
/// Add a class to the list and return the list for method chaining /// Add a class to the list and return the list for method chaining
#[inline]
pub fn with_if(mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) -> Self { pub fn with_if(mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) -> Self {
self.add_if(modifier.into(), condition); self.add_if(modifier.into(), condition);
self self
@@ -260,6 +265,7 @@ pub trait HasModifiers {
self self
} }
#[inline]
fn with_modifier(mut self, modifier: impl Into<WidgetStyleModifier>) -> Self fn with_modifier(mut self, modifier: impl Into<WidgetStyleModifier>) -> Self
where where
Self: Sized, Self: Sized,
@@ -268,6 +274,7 @@ pub trait HasModifiers {
self self
} }
#[inline]
fn with_modifier_if(mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) -> Self fn with_modifier_if(mut self, modifier: impl Into<WidgetStyleModifier>, condition: bool) -> Self
where where
Self: Sized, Self: Sized,
@@ -276,6 +283,7 @@ pub trait HasModifiers {
self self
} }
#[inline]
fn theme(mut self, theme: Theme) -> Self fn theme(mut self, theme: Theme) -> Self
where where
Self: Sized, Self: Sized,
@@ -285,7 +293,7 @@ pub trait HasModifiers {
} }
} }
/// Add a shortcut to add modifiers. The syntax is add_modifiers!([Name]: (modifier1, modifier2, modifier3,)) for any number of modifiers /// Add a shortcut to add modifiers. The syntax is `add_modifiers`!(Name: (modifier1, modifier2, modifier3,)) for any number of modifiers
#[macro_export] #[macro_export]
macro_rules! add_modifiers { macro_rules! add_modifiers {
($trait_name:ident: ($( $name:ident )+),?) => { ($trait_name:ident: ($( $name:ident )+),?) => {
@@ -300,6 +308,7 @@ macro_rules! add_modifiers {
where where
T: HasModifiers, T: HasModifiers,
{ {
#[inline]
$(fn $name(mut self) -> Self { $(fn $name(mut self) -> Self {
self.with_modifier(stringify!($name)) self.with_modifier(stringify!($name))
})? })?