diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index a48b10570..13728413a 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -372,7 +372,7 @@ impl ViewportRepaintInfo { // ---------------------------------------------------------------------------- #[derive(Default)] -struct ContextImpl { +pub(crate) struct ContextImpl { fonts: Option, font_definitions: FontDefinitions, @@ -414,7 +414,7 @@ struct ContextImpl { loaders: Arc, - themes: theme::Themes, + pub(crate) themes: theme::Themes, } impl ContextImpl { @@ -753,6 +753,9 @@ impl Default for Context { ctx.add_plugin(crate::text_selection::LabelSelectionState::default()); ctx.add_plugin(crate::DragAndDrop::default()); + // Register the default theme for all built-in widgets: + theme::DefaultStyle::register(&ctx); + ctx } } @@ -764,7 +767,7 @@ impl Context { } /// Do read-write (exclusive access) transaction on Context - fn write(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R { + pub(crate) fn write(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R { writer(&mut self.0.write()) } diff --git a/crates/egui/src/theme/default_style.rs b/crates/egui/src/theme/default_style.rs index 196bf2280..9c95d981a 100644 --- a/crates/egui/src/theme/default_style.rs +++ b/crates/egui/src/theme/default_style.rs @@ -2,7 +2,7 @@ use emath::Vec2; use epaint::Margin; use crate::{ - Button, Frame, Style, TextStyle, + Button, Context, Frame, Style, TextStyle, style::WidgetVisuals, theme::StyleProvider, widget_style::{ @@ -16,6 +16,20 @@ use crate::{ #[derive(Debug, Clone)] pub struct DefaultStyle; +impl DefaultStyle { + /// Register `Self` as the [`StyleProvider`] of every built-in widget style. + /// + /// [`Context::default`] does this. Any theme you register yourself + /// replaces the default one for that widget style. + pub fn register(ctx: &Context) { + ctx.write(|ctx| { + ctx.themes.register::(Self, false); + ctx.themes.register::(Self, false); + ctx.themes.register::(Self, false); + }); + } +} + /// The text of a widget, based on the [`WidgetVisuals`] of its current state. fn text_visuals(style: &Style, widget_visuals: &WidgetVisuals) -> TextVisuals { TextVisuals { diff --git a/crates/egui/src/theme/themes.rs b/crates/egui/src/theme/themes.rs index ef1429db4..27ca343d4 100644 --- a/crates/egui/src/theme/themes.rs +++ b/crates/egui/src/theme/themes.rs @@ -2,52 +2,23 @@ use std::sync::Arc; use epaint::mutex::Mutex; -use crate::{ - Id, - theme::{StyleProvider, default_style::DefaultStyle}, - util::IdTypeMap, - widget_style::{ButtonStyle, CheckboxStyle, SeparatorStyle, WidgetStyle}, -}; +use crate::{Id, theme::StyleProvider, util::IdTypeMap, widget_style::WidgetStyle}; /// The registry of [`StyleProvider`]s, one per [`WidgetStyle`] type. /// /// Each widget asks this registry for the provider of its style type -/// (e.g. [`ButtonStyle`]), and that provider computes the final style from the +/// (e.g. [`crate::widget_style::ButtonStyle`]), and that provider computes the final style from the /// widget's classes and state. /// -/// A default provider is registered for every built-in style; register your -/// own with [`Context::add_widget_theme`](crate::Context::add_widget_theme) or -/// [`Context::replace_widget_theme`](crate::Context::replace_widget_theme). +/// A default provider is registered for every built-in style. Register your +/// own with [`crate::Context::add_widget_theme`] or [`crate::Context::replace_widget_theme`]. +#[derive(Default)] pub struct Themes { themes: IdTypeMap, } type ThemeWrap = Arc + Send + Sync>>>; -impl Default for Themes { - /// Register the default egui theme - fn default() -> Self { - let mut themes = IdTypeMap::default(); - - themes.insert_temp::>( - Id::NULL, - Arc::new(Mutex::new(Box::new(DefaultStyle))), - ); - - themes.insert_temp::>( - Id::NULL, - Arc::new(Mutex::new(Box::new(DefaultStyle))), - ); - - themes.insert_temp::>( - Id::NULL, - Arc::new(Mutex::new(Box::new(DefaultStyle))), - ); - - Self { themes } - } -} - impl Themes { /// Register a [`StyleProvider`] for the specified widget [`WidgetStyle`] `S` ///