G_volume_get_icon() and the theme/HighDPI

Hi,
This is not explained in the Gio.Volume.get_icon.

I presume the function is not returning the themed/scaled icon for the volume and I have to call gtk_icon_theme_get_default() + gtk_icon_theme_lookup_by_gicon_for_scale() to get the properly scaled theme “approved” icon.

If yes - how do I get the proper scale factor?

Or I don’t and the icon will already be scaled and theme appropriate?

Thank you.

Hi,

Usually, we display GIcons with Gtk.Image , just set the pixel-size before setting the GIcon, and Gtk will manage to load at the most appropriate scale.

Why would it be explained there? It’s the GIO documentation: however you load an icon name from the theme is down to the icon theme.

This stuff is not documented into the icon getter because it’s part of the freedesktop platform.

You don’t: the toolkit knows the scaling factor, and is responsible for loading the correct asset given the scaling factor of the widget you’re using to display the icon.

GIcon is an abstract “icon” resource; how it is made concrete is up to the thing that loads the asset.

1 Like

@gwillems ,
Is there a way to get a file name of the icon? If not - can I get bitmap data as “char *”?

I took a look at GtkLoadableIcon, but I’m not sure how to do that…

Thank you.

Use gtk_icon_theme_lookup_by_gicon() and then use the GtkIconPaintable API.

1 Like

@ebassi,
Something like this, right?

GtkIconTheme *theme = gtk_icon_theme_get_default();
GtkIconPaintable *paintable = gtk_icon_theme_lookup_by_gicon( theme,icon, 16, scale, GTK_TEXT_DIR_NONE, GTK_ICON_LOOKUP_FORCE_REGULAR );
GFile *file = gtk_icon_paintable_get_file( paintable );

The question here is - how do I get the scale value?

Thank you.

You can get that from the destination widget’s Gtk.Widget:scale-factor, or the monitor’s Gdk.Monitor:scale-factor.

This can get a bit more involved if e.g. a user can drag a window between monitors with different scaling. As mentioned, offload that to the toolkit if you can.

@andyholmes
I don’t have a window - this will be called in the static function.

But I don’t see a way to get a proper “GdkMonitor *”… Can you help?

Thank you.

Can you please explain what you’re trying to do with that icon?
Just display it, or save the image in a file?

The scale is determined by the monitor where the Window is displayed. If you’ve no Window, then no way to get it.
You can create a temporary Window (keep it hidden) for the sole purpose of getting the scale, but if the user has a multi-monitor setup with different scaling configured, then you’ve no way to know on which one the Window was created, so the scale value may not be the one you expect.

@gwillems,
All I want to do is to load the icon from the file.
Drawing will be in a different function and it could be in a different time.

This is why I’m confused.
Drawing something should be independent from loading something for drawing. Especially in GTK where realizing/drawing something is a requirements for getting the (proper) size.

So the program goes like this:

  1. Get volume list on the initialization.
  2. Get an icons of every volume during the initialization.
  3. Main window appear.
  4. User hit the button in the “toolbar”.
  5. Child window appear which presents the volume list alone with the icons in a combo box.

However between 3, 4 and 5 the user can move trhe window between the monitors and by the time the drawing will happen - the scale factor can become invalid.

I can catch the appropriate signal of scale changing or window moving or whatever it is called, but that’s not a good solution.

OTOH, I’m not going to question the API that is thereto stay.
All I’m saying is that the choice is weird and ask if there is another way of getting the appropriate icons.

Or maybe there is an SVG file for that I can use or somehow acquire?

Thank you.

In that case, I would just:

  • get the GIcon in step 2 with Gio.Volume.get_icon()
  • in step 5, generate the icon for the combo list with Gtk.Image.new_from_gicon

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.