Add a top-bar icon with a status menu, and a 2-hour off option
Pause now shows an eye in the GNOME top bar (open while breaks are on, closed while they're off). Its menu shows the time to the next break and offers Take a Break Now, Turn Off for an Hour / for 2 Hours, Turn Breaks Back On and Quit. It speaks StatusNotifierItem + dbusmenu over the app's own D-Bus connection, so it needs the AppIndicator Shell extension (packaged in Fedora) but no extra Python dependencies; libayatana-appindicator is GTK 3 only and can't share a process with GTK 4. Without the extension the icon just doesn't appear. Pause re-registers whenever the watcher appears, so enabling the extension later or restarting the Shell works. "Turn Off for 2 Hours" is also added to the app-grid right-click menu and the status notification. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F7w6vnQked3XNiTdTZDP8r
This commit is contained in:
parent
fdd43c389b
commit
1bcbbfd8be
8 changed files with 500 additions and 9 deletions
128
tests/test_tray.py
Normal file
128
tests/test_tray.py
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
import unittest
|
||||
|
||||
from gi.repository import Gio, GLib
|
||||
|
||||
from pause.tray import MENU_PATH, ITEM_PATH, WATCHER, MenuItem, Tray, separator
|
||||
|
||||
WATCHER_XML = Gio.DBusNodeInfo.new_for_xml(f"""
|
||||
<node><interface name="{WATCHER}">
|
||||
<method name="RegisterStatusNotifierItem"><arg name="service" type="s"/></method>
|
||||
</interface></node>""")
|
||||
|
||||
|
||||
class TrayTest(unittest.TestCase):
|
||||
"""Talks to the tray over a private session bus, like the Shell extension does."""
|
||||
|
||||
def setUp(self):
|
||||
self.bus = Gio.TestDBus.new(Gio.TestDBusFlags.NONE)
|
||||
self.bus.up()
|
||||
addr = self.bus.get_bus_address()
|
||||
flags = (Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT
|
||||
| Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION)
|
||||
self.app_conn = Gio.DBusConnection.new_for_address_sync(addr, flags, None, None)
|
||||
self.host = Gio.DBusConnection.new_for_address_sync(addr, flags, None, None)
|
||||
self.clicked = []
|
||||
self.tray = Tray(self.app_conn, "test.App", "Test", "/icons")
|
||||
|
||||
def tearDown(self):
|
||||
self.tray.close()
|
||||
self.app_conn.close_sync(None)
|
||||
self.host.close_sync(None)
|
||||
self.bus.down()
|
||||
|
||||
def spin(self, until):
|
||||
ctx = GLib.MainContext.default()
|
||||
for _ in range(200):
|
||||
if until():
|
||||
return
|
||||
ctx.iteration(False)
|
||||
GLib.usleep(5000)
|
||||
self.fail("timed out waiting for D-Bus")
|
||||
|
||||
def call(self, path, iface, method, args=None):
|
||||
# Async: the tray answers on this same main loop, so a sync call would deadlock.
|
||||
result = []
|
||||
self.host.call(self.app_conn.get_unique_name(), path, iface, method, args, None,
|
||||
Gio.DBusCallFlags.NONE, 1000, None,
|
||||
lambda conn, res: result.append(conn.call_finish(res)))
|
||||
self.spin(lambda: result)
|
||||
return result[0].unpack()
|
||||
|
||||
def prop(self, path, iface, name):
|
||||
return self.call(path, "org.freedesktop.DBus.Properties", "Get",
|
||||
GLib.Variant("(ss)", (iface, name)))[0]
|
||||
|
||||
def show(self, icon="icon-on", tooltip="Next break in 5 min"):
|
||||
self.tray.update(icon, tooltip, [
|
||||
MenuItem("status", tooltip),
|
||||
separator("sep"),
|
||||
MenuItem("now", "Take a Break Now", lambda: self.clicked.append("now")),
|
||||
MenuItem("on", "Turn Back On", lambda: self.clicked.append("on"), visible=False),
|
||||
])
|
||||
|
||||
def test_registers_when_watcher_appears(self):
|
||||
registered = []
|
||||
|
||||
def on_call(conn, sender, path, iface, method, params, invocation):
|
||||
registered.append((sender, params.unpack()[0]))
|
||||
invocation.return_value(None)
|
||||
|
||||
self.host.register_object("/StatusNotifierWatcher", WATCHER_XML.interfaces[0],
|
||||
on_call, None, None)
|
||||
Gio.bus_own_name_on_connection(self.host, WATCHER, Gio.BusNameOwnerFlags.NONE,
|
||||
None, None)
|
||||
self.spin(lambda: registered)
|
||||
self.assertEqual(registered, [(self.app_conn.get_unique_name(), ITEM_PATH)])
|
||||
|
||||
def test_item_properties(self):
|
||||
self.show()
|
||||
sni = "org.kde.StatusNotifierItem"
|
||||
self.assertEqual(self.prop(ITEM_PATH, sni, "IconName"), "icon-on")
|
||||
self.assertEqual(self.prop(ITEM_PATH, sni, "IconThemePath"), "/icons")
|
||||
self.assertEqual(self.prop(ITEM_PATH, sni, "Menu"), MENU_PATH)
|
||||
self.assertEqual(self.prop(ITEM_PATH, sni, "ToolTip"),
|
||||
("", [], "Test", "Next break in 5 min"))
|
||||
props = self.call(ITEM_PATH, "org.freedesktop.DBus.Properties", "GetAll",
|
||||
GLib.Variant("(s)", (sni,)))[0]
|
||||
self.assertTrue(props["ItemIsMenu"])
|
||||
|
||||
def test_layout(self):
|
||||
self.show()
|
||||
revision, (root_id, root_props, children) = self.call(
|
||||
MENU_PATH, "com.canonical.dbusmenu", "GetLayout",
|
||||
GLib.Variant("(iias)", (0, -1, [])))
|
||||
self.assertEqual(root_id, 0)
|
||||
self.assertEqual(root_props["children-display"], "submenu")
|
||||
status, sep, now, on = [props for _, props, _ in children]
|
||||
self.assertEqual(status, {"label": "Next break in 5 min", "enabled": False,
|
||||
"visible": True})
|
||||
self.assertEqual(sep, {"type": "separator"})
|
||||
self.assertEqual(now["label"], "Take a Break Now")
|
||||
self.assertTrue(now["enabled"])
|
||||
self.assertFalse(on["visible"])
|
||||
|
||||
def test_click_runs_action(self):
|
||||
self.show()
|
||||
children = self.call(MENU_PATH, "com.canonical.dbusmenu", "GetLayout",
|
||||
GLib.Variant("(iias)", (0, -1, [])))[1][2]
|
||||
now_id = children[2][0]
|
||||
self.call(MENU_PATH, "com.canonical.dbusmenu", "Event",
|
||||
GLib.Variant("(isvu)", (now_id, "clicked", GLib.Variant("i", 0), 0)))
|
||||
self.spin(lambda: self.clicked)
|
||||
self.assertEqual(self.clicked, ["now"])
|
||||
|
||||
def test_signals_only_on_change(self):
|
||||
signals = []
|
||||
self.host.signal_subscribe(None, None, None, None, None, Gio.DBusSignalFlags.NONE,
|
||||
lambda *a: signals.append(a[4]))
|
||||
self.show()
|
||||
self.spin(lambda: {"NewIcon", "NewToolTip", "LayoutUpdated"} <= set(signals))
|
||||
signals.clear()
|
||||
self.show() # same again
|
||||
self.show(tooltip="Next break in 4 min")
|
||||
self.spin(lambda: "LayoutUpdated" in signals)
|
||||
self.assertEqual(sorted(signals), ["LayoutUpdated", "NewToolTip"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue