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 (); 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 { public bool get_visibility () throws DBusError, IOError {
return cc.visible; return cc.visible;
} }
@@ -33,8 +19,12 @@ namespace SwayNotificatonCenter {
} }
} }
public void update () throws DBusError, IOError { public void add_notification (NotifyParams param) throws DBusError, IOError {
cc.update (this.dbusInit.notifications, dbusInit.notiDaemon); 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); 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 () { public bool toggle_visibility () {
var vis = !this.visible; var vis = !this.visible;
this.set_visible (vis); this.set_visible (vis);
return vis; return vis;
} }
public void update (List<NotifyParams ? > notifications, NotiDaemon notiDaemon) { public void close_notification (uint32 id) {
foreach (var child in box.get_children ()) { foreach (var w in box.get_children ()) {
box.remove (child); if (((Notification) w).param.applied_id == id) {
} removeWidget (w);
var notis = notifications.copy (); break;
notis.reverse (); }
foreach (var param in notis) {
var noti = new Notification (param, notiDaemon, true);
box.add (noti);
} }
} }
public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon, true);
box.pack_end (noti, false, true, 0);
}
} }
} }

View File

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

View File

@@ -18,19 +18,8 @@ namespace SwayNotificatonCenter {
if (len <= 0) box.set_visible (false); 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) { public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon); var noti = new Notification (param, notiDaemon);
// param.printParams ();
box.pack_end (noti, false, false, 0); box.pack_end (noti, false, false, 0);
noti.show_notification ((v_noti) => { noti.show_notification ((v_noti) => {
box.remove (v_noti); box.remove (v_noti);