From 067df84388be41b7b2352918060c2e9d88717740 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 6 Mar 2024 22:20:26 +0100 Subject: [PATCH] notify: Add custom fontsize support for notifications (#4981) * Add custom fontsize support for notifications * Remove debug stuff * Use original default font size * Handle fontsize as keyword arg * Use CVarList::join instead of for loop * Use size_t for msgidx --- src/debug/HyprCtl.cpp | 24 +++++++++++++++----- src/debug/HyprNotificationOverlay.cpp | 32 +++++++++++++-------------- src/debug/HyprNotificationOverlay.hpp | 7 +++--- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 10302b08..332e3969 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -1455,17 +1455,29 @@ std::string dispatchNotify(eHyprCtlOutputFormat format, std::string request) { time = std::stoi(TIME); } catch (std::exception& e) { return "invalid arg 2"; } - CColor color = configStringToInt(vars[3]); + CColor color = configStringToInt(vars[3]); - std::string message = ""; + size_t msgidx = 4; + float fontsize = 13.f; + if (vars[msgidx].length() > 9 && vars[msgidx].compare(0, 10, "fontsize:")) { + const auto FONTSIZE = vars[msgidx].substr(9); - for (size_t i = 4; i < vars.size(); ++i) { - message += vars[i] + " "; + if (!isNumber(FONTSIZE, true)) + return "invalid fontsize kwarg"; + + try { + fontsize = std::stoi(FONTSIZE); + } catch (std::exception& e) { return "invalid fontsize karg"; } + + ++msgidx; } - message.pop_back(); + if (vars.size() <= msgidx) + return "not enough args"; - g_pHyprNotificationOverlay->addNotification(message, color, time, (eIcons)icon); + const auto MESSAGE = vars.join(" ", msgidx); + + g_pHyprNotificationOverlay->addNotification(MESSAGE, color, time, (eIcons)icon, fontsize); return "ok"; } diff --git a/src/debug/HyprNotificationOverlay.cpp b/src/debug/HyprNotificationOverlay.cpp index 98c68f1f..ced27814 100644 --- a/src/debug/HyprNotificationOverlay.cpp +++ b/src/debug/HyprNotificationOverlay.cpp @@ -36,14 +36,15 @@ CHyprNotificationOverlay::CHyprNotificationOverlay() { m_szIconFontName = fonts.substr(COLON + 2, LASTCHAR - (COLON + 2)); } -void CHyprNotificationOverlay::addNotification(const std::string& text, const CColor& color, const float timeMs, const eIcons icon) { +void CHyprNotificationOverlay::addNotification(const std::string& text, const CColor& color, const float timeMs, const eIcons icon, const float fontSize) { const auto PNOTIF = m_dNotifications.emplace_back(std::make_unique()).get(); PNOTIF->text = text; PNOTIF->color = color == CColor(0) ? ICONS_COLORS[icon] : color; PNOTIF->started.reset(); - PNOTIF->timeMs = timeMs; - PNOTIF->icon = icon; + PNOTIF->timeMs = timeMs; + PNOTIF->icon = icon; + PNOTIF->fontSize = fontSize; for (auto& m : g_pCompositor->m_vMonitors) { g_pCompositor->scheduleFrameForMonitor(m.get()); @@ -73,28 +74,25 @@ CBox CHyprNotificationOverlay::drawNotifications(CMonitor* pMonitor) { float offsetY = 10; float maxWidth = 0; - const auto SCALE = pMonitor->scale; - const auto FONTSIZE = std::clamp((int)(13.f * ((pMonitor->vecPixelSize.x * SCALE) / 1920.f)), 8, 40); + const auto SCALE = pMonitor->scale; const auto MONSIZE = pMonitor->vecPixelSize; cairo_text_extents_t cairoExtents; int iconW = 0, iconH = 0; - PangoLayout* pangoLayout; - PangoFontDescription* pangoFD; - - pangoLayout = pango_cairo_create_layout(m_pCairo); - pangoFD = pango_font_description_from_string(("Sans " + std::to_string(FONTSIZE * ICON_SCALE)).c_str()); - pango_layout_set_font_description(pangoLayout, pangoFD); - cairo_select_font_face(m_pCairo, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(m_pCairo, FONTSIZE); const auto PBEZIER = g_pAnimationManager->getBezier("default"); for (auto& notif : m_dNotifications) { - const auto ICONPADFORNOTIF = notif->icon == ICON_NONE ? 0 : ICON_PAD; + const auto ICONPADFORNOTIF = notif->icon == ICON_NONE ? 0 : ICON_PAD; + const auto FONTSIZE = std::clamp((int)(notif->fontSize * ((pMonitor->vecPixelSize.x * SCALE) / 1920.f)), 8, 40); + + PangoLayout* pangoLayout = pango_cairo_create_layout(m_pCairo); + PangoFontDescription* pangoFD = pango_font_description_from_string(("Sans " + std::to_string(FONTSIZE * ICON_SCALE)).c_str()); + pango_layout_set_font_description(pangoLayout, pangoFD); + cairo_set_font_size(m_pCairo, FONTSIZE); // first rect (bg, col) const float FIRSTRECTANIMP = @@ -174,10 +172,10 @@ CBox CHyprNotificationOverlay::drawNotifications(CMonitor* pMonitor) { if (maxWidth < NOTIFSIZE.x) maxWidth = NOTIFSIZE.x; - } - pango_font_description_free(pangoFD); - g_object_unref(pangoLayout); + pango_font_description_free(pangoFD); + g_object_unref(pangoLayout); + } // cleanup notifs std::erase_if(m_dNotifications, [](const auto& notif) { return notif->started.getMillis() > notif->timeMs; }); diff --git a/src/debug/HyprNotificationOverlay.hpp b/src/debug/HyprNotificationOverlay.hpp index ef81b014..5599b71b 100644 --- a/src/debug/HyprNotificationOverlay.hpp +++ b/src/debug/HyprNotificationOverlay.hpp @@ -31,8 +31,9 @@ struct SNotification { std::string text = ""; CColor color; CTimer started; - float timeMs = 0; - eIcons icon = ICON_NONE; + float timeMs = 0; + eIcons icon = ICON_NONE; + float fontSize = 13.f; }; class CHyprNotificationOverlay { @@ -40,7 +41,7 @@ class CHyprNotificationOverlay { CHyprNotificationOverlay(); void draw(CMonitor* pMonitor); - void addNotification(const std::string& text, const CColor& color, const float timeMs, const eIcons icon = ICON_NONE); + void addNotification(const std::string& text, const CColor& color, const float timeMs, const eIcons icon = ICON_NONE, const float fontSize = 13.f); void dismissNotifications(const int amount); bool hasAny();