Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

feat(linux): Linux now shows a dot on the top bar icon and a desktop notification for a new version (held while a meeting is on); clicking it installs

wquguruadc9d5e3bad1
diff --git a/linux/cicada/app/debug.py b/linux/cicada/app/debug.py
index 7a62799..c6cb5c4 100644
--- a/linux/cicada/app/debug.py
+++ b/linux/cicada/app/debug.py
@@ -97,6 +97,11 @@ def _speakers(app, s) -> dict:
return out
+def updates_notified():
+ from ..core import updates
+ return updates.notified()
+
+
def command(app, line: str) -> dict:
parts = line.split(" ", 1)
cmd, arg = parts[0], (parts[1] if len(parts) > 1 else "")
@@ -128,6 +133,13 @@ def command(app, line: str) -> dict:
if cmd == "stop":
app.stop_listening()
return {"ok": True}
+ if cmd == "updatecheck": # the Mac's updateCheck: check the feed now (CICADA_UPDATE_FEED in a dev build)
+ app.updates.check_now()
+ return {"ok": True}
+ if cmd == "updatestate":
+ u = app.updates
+ return {"update": u.update.version if u.update else None, "notified": updates_notified(),
+ "afterMeeting": u.after_meeting, "failure": u.failure}
if cmd == "feed":
if not os.path.isfile(arg):
return {"ok": False, "error": "no such file"}
diff --git a/linux/cicada/app/main.py b/linux/cicada/app/main.py
index be19ff7..6c15c93 100644
--- a/linux/cicada/app/main.py
+++ b/linux/cicada/app/main.py
@@ -641,6 +641,12 @@ class App(Gtk.Application):
speak = Gio.SimpleAction.new("speak", None) # `cicada --speak`: 开始 / 停止说
speak.connect("activate", lambda *_: self.controller.toggle_speaking())
self.add_action(speak)
+ # The update notification's click and its 更新说明 button.
+ for name, action in (("install-update", lambda: self.controller.updates.install()),
+ ("update-notes", lambda: self.controller.updates.open_notes())):
+ a = Gio.SimpleAction.new(name, None)
+ a.connect("activate", lambda _a, _p, f=action: f())
+ self.add_action(a)
for name, action, keys in (("quit", self.controller.quit, ["<Control>q"]),
("settings", self.controller.show_settings, ["<Control>comma"])):
a = Gio.SimpleAction.new(name, None)
diff --git a/linux/cicada/app/tray.py b/linux/cicada/app/tray.py
index ed05ab6..0f83120 100644
--- a/linux/cicada/app/tray.py
+++ b/linux/cicada/app/tray.py
@@ -7,7 +7,7 @@ from __future__ import annotations
import ctypes
import ctypes.util
-from ..core.strings import L
+from ..core.strings import L, LF
from ..core.support import Log
@@ -47,7 +47,10 @@ class Tray:
lib.app_indicator_set_menu.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
lib.app_indicator_set_title.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
lib.app_indicator_set_label.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
+ lib.app_indicator_set_icon_full.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
self.lib = lib
+ self.icon = icon or "cicada"
+ self.badged = False
# An absolute path works as the icon "name", so no icon theme install is needed.
self.ind = lib.app_indicator_new(b"cicada", (icon or "cicada").encode(), self.CATEGORY_APPLICATION_STATUS)
lib.app_indicator_set_status(self.ind, self.STATUS_ACTIVE)
@@ -94,6 +97,19 @@ class Tray:
self.lib.app_indicator_set_menu(self.ind, _gpointer(menu))
self.menu = menu # the indicator holds a ref too; keep ours so Python doesn't free it early
self.lib.app_indicator_set_label(self.ind, (self._clock() if listening else "").encode(), b"")
+ self._badge(self.app.updates.update)
+
+ def _badge(self, update) -> None:
+ """The Mac's update dot on the top-bar icon: the same icon with an orange dot top right."""
+ on = update is not None
+ if on == self.badged:
+ return
+ path = badged_icon(self.icon) if on else self.icon
+ if path is None:
+ return
+ desc = L("知了") + (" · " + LF("有新版本 %@", update.version) if on else "")
+ self.lib.app_indicator_set_icon_full(self.ind, path.encode(), desc.encode())
+ self.badged = on
def _clock(self) -> str:
from datetime import datetime, timezone
@@ -119,3 +135,28 @@ class Tray:
if self.detail_item is not None:
self.detail_item.set_label(self._detail())
self.lib.app_indicator_set_label(self.ind, self._clock().encode(), b"")
+
+
+def badged_icon(icon: str) -> str | None:
+ """A copy of the app icon (an SVG, 100 units square) with the accent dot over its top right
+ corner and a white ring around it, written once under the data folder. The logo itself stays
+ in one place (site/icon.svg); this only adds the dot."""
+ try:
+ with open(icon, encoding="utf-8") as f:
+ svg = f.read()
+ except OSError:
+ return None
+ end = svg.rfind("</svg>")
+ if end < 0 or 'viewBox="0 0 100 100"' not in svg:
+ return None
+ dot = '<circle cx="80" cy="20" r="20" fill="#fff"/><circle cx="80" cy="20" r="13" fill="#E8552B"/>'
+ from ..core.support import Paths
+ import os
+ out = os.path.join(Paths.data(), "tray-update.svg")
+ try:
+ os.makedirs(Paths.data(), exist_ok=True)
+ with open(out, "w", encoding="utf-8") as f:
+ f.write(svg[:end] + dot + svg[end:])
+ except OSError:
+ return None
+ return out
diff --git a/linux/cicada/app/updater.py b/linux/cicada/app/updater.py
index b3c3b65..866d246 100644
--- a/linux/cicada/app/updater.py
+++ b/linux/cicada/app/updater.py
@@ -74,6 +74,7 @@ class Updates:
if u != self.update:
self.failure = None
self.update = u
+ self.notify_if_due()
if manual:
self.check = ("upToDate" if u is None else "idle") if ok else "failed"
self.app.changed()
@@ -138,7 +139,34 @@ class Updates:
except GLib.Error:
pass
+ def notify_if_due(self) -> None:
+ """One desktop notification per version, for people who never open the tray menu; a click
+ installs (会后安装 while a meeting is on). Held while listening or speaking: meeting_ended
+ tells. Withdrawn once there's nothing newer (installed, or the release was pulled)."""
+ u = self.update
+ if u is None:
+ self.app.application.withdraw_notification("update")
+ return
+ busy = self.app.listen.listening or self.app.speak.active
+ if not updates.should_notify(u.version, updates.notified(), busy):
+ return
+ updates.remember_notified(u.version)
+ n = Gio.Notification.new(LF("知了有新版本 %@", u.version))
+ n.set_body(L("点按安装,完成后自动重新打开"))
+ n.set_default_action("app.install-update")
+ n.add_button(L("更新说明"), "app.update-notes")
+ self.app.application.send_notification("update", n)
+ Log.d("update: notified %s" % u.version)
+
+ def open_notes(self) -> None:
+ if self.update is not None:
+ try:
+ Gio.AppInfo.launch_default_for_uri(self.update.url, None)
+ except GLib.Error:
+ pass
+
def meeting_ended(self) -> None:
+ self.notify_if_due()
if self.after_meeting and not self.app.listen.listening:
self.after_meeting = False
self.install()
diff --git a/linux/cicada/core/updates.py b/linux/cicada/core/updates.py
index 8acc91d..746f6f2 100644
--- a/linux/cicada/core/updates.py
+++ b/linux/cicada/core/updates.py
@@ -125,19 +125,48 @@ def _state_file() -> str:
return os.path.join(Paths.data(), "updates.json")
-def last_checked() -> float | None:
+def _state() -> dict:
try:
with open(_state_file(), encoding="utf-8") as f:
- v = json.load(f).get("lastChecked")
+ v = json.load(f)
+ return v if isinstance(v, dict) else {}
+ except (OSError, ValueError):
+ return {}
+
+
+def _save(**changes) -> None:
+ state = _state()
+ state.update(changes)
+ os.makedirs(Paths.data(), exist_ok=True)
+ with open(_state_file(), "w", encoding="utf-8") as f:
+ f.write(jsonfmt.write(state))
+
+
+def last_checked() -> float | None:
+ v = _state().get("lastChecked")
+ try:
return float(v) if v is not None else None
- except (OSError, ValueError, AttributeError):
+ except (TypeError, ValueError):
return None
def _remember(when: float) -> None:
- os.makedirs(Paths.data(), exist_ok=True)
- with open(_state_file(), "w", encoding="utf-8") as f:
- f.write(jsonfmt.write({"lastChecked": when}))
+ _save(lastChecked=when)
+
+
+def notified() -> str | None:
+ """The version the update notification was last shown for: one per version."""
+ v = _state().get("notified")
+ return v if isinstance(v, str) else None
+
+
+def remember_notified(version: str) -> None:
+ _save(notified=version)
+
+
+def should_notify(version: str, told: str | None, busy: bool) -> bool:
+ """A notification per version, none while listening or speaking (the meeting's end tells)."""
+ return not busy and version != told
def _get(url: str, timeout: float = 20):
diff --git a/linux/tests/test_updates.py b/linux/tests/test_updates.py
index 3e8b10c..78bdad2 100644
--- a/linux/tests/test_updates.py
+++ b/linux/tests/test_updates.py
@@ -174,5 +174,27 @@ class Install(unittest.TestCase):
self.assertIsNone(updates.install_root(), "a checkout is not ~/.local/lib/cicada")
+class Notify(unittest.TestCase):
+ def setUp(self):
+ self.dir = tempfile.mkdtemp()
+ Paths.home_override = os.path.join(self.dir, "home")
+
+ def tearDown(self):
+ Paths.home_override = None
+
+ def test_once_per_version_and_not_while_busy(self):
+ self.assertTrue(updates.should_notify("1.1.0", None, False))
+ self.assertTrue(updates.should_notify("1.2.0", "1.1.0", False))
+ self.assertFalse(updates.should_notify("1.1.0", "1.1.0", False))
+ self.assertFalse(updates.should_notify("1.1.0", None, True))
+
+ def test_notified_is_kept_beside_last_checked(self):
+ updates._remember(NOW)
+ updates.remember_notified("1.1.0")
+ updates._remember(NOW + 1)
+ self.assertEqual(updates.notified(), "1.1.0")
+ self.assertEqual(updates.last_checked(), NOW + 1)
+
+
if __name__ == "__main__":
unittest.main()