Skip to content

naneos.gui.tray

The tray icon, its menu and the device manager that runs behind it.

ManagerLike

Bases: Protocol

The part of NaneosDeviceManager the tray uses.

Source code in src/naneos/gui/tray.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class ManagerLike(Protocol):
    """The part of NaneosDeviceManager the tray uses."""

    @property
    def upload_active(self) -> bool: ...

    @property
    def pending_upload_count(self) -> int: ...

    @property
    def diagnostics_in_progress(self) -> bool: ...

    def register_live_queue(self, live_queue: "queue.Queue[NaneosDeviceDataPoint]") -> None: ...

    def start(self) -> None: ...

    def stop(self) -> None: ...

    def join(self, timeout: float | None = None) -> None: ...

    def is_alive(self) -> bool: ...

    def get_devices(self) -> Sequence[DeviceLike]: ...

TrayController

Bases: QObject

Owns the manager and keeps the menu in step with it.

The menu is built once and only its actions change: rebuilding a QMenu that is open misbehaves on macOS and on the Linux D-Bus menu. Everything here runs on the GUI thread; the manager has its own threads, and bleak never runs here.

Source code in src/naneos/gui/tray.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
class TrayController(QObject):
    """Owns the manager and keeps the menu in step with it.

    The menu is built once and only its actions change: rebuilding a QMenu that is
    open misbehaves on macOS and on the Linux D-Bus menu. Everything here runs on
    the GUI thread; the manager has its own threads, and bleak never runs here.
    """

    def __init__(
        self,
        app: QApplication,
        manager: ManagerLike,
        autostart: Autostart,
        version: str,
        quit_timeout_seconds: float = QUIT_TIMEOUT_SECONDS,
    ) -> None:
        super().__init__(app)
        self._app = app
        self._manager = manager
        self._autostart = autostart
        self._version = version
        self._quit_timeout = quit_timeout_seconds

        self._live: queue.Queue[NaneosDeviceDataPoint] = queue.Queue(maxsize=2000)
        self._seen = LastSeen()
        self._stopping = False
        self._gave_up = False  # Quit waited for the manager and it did not stop in time
        self._final_stop_done = False
        self._quit_deadline = 0.0
        self._started_at = time.monotonic()
        self._warned_no_tray = False
        self._death_logged = False

        self._tray = QSystemTrayIcon(QIcon(str(icon_file("png"))), self)
        self._menu = QMenu()  # owned by us: a tray icon does not take ownership
        self._build_menu()
        self._tray.setContextMenu(self._menu)

        self._poll_timer = QTimer(self)
        self._poll_timer.timeout.connect(self.refresh)
        self._quit_timer = QTimer(self)
        self._quit_timer.timeout.connect(self._check_stopped)
        # Python only runs its signal handlers (SIGTERM, Ctrl+C) when bytecode runs,
        # which a Qt event loop alone does not give it.
        self._signal_timer = QTimer(self)
        self._signal_timer.timeout.connect(lambda: None)

        app.aboutToQuit.connect(self._on_about_to_quit)
        app.commitDataRequest.connect(lambda *_: self._on_about_to_quit())

    # ------------------------------------------------------------ menu

    def _build_menu(self) -> None:
        menu = self._menu
        self._title_action = menu.addAction(format_title(self._version))
        self._title_action.setEnabled(False)
        self._status_action = menu.addAction("")
        self._status_action.setEnabled(False)
        menu.addSeparator()

        self._rows: list[QAction] = []
        for _ in range(MAX_ROWS):
            row = menu.addAction("")
            row.setEnabled(False)  # information only; later a submenu with commands
            row.setVisible(False)
            self._rows.append(row)
        menu.addSeparator()

        self._autostart_action = menu.addAction("Start at login")
        self._autostart_action.setCheckable(True)
        self._autostart_action.triggered.connect(self._set_autostart)
        self._log_action = menu.addAction("Open log folder")
        self._log_action.triggered.connect(self._open_log_folder)
        self._quit_action = menu.addAction("Quit")
        self._quit_action.triggered.connect(self.request_quit)

        menu.aboutToShow.connect(self._on_menu_about_to_show)

    def _on_menu_about_to_show(self) -> None:
        self._autostart_action.setChecked(self._safe_autostart_enabled())
        self.refresh()

    def _safe_autostart_enabled(self) -> bool:
        try:
            return self._autostart.enabled()
        except Exception as e:
            logger.warning(f"Cannot read the autostart setting: {e}")
            return False

    def _set_autostart(self, checked: bool) -> None:
        try:
            self._autostart.enable() if checked else self._autostart.disable()
        except Exception as e:
            logger.error(f"Cannot change the autostart setting: {e}")
            self._tray.showMessage("Naneos Devices", f"Could not change the setting: {e}")
        self._autostart_action.setChecked(self._safe_autostart_enabled())

    def _open_log_folder(self) -> None:
        folder = log_dir()
        folder.mkdir(parents=True, exist_ok=True)
        QDesktopServices.openUrl(QUrl.fromLocalFile(str(folder)))

    # ------------------------------------------------------------ life cycle

    def start(self) -> None:
        self._manager.register_live_queue(self._live)
        self._manager.start()
        self._tray.show()
        self.refresh()
        self._poll_timer.start(POLL_MS)
        self._signal_timer.start(500)

    def refresh(self) -> None:
        """Bring the menu up to date. Runs every second and when the menu opens."""
        self._seen.drain(self._live)
        if self._stopping:
            return
        self._show_tray_when_available()

        devices = sort_devices(self._manager.get_devices())
        self._seen.forget_except(d.serial_number for d in devices)

        if not self._manager.is_alive():
            status = "Stopped – see log"
            if not self._death_logged:
                self._death_logged = True
                logger.error("The device manager stopped unexpectedly")
        else:
            status = format_status(
                self._manager.upload_active,
                self._manager.pending_upload_count,
                self._manager.diagnostics_in_progress,
            )
        self._status_action.setText(status)

        texts = [format_device_row(d, self._seen.age(d.serial_number)) for d in devices]
        if not texts:
            texts = ["No devices in reach"]
        if len(texts) > MAX_ROWS:
            texts = texts[: MAX_ROWS - 1] + [f"… and {len(texts) - MAX_ROWS + 1} more"]
        for i, row in enumerate(self._rows):
            row.setVisible(i < len(texts))
            if i < len(texts):
                row.setText(texts[i])

        self._tray.setToolTip(format_tooltip(self._version, len(devices)))

    def _show_tray_when_available(self) -> None:
        """A Linux desktop may start its tray after us; show the icon as soon as it exists."""
        if self._tray.isVisible():
            return
        if QSystemTrayIcon.isSystemTrayAvailable():
            self._tray.show()
        elif (
            not self._warned_no_tray
            and time.monotonic() - self._started_at > TRAY_WARNING_AFTER_SECONDS
        ):
            self._warned_no_tray = True
            logger.warning(
                "No system tray on this desktop. Devices are still read and uploaded. "
                "On GNOME install the 'AppIndicator and KStatusNotifierItem Support' extension."
            )

    def handle_command(self, command: str) -> None:
        """A command from another process, see instance.py."""
        if command == "quit":
            self.request_quit()
        elif command == "show":
            self.refresh()
            self._menu.popup(QCursor.pos())

    def request_quit(self) -> None:
        """Stop the manager without blocking the GUI, then leave the event loop."""
        if self._stopping:
            return
        self._stopping = True
        logger.info("Stopping")
        self._title_action.setText("Stopping…")
        self._status_action.setText("")
        for action in (self._autostart_action, self._log_action, self._quit_action):
            action.setEnabled(False)
        for row in self._rows:
            row.setVisible(False)
        self._tray.setToolTip("Naneos Devices – stopping")

        self._manager.stop()
        self._quit_deadline = time.monotonic() + self._quit_timeout
        self._quit_timer.start(QUIT_POLL_MS)

    def _check_stopped(self) -> None:
        if self._manager.is_alive() and time.monotonic() < self._quit_deadline:
            return
        self._quit_timer.stop()
        if self._manager.is_alive():
            self._gave_up = True
            logger.warning("The device manager did not stop in time, quitting anyway")
        self._tray.hide()
        self._app.quit()

    def _on_about_to_quit(self) -> None:
        """The OS is ending the session, or quit() was called: the last chance to stop.

        Called for both aboutToQuit and commitDataRequest, and it blocks the GUI thread, so
        it waits at most once, and not at all after Quit has already waited for nothing.
        """
        self._poll_timer.stop()
        if self._final_stop_done:
            return
        self._final_stop_done = True
        if self._gave_up or not self._manager.is_alive():
            return
        self._manager.stop()
        self._manager.join(timeout=SESSION_END_JOIN_SECONDS)

