macOS: add ability to show subtitles in notifications

This used to be implemented before 4e3c6e52aad41b9deb98d052c806f4a84846b782, when the now deprecated notifications framework was still being used.
Implement it again for feature parity.
This commit is contained in:
Luflosi 2020-09-28 12:17:26 +02:00
parent d3d2930bd2
commit 7c4ad278d5
No known key found for this signature in database
GPG Key ID: 4E41E29EDCC345D0
4 changed files with 18 additions and 13 deletions

View File

@ -1569,7 +1569,7 @@ class Boss:
from .notify import notify
now = monotonic()
ident = f'test-notify-{now}'
notify(f'Test {now}', f'At: {now}', identifier=ident)
notify(f'Test {now}', f'At: {now}', identifier=ident, subtitle=f'Test subtitle {now}')
def notification_activated(self, identifier: str, window_id: int, focus: bool, report: bool) -> None:
w = self.window_id_map.get(window_id)

View File

@ -156,13 +156,14 @@ static PyObject *notification_activated_callback = NULL;
static void
schedule_notification(const char *identifier, const char *title, const char *body) {
schedule_notification(const char *identifier, const char *title, const char *body, const char *subtitle) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
if (!center) return;
// Configure the notification's payload.
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
if (title) content.title = @(title);
if (body) content.body = @(body);
if (subtitle) content.subtitle = @(subtitle);
// Deliver the notification
static unsigned long counter = 1;
UNNotificationRequest* request = [
@ -178,7 +179,7 @@ schedule_notification(const char *identifier, const char *title, const char *bod
typedef struct {
char *identifier, *title, *body;
char *identifier, *title, *body, *subtitle;
} QueuedNotification;
typedef struct {
@ -188,12 +189,13 @@ typedef struct {
static NotificationQueue notification_queue = {0};
static void
queue_notification(const char *identifier, const char *title, const char* body) {
queue_notification(const char *identifier, const char *title, const char* body, const char* subtitle) {
ensure_space_for((&notification_queue), notifications, QueuedNotification, notification_queue.count + 16, capacity, 16, true);
QueuedNotification *n = notification_queue.notifications + notification_queue.count++;
n->identifier = identifier ? strdup(identifier) : NULL;
n->title = title ? strdup(title) : NULL;
n->body = body ? strdup(body) : NULL;
n->subtitle = subtitle ? strdup(subtitle) : NULL;
}
static void
@ -201,13 +203,13 @@ drain_pending_notifications(BOOL granted) {
if (granted) {
for (size_t i = 0; i < notification_queue.count; i++) {
QueuedNotification *n = notification_queue.notifications + i;
schedule_notification(n->identifier, n->title, n->body);
schedule_notification(n->identifier, n->title, n->body, n->subtitle);
}
}
while(notification_queue.count) {
QueuedNotification *n = notification_queue.notifications + --notification_queue.count;
free(n->identifier); free(n->title); free(n->body);
n->identifier = NULL; n->title = NULL; n->body = NULL;
free(n->identifier); free(n->title); free(n->body); free(n->subtitle);
n->identifier = NULL; n->title = NULL; n->body = NULL; n->subtitle = NULL;
}
}
@ -221,13 +223,13 @@ set_notification_activated_callback(PyObject *self UNUSED, PyObject *callback) {
static PyObject*
cocoa_send_notification(PyObject *self UNUSED, PyObject *args) {
char *identifier = NULL, *title = NULL, *body = NULL;
if (!PyArg_ParseTuple(args, "zsz", &identifier, &title, &body)) return NULL;
char *identifier = NULL, *title = NULL, *body = NULL, *subtitle = NULL;
if (!PyArg_ParseTuple(args, "zsz|z", &identifier, &title, &body, &subtitle)) return NULL;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
if (!center) Py_RETURN_NONE;
if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init];
queue_notification(identifier, title, body);
queue_notification(identifier, title, body, subtitle);
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {

View File

@ -520,6 +520,7 @@ def cocoa_send_notification(
identifier: Optional[str],
title: str,
body: Optional[str],
subtitle: Optional[str],
) -> None:
pass

View File

@ -20,9 +20,10 @@ if is_macos:
timeout: int = 5000,
application: str = 'kitty',
icon: bool = True,
identifier: Optional[str] = None
identifier: Optional[str] = None,
subtitle: Optional[str] = None,
) -> None:
cocoa_send_notification(identifier, title, body)
cocoa_send_notification(identifier, title, body, subtitle)
else:
@ -48,7 +49,8 @@ else:
timeout: int = -1,
application: str = 'kitty',
icon: bool = True,
identifier: Optional[str] = None
identifier: Optional[str] = None,
subtitle: Optional[str] = None,
) -> None:
icf = ''
if icon is True: