gtk: Gracefully fail if no image is found for buttons. Addresses #173

This commit is contained in:
Jordan 2021-06-06 21:43:06 -04:00
parent e49c19e2d9
commit 58e3e17310
3 changed files with 29 additions and 9 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import gi
import logging
import os
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf, Gio, GLib, Pango
@ -134,17 +135,22 @@ class KlippyGtk:
def ButtonImage(self, image_name, label=None, style=None, width_scale=1, height_scale=1,
position=Gtk.PositionType.TOP, word_wrap=True):
filename = "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, str(image_name))
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
filename,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True
)
img = Gtk.Image.new_from_pixbuf(pixbuf)
if not os.path.exists(filename):
logging.error("Unable to find button image (theme, image): (%s, %s)" % (self.theme, str(image_name)))
filename = "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, "warning")
b = Gtk.Button(label=label)
b.set_image(img)
if os.path.exists(filename):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
filename,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True
)
img = Gtk.Image.new_from_pixbuf(pixbuf)
b.set_image(img)
b.set_hexpand(True)
b.set_vexpand(True)
b.set_can_focus(False)

View File

@ -0,0 +1,7 @@
<svg id="Layer_1" enable-background="new 0 0 510.045 510.045" height="512" viewBox="0 0 510.045 510.045" width="512" xmlns="http://www.w3.org/2000/svg" style="path: #ffffff; fill: #ffffff;">
<g>
<path d="m501.499 374.087c-191.626-320.923-182.524-305.679-182.87-306.258-12.801-23.802-36.55-37.999-63.608-37.999s-50.808 14.197-63.608 37.999c-.339.568 9.121-15.275-182.87 306.257-25.553 48.1 9.289 106.127 63.794 106.127h365.369c54.465.001 89.365-57.992 63.793-106.126zm-63.793 76.127h-365.37c-31.721 0-52.104-33.646-37.463-61.744.335-.56-9.067 15.185 182.848-306.219 15.906-29.943 58.7-29.929 74.6 0 191.627 320.922 182.501 305.639 182.847 306.219 14.653 28.119-5.766 61.744-37.462 61.744z"/>
<path d="m240.021 150.214h30v195h-30z"/>
<path d="m240.021 375.214h30v30h-30z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 796 B

View File

@ -0,0 +1,7 @@
<svg id="Layer_1" enable-background="new 0 0 510.045 510.045" height="512" viewBox="0 0 510.045 510.045" width="512" xmlns="http://www.w3.org/2000/svg" style="path: #ffffff; fill: #ffffff;">
<g>
<path d="m501.499 374.087c-191.626-320.923-182.524-305.679-182.87-306.258-12.801-23.802-36.55-37.999-63.608-37.999s-50.808 14.197-63.608 37.999c-.339.568 9.121-15.275-182.87 306.257-25.553 48.1 9.289 106.127 63.794 106.127h365.369c54.465.001 89.365-57.992 63.793-106.126zm-63.793 76.127h-365.37c-31.721 0-52.104-33.646-37.463-61.744.335-.56-9.067 15.185 182.848-306.219 15.906-29.943 58.7-29.929 74.6 0 191.627 320.922 182.501 305.639 182.847 306.219 14.653 28.119-5.766 61.744-37.462 61.744z"/>
<path d="m240.021 150.214h30v195h-30z"/>
<path d="m240.021 375.214h30v30h-30z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 796 B