Refactored notification code to add instead of removing all widgets and adding them again

This commit is contained in:
Erik Reider
2021-07-27 23:44:38 +02:00
parent 05a99eef75
commit 56e034c06d
3 changed files with 37 additions and 62 deletions

View File

@@ -9,20 +9,6 @@ namespace SwayNotificatonCenter {
cc = new ControlCenterWidget ();
}
public void close_notification (uint32 id) throws DBusError, IOError {
try {
foreach (NotifyParams n in dbusInit.notifications) {
if (n.applied_id == id) {
dbusInit.notifications.remove (n);
update ();
break;
}
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
}
public bool get_visibility () throws DBusError, IOError {
return cc.visible;
}
@@ -33,8 +19,12 @@ namespace SwayNotificatonCenter {
}
}
public void update () throws DBusError, IOError {
cc.update (this.dbusInit.notifications, dbusInit.notiDaemon);
public void add_notification (NotifyParams param) throws DBusError, IOError {
cc.add_notification (param, dbusInit.notiDaemon);
}
public void close_notification (uint32 id) throws DBusError, IOError {
cc.close_notification (id);
}
}
@@ -52,22 +42,32 @@ namespace SwayNotificatonCenter {
GtkLayerShell.set_anchor (this, GtkLayerShell.Edge.RIGHT, true);
}
private void removeWidget (Gtk.Widget widget) {
uint len = box.get_children ().length () - 1;
box.remove (widget);
if (len <= 0) {
// Do something in the future!
}
}
public bool toggle_visibility () {
var vis = !this.visible;
this.set_visible (vis);
return vis;
}
public void update (List<NotifyParams ? > notifications, NotiDaemon notiDaemon) {
foreach (var child in box.get_children ()) {
box.remove (child);
public void close_notification (uint32 id) {
foreach (var w in box.get_children ()) {
if (((Notification) w).param.applied_id == id) {
removeWidget (w);
break;
}
var notis = notifications.copy ();
notis.reverse ();
foreach (var param in notis) {
}
}
public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon, true);
box.add (noti);
}
box.pack_end (noti, false, true, 0);
}
}
}

View File

@@ -1,6 +1,6 @@
namespace SwayNotificatonCenter {
public class NotifyParams {
public struct NotifyParams {
public uint32 applied_id { get; set; }
public string app_name { get; set; }
public uint32 replaces_id { get; set; }
@@ -29,38 +29,30 @@ namespace SwayNotificatonCenter {
this.actions = actions;
this.hints = hints;
this.expire_timeout = expire_timeout;
get_icon ();
}
private void get_icon () {
}
public void printParams () {
print ("----START---- \n");
// print (applied_id.to_string () + "\n");
print (app_name + "\n");
// print (app_name + "\n");
// print (replaces_id.to_string () + "\n");
// print (app_icon + "\n");
// print (summary + "\n");
// print (body + "\n");
print ("-----------\n");
print ("----Actions----\n");
foreach (var action in actions) {
print (action + "\n");
}
print ("-----------\n");
// print(hints.get ("icon_data").print (false) + "\n");
// hints.get ("icon_data")
print ("----END----\n");
// foreach (var hint in hints.get_values ()) {
// hint.print (false);
// }
print ("-----------\n");
print ("----Hints----\n");
foreach (var key in hints.get_keys ()) {
print (key + "\n");
}
print ("-----------\n");
print (expire_timeout.to_string () + "\n");
print ("----END----\n");
// print (expire_timeout.to_string () + "\n");
}
}
@@ -93,7 +85,7 @@ namespace SwayNotificatonCenter {
throws DBusError, IOError {
uint32 id = replaces_id == 0 ? ++noti_id : replaces_id;
var param = new NotifyParams (
var param = NotifyParams (
id,
app_name,
replaces_id,
@@ -105,19 +97,13 @@ namespace SwayNotificatonCenter {
expire_timeout);
if (id == replaces_id) {
notiWin.replace_notification (param);
foreach (NotifyParams n in dbusInit.notifications) {
if (n.applied_id == replaces_id) {
dbusInit.notifications.remove (n);
break;
}
}
notiWin.close_notification (id);
dbusInit.ccDaemon.close_notification (id);
}
if (!dbusInit.ccDaemon.get_visibility ()) {
notiWin.add_notification (param, this);
}
dbusInit.notifications.append (param);
dbusInit.ccDaemon.update ();
dbusInit.ccDaemon.add_notification (param);
return id;
}
@@ -126,6 +112,7 @@ namespace SwayNotificatonCenter {
dbusInit.ccDaemon.close_notification (id);
}
// Only remove the popup without removing the it from the panel
public void CloseNotification (uint32 id) throws DBusError, IOError {
notiWin.close_notification (id);
}
@@ -148,7 +135,6 @@ namespace SwayNotificatonCenter {
public class DBusInit {
public List<NotifyParams ? > notifications = new List<NotifyParams ? > ();
public NotiDaemon notiDaemon;
public CcDaemon ccDaemon;

View File

@@ -18,19 +18,8 @@ namespace SwayNotificatonCenter {
if (len <= 0) box.set_visible (false);
}
public void replace_notification (NotifyParams param) {
foreach (var w in box.get_children ()) {
if (((Notification) w).param.applied_id == param.replaces_id) {
removeWidget (w);
break;
}
}
}
// public delegate void Close_click (NotifyParams param);
public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon);
// param.printParams ();
box.pack_end (noti, false, false, 0);
noti.show_notification ((v_noti) => {
box.remove (v_noti);