From 23afef2ea50fdedfce2d48e5524c976cf47d62ad Mon Sep 17 00:00:00 2001 From: Benedikt Terhechte Date: Sun, 14 Nov 2021 18:41:28 -0500 Subject: [PATCH] Improved platform color handling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/gui/app.rs | 9 ++---- src/gui/app_state/import.rs | 4 +-- src/gui/app_state/main.rs | 4 +-- src/gui/app_state/startup.rs | 2 +- src/gui/platform/linux.rs | 24 +++++++++------ src/gui/platform/macos.rs | 18 ++++++----- src/gui/platform/mod.rs | 58 ++++++++++++++++++++++++++++++----- src/gui/platform/windows.rs | 18 ++++++----- src/gui/textures.rs | 3 +- src/gui/widgets/rectangles.rs | 2 +- 12 files changed, 98 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53a1154..6b867a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1645,11 +1645,11 @@ dependencies = [ "eyre", "flate2", "image", - "lazy_static", "lru", "mbox-reader", "num-format", "objc", + "once_cell", "rand", "rayon", "regex", diff --git a/Cargo.toml b/Cargo.toml index 88f729a..e6502d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,10 +28,10 @@ tracing-subscriber = "0.3.0" rusqlite = {version = "0.25.3", features = ["chrono", "trace", "serde_json"]} regex = "1.5.3" flate2 = "1.0.22" +once_cell = "1.8.0" email-parser = { git = "https://github.com/terhechte/email-parser", features = ["sender", "to", "in-reply-to", "date", "subject", "mime", "allow-duplicate-headers", "compatibility-fixes"]} rayon = "1.5.1" chrono = "0.4.19" -lazy_static = "*" serde_json = "*" serde = { version = "*", features = ["derive"]} crossbeam-channel = "0.5.1" diff --git a/src/gui/app.rs b/src/gui/app.rs index 1c5fb24..61a33f7 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -4,6 +4,7 @@ use eframe::{ }; use super::app_state::StateUI; +use super::platform::Theme; use super::textures::Textures; pub struct PostsackApp { @@ -30,13 +31,7 @@ impl App for PostsackApp { } fn setup(&mut self, ctx: &egui::CtxRef, frame: &mut Frame<'_>, _storage: Option<&dyn Storage>) { - super::platform::setup(ctx); - - // Adapt to the platform colors - let platform_colors = super::platform::platform_colors(); - let mut visuals = egui::Visuals::dark(); - visuals.widgets.noninteractive.bg_fill = platform_colors.window_background_dark; - ctx.set_visuals(visuals); + super::platform::setup(ctx, Theme::Dark); // Load textures self.textures = Some(Textures::populated(frame)); diff --git a/src/gui/app_state/import.rs b/src/gui/app_state/import.rs index 539e4ed..cf90e57 100644 --- a/src/gui/app_state/import.rs +++ b/src/gui/app_state/import.rs @@ -202,7 +202,7 @@ impl ImporterUI { shadow_background( ui.painter(), paint_rect, - colors.window_background_dark, + colors.window_background, Stroke::new(1.0, Color32::from_gray(90)), 12.0, Shadow::big_dark(), @@ -271,7 +271,7 @@ impl ImporterUI { } #[cfg(not(target_os = "macos"))] - fn permission_ui(&mut self, ui: &mut egui::Ui, textures: &Option) -> Response { + fn permission_ui(&mut self, ui: &mut egui::Ui, _textures: &Option) -> Response { ui.label("") } } diff --git a/src/gui/app_state/main.rs b/src/gui/app_state/main.rs index 9b60737..09cb5ac 100644 --- a/src/gui/app_state/main.rs +++ b/src/gui/app_state/main.rs @@ -52,7 +52,7 @@ impl StateUIVariant for MainUI { let platform_colors = super::super::platform::platform_colors(); let frame = egui::containers::Frame::none() - .fill(platform_colors.window_background_dark) + .fill(platform_colors.window_background) .stroke(Stroke::none()); egui::TopBottomPanel::top("my_panel") @@ -86,7 +86,7 @@ impl StateUIVariant for MainUI { }); } else { let stroke = Stroke::none(); - let fill = platform_colors.content_background_dark; + let fill = platform_colors.content_background; super::super::widgets::background::color_background( ui, 15.0, diff --git a/src/gui/app_state/startup.rs b/src/gui/app_state/startup.rs index d22713f..6bfb656 100644 --- a/src/gui/app_state/startup.rs +++ b/src/gui/app_state/startup.rs @@ -113,7 +113,7 @@ impl StartupUI { shadow_background( ui.painter(), paint_rect, - colors.window_background_dark, + colors.window_background, Stroke::new(1.0, Color32::from_gray(90)), 12.0, Shadow::big_dark(), diff --git a/src/gui/platform/linux.rs b/src/gui/platform/linux.rs index 8cb9dc3..f8bfc03 100644 --- a/src/gui/platform/linux.rs +++ b/src/gui/platform/linux.rs @@ -1,25 +1,29 @@ #![cfg(target_os = "linux")] -use eyre::Result; use eframe::egui::{self, Color32}; +use eyre::Result; -use super::PlatformColors; +use super::{PlatformColors, Theme}; -pub fn platform_colors() -> PlatformColors { +pub fn platform_colors(theme: Theme) -> PlatformColors { // From Google images, Gtk - PlatformColors { - window_background_dark: Color32::from_rgb(53, 53, 53), - window_background_light: Color32::from_rgb(246, 245, 244), - content_background_dark: Color32::from_rgb(34, 32, 40), - content_background_light: Color32::from_rgb(254, 254, 254), + match theme { + Theme::Light => PlatformColors { + window_background: Color32::from_rgb(246, 245, 244), + content_background: Color32::from_rgb(254, 254, 254), + }, + Theme::Dark => PlatformColors { + window_background: Color32::from_rgb(73, 73, 73), + content_background: Color32::from_rgb(34, 32, 40), + }, } } /// This is called from `App::setup` -pub fn setup(ctx: &egui::CtxRef) {} +pub fn setup(_ctx: &egui::CtxRef) {} /// This is called once from `App::update` on the first run. -pub fn initial_update(ctx: &egui::CtxRef) -> Result<()> { +pub fn initial_update(_ctx: &egui::CtxRef) -> Result<()> { Ok(()) } diff --git a/src/gui/platform/macos.rs b/src/gui/platform/macos.rs index 2719f17..ee2fc9b 100644 --- a/src/gui/platform/macos.rs +++ b/src/gui/platform/macos.rs @@ -11,14 +11,18 @@ use eframe::egui::{self, Color32, FontDefinitions, FontFamily, Stroke}; use eyre::{bail, Result}; use objc::runtime::{Object, YES}; -use super::PlatformColors; +use super::{PlatformColors, Theme}; -pub fn platform_colors() -> PlatformColors { - PlatformColors { - window_background_dark: Color32::from_rgb(36, 30, 42), - window_background_light: Color32::from_rgb(238, 236, 242), - content_background_dark: Color32::from_rgb(20, 14, 26), - content_background_light: Color32::from_rgb(236, 234, 238), +pub fn platform_colors(theme: THeme) -> PlatformColors { + match theme { + Theme::Light => PlatformColors { + window_background: Color32::from_rgb(238, 236, 242), + content_background: Color32::from_rgb(236, 234, 238), + }, + Theme::Dark => PlatformColors { + window_background: Color32::from_rgb(36, 30, 42), + content_background: Color32::from_rgb(20, 14, 26), + }, } } diff --git a/src/gui/platform/mod.rs b/src/gui/platform/mod.rs index 73e8f8e..86a7f58 100644 --- a/src/gui/platform/mod.rs +++ b/src/gui/platform/mod.rs @@ -1,27 +1,69 @@ +use eframe::egui::{self, Color32, Visuals}; +use once_cell::sync::OnceCell; + +static INSTANCE: OnceCell = OnceCell::new(); + #[cfg(target_os = "windows")] mod windows; #[cfg(target_os = "windows")] -pub use windows::{initial_update, navigation_button, platform_colors, setup}; +pub use windows::{initial_update, navigation_button}; #[cfg(target_os = "linux")] mod linux; #[cfg(target_os = "linux")] -pub use linux::{initial_update, navigation_button, platform_colors, setup}; +pub use linux::{initial_update, navigation_button}; #[cfg(target_os = "macos")] mod macos; #[cfg(target_os = "macos")] -pub use macos::{initial_update, navigation_button, platform_colors, setup}; +pub use macos::{initial_update, navigation_button}; -use eframe::egui::Color32; /// Platform-Native Colors #[derive(Debug)] pub struct PlatformColors { - pub window_background_dark: Color32, - pub window_background_light: Color32, - pub content_background_dark: Color32, - pub content_background_light: Color32, + pub window_background: Color32, + pub content_background: Color32, +} + +#[allow(unused)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum Theme { + Light, + Dark, +} + +impl Theme { + pub fn to_visuals(&self) -> Visuals { + match self { + Theme::Light => egui::Visuals::light(), + Theme::Dark => egui::Visuals::dark(), + } + } +} + +pub fn setup(ctx: &egui::CtxRef, theme: Theme) { + #[cfg(target_os = "windows")] + use windows as module; + + #[cfg(target_os = "linux")] + use linux as module; + + #[cfg(target_os = "macos")] + use macos as module; + + INSTANCE + .set(module::platform_colors(theme)) + .expect("Could not setup colors"); + let colors = module::platform_colors(theme); + let mut visuals = theme.to_visuals(); + visuals.widgets.noninteractive.bg_fill = colors.window_background; + ctx.set_visuals(visuals); + module::setup(ctx) +} + +pub fn platform_colors() -> &'static PlatformColors { + INSTANCE.get().unwrap() } diff --git a/src/gui/platform/windows.rs b/src/gui/platform/windows.rs index 48c64da..4528cd4 100644 --- a/src/gui/platform/windows.rs +++ b/src/gui/platform/windows.rs @@ -1,16 +1,20 @@ #![cfg(target_os = "windows")] -use eframe::egui; +use eframe::egui::{self, Color32}; -use super::PlatformColors; +use super::{PlatformColors, Theme}; pub fn platform_colors() -> PlatformColors { // From Google images, Windows 11 - PlatformColors { - window_background_dark: Color32::from_rgb(32, 32, 32), - window_background_light: Color32::from_rgb(241, 243, 246), - content_background_dark: Color32::from_rgb(34, 32, 40), - content_background_light: Color32::from_rgb(251, 251, 253), + match theme { + Theme::Light => PlatformColors { + window_background: Color32::from_rgb(241, 243, 246), + content_background: Color32::from_rgb(251, 251, 253), + }, + Theme::Dark => PlatformColors { + window_background: Color32::from_rgb(32, 32, 32), + content_background: Color32::from_rgb(34, 32, 40), + }, } } diff --git a/src/gui/textures.rs b/src/gui/textures.rs index 25966ca..d0e6e3b 100644 --- a/src/gui/textures.rs +++ b/src/gui/textures.rs @@ -16,7 +16,7 @@ pub struct Textures { } impl Textures { - pub fn populated(frame: &mut epi::Frame<'_>) -> Textures { + pub fn populated(_frame: &mut epi::Frame<'_>) -> Textures { #[cfg(target_os = "macos")] { let missing_permissions_image = install_missing_permission_image( @@ -35,6 +35,7 @@ impl Textures { /// Load the permission image // via: https://github.com/emilk/egui/blob/master/eframe/examples/image.rs +#[allow(unused)] fn install_missing_permission_image( image_data: &[u8], frame: &mut epi::Frame<'_>, diff --git a/src/gui/widgets/rectangles.rs b/src/gui/widgets/rectangles.rs index 80ec8e6..1469d68 100644 --- a/src/gui/widgets/rectangles.rs +++ b/src/gui/widgets/rectangles.rs @@ -46,7 +46,7 @@ impl<'a> Widget for Rectangles<'a> { for (index, item) in items.iter().enumerate() { let item_response = ui.put( item.layout_rect(), - rectangle(&item, active, colors.content_background_dark, index, total), + rectangle(&item, active, colors.content_background, index, total), ); if item_response.clicked() && active { *self.error = self.engine.push(item.clone()).err();