handle_command(command)

A command from another process, see instance.py.

Source code in src/naneos/gui/tray.py
223
224
225
226
227
228
229
def handle_command(self, command: str) -> None:
    """A command from another process, see instance.py."""
    if command == "quit":
        self.request_quit()
    elif command == "show":
        self.refresh()
        self._menu.popup(QCursor.pos())

refresh()

Bring the menu up to date. Runs every second and when the menu opens.

Source code in src/naneos/gui/tray.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def refresh(self) -> None:
    """Bring the menu up to date. Runs every second and when the menu opens."""
    self._seen.drain(self._live)
    if self._stopping:
        return
    self._show_tray_when_available()

    devices = sort_devices(self._manager.get_devices())
    self._seen.forget_except(d.serial_number for d in devices)

    if not self._manager.is_alive():
        status = "Stopped – see log"
        if not self._death_logged:
            self._death_logged = True
            logger.error("The device manager stopped unexpectedly")
    else:
        status = format_status(
            self._manager.upload_active,
            self._manager.pending_upload_count,
            self._manager.diagnostics_in_progress,
        )
    self._status_action.setText(status)

    texts = [format_device_row(d, self._seen.age(d.serial_number)) for d in devices]
    if not texts:
        texts = ["No devices in reach"]
    if len(texts) > MAX_ROWS:
        texts = texts[: MAX_ROWS - 1] + [f"… and {len(texts) - MAX_ROWS + 1} more"]
    for i, row in enumerate(self._rows):
        row.setVisible(i < len(texts))
        if i < len(texts):
            row.setText(texts[i])

    self._tray.setToolTip(format_tooltip(self._version, len(devices)))

request_quit()

Stop the manager without blocking the GUI, then leave the event loop.

Source code in src/naneos/gui/tray.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def request_quit(self) -> None:
    """Stop the manager without blocking the GUI, then leave the event loop."""
    if self._stopping:
        return
    self._stopping = True
    logger.info("Stopping")
    self._title_action.setText("Stopping…")
    self._status_action.setText("")
    for action in (self._autostart_action, self._log_action, self._quit_action):
        action.setEnabled(False)
    for row in self._rows:
        row.setVisible(False)
    self._tray.setToolTip("Naneos Devices – stopping")

    self._manager.stop()
    self._quit_deadline = time.monotonic() + self._quit_timeout
    self._quit_timer.start(QUIT_POLL_MS)