feat: refactor notification system to manage hover states centrally and auto close

This commit is contained in:
Hannes
2026-09-02 00:16:15 +02:00
parent b59eb012ba
commit 27401c25a0
5 changed files with 129 additions and 25 deletions
+9
View File
@@ -84,3 +84,12 @@ PanelWindow {
} }
} }
} }
76 closeNHistory()
157 notificationPopupEnabled
169 notificationPopupEnabled
206, 207 ^^
+11 -9
View File
@@ -13,11 +13,20 @@ MouseArea {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
// NotificationS.soundEnabled = !NotificationS.soundEnabled // NotificationS.notificationPopupEnabled = !NotificationS.notificationPopupEnabled
console.log("[NotificationButton] Opening NotificationHistory") console.log("[NotificationButton] Opening NotificationHistory")
root.toggleNotificationHistory() root.toggleNotificationHistory()
} }
// ── Report hover to NotificationServer for auto-close logic ──
onContainsMouseChanged: NotificationS.buttonHovered = containsMouse
Component.onDestruction: {
if (containsMouse) NotificationS.buttonHovered = false
}
onVisibleChanged: {
if (!visible && NotificationS.buttonHovered) NotificationS.buttonHovered = false
}
Item { Item {
anchors.top: parent.topBar anchors.top: parent.topBar
@@ -47,17 +56,10 @@ MouseArea {
id: notificationText id: notificationText
anchors.centerIn: parent anchors.centerIn: parent
// text: Quickshell.iconPath("discord") // text: Quickshell.iconPath("discord")
text: NotificationS.soundEnabled ? "󰂚" : "󰂛" text: NotificationS.notificationPopupEnabled ? "󰂚" : "󰂛"
font.pixelSize: Config.bigIconSize(bar.height) font.pixelSize: Config.bigIconSize(bar.height)
font.family: Config.font font.family: Config.font
color: Colors.secondaryContainer_fg color: Colors.secondaryContainer_fg
} }
// Image{
// id: iconImage
// source: Quickshell.iconPath("discord")
// width: Config.bigIconSize(notificationButton.height)
// height: Config.bigIconSize(notificationButton.height)
// }
} }
} }
+72 -8
View File
@@ -31,8 +31,7 @@ PanelWindow {
property var grouped: { property var grouped: {
const vals = NotificationS.trackedNotifications.values const vals = NotificationS.trackedNotifications.values
const len = vals.length if (!vals || vals.length === 0) return []
if (len < 0) return []
let map = {} let map = {}
for (let i = 0; i < vals.length; ++i) { for (let i = 0; i < vals.length; ++i) {
const n = vals[i] const n = vals[i]
@@ -65,21 +64,86 @@ PanelWindow {
expandedGroups = ({}) expandedGroups = ({})
} }
function closeNHistory() {
// Centralized close via singleton signal fixes previous ReferenceError
// where this file tried to access notifificationHistoryLoader.id directly
// (that id lives in NotificationHistoryLoader.qml and is not in scope here).
console.log("[NotificationHistory] closeNHistory() requested")
NotificationS.historyHovered = false
NotificationS.closeHistoryRequested()
}
// ── Hover tracking via NotificationServer ──
// History reports its hover state to the singleton; button does the same.
// Timer auto-closes when neither is hovered for historyAutoCloseDelay ms.
property bool _allowAutoClose: false
HoverHandler {
id: historyHoverHandler
onHoveredChanged: NotificationS.historyHovered = hovered
}
// Fallback MouseArea that doesn't steal clicks (acceptedButtons: NoButton)
// Ensures hover is detected even if HoverHandler is missed on PanelWindow boundaries.
// Placed with high z but NoButton so it doesn't block inner MouseAreas.
MouseArea {
id: historyHoverMouseArea
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.NoButton
propagateComposedEvents: true
onContainsMouseChanged: NotificationS.historyHovered = containsMouse
}
// Grace timer: don't allow auto-close immediately after opening,
// giving keyboard users time to move mouse to history.
Timer {
id: graceTimer
interval: 500
running: root.visible && !_allowAutoClose
repeat: false
onTriggered: _allowAutoClose = true
}
Timer {
id: hoverCloseTimer
interval: NotificationS.historyAutoCloseDelay
// Only run when panel is visible, grace passed, and neither element is hovered
running: root.visible && _allowAutoClose && !NotificationS.historyHovered && !NotificationS.buttonHovered
repeat: false
onTriggered: {
if (!NotificationS.historyHovered && !NotificationS.buttonHovered) {
console.log("[NotificationHistory] Auto-closing after hover timeout (" + interval + "ms)")
root.closeNHistory()
}
}
}
// Ensure hover state is cleared when window hides/destroys
onVisibleChanged: {
if (!visible) {
NotificationS.historyHovered = false
_allowAutoClose = false
}
}
Component.onDestruction: NotificationS.historyHovered = false
FocusScope { FocusScope {
id: nHistoryScope id: nHistoryScope
anchors.fill: parent
focus: true focus: true
Keys.onReleased: function(event) { Keys.onReleased: function(event) {
if (event.key === Qt.Key_Escape) { if (event.key === Qt.Key_Escape) {
console.log("Pressed Escape, closing JLearner") console.log("Pressed Escape, closing NotificationHistory")
notifificationHistoryLoader.active = false root.closeNHistory()
} }
} }
Rectangle { Rectangle {
anchors.fill: parent anchors.fill: parent
color: "lightblue" color: "lightblue"
visible: false // debug helper, keep hidden in normal use
} }
} }
@@ -154,7 +218,7 @@ PanelWindow {
spacing: Config.screen_spacing_fun(screen.height) spacing: Config.screen_spacing_fun(screen.height)
Text{ Text{
text: NotificationS.soundEnabled ? "󰂚" : "󰂛" text: NotificationS.notificationPopupEnabled ? "󰂚" : "󰂛"
font.family: Config.font font.family: Config.font
font.pixelSize: topBar.height*0.75 font.pixelSize: topBar.height*0.75
color: textColor color: textColor
@@ -166,7 +230,7 @@ PanelWindow {
implicitWidth: notificationVolumeSwitch.height*2 implicitWidth: notificationVolumeSwitch.height*2
implicitHeight: topBar.height*0.75 implicitHeight: topBar.height*0.75
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
checked: NotificationS.soundEnabled checked: NotificationS.notificationPopupEnabled
indicator: Rectangle { indicator: Rectangle {
implicitWidth: notificationVolumeSwitch.width implicitWidth: notificationVolumeSwitch.width
@@ -203,8 +267,8 @@ PanelWindow {
} }
onCheckedChanged: { onCheckedChanged: {
NotificationS.soundEnabled = notificationVolumeSwitch.checked NotificationS.notificationPopupEnabled = notificationVolumeSwitch.checked
console.log("NotificationS.soundEnabled:", NotificationS.soundEnabled) console.log("NotificationS.notificationPopupEnabled:", NotificationS.notificationPopupEnabled)
} }
} }
@@ -15,10 +15,20 @@ Item { // container, invisible
active: false active: false
NotificationHistory { NotificationHistory {
id: nHistory id: nHistory
// closeNHistory() is defined inside NotificationHistory.qml and
// emits NotificationS.closeHistoryRequested(); the loader listens below
// and actually deactivates. Keeping instance id for potential direct access.
}
}
function closeNHistory() { // React to close requests coming from NotificationHistory (hover timer, Escape)
nHistoryLoader.closeNHistory() // or any other component via the singleton signal. This is the single
} // source of truth for deactivating the history panel.
Connections {
target: NotificationS
function onCloseHistoryRequested() {
console.log("[NotificationHistoryLoader] closeHistoryRequested received -> closing")
nHistoryLoader.closeNHistory()
} }
} }
@@ -37,11 +47,19 @@ Item { // container, invisible
} }
function closeNHistory() { function closeNHistory() {
// Reset hover states so timer doesn't immediately re-trigger on next open
NotificationS.historyHovered = false
// buttonHovered is managed by the button itself; don't force false here
// in case button is still hovered when history closes
notifificationHistoryLoader.active = false notifificationHistoryLoader.active = false
} }
function toggleNHistory() { function toggleNHistory() {
notifificationHistoryLoader.active = !notifificationHistoryLoader.active if (notifificationHistoryLoader.active) {
closeNHistory()
} else {
openNHistory()
}
} }
Component.onCompleted: { Component.onCompleted: {
@@ -12,13 +12,24 @@ Item {
property var focusedMonitor: null property var focusedMonitor: null
property var newestNotification: null property var newestNotification: null
property bool soundEnabled: true property bool notificationPopupEnabled: true
property alias trackedNotifications: notificationServer.trackedNotifications property alias trackedNotifications: notificationServer.trackedNotifications
property int notificationNum: trackedNotifications.values.length property int notificationNum: trackedNotifications.values.length
signal notificationReceived(var notification) signal notificationReceived(var notification)
// ── Hover state for history auto-close ──
// Updated by NotificationHistory.qml (PanelWindow hover) and NotificationButton.qml (bar button hover)
property bool historyHovered: false
property bool buttonHovered: false
property int historyAutoCloseDelay: 1000
signal closeHistoryRequested()
// Debug logging for hover transitions
onHistoryHoveredChanged: Config.debug == "Notification" && console.log("[NotificationServer] historyHovered:", historyHovered)
onButtonHoveredChanged: Config.debug == "Notification" && console.log("[NotificationServer] buttonHovered:", buttonHovered)
function correctMonitor(screen) { function correctMonitor(screen) {
return (Hyprland.monitorFor(screen) === focusedMonitor) return (Hyprland.monitorFor(screen) === focusedMonitor)
} }
@@ -83,7 +94,7 @@ Item {
repeat: false repeat: false
onTriggered: { onTriggered: {
if((!notification.lastGeneration && notificationServerState.soundEnabled) || Config.debug == "Notification"){ if((!notification.lastGeneration && notificationServerState.notificationPopupEnabled) || Config.debug == "Notification"){
focusedMonitor = Hyprland.focusedMonitor focusedMonitor = Hyprland.focusedMonitor
notificationReceived(notification) notificationReceived(notification)
notificationSound.play() notificationSound.play()