1

I have started developing a small app in Rust using GTK4 bindings on Linux Mint.

I have created the following resources.gresource.xml in the project directory:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/org/myapp">
    <file>resources/icon.png</file>
  </gresource>
</gresources>

When the project is compiled, the XML is compiled as well into a binary resources.gresource file. This file is in the project root directory as well.

When the file is reviewed it shows that the PNG is indeed part of that binary. The PNG has the size of 64x64 pixels by the way.

gtk4 is imported as gtk. In the application code, the gresource file is loaded and registered as follows:

let gresource = gtk::gio::Resource::load("resources.gresource")
    .expect("Failed to load gresources");
gtk::gio::resources_register(&gresource);

When checking the children of each node in the gresource object, the following path exists: /org/myapp/resources/icon.png. As a result, everything seems to work so far.

This icon path is then set to be the window icon later in the code:

// window is of type: gtk::ApplicationWindow
window.set_icon_name(Some("/org/myapp/resources/icon.png"));

Everything compiles well. The problem is that the icon is not displayed as application icon during runtime. I have tried to set it as the icon for a button as well with a similar result: In that case a placeholder icon is shown.

When I use a system icon, e.g. "document-print", instead of "/org/myapp/resources/icon.png", then that application icon is displayed in the task bar.

Aside from trying all sorts of different paths I have researched in many places but nowhere I could find a solution to the problem. I hope somebody here with experience in Rust GTK4 development can tell me what I am missing.

1 Answer 1

1

Well like the name suggests set_icon_name:

Sets the icon for the window from a named themed icon.

That means your icon must be loaded as part of an IconTheme or be added to the current one.

You can easily add your resources to the search path of the current theme with this code:

    let display = Display::default().unwrap();
    let theme = gtk::IconTheme::for_display(&display);
    theme.add_resource_path("/org/myapp/resources");

But your icon must be within one of the hicolor theme paths such as /16x16/actions/ or /48x48/apps/

So you'll have to change the <file> in your resources XML-file to something like:

<file>resources/48x48/apps/icon.png</file>

and move the file accordingly.

After that your icon will be available under the name "icon" so you can set it with

window.set_icon_name(Some("icon"));

That being said, most modern OSs and Window managers ignore the icon theme so it's better to set the icon with a .desktop file on Linux, embed it as a .ico in the executable on Windows or include it as the icon in the app bundle on macOS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.