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

Move where the default theme is registered

This commit is contained in:
Lucas Meurer
2026-08-31 12:38:11 +02:00
parent 13dc9d528c
commit 2c856e335d
3 changed files with 26 additions and 38 deletions

View File

@@ -372,7 +372,7 @@ impl ViewportRepaintInfo {
// ----------------------------------------------------------------------------
#[derive(Default)]
struct ContextImpl {
pub(crate) struct ContextImpl {
fonts: Option<Fonts>,
font_definitions: FontDefinitions,
@@ -414,7 +414,7 @@ struct ContextImpl {
loaders: Arc<Loaders>,
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<R>(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R {
pub(crate) fn write<R>(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R {
writer(&mut self.0.write())
}

View File

@@ -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::<ButtonStyle>(Self, false);
ctx.themes.register::<SeparatorStyle>(Self, false);
ctx.themes.register::<CheckboxStyle>(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 {

View File

@@ -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<S> = Arc<Mutex<Box<dyn StyleProvider<S> + Send + Sync>>>;
impl Default for Themes {
/// Register the default egui theme
fn default() -> Self {
let mut themes = IdTypeMap::default();
themes.insert_temp::<ThemeWrap<ButtonStyle>>(
Id::NULL,
Arc::new(Mutex::new(Box::new(DefaultStyle))),
);
themes.insert_temp::<ThemeWrap<SeparatorStyle>>(
Id::NULL,
Arc::new(Mutex::new(Box::new(DefaultStyle))),
);
themes.insert_temp::<ThemeWrap<CheckboxStyle>>(
Id::NULL,
Arc::new(Mutex::new(Box::new(DefaultStyle))),
);
Self { themes }
}
}
impl Themes {
/// Register a [`StyleProvider`] for the specified widget [`WidgetStyle`] `S`
///