From d287e762c66499f426e7ae3f751e22693834c925 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Sun, 17 Aug 2025 10:14:00 -0400 Subject: [PATCH] feat: Add caching for icons and pixbufs (#1609) --- ks_includes/KlippyGtk.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ks_includes/KlippyGtk.py b/ks_includes/KlippyGtk.py index f6e09f0c..6ca7b92e 100644 --- a/ks_includes/KlippyGtk.py +++ b/ks_includes/KlippyGtk.py @@ -2,6 +2,7 @@ import logging import os import pathlib +from functools import lru_cache import gi @@ -126,15 +127,21 @@ class KlippyGtk: def PixbufFromIcon(self, filename, width=None, height=None): width = width if width is not None else self.img_width height = height if height is not None else self.img_height - filename = os.path.join(self.themedir, filename) + return self._PixbufFromIcon(filename, self.themedir, int(width), int(height)) + + @staticmethod + @lru_cache(maxsize=500) + def _PixbufFromIcon(filename, themedir, width, height): + filename = os.path.join(themedir, filename) for ext in ["svg", "png"]: file = f"{filename}.{ext}" - pixbuf = self.PixbufFromFile(file, int(width), int(height)) if os.path.exists(file) else None + pixbuf = KlippyGtk.PixbufFromFile(file, width, height) if os.path.exists(file) else None if pixbuf is not None: return pixbuf return None @staticmethod + @lru_cache(maxsize=500) def PixbufFromFile(filename, width=-1, height=-1): try: return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, int(width), int(height))