Firefox just got support for native gtk symbolic icons (quite crazy after so many years :), looks really good, ie window close button etc) but as that is gtk3 I was thinking of how it would be done in gtk4? I thought it would be simple but it turns out to be quite complicated. But after some reading and tinkering I finally managed to make it work without warnings (gtkimage only used for testing).
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk
gi.require_version('Gdk', '4.0')
from gi.repository import Gdk
from gi.repository import Gsk
from gi.repository import GdkPixbuf
class Main:
def __init__(self, app):
self.mainwin = Gtk.ApplicationWindow.new(app)
_display = Gdk.Display.get_default()
icon_theme = Gtk.IconTheme.get_for_display(_display)
icon_paint = icon_theme.lookup_icon("go-next-symbolic", None, 32, 1, 0, 0)
_rgba = Gdk.RGBA()
_rgba.parse("#d52")
_snapshot = Gtk.Snapshot.new()
icon_paint.snapshot_symbolic(_snapshot, 32, 32, [_rgba])
_surface = Gdk.Surface.new_toplevel(_display)
_renderer = Gsk.Renderer.new_for_surface(_surface)
_texture = _renderer.render_texture(_snapshot.to_node(), None)
_bytes = _texture.save_to_tiff_bytes()
_stride = GdkPixbuf.Pixbuf.calculate_rowstride(0, True, 8, 32, 32)
_pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(_bytes, 0, True, 8, 32, 32, _stride)
_image = Gtk.Image.new_from_pixbuf(_pixbuf)
self.mainwin.set_child(_image)
self.mainwin.set_default_size(400, 300)
self.mainwin.set_visible(True)
app = Gtk.Application()
app.connect('activate', Main)
app.run(None)
But this looks very complicated though, especially the need for creating a gdksurface just to produce a gskrenderer. And so many steps (snapshot-rendernode-texture-tiff-pixbuf) involved. Maybe this could be done in a simpler way? Something like gtksnapshot.to_texture would be nice but couldn’t find it. Or a method in gtkiconpaintable that would return a gdktexture directly (in gtk3 gtkiconinfo.load_symbolic returns a gdkpixbuf).
And Firefox does not seem to even use gdkpixbuf as it just directly converts it into a “bytebuf” with the MozGdkPixbufToByteBuf function. Would it not be possible (easily :)) to port that into taking a gdktexture instead?
Anyways, I put this example out there as I could not find anything similar.