From f05ca409ff43e5c1a97471424876730ab429fd27 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 4 Aug 2026 14:18:51 +0200 Subject: [PATCH] refactor: move `Classes` into its own file `widget_style.rs` becomes `widget_style/mod.rs`, with `Classes`, `HasClasses`, `ClassName` and the class constants in `widget_style/classes.rs`. All public paths are unchanged. Co-Authored-By: Claude Opus 5 (1M context) --- crates/egui/src/widget_style/classes.rs | 109 ++++++++++++++++ .../{widget_style.rs => widget_style/mod.rs} | 116 +----------------- 2 files changed, 115 insertions(+), 110 deletions(-) create mode 100644 crates/egui/src/widget_style/classes.rs rename crates/egui/src/{widget_style.rs => widget_style/mod.rs} (51%) diff --git a/crates/egui/src/widget_style/classes.rs b/crates/egui/src/widget_style/classes.rs new file mode 100644 index 000000000..fe8ad6425 --- /dev/null +++ b/crates/egui/src/widget_style/classes.rs @@ -0,0 +1,109 @@ +use std::{borrow::Cow, fmt}; + +use smallvec::SmallVec; + +use crate::TextBuffer as _; + +/// The root class is a special class present on every top-level [`crate::Ui`]. +pub const ROOT_CLASS: &str = "root"; + +/// The selected class is a special class present on selected [`crate::Button`]. +pub const SELECTED_CLASS: &str = "selected"; + +/// A class is a static string identifier. +pub type ClassName = Cow<'static, str>; + +/// Classes are string identifier that can be set on widget/Ui. +/// +/// This can be used by styling engine to compute a different style +/// based on the set of classes present on the widget/Ui. +#[derive(Debug, Default, Clone, Hash)] +pub struct Classes { + classes: SmallVec<[ClassName; 5]>, +} + +impl Classes { + /// Add a class to the list if the condition is true + #[inline] + fn add_if(&mut self, class: impl Into, condition: bool) { + if condition { + self.classes.push(class.into()); + } + } +} + +impl HasClasses for Classes { + fn classes(&self) -> &Classes { + self + } + + fn classes_mut(&mut self) -> &mut Classes { + self + } +} + +impl std::fmt::Display for Classes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.classes.iter().for_each(|class| { + let _ = f.write_str(class.as_str()); + }); + f.write_str("") + } +} + +/// Any widgets supporting [`Classes`] must implement this trait +pub trait HasClasses { + fn classes(&self) -> &Classes; + + fn classes_mut(&mut self) -> &mut Classes; + + /// Add the given class by consuming [`self`] + #[inline] + fn with_class(mut self, class: impl Into) -> Self + where + Self: Sized, + { + self.classes_mut().add_if(class.into(), true); + self + } + + /// Add the given class by consuming [`self`] if the condition is true + #[inline] + fn with_class_if(mut self, class: impl Into, condition: bool) -> Self + where + Self: Sized, + { + self.classes_mut().add_if(class.into(), condition); + self + } + + /// Add the given class in-place + #[inline] + fn add_class(&mut self, class: impl Into) -> &mut Self + where + Self: Sized, + { + self.classes_mut().add_if(class.into(), true); + self + } + + /// Add the given class in-place if the condition is true + #[inline] + fn add_class_if(&mut self, class: impl Into, condition: bool) -> &mut Self + where + Self: Sized, + { + self.classes_mut().add_if(class.into(), condition); + self + } + + /// True if the class is present + fn has(&self, class: impl Into) -> bool { + self.classes().classes.contains(&class.into()) + } + + /// The list of class + fn as_slice(&self) -> &[ClassName] { + &self.classes().classes + } +} diff --git a/crates/egui/src/widget_style.rs b/crates/egui/src/widget_style/mod.rs similarity index 51% rename from crates/egui/src/widget_style.rs rename to crates/egui/src/widget_style/mod.rs index ccb1f96f1..48ca281a8 100644 --- a/crates/egui/src/widget_style.rs +++ b/crates/egui/src/widget_style/mod.rs @@ -1,13 +1,13 @@ -use std::{ - borrow::Cow, - fmt::{self, Debug}, -}; +mod classes; + +pub use self::classes::{ClassName, Classes, HasClasses, ROOT_CLASS, SELECTED_CLASS}; + +use std::fmt::Debug; use epaint::{Color32, FontId, Stroke, text::TextWrapMode}; -use smallvec::SmallVec; use crate::{ - Context, Frame, Response, Style, TextBuffer as _, UiStack, + Context, Frame, Response, Style, UiStack, style::{WidgetVisuals, Widgets}, }; @@ -136,110 +136,6 @@ impl Response { } } -/// The root class is a special class present on every top-level [`crate::Ui`]. -pub const ROOT_CLASS: &str = "root"; - -/// The selected class is a special class present on selected [`crate::Button`]. -pub const SELECTED_CLASS: &str = "selected"; - -/// A class is a static string identifier. -pub type ClassName = Cow<'static, str>; - -/// Classes are string identifier that can be set on widget/Ui. -/// -/// This can be used by styling engine to compute a different style -/// based on the set of classes present on the widget/Ui. -#[derive(Debug, Default, Clone, Hash)] -pub struct Classes { - classes: SmallVec<[ClassName; 5]>, -} - -impl Classes { - /// Add a class to the list if the condition is true - #[inline] - fn add_if(&mut self, class: impl Into, condition: bool) { - if condition { - self.classes.push(class.into()); - } - } -} - -impl HasClasses for Classes { - fn classes(&self) -> &Classes { - self - } - - fn classes_mut(&mut self) -> &mut Classes { - self - } -} - -impl std::fmt::Display for Classes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.classes.iter().for_each(|class| { - let _ = f.write_str(class.as_str()); - }); - f.write_str("") - } -} - -/// Any widgets supporting [`Classes`] must implement this trait -pub trait HasClasses { - fn classes(&self) -> &Classes; - - fn classes_mut(&mut self) -> &mut Classes; - - /// Add the given class by consuming [`self`] - #[inline] - fn with_class(mut self, class: impl Into) -> Self - where - Self: Sized, - { - self.classes_mut().add_if(class.into(), true); - self - } - - /// Add the given class by consuming [`self`] if the condition is true - #[inline] - fn with_class_if(mut self, class: impl Into, condition: bool) -> Self - where - Self: Sized, - { - self.classes_mut().add_if(class.into(), condition); - self - } - - /// Add the given class in-place - #[inline] - fn add_class(&mut self, class: impl Into) -> &mut Self - where - Self: Sized, - { - self.classes_mut().add_if(class.into(), true); - self - } - - /// Add the given class in-place if the condition is true - #[inline] - fn add_class_if(&mut self, class: impl Into, condition: bool) -> &mut Self - where - Self: Sized, - { - self.classes_mut().add_if(class.into(), condition); - self - } - - /// True if the class is present - fn has(&self, class: impl Into) -> bool { - self.classes().classes.contains(&class.into()) - } - - /// The list of class - fn as_slice(&self) -> &[ClassName] { - &self.classes().classes - } -} - pub struct StyleArgs<'a> { pub classes: &'a Classes, pub state: WidgetState,