Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Basic FreeDesktop icon support #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/pystray/_appindicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, *args, **kwargs):
super(Icon, self).__init__(*args, **kwargs)

self._appindicator = None
self._uses_freedesktop_icon_name = True

if self.icon:
self._update_icon()
Expand All @@ -47,7 +48,7 @@ def _show(self):
AppIndicator.IndicatorCategory.APPLICATION_STATUS)

self._appindicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
self._appindicator.set_icon(self._icon_path)
self._appindicator.set_icon(self._freedesktop_icon_name or self._icon_path)
self._appindicator.set_menu(
self._menu_handle or self._create_default_menu())

Expand All @@ -60,7 +61,7 @@ def _update_icon(self):
self._remove_fs_icon()
self._update_fs_icon()
if self._appindicator:
self._appindicator.set_icon(self._icon_path)
self._appindicator.set_icon(self._freedesktop_icon_name or self._icon_path)

@mainloop
def _update_title(self):
Expand Down
29 changes: 28 additions & 1 deletion lib/pystray/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Icon(object):
:param icon: The icon to use. If this is specified, it must be a
:class:`PIL.Image.Image` instance.

:param str freedesktop_icon_name: An available icon_name as specified by
the FreeDesktop Icon Theme specification, used with compatible modules.

:param str title: A short title for the icon.

:param menu: A menu to use as popup menu. This can be either an instance of
Expand Down Expand Up @@ -67,13 +70,16 @@ class Icon(object):
HAS_NOTIFICATION = True

def __init__(
self, name, icon=None, title=None, menu=None):
self, name, icon=None, freedesktop_icon_name=None,
title=None, menu=None):
self._name = name
self._icon = icon or None
self._freedesktop_icon_name = freedesktop_icon_name or None
self._title = title or ''
self._menu = menu
self._visible = False
self._icon_valid = False
self._uses_freedesktop_icon_name = False
self._log = logging.getLogger(__name__)

self._running = False
Expand Down Expand Up @@ -114,6 +120,27 @@ def icon(self, value):
if self.visible:
self.visible = False

@property
def freedesktop_icon_name(self):
"""The current FreeDesktop icon_name.

Setting this to a falsy value will fall back to `icon` or hide the icon
if no `icon` is present. Setting this to an icon_name while the icon
is hidden has no effect until the icon is shown.
"""
return self._freedesktop_icon_name

@freedesktop_icon_name.setter
def freedesktop_icon_name(self, value):
self._freedesktop_icon_name = value
self._icon_valid = False
if value:
if self.visible and self._uses_freedesktop_icon_name:
self._update_icon()
else:
if self.visible:
self.visible = False

@property
def title(self):
"""The current icon title.
Expand Down
5 changes: 4 additions & 1 deletion lib/pystray/_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def _hide(self):
def _update_icon(self):
self._remove_fs_icon()
self._update_fs_icon()
self._status_icon.set_from_file(self._icon_path)
if self._freedesktop_icon_name:
self._status_icon.set_from_icon_name(self.freedesktop_icon_name)
else:
self._status_icon.set_from_file(self._icon_path)

@mainloop
def _update_menu(self):
Expand Down