Skip to content

Commit

Permalink
Fixed nasty hover bug after first notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Jun 25, 2022
1 parent c1bc6f4 commit 9a251ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
18 changes: 8 additions & 10 deletions src/notiDaemon/notiDaemon.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace SwayNotificationCenter {
new HashTable<string, uint32> (str_hash, str_equal);

public ControlCenter control_center;
public NotificationWindow noti_window;

public NotiDaemon (SwayncDaemon swaync_daemon) {
this.notify["dnd"].connect (() => on_dnd_toggle (dnd));

// Init dnd from gsettings
self_settings.bind ("dnd-state", this, "dnd", SettingsBindFlags.DEFAULT);

this.noti_window = new NotificationWindow ();
this.control_center = new ControlCenter (swaync_daemon, this);
}

Expand All @@ -25,7 +23,7 @@ namespace SwayNotificationCenter {
*/
public void set_noti_window_visibility (bool value)
throws DBusError, IOError {
noti_window.change_visibility (value);
NotificationWindow.instance.change_visibility (value);
}

/** Toggles the current Do Not Disturb state */
Expand Down Expand Up @@ -53,7 +51,7 @@ namespace SwayNotificationCenter {
/** Method to close notification and send DISMISSED signal */
public void manually_close_notification (uint32 id, bool timeout)
throws DBusError, IOError {
noti_window.close_notification (id);
NotificationWindow.instance.close_notification (id);
if (!timeout) {
control_center.close_notification (id);
NotificationClosed (id, ClosedReasons.DISMISSED);
Expand All @@ -66,14 +64,14 @@ namespace SwayNotificationCenter {

/** Closes all popup and controlcenter notifications */
public void close_all_notifications () throws DBusError, IOError {
noti_window.close_all_notifications ();
NotificationWindow.instance.close_all_notifications ();
control_center.close_all_notifications ();
}

/** Closes latest popup notification */
public void hide_latest_notification (bool close)
throws DBusError, IOError {
uint32 ? id = noti_window.get_latest_notification ();
uint32 ? id = NotificationWindow.instance.get_latest_notification ();
if (id == null) return;
manually_close_notification (id, !close);
}
Expand Down Expand Up @@ -157,7 +155,7 @@ namespace SwayNotificationCenter {
// Replace notification logic
if (id == replaces_id) {
param.replaces = true;
noti_window.close_notification (id);
NotificationWindow.instance.close_notification (id);
control_center.close_notification (id, true);
} else if (param.synchronous != null
&& param.synchronous.length > 0) {
Expand All @@ -168,7 +166,7 @@ namespace SwayNotificationCenter {
param.synchronous, null, out r_id)) {
param.replaces = true;
// Close the notification
noti_window.close_notification (r_id);
NotificationWindow.instance.close_notification (r_id);
control_center.close_notification (r_id, true);
}
synchronous_ids.set (param.synchronous, id);
Expand All @@ -179,7 +177,7 @@ namespace SwayNotificationCenter {
&& !control_center.get_visibility ()) {
if (param.urgency == UrgencyLevels.CRITICAL ||
(!dnd && param.urgency != UrgencyLevels.CRITICAL)) {
noti_window.add_notification (param, this);
NotificationWindow.instance.add_notification (param, this);
}
}
// Only add notification to CC if it isn't IGNORED and not transient
Expand Down Expand Up @@ -255,7 +253,7 @@ namespace SwayNotificationCenter {
*/
[DBus (name = "CloseNotification")]
public void close_notification (uint32 id) throws DBusError, IOError {
noti_window.close_notification (id);
NotificationWindow.instance.close_notification (id);
control_center.close_notification (id);
NotificationClosed (id, ClosedReasons.CLOSED_BY_CLOSENOTIFICATION);
}
Expand Down
2 changes: 1 addition & 1 deletion src/notificationWindow/notificationWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<template class="SwayNotificationCenterNotificationWindow" parent="GtkApplicationWindow">
<template class="SwayNotificationCenterNotiWindow" parent="GtkApplicationWindow">
<property name="can-focus">False</property>
<property name="role">NotificationWindow</property>
<property name="resizable">False</property>
Expand Down
34 changes: 30 additions & 4 deletions src/notificationWindow/notificationWindow.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
namespace SwayNotificationCenter {
public class NotificationWindow : Object {
private static NotiWindow ? window = null;
// Use a NotiWindow singleton due to a nasty notification
// enter_notify_event bug where GTK still thinks that the cursor is at
// that location after closing the last notification. The next notification
// would sometimes automatically be hovered...
public static NotiWindow instance {
get {
if (window == null) {
window = new NotiWindow ();
} else if (!window.get_mapped () ||
!window.get_realized () ||
!(window.get_child () is Gtk.Widget)) {
window.destroy ();
window = new NotiWindow ();
}
return window;
}
}
}

[GtkTemplate (ui = "/org/erikreider/sway-notification-center/notificationWindow/notificationWindow.ui")]
public class NotificationWindow : Gtk.ApplicationWindow {
public class NotiWindow : Gtk.ApplicationWindow {

[GtkChild]
unowned Gtk.ScrolledWindow scrolled_window;
Expand All @@ -15,7 +36,7 @@ namespace SwayNotificationCenter {

private const int MAX_HEIGHT = 600;

public NotificationWindow () {
public NotiWindow () {
if (!GtkLayerShell.is_supported ()) {
stderr.printf ("GTKLAYERSHELL IS NOT SUPPORTED!\n");
stderr.printf ("Swaync only works on Wayland!\n");
Expand Down Expand Up @@ -113,8 +134,13 @@ namespace SwayNotificationCenter {
noti.destroy ();
}

if (!this.get_realized ()) return;
if (box.get_children ().length () == 0) this.hide ();
if (!get_realized ()
|| !get_mapped ()
|| !(get_child () is Gtk.Widget)
|| box.get_children ().length () == 0) {
close ();
return;
}
}

public void add_notification (NotifyParams param,
Expand Down

0 comments on commit 9a251ed

Please sign in to comment.