[desktop] Add support for system theme detection

This commit is contained in:
Vladimir Stoilov
2024-07-24 10:53:58 +03:00
parent 5b03076fac
commit a6e1a37239
6 changed files with 159 additions and 20 deletions

View File

@@ -20,7 +20,6 @@ mod window;
use log::{debug, error, info};
use portmaster::PortmasterExt;
use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt};
use traymenu::setup_tray_menu;
use window::{close_splash_window, create_main_window};

View File

@@ -46,6 +46,7 @@ pub trait ServiceManager {
fn start(&self) -> Result<StatusResult>;
}
#[allow(dead_code)]
struct EmptyServiceManager();
impl ServiceManager for EmptyServiceManager {

View File

@@ -41,12 +41,56 @@ lazy_static! {
const PM_TRAY_ICON_ID: &'static str = "pm_icon";
// Icons
const BLUE_ICON: &'static [u8] = include_bytes!("../../../../assets/data/icons/pm_light_blue.ico");
const RED_ICON: &'static [u8] = include_bytes!("../../../../assets/data/icons/pm_light_red.ico");
const YELLOW_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_yellow.ico");
const GREEN_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_green.ico");
fn get_green_icon() -> &'static [u8] {
const LIGHT_GREEN_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_green.ico");
const DARK_GREEN_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_dark_green.ico");
let mode = dark_light::detect();
match mode {
dark_light::Mode::Light => DARK_GREEN_ICON,
_ => LIGHT_GREEN_ICON,
}
}
fn get_blue_icon() -> &'static [u8] {
const LIGHT_BLUE_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_blue.ico");
const DARK_BLUE_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_dark_blue.ico");
let mode = dark_light::detect();
match mode {
dark_light::Mode::Light => DARK_BLUE_ICON,
_ => LIGHT_BLUE_ICON,
}
}
fn get_red_icon() -> &'static [u8] {
const LIGHT_RED_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_red.ico");
const DARK_RED_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_dark_red.ico");
let mode = dark_light::detect();
match mode {
dark_light::Mode::Light => DARK_RED_ICON,
_ => LIGHT_RED_ICON,
}
}
fn get_yellow_icon() -> &'static [u8] {
const LIGHT_YELLOW_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_yellow.ico");
const DARK_YELLOW_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_dark_yellow.ico");
let mode = dark_light::detect();
match mode {
dark_light::Mode::Light => DARK_YELLOW_ICON,
_ => LIGHT_YELLOW_ICON,
}
}
pub fn setup_tray_menu(
app: &mut tauri::App,
@@ -105,7 +149,7 @@ pub fn setup_tray_menu(
.build()?;
let icon = TrayIconBuilder::with_id(PM_TRAY_ICON_ID)
.icon(Image::from_bytes(RED_ICON).unwrap())
.icon(Image::from_bytes(get_red_icon()).unwrap())
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
"exit_ui" => {
@@ -205,11 +249,11 @@ pub fn update_icon(icon: AppIcon, subsystems: HashMap<String, Subsystem>, spn_st
}
let next_icon = match failure.0 {
subsystem::FAILURE_WARNING => YELLOW_ICON,
subsystem::FAILURE_ERROR => RED_ICON,
subsystem::FAILURE_WARNING => get_yellow_icon(),
subsystem::FAILURE_ERROR => get_red_icon(),
_ => match spn_status.as_str() {
"connected" | "connecting" => BLUE_ICON,
_ => GREEN_ICON,
"connected" | "connecting" => get_blue_icon(),
_ => get_green_icon(),
},
};
@@ -289,7 +333,7 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
}
};
_ = icon.set_icon(Some(Image::from_bytes(BLUE_ICON).unwrap()));
_ = icon.set_icon(Some(Image::from_bytes(get_blue_icon()).unwrap()));
let mut subsystems: HashMap<String, Subsystem> = HashMap::new();
let mut spn_status: String = "".to_string();
@@ -407,7 +451,7 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
}
update_spn_ui_state(false);
_ = icon.set_icon(Some(Image::from_bytes(RED_ICON).unwrap()));
_ = icon.set_icon(Some(Image::from_bytes(get_red_icon()).unwrap()));
}
fn update_spn_ui_state(enabled: bool) {

View File

@@ -1,11 +1,15 @@
use log::{debug, error};
use tauri::{
AppHandle, Listener, Manager, Result, Theme, UserAttentionType, WebviewUrl, WebviewWindow,
WebviewWindowBuilder,
image::Image, AppHandle, Listener, Manager, Result, Theme, UserAttentionType, WebviewUrl,
WebviewWindow, WebviewWindowBuilder,
};
use crate::portmaster::PortmasterExt;
const LIGHT_PM_ICON: &'static [u8] =
include_bytes!("../../../../assets/data/icons/pm_light_512.png");
const DARK_PM_ICON: &'static [u8] = include_bytes!("../../../../assets/data/icons/pm_dark_512.png");
/// Either returns the existing "main" window or creates a new one.
///
/// The window is not automatically shown (i.e it starts hidden).
@@ -47,6 +51,11 @@ pub fn create_main_window(app: &AppHandle) -> Result<WebviewWindow> {
// If the window is not yet navigated to the Portmaster UI, do it now.
may_navigate_to_ui(&mut window, false);
let _ = match dark_light::detect() {
dark_light::Mode::Light => window.set_icon(Image::from_bytes(DARK_PM_ICON).unwrap()),
_ => window.set_icon(Image::from_bytes(LIGHT_PM_ICON).unwrap()),
};
#[cfg(debug_assertions)]
if let Ok(_) = std::env::var("TAURI_SHOW_IMMEDIATELY") {
debug!("[tauri] TAURI_SHOW_IMMEDIATELY is set, opening window");