Skip to content

Commit

Permalink
Populate system_theme
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jun 30, 2024
1 parent 3e7bb2c commit 48dad1c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ impl GlowWinitApp {
for viewport in glutin.viewports.values_mut() {
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
egui_winit.set_max_texture_side(max_texture_side);
if let Some(window) = viewport.window.as_deref() {
egui_winit.init_system_theme(window);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ impl WgpuWinitApp {
let event_loop_proxy = self.repaint_proxy.lock().clone();
integration.init_accesskit(&mut egui_winit, &window, event_loop_proxy);
}
egui_winit.init_system_theme(&window);

let theme = system_theme.unwrap_or(self.native_options.default_theme);
egui_ctx.set_visuals(Visuals::theme(theme));

Expand Down
1 change: 1 addition & 0 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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
1 change: 1 addition & 0 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ pub(crate) fn install_color_scheme_change_event(runner_ref: &WebRunner) -> Resul
let theme = theme_from_dark_mode(event.matches());
runner.frame.info.system_theme = Some(theme);
runner.egui_ctx().set_visuals(Visuals::theme(theme));
runner.input.raw.system_theme = Some(theme);
runner.needs_repaint.repaint_asap();
},
)?;
Expand Down
21 changes: 19 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 @@ -172,6 +172,10 @@ impl State {
));
}

pub fn init_system_theme(&mut self, window: &Window) {
self.egui_input.system_theme = window.theme().map(to_egui_theme);
}

/// Call this once a graphics context has been created to update the maximum texture dimensions
/// that egui will use.
pub fn set_max_texture_side(&mut self, max_texture_side: usize) {
Expand Down Expand Up @@ -405,6 +409,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 +475,6 @@ impl State {
| WindowEvent::Occluded(_)
| WindowEvent::Resized(_)
| WindowEvent::Moved(_)
| WindowEvent::ThemeChanged(_)
| WindowEvent::TouchpadPressure { .. }
| WindowEvent::CloseRequested => EventResponse {
repaint: true,
Expand Down Expand Up @@ -891,6 +901,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

0 comments on commit 48dad1c

Please sign in to comment.