mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -04:00
Move where the default theme is registered
This commit is contained in:
@@ -372,7 +372,7 @@ impl ViewportRepaintInfo {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct ContextImpl {
|
pub(crate) struct ContextImpl {
|
||||||
fonts: Option<Fonts>,
|
fonts: Option<Fonts>,
|
||||||
font_definitions: FontDefinitions,
|
font_definitions: FontDefinitions,
|
||||||
|
|
||||||
@@ -414,7 +414,7 @@ struct ContextImpl {
|
|||||||
|
|
||||||
loaders: Arc<Loaders>,
|
loaders: Arc<Loaders>,
|
||||||
|
|
||||||
themes: theme::Themes,
|
pub(crate) themes: theme::Themes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContextImpl {
|
impl ContextImpl {
|
||||||
@@ -753,6 +753,9 @@ impl Default for Context {
|
|||||||
ctx.add_plugin(crate::text_selection::LabelSelectionState::default());
|
ctx.add_plugin(crate::text_selection::LabelSelectionState::default());
|
||||||
ctx.add_plugin(crate::DragAndDrop::default());
|
ctx.add_plugin(crate::DragAndDrop::default());
|
||||||
|
|
||||||
|
// Register the default theme for all built-in widgets:
|
||||||
|
theme::DefaultStyle::register(&ctx);
|
||||||
|
|
||||||
ctx
|
ctx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -764,7 +767,7 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Do read-write (exclusive access) transaction on 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())
|
writer(&mut self.0.write())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use emath::Vec2;
|
|||||||
use epaint::Margin;
|
use epaint::Margin;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Button, Frame, Style, TextStyle,
|
Button, Context, Frame, Style, TextStyle,
|
||||||
style::WidgetVisuals,
|
style::WidgetVisuals,
|
||||||
theme::StyleProvider,
|
theme::StyleProvider,
|
||||||
widget_style::{
|
widget_style::{
|
||||||
@@ -16,6 +16,20 @@ use crate::{
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DefaultStyle;
|
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.
|
/// The text of a widget, based on the [`WidgetVisuals`] of its current state.
|
||||||
fn text_visuals(style: &Style, widget_visuals: &WidgetVisuals) -> TextVisuals {
|
fn text_visuals(style: &Style, widget_visuals: &WidgetVisuals) -> TextVisuals {
|
||||||
TextVisuals {
|
TextVisuals {
|
||||||
|
|||||||
@@ -2,52 +2,23 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use epaint::mutex::Mutex;
|
use epaint::mutex::Mutex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{Id, theme::StyleProvider, util::IdTypeMap, widget_style::WidgetStyle};
|
||||||
Id,
|
|
||||||
theme::{StyleProvider, default_style::DefaultStyle},
|
|
||||||
util::IdTypeMap,
|
|
||||||
widget_style::{ButtonStyle, CheckboxStyle, SeparatorStyle, WidgetStyle},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// The registry of [`StyleProvider`]s, one per [`WidgetStyle`] type.
|
/// The registry of [`StyleProvider`]s, one per [`WidgetStyle`] type.
|
||||||
///
|
///
|
||||||
/// Each widget asks this registry for the provider of its style 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.
|
/// widget's classes and state.
|
||||||
///
|
///
|
||||||
/// A default provider is registered for every built-in style; register your
|
/// A default provider is registered for every built-in style. Register your
|
||||||
/// own with [`Context::add_widget_theme`](crate::Context::add_widget_theme) or
|
/// own with [`crate::Context::add_widget_theme`] or [`crate::Context::replace_widget_theme`].
|
||||||
/// [`Context::replace_widget_theme`](crate::Context::replace_widget_theme).
|
#[derive(Default)]
|
||||||
pub struct Themes {
|
pub struct Themes {
|
||||||
themes: IdTypeMap,
|
themes: IdTypeMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThemeWrap<S> = Arc<Mutex<Box<dyn StyleProvider<S> + Send + Sync>>>;
|
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 {
|
impl Themes {
|
||||||
/// Register a [`StyleProvider`] for the specified widget [`WidgetStyle`] `S`
|
/// Register a [`StyleProvider`] for the specified widget [`WidgetStyle`] `S`
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user