175
by Jason Conti
Ported Notification.py to gobject introspection, everything except dbus. Going to attempt that next, but may revert if it doesn't work so well yet. |
1 |
"""
|
2 |
Icon.py
|
|
3 |
by Jason Conti
|
|
4 |
February 22, 2010
|
|
5 |
updated: March 26, 2011
|
|
6 |
||
7 |
Utility functions for working with icons.
|
|
8 |
"""
|
|
9 |
||
10 |
import gc |
|
11 |
from gi.repository import GdkPixbuf, Gtk |
|
12 |
import logging |
|
13 |
||
14 |
icon_cache = {} |
|
15 |
logger = logging.getLogger("Icon") |
|
16 |
||
17 |
def clear_cache(): |
|
18 |
"""Clear the icon cache, garbage collecting all the icons."""
|
|
19 |
global icon_cache |
|
20 |
for key, icon in icon_cache: |
|
21 |
del icon |
|
22 |
icon_cache = {} |
|
23 |
gc.collect() |
|
24 |
||
25 |
def load(name, size = 48): |
|
26 |
"""Loads an icon from the default icon theme."""
|
|
27 |
global icon_cache |
|
28 |
if (name, size) in icon_cache: |
|
29 |
return icon_cache[(name, size)] |
|
30 |
else: |
|
31 |
try: |
|
32 |
theme = Gtk.IconTheme.get_default() |
|
33 |
icon = theme.load_icon(name, size, Gtk.IconLookupFlags.FORCE_SVG) |
|
34 |
icon_cache[(name, size)] = icon |
|
35 |
return icon |
|
36 |
except: |
|
37 |
logger.exception("failed to load_icon") |
|
38 |
return None |
|
39 |
||
40 |
def load_from_file(path, size = 48): |
|
41 |
"""Loads an icon from the given path."""
|
|
42 |
global icon_cache |
|
43 |
if (path, size) in icon_cache: |
|
44 |
return icon_cache[(path, size)] |
|
45 |
else: |
|
46 |
try: |
|
47 |
icon = GdkPixbuf.Pixbuf.new_from_file_at_size(path, size, size) |
|
48 |
icon_cache[(path, size)] = icon |
|
49 |
return icon |
|
50 |
except: |
|
51 |
logger.exception("failed to load_icon_from_file") |
|
52 |
return None |
|
53 |