From 90dfd00d7b8e9b301ec59049412c288929cc9d10 Mon Sep 17 00:00:00 2001 From: luxluth Date: Sat, 14 Dec 2024 13:52:08 +0100 Subject: [PATCH] Support for custom css --- src/conf.rs | 18 +++++++++++++++++- src/main.rs | 7 +++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index 74360df..79e279c 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -16,6 +16,8 @@ terminal = kitty args = -e "#; +pub const DEFAULT_CSS: &str = include_str!("./style.css"); + #[derive(Clone)] pub struct GeneralConf { pub theme: String, @@ -36,10 +38,24 @@ impl Default for GeneralConf { #[derive(Default, Clone)] pub struct Config { pub general: GeneralConf, + pub css: String, } impl Config { pub fn parse(path: std::path::PathBuf) -> Self { + let mut css = DEFAULT_CSS.to_string(); + let css_path = path.parent().unwrap().join("style.css"); + if css_path.exists() { + if let Ok(mut f) = std::fs::File::open(&css_path) { + css = String::new(); + let _ = f.read_to_string(&mut css); + } + } else { + if let Ok(mut f) = std::fs::File::create(&css_path) { + let _ = f.write(DEFAULT_CSS.as_bytes()); + } + } + if let Ok(mut f) = std::fs::File::open(&path) { let mut data = String::new(); let _ = f.read_to_string(&mut data); @@ -88,7 +104,7 @@ impl Config { } } - return Self { general }; + return Self { general, css }; } return Self::default(); diff --git a/src/main.rs b/src/main.rs index 2eacfaf..dfd63b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -246,11 +246,10 @@ fn activate(config: conf::Config, app: &Application) { } } -fn load_css() { +fn load_css(css: String) { gtk::init().expect("Unable to init gtk"); let provider = gtk::CssProvider::new(); - let css = include_str!("./style.css"); - provider.load_from_string(css); + provider.load_from_string(&css); gtk::style_context_add_provider_for_display( >k::gdk::Display::default().expect("Could not connect to a display."), @@ -272,7 +271,7 @@ async fn main() { if bus::app_is_running() { bus::send_represent_event(); } else { - load_css(); + load_css(config.css.clone()); let application = Application::new(Some(conf::APP_ID), Default::default());