How to use icons from Gnome Icons Libary ? #181

Closed
opened 2025-01-04 13:01:19 +01:00 by Ombrelin · 14 comments
Ombrelin commented 2025-01-04 13:01:19 +01:00 (Migrated from github.com)

Hi,

I'm trying to use icons from this app : https://flathub.org/apps/org.gnome.design.IconLibrary
in my app. I tried taking inspiration in examples (from java-gi-examples) that use resources, but with no success. I would like to use these icons with ViewStack.addTitledWithIcon for instance.

Currently I have :

  • A svg file copied from Gnome Icons library in src/main/resource/icons folder
  • myapp.gresource.xml file in src/main/resource folder
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
    <gresource prefix="/org/gtk/example/icons/scalable/actions/">
        <file preprocess="xml-stripblanks">git-symbolic.svg</file>
    </gresource>
</gresources>
  • In my Gradle script (I use kotlin script):
tasks.register("compileResources") {
    exec {
        workingDir("src/main/resources")
        commandLine("glib-implementation-resources", "myapp.gresource.xml")
    }
}

tasks.named("classes") {
    dependsOn("compileResources")
}

But when running the gradle task, I get :

A problem occurred configuring project ':app'.
Could not create task ':app:compileResources'.
A problem occurred starting process 'command 'glib-implementation-resources''
Cause: error=2, Aucun fichier ou dossier de ce nom

"Aucun fichier ou dossier de ce nom" means "No folder or directory of that name" in my locale

Could you provide an exemple app that shows how to use those custom icons ? Thanks in advance ! (and thanks for the awesome library)

Hi, I'm trying to use icons from this app : https://flathub.org/apps/org.gnome.design.IconLibrary in my app. I tried taking inspiration in examples (from java-gi-examples) that use resources, but with no success. I would like to use these icons with `ViewStack.addTitledWithIcon` for instance. Currently I have : - A svg file copied from Gnome Icons library in `src/main/resource/icons` folder - `myapp.gresource.xml` file in `src/main/resource` folder ```xml <?xml version="1.0" encoding="UTF-8"?> <gresources> <gresource prefix="/org/gtk/example/icons/scalable/actions/"> <file preprocess="xml-stripblanks">git-symbolic.svg</file> </gresource> </gresources> ``` - In my Gradle script (I use kotlin script): ```kts tasks.register("compileResources") { exec { workingDir("src/main/resources") commandLine("glib-implementation-resources", "myapp.gresource.xml") } } tasks.named("classes") { dependsOn("compileResources") } ``` But when running the gradle task, I get : > A problem occurred configuring project ':app'. > Could not create task ':app:compileResources'. > A problem occurred starting process 'command 'glib-implementation-resources'' > Cause: error=2, Aucun fichier ou dossier de ce nom "Aucun fichier ou dossier de ce nom" means "No folder or directory of that name" in my locale Could you provide an exemple app that shows how to use those custom icons ? Thanks in advance ! (and thanks for the awesome library)
jwharm commented 2025-01-04 13:31:23 +01:00 (Migrated from github.com)

Change glib-implementation-resources to glib-compile-resources.

Change `glib-implementation-resources` to `glib-compile-resources`.
Ombrelin commented 2025-01-04 14:03:11 +01:00 (Migrated from github.com)

Just tried it, go the same result 🤔

Just tried it, go the same result :thinking:
jwharm commented 2025-01-04 14:06:51 +01:00 (Migrated from github.com)

Does it work when you run glib-compile-resources from the command line?

if not, install the GLib development utilities. On Debian, it’s the libglib2.0-dev-bin package.

Does it work when you run `glib-compile-resources` from the command line? if not, install the GLib development utilities. On Debian, it’s the `libglib2.0-dev-bin` package.
Ombrelin commented 2025-01-04 14:32:40 +01:00 (Migrated from github.com)

That was it indeed, I installed the package and the gradle task now works.
I also updated the resource file to :

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
    <gresource prefix="/org/gtk/example/icons/scalable/actions/">
        <file preprocess="xml-stripblanks">icons/git-symbolic.svg</file>
    </gresource>
</gresources>

Because I got an error of file not found but no errors now.

But in my app, the icons still does not show (broken image logo instead) 🤔
The thing I'm least sure of is

  1. prefix="/org/gtk/example/icons/scalable/actions/" in my resource file, not sure what I shoudl put here ?
  2. the name I use the call the icon in my code. Currently using just its name, "git-symbolic"
