[GTK] Refactor out get_pixbuf_at_size

The functionality of get_pixbuf and get_pixbuf_at_size is almost
identical so reuse get_pixbuf with an optional size arg.
This commit is contained in:
Calum Lind 2022-02-09 19:00:52 +00:00
parent e87236514d
commit 24a3987c3a
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
4 changed files with 29 additions and 21 deletions

View File

@ -59,12 +59,31 @@ def create_blank_pixbuf(size=16):
return pix
def get_pixbuf(filename):
def get_pixbuf(filename: str, size: int = 0) -> Pixbuf:
"""Creates a new pixbuf by loading an image from file
Args:
filename: An image file to load
size: Specify a size constraint (equal aspect ratio)
Returns:
A newly created pixbuf
"""
if not os.path.isabs(filename):
filename = get_pixmap(filename)
pixbuf = None
try:
return Pixbuf.new_from_file(get_pixmap(filename))
if size:
pixbuf = Pixbuf.new_from_file_at_size(filename, size, size)
else:
pixbuf = Pixbuf.new_from_file(filename)
except GError as ex:
# Failed to load the pixbuf (Bad image file), so return a blank pixbuf.
log.warning(ex)
return create_blank_pixbuf()
return pixbuf or create_blank_pixbuf(size or 16)
# Status icons.. Create them from file only once to avoid constantly re-creating them.
@ -76,17 +95,6 @@ icon_queued = get_pixbuf('queued16.png')
icon_checking = get_pixbuf('checking16.png')
def get_pixbuf_at_size(filename, size):
if not os.path.isabs(filename):
filename = get_pixmap(filename)
try:
return Pixbuf.new_from_file_at_size(filename, size, size)
except GError as ex:
# Failed to load the pixbuf (Bad image file), so return a blank pixbuf.
log.warning(ex)
return create_blank_pixbuf(size)
def get_logo(size):
"""A Deluge logo.
@ -99,7 +107,7 @@ def get_logo(size):
filename = 'deluge.svg'
if windows_check():
filename = 'deluge.png'
return get_pixbuf_at_size(filename, size)
return get_pixbuf(filename, size)
def build_menu_radio_list(

View File

@ -14,7 +14,7 @@ from twisted.internet import defer
import deluge.component as component
from deluge.common import windows_check
from .common import get_deluge_icon, get_pixbuf_at_size
from .common import get_deluge_icon, get_pixbuf
class BaseDialog(Gtk.Dialog):
@ -52,7 +52,7 @@ class BaseDialog(Gtk.Dialog):
# Hack for Windows since it doesn't support svg
if icon.endswith('.svg') and windows_check():
icon = icon.rpartition('.svg')[0] + '16.png'
image.set_from_pixbuf(get_pixbuf_at_size(icon, 24))
image.set_from_pixbuf(get_pixbuf(icon, 24))
else:
image.set_from_icon_name(icon, Gtk.IconSize.LARGE_TOOLBAR)
image.set_alignment(0.5, 0.0)

View File

@ -21,7 +21,7 @@ from deluge.common import TORRENT_STATE, decode_bytes, resource_filename
from deluge.configmanager import ConfigManager
from deluge.ui.client import client
from .common import get_pixbuf, get_pixbuf_at_size
from .common import get_pixbuf
log = logging.getLogger(__name__)
@ -253,7 +253,7 @@ class FilterTreeView(component.Component):
return get_pixbuf('%s16.png' % pix)
def set_row_image(self, cat, value, filename):
pix = get_pixbuf_at_size(filename, 16)
pix = get_pixbuf(filename, size=16)
row = self.filters[(cat, value)]
self.treestore.set_value(row, 4, pix)
return False

View File

@ -14,7 +14,7 @@ import deluge.component as component
from .common import (
create_blank_pixbuf,
get_pixbuf_at_size,
get_pixbuf,
icon_alert,
icon_checking,
icon_downloading,
@ -83,7 +83,7 @@ def set_tracker_icon(tracker_icon, cell):
if tracker_icon:
pixbuf = tracker_icon.get_cached_icon()
if pixbuf is None:
pixbuf = get_pixbuf_at_size(tracker_icon.get_filename(), 16)
pixbuf = get_pixbuf(tracker_icon.get_filename(), 16)
tracker_icon.set_cached_icon(pixbuf)
else:
pixbuf = create_blank_pixbuf()