Cleanly terminate polling timer on window closed

This commit is contained in:
Daniele Verducci 2024-04-26 07:56:56 +02:00
parent 62388e9fbc
commit c574a8f09b

View File

@ -17,8 +17,7 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Adw from gi.repository import Adw, Gtk, GObject
from gi.repository import Gtk
from .sensors_polling_timer import SensorsPollingTimer from .sensors_polling_timer import SensorsPollingTimer
from .ev_calculator import EVCalculator from .ev_calculator import EVCalculator
@ -32,10 +31,17 @@ class LumosWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
# Register for window lifecycle events
self.connect("close-request", self.__on_close_request)
# Start polling sensors # Start polling sensors
self.sensorsPollingTimer = SensorsPollingTimer(0.1, self.onSensorRead) self.sensorsPollingTimer = SensorsPollingTimer(0.1, self.onSensorRead)
self.sensorsPollingTimer.start() self.sensorsPollingTimer.start()
def __on_close_request(self, _obj: GObject.Object) -> None:
# On window destroyed.Stop polling sensors
self.sensorsPollingTimer.cancel()
def onSensorRead(self, value: float, unit: str): def onSensorRead(self, value: float, unit: str):
# Called when the light value changed # Called when the light value changed
if self.lux_label: if self.lux_label: