Skip to content

Commit

Permalink
Add back style and style_mut for the active style
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Sep 6, 2024
1 parent 35e3a0c commit 04c0f22
Show file tree
Hide file tree
Showing 30 changed files with 90 additions and 79 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_visuals`] etc.
/// [`egui::Context::set_visuals_of`] 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.active_style().visuals);
.clear_color(&self.integration.egui_ctx.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.active_style().visuals),
app.clear_color(&egui_ctx.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.active_style().visuals),
self.app.clear_color(&self.egui_ctx.style().visuals),
&clipped_primitives,
self.egui_ctx.pixels_per_point(),
&textures_delta,
Expand Down
5 changes: 2 additions & 3 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.active_style().spacing.default_area_size;
let default_area_size = ctx.style().spacing.default_area_size;
if size.x.is_nan() {
size.x = default_area_size.x;
}
Expand Down Expand Up @@ -553,8 +553,7 @@ 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.active_style().animation_time, 0.0..=1.0);
let opacity = crate::remap_clamp(age, 0.0..=ctx.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.active_style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.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.active_style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.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.active_style().spacing.interact_size.y);
.unwrap_or_else(|| ctx.style().spacing.interact_size.y);

let fake_height = lerp(collapsed_height..=expanded_height, how_expanded);
Self {
Expand Down
6 changes: 2 additions & 4 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.active_style().spacing.tooltip_width)
.default_width(ctx.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,9 +208,7 @@ 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.active_style())
.show_dyn(ui, add_contents)
.inner
Frame::popup(&ctx.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().active_style().debug.show_resize {
if ui.ctx().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: 6 additions & 8 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,9 @@ impl<'open> Window<'open> {
fade_out,
} = self;

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()));
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()));
// Keep the original inner margin for later use
let window_margin = window_frame.inner_margin;

Expand Down Expand Up @@ -470,7 +468,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.active_style();
let style = ctx.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 @@ -851,8 +849,8 @@ fn resize_interaction(

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

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 side_grab_radius = ctx.style().interaction.resize_grab_radius_side;
let corner_grab_radius = ctx.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
52 changes: 33 additions & 19 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ impl Context {
let screen_rect = self.screen_rect();

let text = format!("🔥 {text}");
let color = self.active_style().visuals.error_fg_color;
let color = self.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.active_style());
let font_id = TextStyle::Body.resolve(&self.style());
self.fonts(|f| {
let mut lock = f.lock();
let font = lock.fonts.font(&font_id);
Expand Down Expand Up @@ -1636,11 +1636,25 @@ impl Context {
self.options_mut(|opt| opt.theme_preference = theme_preference.into());
}

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

/// Mutate the currently active [`Style`] used by all subsequent windows, panels etc.
/// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.style_mut(|style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| mutate_style(std::sync::Arc::make_mut(opt.style_mut())));
}

/// Mutate the [`Style`]s used by all subsequent windows, panels etc. in both dark and light mode.
///
/// Example:
Expand All @@ -1658,23 +1672,23 @@ impl Context {
}

/// The [`Style`] used by all subsequent windows, panels etc.
pub fn style(&self, theme: Theme) -> Arc<Style> {
pub fn style_of(&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.
/// Mutate the [`Style`] used by all subsequent windows, panels etc.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.style_mut(egui::Theme::Dark, |style| {
/// ctx.style_mut_of(egui::Theme::Dark, |style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn style_mut(&self, theme: Theme, mutate_style: impl FnOnce(&mut Style)) {
pub fn style_mut_of(&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)),
Expand All @@ -1684,10 +1698,10 @@ impl Context {
/// 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::style_mut`].
/// You can also change this using [`Self::style_mut_of`].
///
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
pub fn set_style(&self, theme: Theme, style: impl Into<std::sync::Arc<crate::Style>>) {
pub fn set_style_of(&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,
Expand All @@ -1702,10 +1716,10 @@ impl Context {
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.set_visuals(egui::Theme::Dark, egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
/// ctx.set_visuals_of(egui::Theme::Dark, egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
/// ```
pub fn set_visuals(&self, theme: Theme, visuals: crate::Visuals) {
self.style_mut(theme, |style| style.visuals = visuals);
pub fn set_visuals_of(&self, theme: Theme, visuals: crate::Visuals) {
self.style_mut_of(theme, |style| style.visuals = visuals);
}

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

if self.active_style().debug.show_interactive_widgets {
if self.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 @@ -2023,7 +2037,7 @@ impl Context {
}
}

if self.active_style().debug.show_widget_hits {
if self.style().debug.show_widget_hits {
let hits = self.write(|ctx| ctx.viewport().hits.clone());
let WidgetHits {
contains_pointer,
Expand Down Expand Up @@ -2560,7 +2574,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.active_style().animation_time;
let animation_time = self.style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, emath::easing::linear)
}

Expand All @@ -2576,7 +2590,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.active_style().animation_time;
let animation_time = self.style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, easing)
}

Expand Down Expand Up @@ -2929,9 +2943,9 @@ impl Context {
impl Context {
/// Edit the [`Style`].
pub fn style_ui(&self, ui: &mut Ui, theme: Theme) {
let mut style: Style = (*self.style(theme)).clone();
let mut style: Style = (*self.style_of(theme)).clone();
style.ui(ui);
self.set_style(theme, style);
self.set_style_of(theme, style);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/debug_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl State {
let available_width = ctx.screen_rect().max.x - pos.x;
let galley = text.into_galley_impl(
ctx,
&ctx.active_style(),
&ctx.style(),
text::TextWrapping::wrap_at_width(available_width),
font_id.clone().into(),
Align::TOP,
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/frame_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl DebugRect {
// Paint rectangle around widget:
{
// Print width and height:
let text_color = if ctx.active_style().visuals.dark_mode {
let text_color = if ctx.style().visuals.dark_mode {
Color32::WHITE
} else {
Color32::BLACK
Expand Down
7 changes: 7 additions & 0 deletions crates/egui/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ impl Options {
Theme::Light => &self.light_style,
}
}

pub(crate) fn style_mut(&mut self) -> &mut std::sync::Arc<Style> {
match self.theme() {
Theme::Dark => &mut self.dark_style,
Theme::Light => &mut self.light_style,
}
}
}

impl Options {
Expand Down
6 changes: 3 additions & 3 deletions crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn menu_popup<'c, R>(
.kind(UiKind::Menu)
.order(Order::Foreground)
.fixed_pos(pos)
.default_width(ctx.active_style().spacing.menu_width)
.default_width(ctx.style().spacing.menu_width)
.sense(Sense::hover());

let mut sizing_pass = false;
Expand Down Expand Up @@ -389,9 +389,9 @@ impl MenuRoot {
// or button hovered while other menu is open
let mut pos = button.rect.left_bottom();

let menu_frame = Frame::menu(&button.ctx.active_style());
let menu_frame = Frame::menu(&button.ctx.style());
pos.x -= menu_frame.total_margin().left; // Make fist button in menu align with the parent button
pos.y += button.ctx.active_style().spacing.menu_spacing;
pos.y += button.ctx.style().spacing.menu_spacing;

if let Some(root) = root.inner.as_mut() {
let menu_rect = root.menu_state.read().rect;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Painter {
}

pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
let color = self.ctx.active_style().visuals.error_fg_color;
let color = self.ctx.style().visuals.error_fg_color;
self.debug_text(pos, Align2::LEFT_TOP, color, format!("🔥 {text}"))
}

Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl Response {
return false;
}

let style = self.ctx.active_style();
let style = self.ctx.style();

let tooltip_delay = style.interaction.tooltip_delay;
let tooltip_grace_time = style.interaction.tooltip_grace_time;
Expand Down Expand Up @@ -887,7 +887,7 @@ impl Response {
/// # });
/// ```
pub fn scroll_to_me(&self, align: Option<Align>) {
self.scroll_to_me_animation(align, self.ctx.active_style().scroll_animation);
self.scroll_to_me_animation(align, self.ctx.style().scroll_animation);
}

/// Like [`Self::scroll_to_me`], but allows you to specify the [`crate::style::ScrollAnimation`].
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl From<TextStyle> for FontSelection {
/// Specifies the look and feel of egui.
///
/// You can change the visuals of a [`Ui`] with [`Ui::style_mut`]
/// and of everything with [`crate::Context::set_style`].
/// and of everything with [`crate::Context::set_style_of`].
/// To choose between dark and light style, use [`crate::Context::set_theme`].
///
/// If you want to change fonts, use [`crate::Context::set_fonts`] instead.
Expand Down Expand Up @@ -854,7 +854,7 @@ impl Default for TextCursorStyle {
/// Controls the visual style (colors etc) of egui.
///
/// You can change the visuals of a [`Ui`] with [`Ui::visuals_mut`]
/// and of everything with [`crate::Context::set_visuals`].
/// and of everything with [`crate::Context::set_visuals_of`].
///
/// If you want to change fonts, use [`crate::Context::set_fonts`] instead.
#[derive(Clone, Debug, PartialEq)]
Expand Down
Loading

0 comments on commit 04c0f22

Please sign in to comment.