Skip to content

Commit

Permalink
Reduce API surface by accepting a theme argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Sep 3, 2024
1 parent aa76ed4 commit 34dff4f
Show file tree
Hide file tree
Showing 33 changed files with 113 additions and 141 deletions.
2 changes: 1 addition & 1 deletion crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct CreationContext<'s> {
/// The egui Context.
///
/// You can use this to customize the look of egui, e.g to call [`egui::Context::set_fonts`],
/// [`egui::Context::set_dark_visuals`] etc.
/// [`egui::Context::set_visuals`] etc.
pub egui_ctx: egui::Context,

/// Information about the surrounding environment.
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl<'app> GlowWinitRunning<'app> {

let clear_color = self
.app
.clear_color(&self.integration.egui_ctx.style().visuals);
.clear_color(&self.integration.egui_ctx.active_style().visuals);

let has_many_viewports = self.glutin.borrow().viewports.len() > 1;
let clear_before_update = !has_many_viewports; // HACK: for some reason, an early clear doesn't "take" on Mac with multiple viewports.
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ impl<'app> WgpuWinitRunning<'app> {
let (vsync_secs, screenshot) = painter.paint_and_update_textures(
viewport_id,
pixels_per_point,
app.clear_color(&egui_ctx.style().visuals),
app.clear_color(&egui_ctx.active_style().visuals),
&clipped_primitives,
&textures_delta,
screenshot_requested,
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl AppRunner {

if let Some(clipped_primitives) = clipped_primitives {
if let Err(err) = self.painter.paint_and_update_textures(
self.app.clear_color(&self.egui_ctx.style().visuals),
self.app.clear_color(&self.egui_ctx.active_style().visuals),
&clipped_primitives,
self.egui_ctx.pixels_per_point(),
&textures_delta,
Expand Down
5 changes: 3 additions & 2 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl Area {
// during the sizing pass we will use this as the max size
let mut size = default_size;

let default_area_size = ctx.style().spacing.default_area_size;
let default_area_size = ctx.active_style().spacing.default_area_size;
if size.x.is_nan() {
size.x = default_area_size.x;
}
Expand Down Expand Up @@ -553,7 +553,8 @@ impl Prepared {
if let Some(last_became_visible_at) = self.state.last_became_visible_at {
let age =
ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt / 2.0);
let opacity = crate::remap_clamp(age, 0.0..=ctx.style().animation_time, 0.0..=1.0);
let opacity =
crate::remap_clamp(age, 0.0..=ctx.active_style().animation_time, 0.0..=1.0);
let opacity = emath::easing::quadratic_out(opacity); // slow fade-out = quick fade-in
ui.multiply_opacity(opacity);
if opacity < 1.0 {
Expand Down
6 changes: 3 additions & 3 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ impl TopBottomPanel {
let expanded_height = PanelState::load(ctx, self.id)
.map(|state| state.rect.height())
.or(self.default_height)
.unwrap_or_else(|| ctx.style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.active_style().spacing.interact_size.y);
let fake_height = how_expanded * expanded_height;
Self {
id: self.id.with("animating_panel"),
Expand Down Expand Up @@ -986,12 +986,12 @@ impl TopBottomPanel {
let collapsed_height = PanelState::load(ctx, collapsed_panel.id)
.map(|state| state.rect.height())
.or(collapsed_panel.default_height)
.unwrap_or_else(|| ctx.style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.active_style().spacing.interact_size.y);

let expanded_height = PanelState::load(ctx, expanded_panel.id)
.map(|state| state.rect.height())
.or(expanded_panel.default_height)
.unwrap_or_else(|| ctx.style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.active_style().spacing.interact_size.y);

let fake_height = lerp(collapsed_height..=expanded_height, how_expanded);
Self {
Expand Down
6 changes: 4 additions & 2 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn show_tooltip_at_dyn<'c, R>(
.order(Order::Tooltip)
.pivot(pivot)
.fixed_pos(anchor)
.default_width(ctx.style().spacing.tooltip_width)
.default_width(ctx.active_style().spacing.tooltip_width)
.sense(Sense::hover()) // don't click to bring to front
.show(ctx, |ui| {
// By default the text in tooltips aren't selectable.
Expand All @@ -208,7 +208,9 @@ fn show_tooltip_at_dyn<'c, R>(
// will stick around when you try to click them.
ui.style_mut().interaction.selectable_labels = false;

Frame::popup(&ctx.style()).show_dyn(ui, add_contents).inner
Frame::popup(&ctx.active_style())
.show_dyn(ui, add_contents)
.inner
});

state.tooltip_count += 1;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl Resize {
state.store(ui.ctx(), id);

#[cfg(debug_assertions)]
if ui.ctx().style().debug.show_resize {
if ui.ctx().active_style().debug.show_resize {
ui.ctx().debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.desired_size),
Color32::GREEN,
Expand Down
14 changes: 8 additions & 6 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,11 @@ impl<'open> Window<'open> {
fade_out,
} = self;

let header_color =
frame.map_or_else(|| ctx.style().visuals.widgets.open.weak_bg_fill, |f| f.fill);
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
let header_color = frame.map_or_else(
|| ctx.active_style().visuals.widgets.open.weak_bg_fill,
|f| f.fill,
);
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.active_style()));
// Keep the original inner margin for later use
let window_margin = window_frame.inner_margin;

Expand Down Expand Up @@ -468,7 +470,7 @@ impl<'open> Window<'open> {

// Calculate roughly how much larger the window size is compared to the inner rect
let (title_bar_height, title_content_spacing) = if with_title_bar {
let style = ctx.style();
let style = ctx.active_style();
let spacing = window_margin.top + window_margin.bottom;
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0.0, height / 2.0);
Expand Down Expand Up @@ -849,8 +851,8 @@ fn resize_interaction(

let id = Id::new(layer_id).with("edge_drag");

let side_grab_radius = ctx.style().interaction.resize_grab_radius_side;
let corner_grab_radius = ctx.style().interaction.resize_grab_radius_corner;
let side_grab_radius = ctx.active_style().interaction.resize_grab_radius_side;
let corner_grab_radius = ctx.active_style().interaction.resize_grab_radius_corner;

let corner_rect =
|center: Pos2| Rect::from_center_size(center, Vec2::splat(2.0 * corner_grab_radius));
Expand Down
124 changes: 43 additions & 81 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
layers::GraphicLayers,
load,
load::{Bytes, Loaders, SizedTexture},
memory::Options,
memory::{Options, Theme},
menu,
os::OperatingSystem,
output::FullOutput,
Expand Down Expand Up @@ -988,7 +988,7 @@ impl Context {
let screen_rect = self.screen_rect();

let text = format!("🔥 {text}");
let color = self.style().visuals.error_fg_color;
let color = self.active_style().visuals.error_fg_color;
let painter = self.debug_painter();
painter.rect_stroke(widget_rect, 0.0, (1.0, color));

Expand Down Expand Up @@ -1357,7 +1357,7 @@ impl Context {
..
} = ModifierNames::SYMBOLS;

let font_id = TextStyle::Body.resolve(&self.style());
let font_id = TextStyle::Body.resolve(&self.active_style());
self.fonts(|f| {
let mut lock = f.lock();
let font = lock.fonts.font(&font_id);
Expand Down Expand Up @@ -1614,17 +1614,17 @@ impl Context {

/// Does the OS use dark or light mode?
/// This is used when the theme preference is set to [`crate::ThemePreference::System`].
pub fn system_theme(&self) -> Option<crate::Theme> {
pub fn system_theme(&self) -> Option<Theme> {
self.memory(|mem| mem.options.system_theme)
}

/// The [`crate::Theme`] used to select the appropriate [`Style`] (dark or light)
/// The [`Theme`] used to select the appropriate [`Style`] (dark or light)
/// used by all subsequent windows, panels etc.
pub fn theme(&self) -> crate::Theme {
pub fn theme(&self) -> Theme {
self.options(|opt| opt.theme())
}

/// The [`crate::Theme`] used to select between [`Self::dark_style()`] and [`Self::light_style()`]
/// The [`Theme`] used to select between dark and light [`Self::style`]
/// as the active style used by all subsequent windows, panels etc.
///
/// Example:
Expand All @@ -1637,7 +1637,7 @@ impl Context {
}

/// The [`Style`] used by all subsequent windows, panels etc.
pub fn style(&self) -> Arc<Style> {
pub fn active_style(&self) -> Arc<Style> {
self.options(|opt| opt.style().clone())
}

Expand All @@ -1646,97 +1646,66 @@ impl Context {
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.style_mut(|style| {
/// ctx.styles_mut(|style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn style_mut(&self, mut mutate_style: impl FnMut(&mut Style)) {
pub fn styles_mut(&self, mut mutate_style: impl FnMut(&mut Style)) {
self.options_mut(|opt| {
mutate_style(std::sync::Arc::make_mut(&mut opt.dark_style));
mutate_style(std::sync::Arc::make_mut(&mut opt.light_style));
});
}

/// The [`Style`] used by all subsequent windows, panels etc in dark mode.
pub fn dark_style(&self) -> Arc<Style> {
self.options(|opt| opt.dark_style.clone())
/// The [`Style`] used by all subsequent windows, panels etc.
pub fn style(&self, theme: Theme) -> Arc<Style> {
self.options(|opt| match theme {
Theme::Dark => opt.dark_style.clone(),
Theme::Light => opt.light_style.clone(),
})
}

/// Mutate the [`Style`] used by all subsequent windows, panels etc in dark mode.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.dark_style_mut(|style| {
/// ctx.style_mut(egui::Theme::Dark, |style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn dark_style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| mutate_style(std::sync::Arc::make_mut(&mut opt.dark_style)));
}

/// The [`Style`] used by all new windows, panels etc. in dark mode.
/// Use [`Self::set_theme`] to choose between dark and light mode.
///
/// You can also change this using [`Self::dark_style_mut`]
///
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
pub fn set_dark_style(&self, style: impl Into<std::sync::Arc<crate::Style>>) {
self.options_mut(|opt| opt.dark_style = style.into());
}

/// The [`crate::Visuals`] used by all subsequent windows, panels etc. in dark mode.
///
/// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.set_dark_visuals(egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
/// ```
pub fn set_dark_visuals(&self, visuals: crate::Visuals) {
self.dark_style_mut(|style| style.visuals = visuals);
}

/// The [`Style`] used by all subsequent windows, panels etc in light mode.
pub fn light_style(&self) -> Arc<Style> {
self.options(|opt| opt.light_style.clone())
}

/// Mutate the [`Style`] used by all subsequent windows, panels etc in light mode.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.light_style_mut(|style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn light_style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| mutate_style(std::sync::Arc::make_mut(&mut opt.light_style)));
pub fn style_mut(&self, theme: Theme, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| match theme {
Theme::Dark => mutate_style(std::sync::Arc::make_mut(&mut opt.dark_style)),
Theme::Light => mutate_style(std::sync::Arc::make_mut(&mut opt.light_style)),
});
}

/// The [`Style`] used by all new windows, panels etc. in light mode.
/// The [`Style`] used by all new windows, panels etc.
/// Use [`Self::set_theme`] to choose between dark and light mode.
///
/// You can also change this using [`Self::light_style_mut`]
/// You can also change this using [`Self::style_mut`].
///
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
pub fn set_light_style(&self, style: impl Into<std::sync::Arc<crate::Style>>) {
self.options_mut(|opt| opt.light_style = style.into());
pub fn set_style(&self, theme: Theme, style: impl Into<std::sync::Arc<crate::Style>>) {
let style = style.into();
self.options_mut(|opt| match theme {
Theme::Dark => opt.dark_style = style,
Theme::Light => opt.light_style = style,
});
}

/// The [`crate::Visuals`] used by all subsequent windows, panels etc. in light mode.
/// The [`crate::Visuals`] used by all subsequent windows, panels etc.
///
/// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.set_light_visuals(egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
/// ctx.set_visuals(egui::Theme::Dark, egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
/// ```
pub fn set_light_visuals(&self, visuals: crate::Visuals) {
self.light_style_mut(|style| style.visuals = visuals);
pub fn set_visuals(&self, theme: Theme, visuals: crate::Visuals) {
self.style_mut(theme, |style| style.visuals = visuals);
}

/// The number of physical pixels for each logical point.
Expand Down Expand Up @@ -1978,7 +1947,7 @@ impl Context {
}
};

if self.style().debug.show_interactive_widgets {
if self.active_style().debug.show_interactive_widgets {
// Show all interactive widgets:
let rects = self.write(|ctx| ctx.viewport().this_frame.widgets.clone());
for (layer_id, rects) in rects.layers() {
Expand Down Expand Up @@ -2054,7 +2023,7 @@ impl Context {
}
}

if self.style().debug.show_widget_hits {
if self.active_style().debug.show_widget_hits {
let hits = self.write(|ctx| ctx.viewport().hits.clone());
let WidgetHits {
contains_pointer,
Expand Down Expand Up @@ -2574,7 +2543,7 @@ impl Context {
/// Turn on/off whether or not to debug widget layout on hover.
#[cfg(debug_assertions)]
pub fn set_debug_on_hover(&self, debug_on_hover: bool) {
self.style_mut(|style| style.debug.debug_on_hover = debug_on_hover);
self.styles_mut(|style| style.debug.debug_on_hover = debug_on_hover);
}
}

Expand All @@ -2591,7 +2560,7 @@ impl Context {
/// The animation time is taken from [`Style::animation_time`].
#[track_caller] // To track repaint cause
pub fn animate_bool(&self, id: Id, value: bool) -> f32 {
let animation_time = self.style().animation_time;
let animation_time = self.active_style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, emath::easing::linear)
}

Expand All @@ -2607,7 +2576,7 @@ impl Context {
/// Like [`Self::animate_bool`] but allows you to control the easing function.
#[track_caller] // To track repaint cause
pub fn animate_bool_with_easing(&self, id: Id, value: bool, easing: fn(f32) -> f32) -> f32 {
let animation_time = self.style().animation_time;
let animation_time = self.active_style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, easing)
}

Expand Down Expand Up @@ -2958,18 +2927,11 @@ impl Context {
}

impl Context {
/// Edit the [`Style`] used in dark mode.
pub fn dark_style_ui(&self, ui: &mut Ui) {
let mut style: Style = (*self.dark_style()).clone();
style.ui(ui);
self.set_dark_style(style);
}

/// Edit the [`Style`] used in light mode.
pub fn light_style_ui(&self, ui: &mut Ui) {
let mut style: Style = (*self.light_style()).clone();
/// Edit the [`Style`].
pub fn style_ui(&self, ui: &mut Ui, theme: Theme) {
let mut style: Style = (*self.style(theme)).clone();
style.ui(ui);
self.set_light_style(style);
self.set_style(theme, style);
}
}

Expand Down
Loading

0 comments on commit 34dff4f

Please sign in to comment.