diff --git a/CHANGELOG.md b/CHANGELOG.md index 2231efa0..b7a0dd24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * `egui::popup::popup_below_widget`: show a popup area below another widget. * Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range. * Add: `ui.spacing()`, `ui.spacing_mut()`, `ui.visuals()`, `ui.visuals_mut()`. +* Add: `ctx.set_visuals()`. ### Changed 🔧 diff --git a/egui/src/context.rs b/egui/src/context.rs index a7301f67..6980c9df 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -462,6 +462,17 @@ impl Context { self.memory().options.style = style.into(); } + /// The [`Visuals`] used by all new windows, panels etc. + /// + /// Example: + /// ``` + /// # let mut ctx = egui::CtxRef::default(); + /// ctx.set_visuals(egui::Visuals::light()); // Switch to light mode + /// ``` + pub fn set_visuals(&self, visuals: crate::Visuals) { + std::sync::Arc::make_mut(&mut self.memory().options.style).visuals = visuals; + } + /// The number of physical pixels for each logical point. pub fn pixels_per_point(&self) -> f32 { self.input.pixels_per_point() diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 1c73fd98..299ed97a 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -126,7 +126,7 @@ pub use { painter::Painter, response::Response, sense::Sense, - style::Style, + style::{Style, Visuals}, ui::Ui, widgets::*, }; diff --git a/egui/src/ui.rs b/egui/src/ui.rs index d3178acd..d7eaf8ac 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -122,13 +122,13 @@ impl Ui { /// Short for `&self.style().visuals` /// visuals options for this `Ui` and its children. - pub fn visuals(&self) -> &crate::style::Visuals { + pub fn visuals(&self) -> &crate::Visuals { &self.style.visuals } /// Mutably borrow internal `visuals`. /// Changes apply to this `Ui` and its subsequent children. - pub fn visuals_mut(&mut self) -> &mut crate::style::Visuals { + pub fn visuals_mut(&mut self) -> &mut crate::Visuals { &mut self.style_mut().visuals } diff --git a/egui_demo_lib/src/wrap_app.rs b/egui_demo_lib/src/wrap_app.rs index 2bd5aa65..35f0bba1 100644 --- a/egui_demo_lib/src/wrap_app.rs +++ b/egui_demo_lib/src/wrap_app.rs @@ -137,7 +137,7 @@ fn dark_light_mode_switch(ui: &mut egui::Ui) { let style: egui::Style = (*ui.ctx().style()).clone(); let new_visuals = style.visuals.light_dark_small_toggle_button(ui); if let Some(visuals) = new_visuals { - ui.ctx().set_style(egui::Style { visuals, ..style }); + ui.ctx().set_visuals(visuals); } }