From 5e5df5c401765ef78789d5e68d17fb1811e06257 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Wed, 21 Jan 2026 16:56:05 +0100 Subject: [PATCH] Include CSS as GResource Also use explicit namespace for bare functions and constants used in main.rs. --- Cargo.lock | 10 ++++++++++ Cargo.toml | 4 ++++ build.rs | 7 +++++++ {src => data}/application.css | 0 data/zoodex.gresource.xml | 6 ++++++ src/main.rs | 18 ++++++++++++------ 6 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 build.rs rename {src => data}/application.css (100%) create mode 100644 data/zoodex.gresource.xml diff --git a/Cargo.lock b/Cargo.lock index 7cffdb0..754a941 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -364,6 +364,15 @@ dependencies = [ "smallvec", ] +[[package]] +name = "glib-build-tools" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86aebe63bb050d4918cb1d629880cb35fcba7ccda6f6fc0ec1beffdaa1b9d5c3" +dependencies = [ + "gio", +] + [[package]] name = "glib-macros" version = "0.21.5" @@ -1109,6 +1118,7 @@ name = "zoodex" version = "1.0.0" dependencies = [ "async-sqlite", + "glib-build-tools", "gtk4", "libadwaita", "relm4", diff --git a/Cargo.toml b/Cargo.toml index 6103029..2ee6266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,10 @@ license = "GPL-3.0-or-later" [profile.release] lto = true +[build-dependencies] +# Keep version in sync with GLib as included in GTK4 +glib-build-tools = "0.21.0" + [dependencies] async-sqlite = { version = "0.5.4", default-features = false } gtk4 = { version = "0.10.3", features = ["v4_20"] } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..67415f9 --- /dev/null +++ b/build.rs @@ -0,0 +1,7 @@ +use glib_build_tools::compile_resources; + + + +fn main() { + compile_resources(&["data"], "data/zoodex.gresource.xml", "zoodex.gresource"); +} diff --git a/src/application.css b/data/application.css similarity index 100% rename from src/application.css rename to data/application.css diff --git a/data/zoodex.gresource.xml b/data/zoodex.gresource.xml new file mode 100644 index 0000000..8445e54 --- /dev/null +++ b/data/zoodex.gresource.xml @@ -0,0 +1,6 @@ + + + + application.css + + diff --git a/src/main.rs b/src/main.rs index b9ca37a..dbee00c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,8 @@ mod ui; mod views; use gtk4::gdk::Display; -use gtk4::{ - CssProvider, STYLE_PROVIDER_PRIORITY_APPLICATION, Settings, - style_context_add_provider_for_display, -}; +use gtk4::gio::File; +use gtk4::{CssProvider, Settings, gio}; use relm4::RelmApp; use crate::ui::App; @@ -30,11 +28,19 @@ fn include_app_css() { // We can't use relm4::set_global_css because we need access to the CSS provider // to relay color scheme changes to it. + gio::resources_register_include!("zoodex.gresource") + .expect("CSS resource bundle should have valid format"); + let app_css = File::for_uri("resource:///com/kernelmaft/zoodex/application.css"); + let provider = CssProvider::new(); let display = Display::default().expect("getting the default GDK4 display should never fail"); - provider.load_from_string(include_str!("application.css")); - style_context_add_provider_for_display(&display, &provider, STYLE_PROVIDER_PRIORITY_APPLICATION); + provider.load_from_file(&app_css); + gtk4::style_context_add_provider_for_display( + &display, + &provider, + gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION, + ); let settings = Settings::for_display(&display); provider.set_prefers_color_scheme(settings.gtk_interface_color_scheme());