diff --git a/docs/changelog.rst b/docs/changelog.rst index 768763a61..406def959 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -260,9 +260,9 @@ Detailed list of changes to the server has silently died, for example, after a network change or suspend. Now the kitten detects this and reconnects (:iss:`10691`) -- Linux: Desktop notifications: Fix a request to close a notification being - ignored if it arrives before the notification server has finished creating - the notification (:iss:`10695`) +- Linux: Desktop notifications: Fix a request to close or update a + notification being ignored if it arrives before the notification server has + finished creating the notification (:iss:`10695`) - diff kitten: Fix comparing remote directories over SSH failing when they contain read-only directories (:pull:`10676`) diff --git a/kitty/notifications.py b/kitty/notifications.py index e7e85d2cd..b3f63c706 100644 --- a/kitty/notifications.py +++ b/kitty/notifications.py @@ -714,16 +714,23 @@ class FreeDesktopIntegration(DesktopIntegration): close_succeeded = dbus_close_notification(dbus_id) if debug_desktop_integration: log_error(f'Close request for {desktop_notification_id=} {"succeeded" if close_succeeded else "failed"}') - elif (n := self.notification_manager.in_progress_notification_commands.get(desktop_notification_id)) and not n.created_by_desktop: - # The notification daemon has not yet replied to Notify, close it once it does - if len(self.pending_closes) > 128: - self.pending_closes &= set(self.notification_manager.in_progress_notification_commands) - self.pending_closes.add(desktop_notification_id) - close_succeeded = True - if debug_desktop_integration: - log_error(f'Close request for {desktop_notification_id=} deferred till notification is created') + else: + close_succeeded = self.defer_close_till_created(desktop_notification_id) return close_succeeded + def defer_close_till_created(self, desktop_notification_id: int) -> bool: + # If the notification daemon has not yet replied to Notify, close the + # notification once it does + n = self.notification_manager.in_progress_notification_commands.get(desktop_notification_id) + if n is None or n.created_by_desktop: + return False + if len(self.pending_closes) > 128: + self.pending_closes &= set(self.notification_manager.in_progress_notification_commands) + self.pending_closes.add(desktop_notification_id) + if debug_desktop_integration: + log_error(f'Close request for {desktop_notification_id=} deferred till notification is created') + return True + def get_desktop_notification_id(self, dbus_notification_id: int, event: str) -> int | None: q = self.dbus_to_desktop.get(dbus_notification_id) if q is None: @@ -816,6 +823,10 @@ class FreeDesktopIntegration(DesktopIntegration): replaces_dbus_id = 0 if existing_desktop_notification_id: replaces_dbus_id = self.get_dbus_notification_id(existing_desktop_notification_id, 'notify') or 0 + if not replaces_dbus_id: + # The notification being replaced has not been created yet so + # it cannot be replaced, instead close it once it is created + self.defer_close_till_created(existing_desktop_notification_id) actions = {'default': ''} for i, b in enumerate(nc.buttons): actions[str(i + 1)] = b diff --git a/kitty_tests/notifications.py b/kitty_tests/notifications.py index 258829cb9..2351945ac 100644 --- a/kitty_tests/notifications.py +++ b/kitty_tests/notifications.py @@ -369,8 +369,10 @@ class TestNotifications(BaseTest): sent: list[int] = [] closed: list[int] = [] + replaces: list[object] = [] def send_notification(**kw: object) -> int: + replaces.append(kw['replaces']) sent.append(len(sent) + 1) return sent[-1] @@ -378,9 +380,15 @@ class TestNotifications(BaseTest): closed.append(dbus_id) return True - with tempfile.TemporaryDirectory() as tdir, patch.multiple( - fdt, create=True, dbus_set_notification_callback=lambda *a: None, - dbus_send_notification=send_notification, dbus_close_notification=close_notification, + with ( + tempfile.TemporaryDirectory() as tdir, + patch.multiple( + fdt, + create=True, + dbus_set_notification_callback=lambda *a: None, + dbus_send_notification=send_notification, + dbus_close_notification=close_notification, + ), ): di = FreeDesktopIntegration(None) ch = Channel() @@ -400,6 +408,24 @@ class TestNotifications(BaseTest): self.ae(ch.responses, ['99;i=x:p=close;']) self.ae(di.pending_closes, set()) + # replacement sent before the notification being replaced is created + closed.clear(), ch.reset() + nm.handle_notification_cmd(1, 99, 'i=r;first') + first = sent[-1] + nm.handle_notification_cmd(1, 99, 'i=r;second') + second = sent[-1] + self.ae(nm.in_progress_notification_commands_by_client_id['r'].title, 'second') + di.dispatch_event_from_desktop('created', first, 201) + self.ae(closed, [201]) + di.dispatch_event_from_desktop('closed', 201, 0) + di.dispatch_event_from_desktop('created', second, 202) + self.ae(closed, [201]) + self.ae(nm.in_progress_notification_commands_by_client_id['r'].title, 'second') + self.ae(ch.responses, []) + nm.handle_notification_cmd(1, 99, 'i=r;third') + self.ae(closed, [201]) # replaced via the replaces argument to Notify, not closed + self.ae(replaces[-3:], [0, 0, 202]) + # normal close after creation closed.clear(), ch.reset() nm.handle_notification_cmd(1, 99, 'i=y;title')