Skip to content

Commit

Permalink
Uses gresources
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Dec 18, 2024
1 parent 86348ae commit db0beee
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 43 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,3 +32,6 @@ features = ["std"]
[dependencies.todo-txt]
version = "3.1"
features = ["config", "extended"]

[build-dependencies]
glib-build-tools = "0.20"
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
glib_build_tools::compile_resources(
&["resources/"],
"resources/resource.xml",
"resources",
);
}
15 changes: 15 additions & 0 deletions resources/resource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/txt/todo/effitask">
<file>agenda.png</file>
<file>contexts.png</file>
<file>done.png</file>
<file>flag.png</file>
<file>inbox.png</file>
<file>projects.png</file>
<file>search.png</file>
<file compressed="true">style.css</file>
<file compressed="true">style_dark.css</file>
<file compressed="true">style_light.css</file>
</gresource>
</gresources>
51 changes: 12 additions & 39 deletions src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,21 @@ pub struct Model {
logger: relm4::Controller<crate::logger::Model>,
projects: relm4::Controller<crate::widgets::tags::Model>,
search: relm4::Controller<crate::search::Model>,
#[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(
&gtk::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(
&gtk::gdk::Display::default().unwrap(),
&css,
0,
);
}

fn stylesheet(&self) -> Option<std::path::PathBuf> {
fn stylesheet(&self) -> String {
let mut stylesheet = "style_light.css";

if let Ok(theme) = std::env::var("GTK_THEME") {
Expand All @@ -100,7 +94,7 @@ impl Model {
}
}

self.find_data_file(stylesheet)
format!("/txt/todo/effitask/{stylesheet}")
}

fn add_tab_widgets(&self, notebook: &gtk::Notebook) {
Expand Down Expand Up @@ -128,36 +122,16 @@ 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);

vbox
}

#[cfg(not(debug_assertions))]
fn find_data_file(&self, stylesheet: &str) -> Option<std::path::PathBuf> {
self.xdg.find_data_file(stylesheet)
}

#[cfg(debug_assertions)]
#[allow(clippy::unnecessary_wraps)]
fn find_data_file(&self, stylesheet: &str) -> Option<std::path::PathBuf> {
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),
Expand Down Expand Up @@ -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!();
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<application::Model>(config);
}

Expand All @@ -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");
}

0 comments on commit db0beee

Please sign in to comment.