Skip to content

Timeouts implementation #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ async def main():
),
on_clicked=lambda: print("Notification clicked"),
on_dismissed=lambda: print("Notification dismissed"),
on_timeout=lambda: print("Notification timed out"),
sound=True,
timeout=10,
)


Expand Down
2 changes: 1 addition & 1 deletion src/desktop_notifier/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Notification:
callback will be called without any arguments.
:param on_dismissed: Callback to call when the notification is dismissed. The
callback will be called without any arguments.
:attachment: URI for an attachment to the notification.
:param attachment: URI for an attachment to the notification.
:param sound: Whether to play a sound when the notification is shown.
:param thread: An identifier to group related notifications together.
:param timeout: Duration for which the notification in shown.
Expand Down
17 changes: 17 additions & 0 deletions src/desktop_notifier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ async def send(
reply_field: Optional[ReplyField] = None,
on_clicked: Optional[Callable[[], Any]] = None,
on_dismissed: Optional[Callable[[], Any]] = None,
on_timeout: Optional[Callable[[], Any]] = None,
attachment: Union[Path, str, None] = None,
sound: bool = False,
thread: Optional[str] = None,
Expand Down Expand Up @@ -271,6 +272,9 @@ async def send(
:param on_dismissed: Optional callback to call when the notification is
dismissed. The callback will be called without any arguments. This is
ignored by some implementations.
:param on_timeout: Optional callback to call when the notification is
timed out. The callback will be called without any arguments. This is
ignored by some implementations.
:param attachment: Optional URI string or :class:`pathlib.Path` for an
attachment to the notification such as an image, movie, or audio file. A
preview of this may be displayed together with the notification. Different
Expand Down Expand Up @@ -317,6 +321,17 @@ async def send(

await self._impl.send(notification)

async def timeout_task():
await asyncio.sleep(timeout)
if notification in self.current_notifications:
# The notification may have been dismissed in the meantime
await self.clear(notification)
if on_timeout:
on_timeout()

if timeout > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, this only works for timeouts that are explicitly specified by the dev.
There's also the default timeout of the desktop environment/notification daemon that will get applied when timeout == -1 - ideally, the callback should be invoked even in that case. This will likely need platform-specific code though.

asyncio.create_task(timeout_task())

return notification

def send_sync(
Expand All @@ -329,6 +344,7 @@ def send_sync(
reply_field: Optional[ReplyField] = None,
on_clicked: Optional[Callable[[], Any]] = None,
on_dismissed: Optional[Callable[[], Any]] = None,
on_timeout: Optional[Callable[[], Any]] = None,
attachment: Union[Path, str, None] = None,
sound: bool = False,
thread: Optional[str] = None,
Expand All @@ -349,6 +365,7 @@ def send_sync(
reply_field,
on_clicked,
on_dismissed,
on_timeout,
attachment,
sound,
thread,
Expand Down