Split get_pixbuf_by_path() in two also

get_pixbuf_by_path() will return None for missing images but get_required_pixbuf_by_path() will throw MissingMediaError.
This commit is contained in:
Daniel Johnson 2024-03-01 19:03:00 -05:00
parent cf774dfb08
commit d689747b7e
3 changed files with 34 additions and 22 deletions

View file

@ -524,13 +524,11 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
if key in self.cached_surfaces_old: if key in self.cached_surfaces_old:
surface = self.cached_surfaces_old[key] surface = self.cached_surfaces_old[key]
else: else:
try: surface = self._get_surface_by_path(widget, path, size, preserve_aspect_ratio)
surface = self._get_surface_by_path(widget, path, size, preserve_aspect_ratio) if surface:
# We cache missing surfaces too, but only a successful load trigger # We cache missing surfaces too, but only a successful load trigger
# cache cycling # cache cycling
self.cached_surfaces_loaded += 1 self.cached_surfaces_loaded += 1
except MissingMediaError:
surface = None
self.cached_surfaces_new[key] = surface self.cached_surfaces_new[key] = surface
return surface return surface

View file

@ -5,6 +5,7 @@ from lutris.gui.widgets.utils import (
ICON_SIZE, ICON_SIZE,
get_default_icon_path, get_default_icon_path,
get_pixbuf_by_path, get_pixbuf_by_path,
get_required_pixbuf_by_path,
get_runtime_icon_path, get_runtime_icon_path,
has_stock_icon, has_stock_icon,
) )
@ -29,7 +30,7 @@ class ScaledImage(Gtk.Image):
pass your widget's get_scale_factor() here.""" pass your widget's get_scale_factor() here."""
pixbuf_size = (size[0] * scale_factor, size[1] * scale_factor) if size else None pixbuf_size = (size[0] * scale_factor, size[1] * scale_factor) if size else None
pixbuf = get_pixbuf_by_path(path, pixbuf_size) pixbuf = get_required_pixbuf_by_path(path, pixbuf_size)
image = ScaledImage(1 / scale_factor) image = ScaledImage(1 / scale_factor)
image.set_from_pixbuf(pixbuf) image.set_from_pixbuf(pixbuf)
return image return image
@ -45,11 +46,10 @@ class ScaledImage(Gtk.Image):
pixbuf_size = (size[0] * scale_factor, size[1] * scale_factor) pixbuf_size = (size[0] * scale_factor, size[1] * scale_factor)
try: pixbuf = get_pixbuf_by_path(path, pixbuf_size)
pixbuf = get_pixbuf_by_path(path, pixbuf_size) if not pixbuf:
except MissingMediaError:
default_icon = get_default_icon_path(size) default_icon = get_default_icon_path(size)
pixbuf = get_pixbuf_by_path(default_icon, pixbuf_size, preserve_aspect_ratio=False) pixbuf = get_required_pixbuf_by_path(default_icon, pixbuf_size, preserve_aspect_ratio=False)
image = ScaledImage(1 / scale_factor) image = ScaledImage(1 / scale_factor)
image.set_from_pixbuf(pixbuf) image.set_from_pixbuf(pixbuf)

View file

@ -76,9 +76,12 @@ def get_scaled_surface_by_path(path, size, device_scale, preserve_aspect_ratio=T
If you pass True for preserve_aspect_ratio, the aspect ratio of the image is preserved, If you pass True for preserve_aspect_ratio, the aspect ratio of the image is preserved,
but will be no larger than the size (times the device_scale). but will be no larger than the size (times the device_scale).
If the path cannot be read, this returns None. If there's no file at the path, or it is empty, this function returns None.
""" """
pixbuf = get_pixbuf_by_path(path) pixbuf = get_pixbuf_by_path(path)
if not pixbuf:
return None
pixbuf_width = pixbuf.get_width() pixbuf_width = pixbuf.get_width()
pixbuf_height = pixbuf.get_height() pixbuf_height = pixbuf.get_height()
@ -115,21 +118,32 @@ def get_default_icon_path(size):
def get_pixbuf_by_path(path, size=None, preserve_aspect_ratio=True): def get_pixbuf_by_path(path, size=None, preserve_aspect_ratio=True):
"""Reads an image file and returns the pixbuf. If you provide a size, this scales """Reads an image file and returns the pixbuf. If you provide a size, this scales
the file to fit that size, preserving the aspect ratio if preserve_aspect_ratio is the file to fit that size, preserving the aspect ratio if preserve_aspect_ratio is
True. If the file is missing or unreadable, or if 'path' is None, this raises True. If the file is missing or empty, or if 'path' is None or empty,
MissingMediaError.""" this returns None. Still raises GLib.GError for corrupt files."""
if not system.path_exists(path, exclude_empty=True): if not system.path_exists(path, exclude_empty=True):
raise MissingMediaError(filename=path) return None
if size:
# new_from_file_at_size scales but preserves aspect ratio
width, height = size
if preserve_aspect_ratio:
return GdkPixbuf.Pixbuf.new_from_file_at_size(path, width, height)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, preserve_aspect_ratio=False)
return GdkPixbuf.Pixbuf.new_from_file(path)
def get_required_pixbuf_by_path(path, size=None, preserve_aspect_ratio=True):
"""Reads an image file and returns the pixbuf. If you provide a size, this scales
the file to fit that size, preserving the aspect ratio if preserve_aspect_ratio is
True. If the file is missing or unreadable, or if 'path' is None or empty, this raises
MissingMediaError."""
try: try:
if size: pixbuf = get_pixbuf_by_path(path, size, preserve_aspect_ratio)
# new_from_file_at_size scales but preserves aspect ratio if not pixbuf:
width, height = size raise MissingMediaError(filename=path)
if preserve_aspect_ratio: return pixbuf
return GdkPixbuf.Pixbuf.new_from_file_at_size(path, width, height)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, preserve_aspect_ratio=False)
return GdkPixbuf.Pixbuf.new_from_file(path)
except GLib.GError as ex: except GLib.GError as ex:
logger.exception("Unable to load icon from image %s", path) logger.exception("Unable to load icon from image %s", path)
raise MissingMediaError(message=str(ex), filename=path) from ex raise MissingMediaError(message=str(ex), filename=path) from ex