From db0beee7a4b6a7f1662e2b35b1d509ca6e1d313d Mon Sep 17 00:00:00 2001 From: Sanpi Date: Tue, 17 Dec 2024 23:09:13 +0100 Subject: [PATCH] Uses gresources --- Cargo.toml | 4 +++- Makefile | 3 --- build.rs | 7 ++++++ resources/resource.xml | 15 +++++++++++++ src/application/mod.rs | 51 ++++++++++-------------------------------- src/main.rs | 10 +++++++++ 6 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 build.rs create mode 100644 resources/resource.xml diff --git a/Cargo.toml b/Cargo.toml index 42a90ca..7b039ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ rand = "0.8" regex = "1.0" relm4 = "0.9" relm4-components = "0.9" -xdg = "2.1" [dependencies.chrono] version = "0.4" @@ -33,3 +32,6 @@ features = ["std"] [dependencies.todo-txt] version = "3.1" features = ["config", "extended"] + +[build-dependencies] +glib-build-tools = "0.20" diff --git a/Makefile b/Makefile index 78c3c97..1da02f3 100644 --- a/Makefile +++ b/Makefile @@ -23,9 +23,6 @@ gtk+-4.0: install: install --directory $(PREFIX)/bin install $(TARGET) $(PREFIX)/bin/ - install --directory $(PREFIX)/share/effitask - install --mode 644 resources/*.png $(PREFIX)/share/effitask/ - install --mode 644 resources/*.css $(PREFIX)/share/effitask/ test: $(CARGO) test $(CARGO_FLAGS) diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..c0b2c69 --- /dev/null +++ b/build.rs @@ -0,0 +1,7 @@ +fn main() { + glib_build_tools::compile_resources( + &["resources/"], + "resources/resource.xml", + "resources", + ); +} diff --git a/resources/resource.xml b/resources/resource.xml new file mode 100644 index 0000000..09457a6 --- /dev/null +++ b/resources/resource.xml @@ -0,0 +1,15 @@ + + + + agenda.png + contexts.png + done.png + flag.png + inbox.png + projects.png + search.png + style.css + style_dark.css + style_light.css + + diff --git a/src/application/mod.rs b/src/application/mod.rs index 35b0e8f..10180e4 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -67,27 +67,21 @@ pub struct Model { logger: relm4::Controller, projects: relm4::Controller, search: relm4::Controller, - #[allow(dead_code)] - xdg: xdg::BaseDirectories, } impl Model { fn load_style(&self) { let css = gtk::CssProvider::new(); - if let Some(stylesheet) = self.stylesheet() { - css.load_from_path(stylesheet); - - gtk::style_context_add_provider_for_display( - >k::gdk::Display::default().unwrap(), - &css, - 0, - ); - } else { - log::error!("Unable to find stylesheet"); - } + css.load_from_resource(&self.stylesheet()); + + gtk::style_context_add_provider_for_display( + >k::gdk::Display::default().unwrap(), + &css, + 0, + ); } - fn stylesheet(&self) -> Option { + fn stylesheet(&self) -> String { let mut stylesheet = "style_light.css"; if let Ok(theme) = std::env::var("GTK_THEME") { @@ -100,7 +94,7 @@ impl Model { } } - self.find_data_file(stylesheet) + format!("/txt/todo/effitask/{stylesheet}") } fn add_tab_widgets(&self, notebook: >k::Notebook) { @@ -128,13 +122,9 @@ impl Model { Page::Search => "search", }; - if let Some(filename) = self.find_data_file(&format!("{title}.png")) { - let image = gtk::Image::from_file(filename); - image.set_icon_size(gtk::IconSize::Large); - vbox.append(&image); - } else { - log::error!("Unable to find resource '{title}.png'"); - } + let image = gtk::Image::from_icon_name(title); + image.set_icon_size(gtk::IconSize::Large); + vbox.append(&image); let label = gtk::Label::new(Some(title)); vbox.append(&label); @@ -142,22 +132,6 @@ impl Model { vbox } - #[cfg(not(debug_assertions))] - fn find_data_file(&self, stylesheet: &str) -> Option { - self.xdg.find_data_file(stylesheet) - } - - #[cfg(debug_assertions)] - #[allow(clippy::unnecessary_wraps)] - fn find_data_file(&self, stylesheet: &str) -> Option { - let mut path = std::path::PathBuf::new(); - - path.push("resources"); - path.push(stylesheet); - - Some(path) - } - fn add(&mut self, widgets: &ModelWidgets, text: &str) { match add_task(text) { Ok(_) => self.update_tasks(widgets), @@ -385,7 +359,6 @@ impl relm4::Component for Model { logger, projects, search, - xdg: xdg::BaseDirectories::with_prefix(NAME.to_lowercase()).unwrap(), }; let widgets = view_output!(); diff --git a/src/main.rs b/src/main.rs index daf40de..7543d5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,8 @@ fn main() { let config = todo_txt::Config::from_env(); let app = relm4::RelmApp::new("txt.todo.effitask").with_args(Vec::new()); + initialize_resources(); + app.run::(config); } @@ -35,3 +37,11 @@ fn usage(program: &str) { println!(" {}", path.file_name().unwrap().to_str().unwrap()); println!(" Launch focus graphical interface"); } + +fn initialize_resources() { + gtk::gio::resources_register_include!("resources").unwrap(); + + let display = gtk::gdk::Display::default().unwrap(); + let theme = gtk::IconTheme::for_display(&display); + theme.add_resource_path("/txt/todo/effitask"); +}