Add ctx.set_visuals()

This commit is contained in:
Emil Ernerfeldt 2021-02-03 19:38:50 +01:00
parent a19fd7b780
commit af3195f086
5 changed files with 16 additions and 4 deletions

View file

@ -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 🔧

View file

@ -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()

View file

@ -126,7 +126,7 @@ pub use {
painter::Painter,
response::Response,
sense::Sense,
style::Style,
style::{Style, Visuals},
ui::Ui,
widgets::*,
};

View file

@ -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
}

View file

@ -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);
}
}