mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Follow the System Theme in egui (#4860)
* Some initial progress towards #4490 This PR just moves `Theme` and the "follow system theme" settings to egui and adds `RawInput.system_theme`. A follow-up PR can then introduce the two separate `dark_mode_style` and `light_mode_style` fields on `Options`. <!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * [x] I have followed the instructions in the PR template ### Breaking changes The options `follow_system_theme` and `default_theme` has been moved from `eframe` into `egui::Options`, settable with `ctx.options_mut` --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -10,7 +10,7 @@ use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
|
||||
use egui::{DeferredViewportUiCallback, NumExt as _, ViewportBuilder, ViewportId};
|
||||
use egui_winit::{EventResponse, WindowSettings};
|
||||
|
||||
use crate::{epi, Theme};
|
||||
use crate::epi;
|
||||
|
||||
pub fn viewport_builder(
|
||||
egui_zoom_factor: f32,
|
||||
@@ -158,7 +158,6 @@ pub struct EpiIntegration {
|
||||
close: bool,
|
||||
|
||||
can_drag_window: bool,
|
||||
follow_system_theme: bool,
|
||||
#[cfg(feature = "persistence")]
|
||||
persist_window: bool,
|
||||
app_icon_setter: super::app_icon::AppTitleIconSetter,
|
||||
@@ -169,7 +168,6 @@ impl EpiIntegration {
|
||||
pub fn new(
|
||||
egui_ctx: egui::Context,
|
||||
window: &winit::window::Window,
|
||||
system_theme: Option<Theme>,
|
||||
app_name: &str,
|
||||
native_options: &crate::NativeOptions,
|
||||
storage: Option<Box<dyn epi::Storage>>,
|
||||
@@ -180,10 +178,7 @@ impl EpiIntegration {
|
||||
#[cfg(feature = "wgpu")] wgpu_render_state: Option<egui_wgpu::RenderState>,
|
||||
) -> Self {
|
||||
let frame = epi::Frame {
|
||||
info: epi::IntegrationInfo {
|
||||
system_theme,
|
||||
cpu_usage: None,
|
||||
},
|
||||
info: epi::IntegrationInfo { cpu_usage: None },
|
||||
storage,
|
||||
#[cfg(feature = "glow")]
|
||||
gl,
|
||||
@@ -217,7 +212,6 @@ impl EpiIntegration {
|
||||
pending_full_output: Default::default(),
|
||||
close: false,
|
||||
can_drag_window: false,
|
||||
follow_system_theme: native_options.follow_system_theme,
|
||||
#[cfg(feature = "persistence")]
|
||||
persist_window: native_options.persist_window,
|
||||
app_icon_setter,
|
||||
@@ -251,11 +245,6 @@ impl EpiIntegration {
|
||||
state: ElementState::Pressed,
|
||||
..
|
||||
} => self.can_drag_window = true,
|
||||
WindowEvent::ThemeChanged(winit_theme) if self.follow_system_theme => {
|
||||
let theme = theme_from_winit_theme(*winit_theme);
|
||||
self.frame.info.system_theme = Some(theme);
|
||||
self.egui_ctx.set_visuals(theme.egui_visuals());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -398,10 +387,3 @@ pub fn load_egui_memory(_storage: Option<&dyn epi::Storage>) -> Option<egui::Mem
|
||||
#[cfg(not(feature = "persistence"))]
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn theme_from_winit_theme(theme: winit::window::Theme) -> Theme {
|
||||
match theme {
|
||||
winit::window::Theme::Dark => Theme::Dark,
|
||||
winit::window::Theme::Light => Theme::Light,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,14 +228,11 @@ impl GlowWinitApp {
|
||||
}
|
||||
}
|
||||
|
||||
let system_theme =
|
||||
winit_integration::system_theme(&glutin.window(ViewportId::ROOT), &self.native_options);
|
||||
let painter = Rc::new(RefCell::new(painter));
|
||||
|
||||
let integration = EpiIntegration::new(
|
||||
egui_ctx,
|
||||
&glutin.window(ViewportId::ROOT),
|
||||
system_theme,
|
||||
&self.app_name,
|
||||
&self.native_options,
|
||||
storage,
|
||||
@@ -281,9 +278,6 @@ impl GlowWinitApp {
|
||||
}
|
||||
}
|
||||
|
||||
let theme = system_theme.unwrap_or(self.native_options.default_theme);
|
||||
integration.egui_ctx.set_visuals(theme.egui_visuals());
|
||||
|
||||
if self
|
||||
.native_options
|
||||
.viewport
|
||||
@@ -1120,6 +1114,7 @@ impl GlutinWindowContext {
|
||||
viewport_id,
|
||||
event_loop,
|
||||
Some(window.scale_factor() as f32),
|
||||
window.theme(),
|
||||
self.max_texture_side,
|
||||
)
|
||||
});
|
||||
|
||||
@@ -203,11 +203,9 @@ impl WgpuWinitApp {
|
||||
|
||||
let wgpu_render_state = painter.render_state();
|
||||
|
||||
let system_theme = winit_integration::system_theme(&window, &self.native_options);
|
||||
let integration = EpiIntegration::new(
|
||||
egui_ctx.clone(),
|
||||
&window,
|
||||
system_theme,
|
||||
&self.app_name,
|
||||
&self.native_options,
|
||||
storage,
|
||||
@@ -243,6 +241,7 @@ impl WgpuWinitApp {
|
||||
ViewportId::ROOT,
|
||||
event_loop,
|
||||
Some(window.scale_factor() as f32),
|
||||
window.theme(),
|
||||
painter.max_texture_side(),
|
||||
);
|
||||
|
||||
@@ -251,8 +250,6 @@ impl WgpuWinitApp {
|
||||
let event_loop_proxy = self.repaint_proxy.lock().clone();
|
||||
egui_winit.init_accesskit(&window, event_loop_proxy);
|
||||
}
|
||||
let theme = system_theme.unwrap_or(self.native_options.default_theme);
|
||||
egui_ctx.set_visuals(theme.egui_visuals());
|
||||
|
||||
let app_creator = std::mem::take(&mut self.app_creator)
|
||||
.expect("Single-use AppCreator has unexpectedly already been taken");
|
||||
@@ -872,6 +869,7 @@ impl Viewport {
|
||||
viewport_id,
|
||||
event_loop,
|
||||
Some(window.scale_factor() as f32),
|
||||
window.theme(),
|
||||
painter.max_texture_side(),
|
||||
));
|
||||
|
||||
|
||||
@@ -118,16 +118,6 @@ pub enum EventResult {
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub fn system_theme(window: &Window, options: &crate::NativeOptions) -> Option<crate::Theme> {
|
||||
if options.follow_system_theme {
|
||||
window
|
||||
.theme()
|
||||
.map(super::epi_integration::theme_from_winit_theme)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "accesskit")]
|
||||
pub(crate) fn on_accesskit_window_event(
|
||||
egui_winit: &mut egui_winit::State,
|
||||
|
||||
Reference in New Issue
Block a user