From 0643e4c7cd4a6c9e320becd5560d6fd44443a033 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Mon, 4 Dec 2023 17:15:10 +0100 Subject: [PATCH] Sort critical notification groups before regular groups --- src/controlCenter/controlCenter.vala | 36 +++++++++++++++----- src/notificationGroup/notificationGroup.vala | 21 ++++++++++++ src/style.css | 24 +++++++++++-- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/controlCenter/controlCenter.vala b/src/controlCenter/controlCenter.vala index 2b1be9bb..e6622412 100644 --- a/src/controlCenter/controlCenter.vala +++ b/src/controlCenter/controlCenter.vala @@ -427,21 +427,39 @@ namespace SwayNotificationCenter { box.set_valign (align_y); list_box.set_valign (list_align); - list_box.set_sort_func ((w1, w2) => { - var a_time = ((NotificationGroup) w1).get_time (); - var b_time = ((NotificationGroup) w2).get_time (); - if (a_time < 0 || b_time < 0)return 0; - // Sort the list in reverse if needed - if (a_time == b_time)return 0; - int val = list_reverse ? 1 : -1; - return a_time > b_time ? val : val * -1; - }); + list_box.set_sort_func (list_box_sort_func); // Always set the size request in all events. box.set_size_request (ConfigModel.instance.control_center_width, ConfigModel.instance.control_center_height); } + /** + * Returns < 0 if row1 should be before row2, 0 if they are equal + * and > 0 otherwise + */ + private int list_box_sort_func (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) { + int val = list_reverse ? 1 : -1; + + var a_group = (NotificationGroup) row1; + var b_group = (NotificationGroup) row2; + + // Check urgency before time + var a_urgency = a_group.get_is_urgent (); + var b_urgency = b_group.get_is_urgent (); + if (a_urgency != b_urgency) { + return a_urgency ? val : val * -1; + } + + // Check time + var a_time = a_group.get_time (); + var b_time = b_group.get_time (); + if (a_time < 0 || b_time < 0) return 0; + // Sort the list in reverse if needed + if (a_time == b_time) return 0; + return a_time > b_time ? val : val * -1; + } + private void scroll_to_start (bool reverse) { Gtk.ScrollType scroll_type = Gtk.ScrollType.START; if (reverse) { diff --git a/src/notificationGroup/notificationGroup.vala b/src/notificationGroup/notificationGroup.vala index 9a0e9968..147136db 100644 --- a/src/notificationGroup/notificationGroup.vala +++ b/src/notificationGroup/notificationGroup.vala @@ -1,5 +1,7 @@ namespace SwayNotificationCenter { public class NotificationGroup : Gtk.ListBoxRow { + const string STYLE_CLASS_URGENT = "critical"; + public string name_id; private ExpandableGroup group; @@ -9,6 +11,9 @@ namespace SwayNotificationCenter { private bool gesture_down = false; private bool gesture_in = false; + private HashTable urgent_notifications + = new HashTable (direct_hash, direct_equal); + public signal void on_expand_change (bool state); public NotificationGroup (string name_id, string display_name) { @@ -141,6 +146,13 @@ namespace SwayNotificationCenter { } public void add_notification (Notification noti) { + if (noti.param.urgency == UrgencyLevels.CRITICAL) { + urgent_notifications.insert (noti.param.applied_id, true); + unowned Gtk.StyleContext ctx = get_style_context (); + if (!ctx.has_class (STYLE_CLASS_URGENT)) { + ctx.add_class (STYLE_CLASS_URGENT); + } + } group.add (noti); if (!single_notification ()) { group.set_sensitive (false); @@ -148,6 +160,10 @@ namespace SwayNotificationCenter { } public void remove_notification (Notification noti) { + urgent_notifications.remove (noti.param.applied_id); + if (urgent_notifications.length == 0) { + get_style_context ().remove_class (STYLE_CLASS_URGENT); + } group.remove (noti); if (single_notification ()) { set_expanded (false); @@ -163,6 +179,10 @@ namespace SwayNotificationCenter { return ((Notification) group.widgets.last ().data).param.time; } + public bool get_is_urgent () { + return urgent_notifications.length > 0; + } + public uint get_num_notifications () { return group.widgets.length (); } @@ -172,6 +192,7 @@ namespace SwayNotificationCenter { } public void close_all_notifications () { + urgent_notifications.remove_all (); foreach (unowned Gtk.Widget widget in group.widgets) { var noti = (Notification) widget; if (noti != null) noti.close_notification (false); diff --git a/src/style.css b/src/style.css index 4e3e69ff..bd3e587b 100644 --- a/src/style.css +++ b/src/style.css @@ -35,19 +35,37 @@ } /* Uncomment to enable specific urgency colors -.low { +.notification.low { background: yellow; padding: 6px; border-radius: 12px; } -.normal { +.notification.normal { background: green; padding: 6px; border-radius: 12px; } -.critical { +.notification.critical { + background: red; + padding: 6px; + border-radius: 12px; +} + +.notification-group.low { + background: yellow; + padding: 6px; + border-radius: 12px; +} + +.notification-group.normal { + background: green; + padding: 6px; + border-radius: 12px; +} + +.notification-group.critical { background: red; padding: 6px; border-radius: 12px;