That was it indeed, I installed the package and the gradle task now works. I also updated the resource file to : ```xml <?xml version="1.0" encoding="UTF-8"?> <gresources> <gresource prefix="/org/gtk/example/icons/scalable/actions/"> <file preprocess="xml-stripblanks">icons/git-symbolic.svg</file> </gresource> </gresources> ``` Because I got an error of file not found but no errors now. But in my app, the icons still does not show (broken image logo instead) :thinking: The thing I'm least sure of is 1. prefix="/org/gtk/example/icons/scalable/actions/" in my resource file, not sure what I shoudl put here ? 2. the name I use the call the icon in my code. Currently using just its name, "git-symbolic"
jwharm commented 2025-01-04 15:04:15 +01:00 (Migrated from github.com)

You can choose the prefix yourself.

Use Image.fromResource() to load the resource (use the prefix + filename) and then use that image as the icon.

You can choose the prefix yourself. Use `Image.fromResource()` to load the resource (use the prefix + filename) and then use that image as the icon.
jwharm commented 2025-01-04 15:09:32 +01:00 (Migrated from github.com)

Oh and you might want to use an alias to strip the “icons” folder name from the filename:

<file alias="git-symbolic.svg">icons/git-symbolic.svg</file>

You can then load the image from the resource as “/my/prefix/git-symbolic.svg”

Oh and you might want to use an alias to strip the “icons” folder name from the filename: ```xml <file alias="git-symbolic.svg">icons/git-symbolic.svg</file> ``` You can then load the image from the resource as “/my/prefix/git-symbolic.svg”
Ombrelin commented 2025-01-04 15:21:01 +01:00 (Migrated from github.com)

Thanks it works !!
Maybe I contribute a page to the docs that explains all this ?

Thanks it works !! Maybe I contribute a page to the docs that explains all this ?
jwharm commented 2025-01-04 15:27:35 +01:00 (Migrated from github.com)

Sure! PRs are always welcome!
The website is in the site docs folder. Most of it is plain markdown files.

Closing this issue.

Sure! PRs are always welcome! The website is in the ~site~ `docs` folder. Most of it is plain markdown files. Closing this issue.
Ombrelin commented 2025-01-04 15:39:55 +01:00 (Migrated from github.com)

Okay will do !
Last question : any idea on how to use a custom icon in ViewSwitcher title ? WiewStack.addTitledWithIcon only takes icon name as argument 🤔

Okay will do ! Last question : any idea on how to use a custom icon in `ViewSwitcher` title ? `WiewStack.addTitledWithIcon` only takes icon name as argument :thinking:
jwharm commented 2025-01-04 16:02:08 +01:00 (Migrated from github.com)

You have to add the resource to the current Gtk icon theme, and then you can refer to them by name:

// Get the icon theme used for the current window
var display = window.getDisplay();
var iconTheme = IconTheme.getForDisplay(display);

// Add icons to the theme
iconTheme.addResourcePath("/my/prefix");

I expect you can then refer to the icons using just the filename (without the prefix). But I haven't tested this myself; this is from the Gtk documentation.

You have to add the resource to the current Gtk icon theme, and then you can refer to them by name: ```java // Get the icon theme used for the current window var display = window.getDisplay(); var iconTheme = IconTheme.getForDisplay(display); // Add icons to the theme iconTheme.addResourcePath("/my/prefix"); ``` I expect you can then refer to the icons using just the filename (without the prefix). But I haven't tested this myself; this is from the Gtk documentation.
Ombrelin commented 2025-01-04 16:03:25 +01:00 (Migrated from github.com)

Ok thanks again will try this out 👍

Ok thanks again will try this out :+1:
Ombrelin commented 2025-01-05 21:13:29 +01:00 (Migrated from github.com)

Tested today your recommendation about Gtk Icon Theme for the view switcher, but didn't work sadly. Thanks for looking it up though 👍
Also sent you a PR for the docs 😊

Tested today your recommendation about Gtk Icon Theme for the view switcher, but didn't work sadly. Thanks for looking it up though :+1: Also sent you a PR for the docs :blush:
jwharm commented 2025-01-08 17:29:23 +01:00 (Migrated from github.com)

I'm afraid I don't have time to look into this further... It's not a Java-GI specific issue, so perhaps you can find examples in an existing application written in Vala or PyGObject.

I'm afraid I don't have time to look into this further... It's not a Java-GI specific issue, so perhaps you can find examples in an existing application written in Vala or PyGObject.
Ombrelin commented 2025-01-08 21:58:56 +01:00 (Migrated from github.com)

No worries ! You helped me a lot already regarding this subject.
Will write here if I find something 👍

No worries ! You helped me a lot already regarding this subject. Will write here if I find something 👍
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
java-gi/java-gi#181
No description provided.