From be89d6faa988f6bd33a66f02746872c649a16180 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 2 Mar 2024 19:12:31 +0100 Subject: [PATCH] notifs: Implement notification dimissing (#4790) --- hyprctl/main.cpp | 3 +++ src/debug/HyprCtl.cpp | 21 +++++++++++++++++++++ src/debug/HyprNotificationOverlay.cpp | 12 ++++++++++++ src/debug/HyprNotificationOverlay.hpp | 1 + 4 files changed, 37 insertions(+) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 02da5f43..e6ae15bb 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -33,6 +33,7 @@ commands: cursorpos decorations devices + dismissnotify dispatch getoption globalshortcuts @@ -388,6 +389,8 @@ int main(int argc, char** argv) { request(fullRequest, 3); else if (fullRequest.contains("/plugin")) request(fullRequest, 1); + else if (fullRequest.contains("/dismissnotify")) + request(fullRequest, 0); else if (fullRequest.contains("/notify")) request(fullRequest, 2); else if (fullRequest.contains("/output")) diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index b3fb0af3..962496f7 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -1430,6 +1430,26 @@ std::string dispatchNotify(eHyprCtlOutputFormat format, std::string request) { return "ok"; } +std::string dispatchDismissNotify(eHyprCtlOutputFormat format, std::string request) { + CVarList vars(request, 0, ' '); + + int amount = -1; + + if (vars.size() > 1) { + const auto AMOUNT = vars[1]; + if (!isNumber(AMOUNT)) + return "invalid arg 1"; + + try { + amount = std::stoi(AMOUNT); + } catch (std::exception& e) { return "invalid arg 1"; } + } + + g_pHyprNotificationOverlay->dismissNotifications(amount); + + return "ok"; +} + CHyprCtl::CHyprCtl() { registerCommand(SHyprCtlCommand{"workspaces", true, workspacesRequest}); registerCommand(SHyprCtlCommand{"workspacerules", true, workspaceRulesRequest}); @@ -1453,6 +1473,7 @@ CHyprCtl::CHyprCtl() { registerCommand(SHyprCtlCommand{"reload", false, reloadRequest}); registerCommand(SHyprCtlCommand{"plugin", false, dispatchPlugin}); registerCommand(SHyprCtlCommand{"notify", false, dispatchNotify}); + registerCommand(SHyprCtlCommand{"dismissnotify", false, dispatchDismissNotify}); registerCommand(SHyprCtlCommand{"setprop", false, dispatchSetProp}); registerCommand(SHyprCtlCommand{"seterror", false, dispatchSeterror}); registerCommand(SHyprCtlCommand{"switchxkblayout", false, switchXKBLayoutRequest}); diff --git a/src/debug/HyprNotificationOverlay.cpp b/src/debug/HyprNotificationOverlay.cpp index 2968134b..98c68f1f 100644 --- a/src/debug/HyprNotificationOverlay.cpp +++ b/src/debug/HyprNotificationOverlay.cpp @@ -50,6 +50,18 @@ void CHyprNotificationOverlay::addNotification(const std::string& text, const CC } } +void CHyprNotificationOverlay::dismissNotifications(const int amount) { + if (amount == -1) + m_dNotifications.clear(); + else { + const int AMT = std::min(amount, static_cast(m_dNotifications.size())); + + for (int i = 0; i < AMT; ++i) { + m_dNotifications.pop_front(); + } + } +} + CBox CHyprNotificationOverlay::drawNotifications(CMonitor* pMonitor) { static constexpr auto ANIM_DURATION_MS = 600.0; static constexpr auto ANIM_LAG_MS = 100.0; diff --git a/src/debug/HyprNotificationOverlay.hpp b/src/debug/HyprNotificationOverlay.hpp index 7a883f15..ef81b014 100644 --- a/src/debug/HyprNotificationOverlay.hpp +++ b/src/debug/HyprNotificationOverlay.hpp @@ -41,6 +41,7 @@ class CHyprNotificationOverlay { void draw(CMonitor* pMonitor); void addNotification(const std::string& text, const CColor& color, const float timeMs, const eIcons icon = ICON_NONE); + void dismissNotifications(const int amount); bool hasAny(); private: