From df5aa3102f9d0257224ee9423afdfd42f00592ac Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 13:20:28 +0200 Subject: Refactor. Move. --- parrot_zik/indicator/__init__.py | 23 +++++++++++++ parrot_zik/indicator/base.py | 44 ++++++++++++++++++++++++ parrot_zik/indicator/gtk_wrapping.py | 40 ++++++++++++++++++++++ parrot_zik/indicator/linux.py | 44 ++++++++++++++++++++++++ parrot_zik/indicator/mac.py | 66 ++++++++++++++++++++++++++++++++++++ parrot_zik/indicator/windows.py | 43 +++++++++++++++++++++++ 6 files changed, 260 insertions(+) create mode 100644 parrot_zik/indicator/__init__.py create mode 100644 parrot_zik/indicator/base.py create mode 100644 parrot_zik/indicator/gtk_wrapping.py create mode 100644 parrot_zik/indicator/linux.py create mode 100644 parrot_zik/indicator/mac.py create mode 100644 parrot_zik/indicator/windows.py (limited to 'parrot_zik/indicator') diff --git a/parrot_zik/indicator/__init__.py b/parrot_zik/indicator/__init__.py new file mode 100644 index 0000000..aeda305 --- /dev/null +++ b/parrot_zik/indicator/__init__.py @@ -0,0 +1,23 @@ +__all__ = ('SysIndicator', 'Menu', 'MenuItem') + +import sys + +if sys.platform == 'linux2': + import linux + import gtk_wrapping + SysIndicator = linux.LinuxIndicator + Menu = gtk_wrapping.GTKMenu + MenuItem = gtk_wrapping.GTKMenuItem +elif sys.platform == 'win32': + import gtk_wrapping + import windows + SysIndicator = windows.WindowsIndicator + Menu = gtk_wrapping.GTKMenu + MenuItem = gtk_wrapping.GTKMenuItem +elif sys.platform == 'darwin': + import mac + SysIndicator = mac.DarwinIndicator + Menu = mac.NSMenu + MenuItem = mac.NSMenuItem +else: + raise Exception('Platform not supported') diff --git a/parrot_zik/indicator/base.py b/parrot_zik/indicator/base.py new file mode 100644 index 0000000..7101790 --- /dev/null +++ b/parrot_zik/indicator/base.py @@ -0,0 +1,44 @@ +class BaseIndicator(object): + def __init__(self, icon, menu, statusicon): + self.menu = menu + self.statusicon = statusicon + self.setIcon(icon) + + def setIcon(self, name): + raise NotImplementedError + + def main(self): + raise NotImplementedError + + def show_about_dialog(self, widget): + raise NotImplementedError + +class MenuItemBase(object): + def __init__(self, base_item, sensitive, visible): + self.base_item = base_item + self.set_sensitive(sensitive) + if visible: + self.show() + else: + self.hide() + + def set_sensitive(self, option): + raise NotImplementedError + + def set_active(self, option): + raise NotImplementedError + + def get_active(self): + raise NotImplementedError + + def set_label(self, option): + raise NotImplementedError + + def show(self): + self.base_item.show() + + def hide(self): + self.base_item.hide() + + def set_submenu(self, menu): + raise NotImplementedError diff --git a/parrot_zik/indicator/gtk_wrapping.py b/parrot_zik/indicator/gtk_wrapping.py new file mode 100644 index 0000000..9494adc --- /dev/null +++ b/parrot_zik/indicator/gtk_wrapping.py @@ -0,0 +1,40 @@ +import gtk + +from .base import MenuItemBase + + +class GTKMenu(object): + def __init__(self): + self.gtk_menu = gtk.Menu() + + def append(self, menu_item): + self.gtk_menu.append(menu_item.base_item) + + def reposition(self): + self.gtk_menu.reposition() + + +class GTKMenuItem(MenuItemBase): + def __init__(self, name, action, sensitive=True, checkitem=False, visible=True): + if checkitem: + gtk_item = gtk.CheckMenuItem(name) + else: + gtk_item = gtk.MenuItem(name) + if action: + gtk_item.connect("activate", action) + super(GTKMenuItem, self).__init__(gtk_item, sensitive, visible) + + def set_sensitive(self, option): + return self.base_item.set_sensitive(option) + + def set_active(self, option): + return self.base_item.set_active(option) + + def get_active(self): + return self.base_item.get_active() + + def set_label(self, option): + return self.base_item.set_label(option) + + def set_submenu(self, menu): + self.base_item.set_submenu(menu.gtk_menu) diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py new file mode 100644 index 0000000..cbec1dd --- /dev/null +++ b/parrot_zik/indicator/linux.py @@ -0,0 +1,44 @@ +import os + +import gtk + +from .base import BaseIndicator + + +class LinuxIndicator(BaseIndicator): + def __init__(self, icon, menu): + import appindicator + self.icon_directory = os.path.join('/', 'usr', 'share', 'icons', 'zik') + if not os.path.isdir(self.icon_directory): + self.icon_directory = os.path.join('share', 'icons', 'zik') + statusicon = appindicator.Indicator( + "new-parrotzik-indicator", "indicator-messages", + appindicator.CATEGORY_APPLICATION_STATUS) + statusicon.set_status(appindicator.STATUS_ACTIVE) + statusicon.set_icon_theme_path(self.icon_directory) + statusicon.set_menu(menu.gtk_menu) + super(LinuxIndicator, self).__init__(icon, menu, statusicon) + + def gtk_right_click_event(self, icon, button, time): + if not self.menu_shown: + self.menu_shown = True + self.menu.popup(None, None, gtk.status_icon_position_menu, + button, time, self.statusicon) + else: + self.menu_shown = False + self.menu.poVpdown() + + def setIcon(self, name): + self.statusicon.set_icon(name) + + def main(self): + gtk.main() + + def show_about_dialog(self, widget): + about_dialog = gtk.AboutDialog() + about_dialog.set_destroy_with_parent(True) + about_dialog.set_name("Parrot Zik Tray") + about_dialog.set_version("0.3") + about_dialog.set_authors(["Dmitry Moiseev m0sia@m0sia.ru"]) + about_dialog.run() + about_dialog.destroy() diff --git a/parrot_zik/indicator/mac.py b/parrot_zik/indicator/mac.py new file mode 100644 index 0000000..8fdd1e6 --- /dev/null +++ b/parrot_zik/indicator/mac.py @@ -0,0 +1,66 @@ +import os +import sys + +from Foundation import * +from AppKit import * +from PyObjCTools import AppHelper + +from .base import BaseIndicator +from .base import MenuItemBase +from ..status_app_mac import StatusApp + + +class DarwinIndicator(BaseIndicator): + def __init__(self, icon, menu): + self.icon_directory = os.path.join( + os.path.dirname(os.path.realpath(sys.argv[0])), 'share', 'icons', 'zik') + statusicon = StatusApp.sharedApplication() + statusicon.initMenu(menu) + super(DarwinIndicator, self).__init__(icon, menu, statusicon) + + def setIcon(self, name): + self.statusicon.setIcon(name, self.icon_directory) + + def main(self): + AppHelper.runEventLoop() + + def show_about_dialog(self, widget): + pass + + +class NSMenu(object): + def __init__(self): + self.actions = {} + self.menubarMenu = NSMenu.alloc().init() + self.menubarMenu.setAutoenablesItems_(False) + + def append(self, menu_item): + self.actions[menu_item.title] = menu_item.action + self.menubarMenu.addItem_(menu_item.nsmenu_item) + + def reposition(self): + # TODO + pass + + +class NSMenuItem(MenuItemBase): + def __init__(self, name, action, sensitive=True, checkitem=False, visible=True): + self.title = name + self.action = action + nsmenu_item = ( + NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( + name, 'clicked:', '')) + super(NSMenuItem, self).__init__(nsmenu_item, sensitive, visible) + + def set_sensitive(self, option): + self.base_item.setEnabled_(option) + + def set_active(self, option): + self.base_item.setState_(option) + + def get_active(self): + return self.base_item.state + + def set_label(self, option): + self.title = option + self.base_item.setTitle_(option) diff --git a/parrot_zik/indicator/windows.py b/parrot_zik/indicator/windows.py new file mode 100644 index 0000000..0f81e55 --- /dev/null +++ b/parrot_zik/indicator/windows.py @@ -0,0 +1,43 @@ +import sys +import tempfile +import gtk +import os + +from .base import BaseIndicator + + +class WindowsIndicator(BaseIndicator): + def __init__(self, icon, menu): + self.icon_directory = os.path.join( + os.path.dirname(os.path.realpath(sys.argv[0])), 'share', 'icons', 'zik') + self.menu_shown = False + sys.stdout = open(os.path.join(tempfile.gettempdir(), "zik_tray_stdout.log", "w")) + sys.stderr = open(os.path.join(tempfile.gettempdir(), "zik_tray_stderr.log", "w")) + statusicon = gtk.StatusIcon() + statusicon.connect("popup-menu", self.gtk_right_click_event) + statusicon.set_tooltip("Parrot Zik") + super(WindowsIndicator, self).__init__(icon, menu, statusicon) + + def gtk_right_click_event(self, icon, button, time): + if not self.menu_shown: + self.menu_shown = True + self.menu.popup(None, None, gtk.status_icon_position_menu, + button, time, self.statusicon) + else: + self.menu_shown = False + self.menu.poVpdown() + + def setIcon(self, name): + self.statusicon.set_from_file(self.icon_directory + name + '.png') + + def main(self): + gtk.main() + + def show_about_dialog(self, widget): + about_dialog = gtk.AboutDialog() + about_dialog.set_destroy_with_parent(True) + about_dialog.set_name("Parrot Zik Tray") + about_dialog.set_version("0.3") + about_dialog.set_authors(["Dmitry Moiseev m0sia@m0sia.ru"]) + about_dialog.run() + about_dialog.destroy() -- cgit v1.2.1 From 3b4f022722e43d5821f4d2276018242c88cb1ed3 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 13:30:06 +0200 Subject: Fix quit wrapping. --- parrot_zik/indicator/base.py | 3 +++ parrot_zik/indicator/linux.py | 3 +++ parrot_zik/indicator/mac.py | 3 +++ parrot_zik/indicator/windows.py | 3 +++ 4 files changed, 12 insertions(+) (limited to 'parrot_zik/indicator') diff --git a/parrot_zik/indicator/base.py b/parrot_zik/indicator/base.py index 7101790..8204fa9 100644 --- a/parrot_zik/indicator/base.py +++ b/parrot_zik/indicator/base.py @@ -13,6 +13,9 @@ class BaseIndicator(object): def show_about_dialog(self, widget): raise NotImplementedError + def quit(self, _): + raise NotImplementedError + class MenuItemBase(object): def __init__(self, base_item, sensitive, visible): self.base_item = base_item diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py index cbec1dd..ff06587 100644 --- a/parrot_zik/indicator/linux.py +++ b/parrot_zik/indicator/linux.py @@ -34,6 +34,9 @@ class LinuxIndicator(BaseIndicator): def main(self): gtk.main() + def quit(self, _): + gtk.main_quit() + def show_about_dialog(self, widget): about_dialog = gtk.AboutDialog() about_dialog.set_destroy_with_parent(True) diff --git a/parrot_zik/indicator/mac.py b/parrot_zik/indicator/mac.py index 8fdd1e6..d28ee26 100644 --- a/parrot_zik/indicator/mac.py +++ b/parrot_zik/indicator/mac.py @@ -27,6 +27,9 @@ class DarwinIndicator(BaseIndicator): def show_about_dialog(self, widget): pass + def quit(self, _): + pass + class NSMenu(object): def __init__(self): diff --git a/parrot_zik/indicator/windows.py b/parrot_zik/indicator/windows.py index 0f81e55..118f620 100644 --- a/parrot_zik/indicator/windows.py +++ b/parrot_zik/indicator/windows.py @@ -33,6 +33,9 @@ class WindowsIndicator(BaseIndicator): def main(self): gtk.main() + def quit(self, _): + gtk.main_quit() + def show_about_dialog(self, widget): about_dialog = gtk.AboutDialog() about_dialog.set_destroy_with_parent(True) -- cgit v1.2.1 From f291feaa8a33645850818f811ba8d7f9eb9d4c31 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 13:41:06 +0200 Subject: Refactor. --- parrot_zik/indicator/base.py | 3 ++- parrot_zik/indicator/linux.py | 3 ++- parrot_zik/indicator/mac.py | 3 ++- parrot_zik/indicator/windows.py | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) (limited to 'parrot_zik/indicator') diff --git a/parrot_zik/indicator/base.py b/parrot_zik/indicator/base.py index 8204fa9..b29368e 100644 --- a/parrot_zik/indicator/base.py +++ b/parrot_zik/indicator/base.py @@ -7,7 +7,8 @@ class BaseIndicator(object): def setIcon(self, name): raise NotImplementedError - def main(self): + @classmethod + def main(cls): raise NotImplementedError def show_about_dialog(self, widget): diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py index ff06587..fc0f425 100644 --- a/parrot_zik/indicator/linux.py +++ b/parrot_zik/indicator/linux.py @@ -31,7 +31,8 @@ class LinuxIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.set_icon(name) - def main(self): + @classmethod + def main(cls): gtk.main() def quit(self, _): diff --git a/parrot_zik/indicator/mac.py b/parrot_zik/indicator/mac.py index d28ee26..232e010 100644 --- a/parrot_zik/indicator/mac.py +++ b/parrot_zik/indicator/mac.py @@ -21,7 +21,8 @@ class DarwinIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.setIcon(name, self.icon_directory) - def main(self): + @classmethod + def main(cls): AppHelper.runEventLoop() def show_about_dialog(self, widget): diff --git a/parrot_zik/indicator/windows.py b/parrot_zik/indicator/windows.py index 118f620..79a4ef3 100644 --- a/parrot_zik/indicator/windows.py +++ b/parrot_zik/indicator/windows.py @@ -30,7 +30,8 @@ class WindowsIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.set_from_file(self.icon_directory + name + '.png') - def main(self): + @classmethod + def main(cls): gtk.main() def quit(self, _): -- cgit v1.2.1 From e7089b04e59ae1cf7a24649ec2ef64af56eb7dce Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 17:46:22 +0200 Subject: Use absolute package imports. --- parrot_zik/indicator/__init__.py | 25 ++++++++++--------------- parrot_zik/indicator/gtk_wrapping.py | 2 +- parrot_zik/indicator/linux.py | 2 +- parrot_zik/indicator/mac.py | 6 +++--- parrot_zik/indicator/windows.py | 2 +- 5 files changed, 16 insertions(+), 21 deletions(-) (limited to 'parrot_zik/indicator') diff --git a/parrot_zik/indicator/__init__.py b/parrot_zik/indicator/__init__.py index aeda305..4194dde 100644 --- a/parrot_zik/indicator/__init__.py +++ b/parrot_zik/indicator/__init__.py @@ -2,22 +2,17 @@ __all__ = ('SysIndicator', 'Menu', 'MenuItem') import sys -if sys.platform == 'linux2': - import linux - import gtk_wrapping - SysIndicator = linux.LinuxIndicator - Menu = gtk_wrapping.GTKMenu - MenuItem = gtk_wrapping.GTKMenuItem +if sys.platform in ['linux', 'linux2']: + from parrot_zik.indicator.linux import LinuxIndicator as SysIndicator + from parrot_zik.indicator.gtk_wrapping import GTKMenuItem as MenuItem + from parrot_zik.indicator.gtk_wrapping import GTKMenu as Menu elif sys.platform == 'win32': - import gtk_wrapping - import windows - SysIndicator = windows.WindowsIndicator - Menu = gtk_wrapping.GTKMenu - MenuItem = gtk_wrapping.GTKMenuItem + from parrot_zik.indicator.windows import WindowsIndicator as SysIndicator + from parrot_zik.indicator.gtk_wrapping import GTKMenuItem as MenuItem + from parrot_zik.indicator.gtk_wrapping import GTKMenu as Menu elif sys.platform == 'darwin': - import mac - SysIndicator = mac.DarwinIndicator - Menu = mac.NSMenu - MenuItem = mac.NSMenuItem + from parrot_zik.indicator.mac import DarwinIndicator as SysIndicator + from parrot_zik.indicator.mac import NSMenuItem as MenuItem + from parrot_zik.indicator.mac import NSMenu as Menu else: raise Exception('Platform not supported') diff --git a/parrot_zik/indicator/gtk_wrapping.py b/parrot_zik/indicator/gtk_wrapping.py index 9494adc..ce78c1d 100644 --- a/parrot_zik/indicator/gtk_wrapping.py +++ b/parrot_zik/indicator/gtk_wrapping.py @@ -1,6 +1,6 @@ import gtk -from .base import MenuItemBase +from parrot_zik.indicator.base import MenuItemBase class GTKMenu(object): diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py index fc0f425..278f781 100644 --- a/parrot_zik/indicator/linux.py +++ b/parrot_zik/indicator/linux.py @@ -2,7 +2,7 @@ import os import gtk -from .base import BaseIndicator +from parrot_zik.indicator.base import BaseIndicator class LinuxIndicator(BaseIndicator): diff --git a/parrot_zik/indicator/mac.py b/parrot_zik/indicator/mac.py index 232e010..ceeacb0 100644 --- a/parrot_zik/indicator/mac.py +++ b/parrot_zik/indicator/mac.py @@ -5,9 +5,9 @@ from Foundation import * from AppKit import * from PyObjCTools import AppHelper -from .base import BaseIndicator -from .base import MenuItemBase -from ..status_app_mac import StatusApp +from parrot_zik.indicator.base import BaseIndicator +from parrot_zik.indicator.base import MenuItemBase +from parrot_zik.status_app_mac import StatusApp class DarwinIndicator(BaseIndicator): diff --git a/parrot_zik/indicator/windows.py b/parrot_zik/indicator/windows.py index 79a4ef3..f10c91a 100644 --- a/parrot_zik/indicator/windows.py +++ b/parrot_zik/indicator/windows.py @@ -3,7 +3,7 @@ import tempfile import gtk import os -from .base import BaseIndicator +from parrot_zik.indicator.base import BaseIndicator class WindowsIndicator(BaseIndicator): -- cgit v1.2.1 From 4098b681ca9bc4b57e8283f6f21d139fc1c9940b Mon Sep 17 00:00:00 2001 From: Max Grishkin Date: Tue, 8 Dec 2015 23:44:28 +0300 Subject: Use gtk on linux --- parrot_zik/indicator/__init__.py | 11 ++++++-- parrot_zik/indicator/gtk_wrapping.py | 6 +++++ parrot_zik/indicator/linux.py | 49 +++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 16 deletions(-) (limited to 'parrot_zik/indicator') diff --git a/parrot_zik/indicator/__init__.py b/parrot_zik/indicator/__init__.py index 4194dde..156f7de 100644 --- a/parrot_zik/indicator/__init__.py +++ b/parrot_zik/indicator/__init__.py @@ -3,10 +3,17 @@ __all__ = ('SysIndicator', 'Menu', 'MenuItem') import sys if sys.platform in ['linux', 'linux2']: - from parrot_zik.indicator.linux import LinuxIndicator as SysIndicator + import argparse + parser = argparse.ArgumentParser() + parser.add_argument("--gtk", action="store_true") + args = parser.parse_args() + if args.gtk: + from parrot_zik.indicator.linux import LinuxGtkIndicator as SysIndicator + else: + from parrot_zik.indicator.linux import LinuxAppIndicator as SysIndicator from parrot_zik.indicator.gtk_wrapping import GTKMenuItem as MenuItem from parrot_zik.indicator.gtk_wrapping import GTKMenu as Menu -elif sys.platform == 'win32': +elif sys.platform in ['win32']: from parrot_zik.indicator.windows import WindowsIndicator as SysIndicator from parrot_zik.indicator.gtk_wrapping import GTKMenuItem as MenuItem from parrot_zik.indicator.gtk_wrapping import GTKMenu as Menu diff --git a/parrot_zik/indicator/gtk_wrapping.py b/parrot_zik/indicator/gtk_wrapping.py index ce78c1d..6d4a5a8 100644 --- a/parrot_zik/indicator/gtk_wrapping.py +++ b/parrot_zik/indicator/gtk_wrapping.py @@ -13,6 +13,12 @@ class GTKMenu(object): def reposition(self): self.gtk_menu.reposition() + def popup(self, *args, **kwargs): + self.gtk_menu.popup(*args, **kwargs) + + def poVpdown(self, *args, **kwargs): + pass + class GTKMenuItem(MenuItemBase): def __init__(self, name, action, sensitive=True, checkitem=False, visible=True): diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py index 278f781..f6f9cfc 100644 --- a/parrot_zik/indicator/linux.py +++ b/parrot_zik/indicator/linux.py @@ -6,17 +6,7 @@ from parrot_zik.indicator.base import BaseIndicator class LinuxIndicator(BaseIndicator): - def __init__(self, icon, menu): - import appindicator - self.icon_directory = os.path.join('/', 'usr', 'share', 'icons', 'zik') - if not os.path.isdir(self.icon_directory): - self.icon_directory = os.path.join('share', 'icons', 'zik') - statusicon = appindicator.Indicator( - "new-parrotzik-indicator", "indicator-messages", - appindicator.CATEGORY_APPLICATION_STATUS) - statusicon.set_status(appindicator.STATUS_ACTIVE) - statusicon.set_icon_theme_path(self.icon_directory) - statusicon.set_menu(menu.gtk_menu) + def __init__(self, icon, menu, statusicon): super(LinuxIndicator, self).__init__(icon, menu, statusicon) def gtk_right_click_event(self, icon, button, time): @@ -28,9 +18,6 @@ class LinuxIndicator(BaseIndicator): self.menu_shown = False self.menu.poVpdown() - def setIcon(self, name): - self.statusicon.set_icon(name) - @classmethod def main(cls): gtk.main() @@ -46,3 +33,37 @@ class LinuxIndicator(BaseIndicator): about_dialog.set_authors(["Dmitry Moiseev m0sia@m0sia.ru"]) about_dialog.run() about_dialog.destroy() + + +class LinuxAppIndicator(LinuxIndicator): + def __init__(self, icon, menu): + import appindicator + self.icon_directory = os.path.join('/', 'usr', 'share', 'icons', 'zik') + if not os.path.isdir(self.icon_directory): + self.icon_directory = os.path.join('share', 'icons', 'zik') + statusicon = appindicator.Indicator( + "new-parrotzik-indicator", "indicator-messages", + appindicator.CATEGORY_APPLICATION_STATUS) + statusicon.set_status(appindicator.STATUS_ACTIVE) + statusicon.set_icon_theme_path(self.icon_directory) + statusicon.set_menu(menu.gtk_menu) + super(LinuxIndicator, self).__init__(icon, menu, statusicon) + + def setIcon(self, name): + self.statusicon.set_icon(name) + + + +class LinuxGtkIndicator(LinuxIndicator): + def __init__(self, icon, menu): + self.icon_directory = os.path.join( + '/usr', 'share', 'icons/') + self.menu_shown = False + statusicon = gtk.StatusIcon() + statusicon.connect("popup-menu", self.gtk_right_click_event) + statusicon.set_tooltip("Parrot Zik") + super(LinuxIndicator, self).__init__(icon, menu, statusicon) + + def setIcon(self, name): + self.statusicon.set_from_file(self.icon_directory + name + '.png') + -- cgit v1.2.1