How to include custom icons in a Relm4 app? #496
-
Say that you have made some application-specific custom icons which live in your Relm4 app's repository, e.g. You want to reference these from code with the gtk::Button {
set_icon_name: "icon-foo"
} How would you configure your Relm4 app to include these icons? (And if applicable, is there a preferred location / folder structure for the icons within your repository?) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
If you use a meson based build system like relm4-template, you can do the following steps:
If you do not use meson, you can do something similar by including the gresource into your binary instead of installing it: https://github.com/Relm4/icons/blob/main/src/lib.rs#L28-L32 Alternatively, you can also contribute or suggest icons for relm4-icons to make them easier to access for others as well. |
Beta Was this translation helpful? Give feedback.
-
This is a Cargo-only approach to including resources (like icons) which does not require Meson, and is also (fairly) portable across environments. We add the following new files:
The icons are placed under the The <?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="com/example/Foobar/icons/24x24/actions/">
<file preprocess="xml-stripblanks" alias="icon-foo.svg">icons/icon-foo.svg</file>
<file preprocess="xml-stripblanks" alias="icon-bar.svg">icons/icon-bar.svg</file>
</gresource>
</gresources> In [package]
name = "foobar"
[build-dependencies]
glib-build-tools = "0.17.10" In use glib_build_tools::compile_resources;
fn main() {
compile_resources(
&["data"],
"data/icons.gresource.xml",
"icons.gresource",
);
} Finally, in fn initialize_custom_icons() {
gio::resources_register_include!("icons.gresource").unwrap();
let display = gdk::Display::default().unwrap();
let theme = gtk::IconTheme::for_display(&display);
theme.add_resource_path("/com/example/Foobar/icons");
}
fn main() {
let app = RelmApp::new("com.example.Foobar");
// default icons
relm4_icons::initialize_icons();
// custom icons
initialize_custom_icons();
} Side note: By adding a Cargo dependency on |
Beta Was this translation helpful? Give feedback.
-
not exactly an Icon, but this example shows how to embed PNG images (Logos) into your executable https://github.com/Relm4/Relm4/blob/main/examples/embedded_logo.rs |
Beta Was this translation helpful? Give feedback.
This is a Cargo-only approach to including resources (like icons) which does not require Meson, and is also (fairly) portable across environments.
We add the following new files:
The icons are placed under the
data/icons
directory.The
icons.gresource.xml
file looks like this (adapt it as required, e.g. using-symbolic
icon names):