Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Silly icon scaling experiment
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import cairo
class IconWindow(Gtk.Window):
def __init__(self, *args, **kwargs):
super().__init__(default_width=150, default_height=150, **kwargs)
self.icon_name = "thunderbird"
self._image = Gtk.Image()
self.grid = Gtk.Grid()
self.add(self.grid)
self.grid.add(self._image)
self._icon_theme = Gtk.IconTheme.get_default()
self.connect("style-set", self._on_style_set)
self._set_icon()
self.connect("delete-event", Gtk.main_quit)
@property
def device_scale(self):
scale = self.get_scale_factor()
return (scale, scale)
def _get_icon_surface(self, icon_name, size=48):
icon_info = self._icon_theme.lookup_icon_for_scale(
icon_name, size, self.get_scale_factor(),
Gtk.IconLookupFlags.FORCE_SIZE)
surface = icon_info.load_surface(self.get_window())
return surface
def _set_icon(self):
surface = self._get_icon_surface(self.icon_name)
x_scale, y_scale = surface.get_device_scale()
context = cairo.Context(surface)
mini_one = self._get_icon_surface(self.icon_name, 16)
#mini_one.set_device_scale(x_scale, y_scale)
context.set_source_surface(mini_one, 1 / x_scale, 1 / y_scale)
context.paint()
mini_two = self._get_icon_surface(self.icon_name, 16)
#mini_two.set_device_scale(x_scale, y_scale)
# Paint one pixel from the bottom
height = surface.get_height()
mini_height = mini_two.get_height()
y = height / y_scale - mini_height / y_scale - 1
#y = height / y_scale - mini_height / y_scale - 1 / y_scale
print("height: %s / mini height: %s / y: %s" % (height, mini_height, y))
# Paint one pixel from the right
width = surface.get_width()
mini_width = mini_two.get_width()
x = width / x_scale - mini_height / x_scale - 1 / x_scale
print("width: %s / mini width: %s / x: %s" % (width, mini_width, x))
context.set_source_surface(mini_two, x, y)
context.paint()
self._image.set_from_surface(surface)
#pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height)
#self._image.set_from_pixbuf(pixbuf)
def _on_style_set(self, *args, **kwargs):
print(*args, **kwargs)
my_win = IconWindow()
my_win.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.