Browse Source

Restrict maximum icon size with --max-icon-size

By using the --max-icon-size option any icons that are larger than the
requested icon size get discarded. This is useful for at least Fvwm,
where the menus can hold icons of any size, with no option to resize to
a maximum size, making for some funny looking menus.

This adds a new dependency on the Pillow library, which is only loaded
if the --max-icon-size option is used.
pull/6/head
George Vlahavas 8 years ago
parent
commit
ab2808fb79
  1. 38
      src/xdgmenumaker

38
src/xdgmenumaker

@ -35,6 +35,7 @@ iconsize = 16
desktop = False desktop = False
submenu = True submenu = True
pekwmdynamic = False pekwmdynamic = False
max_icon_size = False
# the following line gets changed by the Makefile. If it is set to # the following line gets changed by the Makefile. If it is set to
# 'not_set' it looks in the currect directory tree for the .directory # 'not_set' it looks in the currect directory tree for the .directory
@ -167,10 +168,12 @@ def main(argv):
global iconsize global iconsize
global submenu global submenu
global pekwmdynamic global pekwmdynamic
global max_icon_size
try: try:
opts, args = getopt.getopt(argv, "hins:f:", ["help", "icons", opts, args = getopt.getopt(argv, "hins:f:", ["help", "icons",
"no-submenu", "no-submenu",
"pekwm-dynamic", "pekwm-dynamic",
"max-icon-size",
"size=", "size=",
"format="]) "format="])
except getopt.GetoptError: except getopt.GetoptError:
@ -192,6 +195,20 @@ def main(argv):
submenu = False submenu = False
elif opt in ("--pekwm-dynamic",): elif opt in ("--pekwm-dynamic",):
pekwmdynamic = True pekwmdynamic = True
elif opt in ("--max-icon-size",):
try:
# Pillow is optional and loaded only if we want to restrict the
# icon sizes (useful for Fvwm). Yeah, I know it's not a good
# idea to load a module in here, but I really don't want to
# load it by default at the top. It would make xdgmenumaker a
# bit slower to run even if it is not needed. This way it only
# slows down when it is actually needed.
global Image
from PIL import Image
max_icon_size = True
except ImportError:
usage()
sys.exit('ERROR: --max-icon-size requires Pillow')
elif opt in ("-f", "--format"): elif opt in ("-f", "--format"):
desktop = arg desktop = arg
if not desktop: if not desktop:
@ -251,22 +268,39 @@ def icon_strip(icon):
icon = icon.replace('.xpm', '') icon = icon.replace('.xpm', '')
return icon return icon
def icon_max_size(icon):
# Checks if the icon size is bigger than the requested size and discards
# the icon if it is, only allowing sizes smaller or equal to the requested
# size
try:
img = Image.open(icon)
except:
# if there is any error reading the icon, just discard it
return None
if img.size[0] <= iconsize:
return icon
else:
return None
def icon_full_path(icon): def icon_full_path(icon):
# If the icon path is absolute and exists, leave it alone. # If the icon path is absolute and exists, leave it alone.
# This takes care of software that has its own icons stored # This takes care of software that has its own icons stored
# in non-standard directories # in non-standard directories
if os.path.exists(icon): if os.path.exists(icon):
return icon if max_icon_size:
return icon_max_size(icon)
else:
return None
else: else:
icon = icon_strip(icon) icon = icon_strip(icon)
icon_theme = gtk.icon_theme_get_default() icon_theme = gtk.icon_theme_get_default()
icon = icon_theme.lookup_icon(icon, iconsize, gtk.ICON_LOOKUP_NO_SVG) icon = icon_theme.lookup_icon(icon, iconsize, gtk.ICON_LOOKUP_NO_SVG)
if icon: if icon:
icon = icon.get_filename() icon = icon.get_filename()
if max_icon_size:
icon = icon_max_size(icon)
return icon return icon
def get_entry_info(desktopfile, ico_paths=True): def get_entry_info(desktopfile, ico_paths=True):
de = dentry.DesktopEntry(filename=desktopfile) de = dentry.DesktopEntry(filename=desktopfile)

Loading…
Cancel
Save