Include CSS as GResource

Also use explicit namespace for bare functions and constants used in
main.rs.
This commit is contained in:
Reinout Meliesie 2026-01-21 16:56:05 +01:00
commit 5e5df5c401
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
6 changed files with 39 additions and 6 deletions

10
Cargo.lock generated
View file

@ -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",

View file

@ -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"] }

7
build.rs Normal file
View file

@ -0,0 +1,7 @@
use glib_build_tools::compile_resources;
fn main() {
compile_resources(&["data"], "data/zoodex.gresource.xml", "zoodex.gresource");
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/kernelmaft/zoodex/">
<file>application.css</file>
</gresource>
</gresources>

View file

@ -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());