macOS: add ability to show subtitles in notifications

This used to be implemented before 4e3c6e52aa, 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
4 changed files with 18 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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