Skip to content

Commit

Permalink
Populate system_theme
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 21, 2024
1 parent 06bb08e commit cd7ad15
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 101 deletions.
37 changes: 0 additions & 37 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,6 @@ pub struct NativeOptions {
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub renderer: Renderer,

/// Try to detect and follow the system preferred setting for dark vs light mode.
///
/// The theme will automatically change when the dark vs light mode preference is changed.
///
/// Does not work on Linux (see <https://github.com/rust-windowing/winit/issues/1549>).
///
/// See also [`Self::default_theme`].
pub follow_system_theme: bool,

/// Which theme to use in case [`Self::follow_system_theme`] is `false`
/// or eframe fails to detect the system theme.
///
/// Default: [`egui::Theme::Dark`].
pub default_theme: egui::Theme,

/// This controls what happens when you close the main eframe window.
///
/// If `true`, execution will continue after the eframe window is closed.
Expand Down Expand Up @@ -417,8 +402,6 @@ impl Default for NativeOptions {
#[cfg(any(feature = "glow", feature = "wgpu"))]
renderer: Renderer::default(),

follow_system_theme: cfg!(target_os = "macos") || cfg!(target_os = "windows"),
default_theme: egui::Theme::Dark,
run_and_return: true,

#[cfg(any(feature = "glow", feature = "wgpu"))]
Expand Down Expand Up @@ -449,19 +432,6 @@ impl Default for NativeOptions {
/// Options when using `eframe` in a web page.
#[cfg(target_arch = "wasm32")]
pub struct WebOptions {
/// Try to detect and follow the system preferred setting for dark vs light mode.
///
/// See also [`Self::default_theme`].
///
/// Default: `true`.
pub follow_system_theme: bool,

/// Which theme to use in case [`Self::follow_system_theme`] is `false`
/// or system theme detection fails.
///
/// Default: `egui::Theme::Dark`.
pub default_theme: egui::Theme,

/// Sets the number of bits in the depth buffer.
///
/// `egui` doesn't need the depth buffer, so the default value is 0.
Expand Down Expand Up @@ -492,8 +462,6 @@ pub struct WebOptions {
impl Default for WebOptions {
fn default() -> Self {
Self {
follow_system_theme: true,
default_theme: egui::Theme::Dark,
depth_buffer: 0,

#[cfg(feature = "glow")]
Expand Down Expand Up @@ -789,11 +757,6 @@ pub struct IntegrationInfo {
#[cfg(target_arch = "wasm32")]
pub web_info: WebInfo,

/// Does the OS use dark or light mode?
///
/// `None` means "don't know".
pub system_theme: Option<egui::Theme>,

/// Seconds of cpu usage (in seconds) on the previous frame.
///
/// This includes [`App::update`] as well as rendering (except for vsync waiting).
Expand Down
22 changes: 2 additions & 20 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winit::event_loop::EventLoopWindowTarget;

use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};

use egui::{DeferredViewportUiCallback, NumExt as _, Theme, ViewportBuilder, ViewportId};
use egui::{DeferredViewportUiCallback, NumExt as _, ViewportBuilder, ViewportId};
use egui_winit::{EventResponse, WindowSettings};

use crate::epi;
Expand Down Expand Up @@ -161,7 +161,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,
Expand All @@ -172,7 +171,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>>,
Expand All @@ -183,10 +181,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,
Expand Down Expand Up @@ -220,7 +215,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,
Expand Down Expand Up @@ -275,11 +269,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());
}
_ => {}
}

Expand Down Expand Up @@ -422,10 +411,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,
}
}
7 changes: 1 addition & 6 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,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,
Expand Down Expand Up @@ -283,9 +280,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
Expand Down Expand Up @@ -1138,6 +1132,7 @@ impl GlutinWindowContext {
viewport_id,
event_loop,
Some(window.scale_factor() as f32),
window.theme(),
self.max_texture_side,
)
});
Expand Down
6 changes: 2 additions & 4 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,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,
Expand Down Expand Up @@ -246,6 +244,7 @@ impl WgpuWinitApp {
ViewportId::ROOT,
event_loop,
Some(window.scale_factor() as f32),
window.theme(),
painter.max_texture_side(),
);

Expand All @@ -254,8 +253,6 @@ impl WgpuWinitApp {
let event_loop_proxy = self.repaint_proxy.lock().clone();
integration.init_accesskit(&mut egui_winit, &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");
Expand Down Expand Up @@ -895,6 +892,7 @@ impl Viewport {
viewport_id,
event_loop,
Some(window.scale_factor() as f32),
window.theme(),
painter.max_texture_side(),
));

Expand Down
10 changes: 0 additions & 10 deletions crates/eframe/src/native/winit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,6 @@ pub enum EventResult {
Exit,
}

pub fn system_theme(window: &Window, options: &crate::NativeOptions) -> Option<egui::Theme> {
if options.follow_system_theme {
window
.theme()
.map(super::epi_integration::theme_from_winit_theme)
} else {
None
}
}

/// Short and fast description of an event.
/// Useful for logging and profiling.
pub fn short_event_description(event: &winit::event::Event<UserEvent>) -> &'static str {
Expand Down
11 changes: 1 addition & 10 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ impl AppRunner {
) -> Result<Self, String> {
let painter = super::ActiveWebPainter::new(canvas, &web_options).await?;

let system_theme = if web_options.follow_system_theme {
super::system_theme()
} else {
None
};

let info = epi::IntegrationInfo {
web_info: epi::WebInfo {
user_agent: super::user_agent().unwrap_or_default(),
location: super::web_location(),
},
system_theme,
cpu_usage: None,
};
let storage = LocalStorage::default();
Expand All @@ -68,9 +61,6 @@ impl AppRunner {
o.zoom_factor = 1.0;
});

let theme = system_theme.unwrap_or(web_options.default_theme);
egui_ctx.set_visuals(theme.egui_visuals());

let cc = epi::CreationContext {
egui_ctx: egui_ctx.clone(),
integration_info: info.clone(),
Expand Down Expand Up @@ -132,6 +122,7 @@ impl AppRunner {
.entry(egui::ViewportId::ROOT)
.or_default()
.native_pixels_per_point = Some(super::native_pixels_per_point());
runner.input.raw.system_theme = super::system_theme();

Ok(runner)
}
Expand Down
11 changes: 6 additions & 5 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub(crate) fn install_event_handlers(runner_ref: &WebRunner) -> Result<(), JsVal
install_wheel(runner_ref, &canvas)?;
install_drag_and_drop(runner_ref, &canvas)?;
install_window_events(runner_ref, &window)?;
install_color_scheme_change_event(runner_ref, &window)?;
Ok(())
}

Expand Down Expand Up @@ -353,17 +354,17 @@ fn install_window_events(runner_ref: &WebRunner, window: &EventTarget) -> Result
Ok(())
}

pub(crate) fn install_color_scheme_change_event(runner_ref: &WebRunner) -> Result<(), JsValue> {
let window = web_sys::window().unwrap();

fn install_color_scheme_change_event(
runner_ref: &WebRunner,
window: &web_sys::Window,
) -> Result<(), JsValue> {
if let Some(media_query_list) = prefers_color_scheme_dark(&window)? {
runner_ref.add_event_listener::<web_sys::MediaQueryListEvent>(
&media_query_list,
"change",
|event, runner| {
let theme = theme_from_dark_mode(event.matches());
runner.frame.info.system_theme = Some(theme);
runner.egui_ctx().set_visuals(theme.egui_visuals());
runner.input.raw.system_theme = Some(theme);
runner.needs_repaint.repaint_asap();
},
)?;
Expand Down
6 changes: 0 additions & 6 deletions crates/eframe/src/web/web_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ impl WebRunner {
) -> Result<(), JsValue> {
self.destroy();

let follow_system_theme = web_options.follow_system_theme;

let text_agent = TextAgent::attach(self)?;

let runner = AppRunner::new(canvas, web_options, app_creator, text_agent).await?;
Expand All @@ -83,10 +81,6 @@ impl WebRunner {
{
events::install_event_handlers(self)?;

if follow_system_theme {
events::install_color_scheme_change_event(self)?;
}

// The resize observer handles calling `request_animation_frame` to start the render loop.
events::install_resize_observer(self)?;
}
Expand Down
19 changes: 17 additions & 2 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use accesskit_winit;
pub use egui;
#[cfg(feature = "accesskit")]
use egui::accesskit;
use egui::{Pos2, Rect, Vec2, ViewportBuilder, ViewportCommand, ViewportId, ViewportInfo};
use egui::{Pos2, Rect, Theme, Vec2, ViewportBuilder, ViewportCommand, ViewportId, ViewportInfo};
pub use winit;

pub mod clipboard;
Expand Down Expand Up @@ -111,6 +111,7 @@ impl State {
viewport_id: ViewportId,
display_target: &dyn HasDisplayHandle,
native_pixels_per_point: Option<f32>,
theme: Option<winit::window::Theme>,
max_texture_side: Option<usize>,
) -> Self {
crate::profile_function!();
Expand Down Expand Up @@ -150,6 +151,7 @@ impl State {
.entry(ViewportId::ROOT)
.or_default()
.native_pixels_per_point = native_pixels_per_point;
slf.egui_input.system_theme = theme.map(to_egui_theme);

if let Some(max_texture_side) = max_texture_side {
slf.set_max_texture_side(max_texture_side);
Expand Down Expand Up @@ -405,6 +407,13 @@ impl State {
consumed: false,
}
}
WindowEvent::ThemeChanged(winit_theme) => {
self.egui_input.system_theme = Some(to_egui_theme(*winit_theme));
EventResponse {
repaint: true,
consumed: false,
}
}
WindowEvent::HoveredFile(path) => {
self.egui_input.hovered_files.push(egui::HoveredFile {
path: Some(path.clone()),
Expand Down Expand Up @@ -464,7 +473,6 @@ impl State {
| WindowEvent::Occluded(_)
| WindowEvent::Resized(_)
| WindowEvent::Moved(_)
| WindowEvent::ThemeChanged(_)
| WindowEvent::TouchpadPressure { .. }
| WindowEvent::CloseRequested => EventResponse {
repaint: true,
Expand Down Expand Up @@ -891,6 +899,13 @@ impl State {
}
}

fn to_egui_theme(theme: winit::window::Theme) -> Theme {
match theme {
winit::window::Theme::Dark => Theme::Dark,
winit::window::Theme::Light => Theme::Light,
}
}

pub fn inner_rect_in_points(window: &Window, pixels_per_point: f32) -> Option<Rect> {
let inner_pos_px = window.inner_position().ok()?;
let inner_pos_px = egui::pos2(inner_pos_px.x as f32, inner_pos_px.y as f32);
Expand Down
3 changes: 2 additions & 1 deletion crates/egui_glow/examples/pure_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ fn main() {
let (gl_window, gl) = create_display(&event_loop);
let gl = std::sync::Arc::new(gl);

let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None, None, true);
let mut egui_glow =
egui_glow::EguiGlow::new(&event_loop, &gl_window.window, gl.clone(), None, None, true);

let event_loop_proxy = egui::mutex::Mutex::new(event_loop.create_proxy());
egui_glow
Expand Down
Loading

0 comments on commit cd7ad15

Please sign in to comment.