mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 17:52:02 +02:00
Add urgency support to cocoa notification API
This commit is contained in:
@@ -391,8 +391,8 @@ 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, *informativeText = NULL, *subtitle = NULL;
|
char *identifier = NULL, *title = NULL, *informativeText = NULL, *subtitle = NULL; int urgency = 1;
|
||||||
if (!PyArg_ParseTuple(args, "zsz|z", &identifier, &title, &informativeText, &subtitle)) return NULL;
|
if (!PyArg_ParseTuple(args, "zsz|zi", &identifier, &title, &informativeText, &subtitle, &urgency)) return NULL;
|
||||||
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
||||||
if (!center) {PyErr_SetString(PyExc_RuntimeError, "Failed to get the user notification center"); return NULL; }
|
if (!center) {PyErr_SetString(PyExc_RuntimeError, "Failed to get the user notification center"); return NULL; }
|
||||||
if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init];
|
if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init];
|
||||||
@@ -455,7 +455,7 @@ get_notification_center_safely(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
schedule_notification(const char *identifier, const char *title, const char *body, const char *subtitle) {
|
schedule_notification(const char *identifier, const char *title, const char *body, const char *subtitle, int urgency) {
|
||||||
UNUserNotificationCenter *center = get_notification_center_safely();
|
UNUserNotificationCenter *center = get_notification_center_safely();
|
||||||
if (!center) return;
|
if (!center) return;
|
||||||
// Configure the notification's payload.
|
// Configure the notification's payload.
|
||||||
@@ -464,6 +464,14 @@ schedule_notification(const char *identifier, const char *title, const char *bod
|
|||||||
if (body) content.body = @(body);
|
if (body) content.body = @(body);
|
||||||
if (subtitle) content.subtitle = @(subtitle);
|
if (subtitle) content.subtitle = @(subtitle);
|
||||||
content.sound = [UNNotificationSound defaultSound];
|
content.sound = [UNNotificationSound defaultSound];
|
||||||
|
switch (urgency) {
|
||||||
|
case 0:
|
||||||
|
content.interruptionLevel = UNNotificationInterruptionLevelPassive;
|
||||||
|
case 2:
|
||||||
|
content.interruptionLevel = UNNotificationInterruptionLevelCritical;
|
||||||
|
default:
|
||||||
|
content.interruptionLevel = UNNotificationInterruptionLevelActive;
|
||||||
|
}
|
||||||
// Deliver the notification
|
// Deliver the notification
|
||||||
static unsigned long counter = 1;
|
static unsigned long counter = 1;
|
||||||
UNNotificationRequest* request = [
|
UNNotificationRequest* request = [
|
||||||
@@ -480,6 +488,7 @@ schedule_notification(const char *identifier, const char *title, const char *bod
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *identifier, *title, *body, *subtitle;
|
char *identifier, *title, *body, *subtitle;
|
||||||
|
int urgency;
|
||||||
} QueuedNotification;
|
} QueuedNotification;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -489,13 +498,14 @@ 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, const char* subtitle) {
|
queue_notification(const char *identifier, const char *title, const char* body, const char* subtitle, int urgency) {
|
||||||
ensure_space_for((¬ification_queue), notifications, QueuedNotification, notification_queue.count + 16, capacity, 16, true);
|
ensure_space_for((¬ification_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;
|
n->subtitle = subtitle ? strdup(subtitle) : NULL;
|
||||||
|
n->urgency = urgency;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -503,25 +513,25 @@ 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, n->subtitle);
|
schedule_notification(n->identifier, n->title, n->body, n->subtitle, n->urgency);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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->subtitle);
|
free(n->identifier); free(n->title); free(n->body); free(n->subtitle);
|
||||||
n->identifier = NULL; n->title = NULL; n->body = NULL; n->subtitle = NULL;
|
memset(n, 0, sizeof(QueuedNotification));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, *subtitle = NULL;
|
char *identifier = NULL, *title = NULL, *body = NULL, *subtitle = NULL; int urgency = 1;
|
||||||
if (!PyArg_ParseTuple(args, "zsz|z", &identifier, &title, &body, &subtitle)) return NULL;
|
if (!PyArg_ParseTuple(args, "zsz|zi", &identifier, &title, &body, &subtitle, &urgency)) return NULL;
|
||||||
|
|
||||||
UNUserNotificationCenter *center = get_notification_center_safely();
|
UNUserNotificationCenter *center = get_notification_center_safely();
|
||||||
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, subtitle);
|
queue_notification(identifier, title, body, subtitle, urgency);
|
||||||
|
|
||||||
// The badge permission needs to be requested as well, even though it is not used,
|
// The badge permission needs to be requested as well, even though it is not used,
|
||||||
// otherwise macOS refuses to show the preference checkbox for enable/disable notification sound.
|
// otherwise macOS refuses to show the preference checkbox for enable/disable notification sound.
|
||||||
|
|||||||
@@ -504,6 +504,7 @@ def cocoa_send_notification(
|
|||||||
title: str,
|
title: str,
|
||||||
body: Optional[str],
|
body: Optional[str],
|
||||||
subtitle: Optional[str],
|
subtitle: Optional[str],
|
||||||
|
urgency: int = 1,
|
||||||
) -> None:
|
) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ if is_macos:
|
|||||||
subtitle: Optional[str] = None,
|
subtitle: Optional[str] = None,
|
||||||
urgency: Urgency = Urgency.Normal,
|
urgency: Urgency = Urgency.Normal,
|
||||||
) -> None:
|
) -> None:
|
||||||
cocoa_send_notification(identifier, title, body, subtitle)
|
cocoa_send_notification(identifier, title, body, subtitle, urgency.value)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user