Skip to content

Commit

Permalink
Support for custom css
Browse files Browse the repository at this point in the history
  • Loading branch information
luxluth committed Dec 14, 2024
1 parent 899c7a1 commit 90dfd00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -88,7 +104,7 @@ impl Config {
}
}

return Self { general };
return Self { general, css };
}

return Self::default();
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
&gtk::gdk::Display::default().expect("Could not connect to a display."),
Expand All @@ -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());

Expand Down

0 comments on commit 90dfd00

Please sign in to comment.