Skip to content

Commit

Permalink
Sort critical notification groups before regular groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Dec 4, 2023
1 parent 44d80cd commit 0643e4c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
36 changes: 27 additions & 9 deletions src/controlCenter/controlCenter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/notificationGroup/notificationGroup.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace SwayNotificationCenter {
public class NotificationGroup : Gtk.ListBoxRow {
const string STYLE_CLASS_URGENT = "critical";

public string name_id;

private ExpandableGroup group;
Expand All @@ -9,6 +11,9 @@ namespace SwayNotificationCenter {
private bool gesture_down = false;
private bool gesture_in = false;

private HashTable<uint32, bool> urgent_notifications
= new HashTable<uint32, bool> (direct_hash, direct_equal);

public signal void on_expand_change (bool state);

public NotificationGroup (string name_id, string display_name) {
Expand Down Expand Up @@ -141,13 +146,24 @@ 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);
}
}

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);
Expand All @@ -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 ();
}
Expand All @@ -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);
Expand Down
24 changes: 21 additions & 3 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0643e4c

Please sign in to comment.