Skip to content

Commit

Permalink
categorized module menu by location of module
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 1, 2017
1 parent 4078647 commit 70cca98
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
8 changes: 2 additions & 6 deletions aw_qt/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, name: str, testing: bool = False) -> None:
self.name = name
self.started = False # Should be True if module is supposed to be running, else False
self.testing = testing
self.location = "system" if _is_system_module(name) else "bundled"
self._process = None # type: Optional[subprocess.Popen]
self._last_process = None # type: Optional[subprocess.Popen]

Expand Down Expand Up @@ -168,12 +169,7 @@ def __init__(self, testing: bool = False) -> None:

def discover_modules(self):
# These should always be bundled with aw-qt
found_modules = {
"aw-server",
"aw-watcher-afk",
"aw-watcher-window"
}
found_modules |= set(_discover_modules_bundled())
found_modules = set(_discover_modules_bundled())
found_modules |= set(_discover_modules_system())
found_modules ^= {"aw-qt"} # Exclude self

Expand Down
43 changes: 31 additions & 12 deletions aw_qt/trayicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import webbrowser
import os
import subprocess
from collections import defaultdict

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMessageBox, QMenu, QWidget, QPushButton
Expand Down Expand Up @@ -93,12 +94,18 @@ def show_module_failed_dialog(module):
box.show()

def rebuild_modules_menu():
for module in modulesMenu.actions():
name = module.text()
alive = self.manager.modules[name].is_alive()
module.setChecked(alive)
# print(module.text(), alive)
for action in modulesMenu.actions():
if action.isEnabled():
name = action.module.name
alive = self.manager.modules[name].is_alive()
action.setChecked(alive)
# print(module.text(), alive)

# TODO: Do it in a better way, singleShot isn't pretty...
QtCore.QTimer.singleShot(2000, rebuild_modules_menu)
QtCore.QTimer.singleShot(2000, rebuild_modules_menu)

def check_module_status():
unexpected_exits = self.manager.get_unexpected_stops()
if unexpected_exits:
for module in unexpected_exits:
Expand All @@ -107,22 +114,34 @@ def rebuild_modules_menu():

# TODO: Do it in a better way, singleShot isn't pretty...
QtCore.QTimer.singleShot(2000, rebuild_modules_menu)

QtCore.QTimer.singleShot(2000, rebuild_modules_menu)
QtCore.QTimer.singleShot(2000, check_module_status)

def _build_modulemenu(self, moduleMenu):
moduleMenu.clear()

def add_module_menuitem(module):
ac = moduleMenu.addAction(module.name, lambda: module.toggle())
title = module.name
ac = moduleMenu.addAction(title, lambda: module.toggle())
# Kind of nasty, but a quick way to affiliate the module to the menu action for when it needs updating
ac.module = module
ac.setCheckable(True)
ac.setChecked(module.is_alive())

add_module_menuitem(self.manager.modules["aw-server"])
modules_by_location = defaultdict(lambda: list())
for module in sorted(self.manager.modules.values(), key=lambda m: m.name):
modules_by_location[module.location].append(module)

for location, modules in sorted(modules_by_location.items(), key=lambda kv: kv[0]):
header = moduleMenu.addAction(location)
header.setEnabled(False)

# Always show aw-server first
if "aw-server" in (m.name for m in modules):
add_module_menuitem(self.manager.modules["aw-server"])

for module_name in sorted(self.manager.modules.keys()):
if module_name != "aw-server":
add_module_menuitem(self.manager.modules[module_name])
for module in sorted(modules, key=lambda m: m.name):
if module.name != "aw-server":
add_module_menuitem(self.manager.modules[module.name])


def exit_dialog():
Expand Down

0 comments on commit 70cca98

Please sign in to comment.