Compare commits

..

2 Commits

Author SHA1 Message Date
178a4451d4 refactor notifications 2026-02-01 16:10:42 +01:00
17aef717b7 persitent cache for image download 2026-02-01 12:06:48 +01:00
21 changed files with 610 additions and 235 deletions

View File

@@ -28,7 +28,9 @@ target_sources(bar_lib
src/widgets/clock.cpp src/widgets/clock.cpp
src/widgets/date.cpp src/widgets/date.cpp
src/widgets/notification.cpp src/widgets/notification/baseNotification.cpp
src/widgets/notification/notification.cpp
src/widgets/notification/spotifyNotification.cpp
src/widgets/volumeWidget.cpp src/widgets/volumeWidget.cpp
src/widgets/webWidget.cpp src/widgets/webWidget.cpp
@@ -54,28 +56,35 @@ add_executable(bar main.cpp)
target_link_libraries(bar bar_lib ${GTKMM_LIBRARIES} ${LAYERSHELL_LIBRARIES} ${WEBKIT_LIBRARIES} ${CURL_LIBRARIES} nlohmann_json::nlohmann_json) target_link_libraries(bar bar_lib ${GTKMM_LIBRARIES} ${LAYERSHELL_LIBRARIES} ${WEBKIT_LIBRARIES} ${CURL_LIBRARIES} nlohmann_json::nlohmann_json)
# Copy `resources/bar.css` into the build directory when it changes # Copy all CSS files in resources/ into build and user config directories
set(RES_SRC "${CMAKE_CURRENT_SOURCE_DIR}/resources/bar.css") file(GLOB RES_CSS_FILES CONFIGURE_DEPENDS
set(RES_DST "${CMAKE_CURRENT_BINARY_DIR}/resources/bar.css") "${CMAKE_CURRENT_SOURCE_DIR}/resources/*.css"
set(USER_CONFIG_CSS "$ENV{HOME}/.config/bar/bar.css")
add_custom_command(
OUTPUT ${RES_DST}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/resources"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${RES_SRC}" "${RES_DST}"
DEPENDS "${RES_SRC}"
COMMENT "Copy resources/bar.css to build directory"
VERBATIM
) )
set(RES_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/resources")
set(RES_USER_DIR "$ENV{HOME}/.config/bar")
add_custom_command( set(RES_DST_FILES "")
OUTPUT ${USER_CONFIG_CSS} set(USER_DST_FILES "")
COMMAND ${CMAKE_COMMAND} -E make_directory "$ENV{HOME}/.config/bar"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${RES_SRC}" "${USER_CONFIG_CSS}" foreach(RES_FILE IN LISTS RES_CSS_FILES)
DEPENDS "${RES_SRC}" get_filename_component(RES_NAME "${RES_FILE}" NAME)
COMMENT "Copy resources/bar.css to ~/.config/bar/bar.css" set(RES_DST "${RES_BUILD_DIR}/${RES_NAME}")
set(USER_DST "${RES_USER_DIR}/${RES_NAME}")
list(APPEND RES_DST_FILES "${RES_DST}")
list(APPEND USER_DST_FILES "${USER_DST}")
add_custom_command(
OUTPUT "${RES_DST}" "${USER_DST}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${RES_BUILD_DIR}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${RES_USER_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${RES_FILE}" "${RES_DST}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${RES_FILE}" "${USER_DST}"
DEPENDS "${RES_FILE}"
COMMENT "Copy ${RES_NAME} to build and config directories"
VERBATIM VERBATIM
) )
endforeach()
add_custom_target(copy_resources ALL DEPENDS ${RES_DST} ${USER_CONFIG_CSS}) add_custom_target(copy_resources ALL DEPENDS ${RES_DST_FILES} ${USER_DST_FILES})
add_dependencies(bar copy_resources) add_dependencies(bar copy_resources)

View File

@@ -0,0 +1,33 @@
#pragma once
#include <cstdint>
#include <functional>
#include <string>
#include <map>
#include <vector>
#include "glibmm/variant.h"
struct MprisPlayer2Message {
std::string title;
std::string artist;
std::string artwork_url;
std::function<void()> play_pause;
std::function<void()> next;
std::function<void()> previous;
};
struct NotifyMessage {
std::string app_name;
uint32_t replaces_id;
std::string app_icon;
std::string summary;
std::string body;
std::vector<std::string> actions;
std::map<std::string, Glib::VariantBase> hints;
int32_t expire_timeout;
// Callback to invoke when an action is triggered
std::function<void(const std::string& action_id)> on_action;
};

View File

@@ -1,20 +1,10 @@
#pragma once #pragma once
#include <giomm.h> #include <giomm.h>
#include <string>
#include <vector> #include <vector>
class MprisController { class MprisController {
public: public:
struct MprisPlayer2Message {
std::string title;
std::string artist;
std::string artwork_url;
std::function<void()> play_pause;
std::function<void()> next;
std::function<void()> previous;
};
MprisController(); MprisController();

View File

@@ -3,11 +3,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <giomm.h> #include <giomm.h>
#include <gtkmm.h> #include <gtkmm.h>
#include <memory>
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include <vector>
#include "gdkmm/monitor.h"
#include "giomm/dbusconnection.h" #include "giomm/dbusconnection.h"
#include "giomm/dbusownname.h" #include "giomm/dbusownname.h"
#include "glib.h" #include "glib.h"
@@ -43,7 +40,6 @@ const Glib::ustring introspection_xml = R"(
)"; )";
class NotificationService { class NotificationService {
public: public:
NotificationService() : notificationIdCounter(1) { NotificationService() : notificationIdCounter(1) {
Gio::DBus::own_name( Gio::DBus::own_name(

View File

@@ -2,10 +2,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "services/dbus/mpris.hpp" #include "services/dbus/messages.hpp"
#include "gdkmm/monitor.h" #include "gdkmm/monitor.h"
#include "gtkmm/window.h"
class NotificationController { class NotificationController {
static std::shared_ptr<NotificationController> instance; static std::shared_ptr<NotificationController> instance;
@@ -17,11 +17,10 @@ class NotificationController {
return NotificationController::instance; return NotificationController::instance;
} }
void showSpotifyNotification(MprisController::MprisPlayer2Message mpris); void showSpotifyNotification(MprisPlayer2Message mpris);
void showNotificationOnAllMonitors(const std::string &title, const std::string &message); void showNotificationOnAllMonitors(NotifyMessage notify);
private: private:
NotificationController(); NotificationController();
std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors; std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors;
void baseWindowSetup(std::shared_ptr<Gtk::Window> win, std::shared_ptr<Gdk::Monitor> monitor);
}; };

View File

@@ -11,6 +11,7 @@ class TextureCacheService {
static TextureCacheService *getInstance(); static TextureCacheService *getInstance();
Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url); Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url);
void pruneCache();
private: private:
TextureCacheService() = default; TextureCacheService() = default;

View File

@@ -1,10 +0,0 @@
#pragma once
#include <giomm.h>
#include <gtkmm.h>
#include <memory>
#include "gdkmm/monitor.h"
class NotificationWidget {
public:
NotificationWidget(std::shared_ptr<Gdk::Monitor> monitor, const Glib::ustring &title, const Glib::ustring &message);
};

View File

@@ -0,0 +1,24 @@
#pragma once
#include <filesystem>
#include <string>
#include "helpers/system.hpp"
#include "gdkmm/monitor.h"
#include "gtk4-layer-shell.h"
#include "gtkmm/cssprovider.h"
#include "gtkmm/window.h"
#define DEFAULT_NOTIFICATION_TIMEOUT 4000
class BaseNotification : public Gtk::Window {
public:
BaseNotification(std::shared_ptr<Gdk::Monitor> monitor);
virtual ~BaseNotification() = default;
private:
void ensure_notification_css_loaded();
};

View File

@@ -0,0 +1,11 @@
#pragma once
#include "services/dbus/messages.hpp"
#include "widgets/notification/baseNotification.hpp"
class NotificationWindow : public BaseNotification {
public:
NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage message);
virtual ~NotificationWindow() = default;
};

View File

@@ -0,0 +1,16 @@
#pragma once
#include <memory>
#include <type_traits>
#include "services/dbus/messages.hpp"
#include "widgets/notification/baseNotification.hpp"
#include "gtkmm/box.h"
#include "gtkmm/centerbox.h"
class SpotifyNotification : public BaseNotification {
public:
SpotifyNotification(std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message message);
virtual ~SpotifyNotification() = default;
private:
std::unique_ptr<Gtk::CenterBox> createButtonBox(MprisPlayer2Message mpris);
};

View File

@@ -3,6 +3,14 @@
all: unset; all: unset;
} }
/* variable */
:root {
--icon-font-material: "Material Icons", sans-serif;
--icon-font-awesome: "Font Awesome 7 Free", sans-serif;
--text-font: "Hack Nerd Font Mono", sans-serif;
--text-font-mono: "Hack Nerd Font Mono", monospace;
}
window { window {
background-color: #191919c6; background-color: #191919c6;
color: #ffffff; color: #ffffff;
@@ -11,15 +19,12 @@ window {
padding-top: 2px; padding-top: 2px;
padding-bottom: 2px; padding-bottom: 2px;
font-size: 14px; font-size: 14px;
font-family: font-family: var(--text-font);
"Hack Nerd Font Mono", "Font Awesome 7 Brands", "Font Awesome 7 Free",
sans-serif;
} }
popover { popover {
margin-top: 4px; margin-top: 4px;
font-family: font-family: var(--text-font);
"Hack Nerd Font Mono", "Material Icons", "Font Awesome 7 Free", sans-serif;
padding: 6px; padding: 6px;
border-radius: 8px; border-radius: 8px;
@@ -38,20 +43,15 @@ tooltip {
} }
button { button {
font-family: "Material Icons", sans-serif;
font-size: 20px; font-size: 20px;
padding: 3px 6px;
} }
#spacer { #spacer {
font-weight: 900; font-weight: 900;
padding: 0 5px; padding: 0 5px;
text-shadow: 0 0 5px #ffffffaa; text-shadow: 0 0 5px #ffffffaa;
}
.button {
padding: 4px 8px;
border-radius: 4px; border-radius: 4px;
font-family: "Material Icons", sans-serif;
} }
.button:hover { .button:hover {
@@ -83,12 +83,12 @@ button {
.workspace-pill-alive { .workspace-pill-alive {
background-color: rgba(255, 255, 255, 0.153); background-color: rgba(255, 255, 255, 0.153);
color: #ffffff;
} }
.workspace-pill-presenting { .workspace-pill-presenting {
background-color: #666666; background-color: #666666;
color: #ffffff; color: #ffffff;
/* animation: workspace-updown 1.2s ease-in-out infinite; */
} }
.workspace-pill-focused { .workspace-pill-focused {
@@ -111,7 +111,6 @@ button {
animation: workspace-updown 1.2s ease-in-out infinite; animation: workspace-updown 1.2s ease-in-out infinite;
margin-left: -4px; margin-left: -4px;
margin-top: 4px; margin-top: 4px;
font-size: 12px;
} }
.workspace-pill-seven { .workspace-pill-seven {
@@ -119,7 +118,6 @@ button {
animation-delay: 0.6s; animation-delay: 0.6s;
margin-right: -4px; margin-right: -4px;
margin-top: 4px; margin-top: 4px;
font-size: 12px;
} }
@keyframes workspace-updown { @keyframes workspace-updown {
@@ -149,15 +147,3 @@ button {
opacity: 1; opacity: 1;
} }
} }
.notification-popup {
border-radius: 8px;
padding: 8px 12px;
background: rgba(30, 30, 30, 0.948);
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
border: 1px solid rgba(80, 80, 80, 0.8);
font-size: 14px;
}
.notification-button-box {
}

View File

@@ -0,0 +1,42 @@
:root {
--icon-font-material: "Material Icons", sans-serif;
--icon-font-awesome: "Font Awesome 7 Free", sans-serif;
--text-font: "Hack Nerd Font Mono", sans-serif;
--text-font-mono: "Hack Nerd Font Mono", monospace;
--color-notification-bg: rgba(30, 30, 30, 0.95);
--color-border: rgba(80, 80, 80, 0.8);
}
.notification-popup {
border-radius: 8px;
padding: 8px 12px;
background: var(--color-notification-bg);
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
border: 1px solid var(--color-border);
font-size: 14px;
}
.notification-button-box {
border-top: 1px solid var(--color-border);
padding-top: 6px;
}
.notification-button {
background-color: #444444;
color: #ffffff;
padding: 4px 8px;
border-radius: 6px;
margin-right: 6px;
font-family: var(--text-font-mono);
font-size: 13px;
}
.notification-icon-button {
font-family: var(--icon-font-material);
font-size: 16px;
}
.notification-button:hover {
background-color: #555555;
}

View File

@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include "services/dbus/notification.hpp" #include "services/dbus/notification.hpp"
#include "services/dbus/mpris.hpp" #include "services/dbus/mpris.hpp"
#include "services/textureCache.hpp"
App::App() { App::App() {
this->app = Gtk::Application::create("org.example.mybar"); this->app = Gtk::Application::create("org.example.mybar");
@@ -43,6 +44,7 @@ App::App() {
} }
void App::setupServices() { void App::setupServices() {
TextureCacheService::getInstance()->pruneCache();
this->trayService->start(); this->trayService->start();
} }

View File

@@ -1,4 +1,5 @@
#include "services/dbus/mpris.hpp" #include "services/dbus/mpris.hpp"
#include "services/dbus/messages.hpp"
#include <iostream> #include <iostream>
#include <map> #include <map>
@@ -94,7 +95,7 @@ void MprisController::launchNotification() {
auto notifactionController = NotificationController::getInstance(); auto notifactionController = NotificationController::getInstance();
MprisController::MprisPlayer2Message mpris; MprisPlayer2Message mpris;
mpris.title = StringHelper::trimToSize(title, 30); mpris.title = StringHelper::trimToSize(title, 30);
mpris.artist = StringHelper::trimToSize(artist, 30); mpris.artist = StringHelper::trimToSize(artist, 30);
mpris.artwork_url = artwork_url; mpris.artwork_url = artwork_url;

View File

@@ -1,8 +1,11 @@
#include "services/dbus/notification.hpp" #include "services/dbus/notification.hpp"
#include <iostream> #include <iostream>
#include "services/notificationController.hpp" #include "services/notificationController.hpp"
#include "widgets/notification.hpp"
#include "glib.h"
#include "glibconfig.h"
void NotificationService::onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name) { void NotificationService::onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name) {
std::cout << "Acquired bus name: " << name << std::endl; std::cout << "Acquired bus name: " << name << std::endl;
@@ -52,23 +55,61 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase &parame
Glib::VariantBase app_name_var, replaces_id_var, app_icon_var, summary_var, body_var, actions_var, hints_var, timeout_var; Glib::VariantBase app_name_var, replaces_id_var, app_icon_var, summary_var, body_var, actions_var, hints_var, timeout_var;
parameters.get_child(app_name_var, 0); parameters.get_child(app_name_var, 0);
parameters.get_child(replaces_id_var, 1);
parameters.get_child(app_icon_var, 2);
parameters.get_child(summary_var, 3); parameters.get_child(summary_var, 3);
parameters.get_child(body_var, 4); parameters.get_child(body_var, 4);
parameters.get_child(actions_var, 5);
parameters.get_child(hints_var, 6);
parameters.get_child(timeout_var, 7);
Glib::ustring app_name = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(app_name_var).get();
guint32 replaces_id = Glib::VariantBase::cast_dynamic<Glib::Variant<guint32>>(replaces_id_var).get();
Glib::ustring app_icon = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(app_icon_var).get();
Glib::ustring summary = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(summary_var).get(); Glib::ustring summary = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(summary_var).get();
Glib::ustring body = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(body_var).get(); Glib::ustring body = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(body_var).get();
std::vector<Glib::ustring> actions = Glib::VariantBase::cast_dynamic<Glib::Variant<std::vector<Glib::ustring>>>(actions_var).get();
// std::map<Glib::ustring, Glib::VariantBase> hints = Glib::VariantBase::cast_dynamic<Glib::Variant<std::map<Glib::ustring, Glib::VariantBase>>>(hints_var).get();
gint32 expire_timeout = Glib::VariantBase::cast_dynamic<Glib::Variant<gint32>>(timeout_var).get();
std::cout << "Notification Received: " << summary << " - " << body << std::endl; std::cout << "Notification Received: " << summary << " - " << body << std::endl;
createNotificationPopup(summary, body); NotifyMessage notify;
notify.app_name = app_name;
notify.replaces_id = replaces_id;
notify.app_icon = app_icon;
notify.summary = summary;
notify.body = body;
std::vector<std::string> actions_converted;
actions_converted.reserve(actions.size());
for (const auto &a : actions) {
actions_converted.emplace_back(static_cast<std::string>(a));
}
notify.actions = actions_converted;
// notify.hints = hints;
notify.expire_timeout = expire_timeout;
guint id = notificationIdCounter++; guint id = notificationIdCounter++;
// Set up the callback to emit ActionInvoked on D-Bus
Glib::RefPtr<Gio::DBus::Connection> dbus_conn = invocation->get_connection();
notify.on_action = [dbus_conn, id](const std::string &action_id) {
try {
dbus_conn->emit_signal(
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"ActionInvoked",
"", // destination bus name (empty for broadcast)
Glib::VariantContainerBase::create_tuple({Glib::Variant<guint>::create(id),
Glib::Variant<Glib::ustring>::create(action_id)}));
} catch (const std::exception &e) {
std::cerr << "Failed to emit ActionInvoked: " << e.what() << std::endl;
}
};
NotificationController::getInstance()->showNotificationOnAllMonitors(notify);
invocation->return_value(Glib::VariantContainerBase::create_tuple( invocation->return_value(Glib::VariantContainerBase::create_tuple(
Glib::Variant<guint>::create(id))); Glib::Variant<guint>::create(id)));
} }
void NotificationService::createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message) {
auto controller = NotificationController::getInstance();
controller->showNotificationOnAllMonitors(title, message);
}

View File

@@ -2,19 +2,15 @@
#include <memory> #include <memory>
#include "services/dbus/mpris.hpp" #include "services/dbus/messages.hpp"
#include "services/textureCache.hpp" #include "widgets/notification/notification.hpp"
#include "widgets/notification/spotifyNotification.hpp"
#include "gdkmm/display.h" #include "gdkmm/display.h"
#include "giomm/listmodel.h"
#include "glibmm/main.h" #include "glibmm/main.h"
#include "gtk4-layer-shell.h"
#include "gtkmm/box.h"
#include "gtkmm/button.h" #define DEFAULT_NOTIFICATION_TIMEOUT 4000
#include "gtkmm/centerbox.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
#include "gtkmm/window.h"
std::shared_ptr<NotificationController> NotificationController::instance = nullptr; std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
@@ -41,112 +37,34 @@ NotificationController::NotificationController() {
} }
} }
void NotificationController::showSpotifyNotification(MprisController::MprisPlayer2Message mpris) { void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) {
for (const auto &monitor : this->activeMonitors) { for (const auto &monitor : this->activeMonitors) {
auto notification = std::make_shared<SpotifyNotification>(monitor, mpris);
notification->show();
auto win = std::make_shared<Gtk::Window>(); Glib::signal_timeout().connect([notification]() {
win->set_title(mpris.title); notification->close();
this->baseWindowSetup(win, monitor);
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
if (auto texture = TextureCacheService::getInstance()->getTexture(mpris.artwork_url)) {
auto img = Gtk::make_managed<Gtk::Image>(texture);
img->set_pixel_size(64);
container->append(*img);
}
auto rightArea = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
rightArea->set_halign(Gtk::Align::CENTER);
rightArea->set_valign(Gtk::Align::CENTER);
// make sure rigfht area takes the remaining space
rightArea->set_hexpand(true);
// also set a max width
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + mpris.title + "</b>");
title_label->set_use_markup(true);
title_label->set_halign(Gtk::Align::START);
rightArea->append(*title_label);
auto artistLabel = Gtk::make_managed<Gtk::Label>(mpris.artist);
artistLabel->set_halign(Gtk::Align::CENTER);
rightArea->append(*artistLabel);
auto buttonBox = Gtk::make_managed<Gtk::CenterBox>();
buttonBox->add_css_class("notification-button-box");
buttonBox->set_hexpand(true);
auto backButton = Gtk::make_managed<Gtk::Button>("<");
backButton->add_css_class("flat-button");
auto playPauseButton = Gtk::make_managed<Gtk::Button>("=<");
playPauseButton->add_css_class("flat-button");
auto nextButton = Gtk::make_managed<Gtk::Button>(">");
nextButton->add_css_class("flat-button");
backButton->signal_clicked().connect([mpris]() {
if (mpris.previous) {
mpris.previous();
}
});
playPauseButton->signal_clicked().connect([mpris]() {
if (mpris.play_pause) {
mpris.play_pause();
}
});
nextButton->signal_clicked().connect([mpris]() {
if (mpris.next) {
mpris.next();
}
});
buttonBox->set_start_widget(*backButton);
buttonBox->set_center_widget(*playPauseButton);
buttonBox->set_end_widget(*nextButton);
// rightArea->append(*buttonBox);
container->append(*rightArea);
win->set_child(*container);
win->show();
Glib::signal_timeout().connect([win]() {
win->close();
return false; // Don't repeat return false; // Don't repeat
}, },
3000); DEFAULT_NOTIFICATION_TIMEOUT);
} }
} }
void NotificationController::baseWindowSetup(std::shared_ptr<Gtk::Window> win, std::shared_ptr<Gdk::Monitor> monitor) { void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) {
win->set_default_size(300, 100);
gtk_layer_init_for_window(win->gobj());
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
win->add_css_class("notification-popup");
}
void NotificationController::showNotificationOnAllMonitors(const std::string &title, const std::string &message) {
for (const auto &monitor : this->activeMonitors) { for (const auto &monitor : this->activeMonitors) {
auto win = std::make_shared<Gtk::Window>(); auto notification = std::make_shared<NotificationWindow>(monitor, notify);
win->set_title(title);
this->baseWindowSetup(win, monitor); auto timeout = notify.expire_timeout;
notification->show();
// -1 means use default timeout, 0 means never expire
if (timeout <= 0) {
timeout = DEFAULT_NOTIFICATION_TIMEOUT; // default to 3 seconds
}
auto label = Gtk::make_managed<Gtk::Label>(message); Glib::signal_timeout().connect([notification]() {
label->set_use_markup(true); notification->close();
win->set_child(*label);
win->show();
Glib::signal_timeout().connect([win]() {
win->close();
return false; // Don't repeat return false; // Don't repeat
}, },
3000); timeout);
} }
} }

View File

@@ -1,11 +1,75 @@
#include "services/textureCache.hpp" #include "services/textureCache.hpp"
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <curl/curl.h> #include <curl/curl.h>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <vector> #include <vector>
#include "giomm/file.h"
#include "glibmm/bytes.h" #include "glibmm/bytes.h"
namespace { namespace {
#define CACHE_SIZE 128
#define CACHE_AGE 168
constexpr std::uint64_t kFnvOffsetBasis = 14695981039346656037ull;
constexpr std::uint64_t kFnvPrime = 1099511628211ull;
std::string to_hex(std::uint64_t value) {
std::ostringstream stream;
stream << std::hex << std::setw(16) << std::setfill('0') << value;
return stream.str();
}
std::string hash_url(const std::string &url) {
std::uint64_t hash = kFnvOffsetBasis;
for (unsigned char c : url) {
hash ^= c;
hash *= kFnvPrime;
}
return to_hex(hash);
}
std::filesystem::path get_cache_dir() {
const char *xdg_cache = std::getenv("XDG_CACHE_HOME");
if (xdg_cache && *xdg_cache) {
return std::filesystem::path(xdg_cache) / "bar";
}
const char *home = std::getenv("HOME");
if (home && *home) {
return std::filesystem::path(home) / ".cache" / "bar";
}
return std::filesystem::temp_directory_path() / "bar";
}
std::filesystem::path get_cache_path_for_url(const std::string &url) {
auto filename = hash_url(url);
auto last_slash = url.find_last_of('/');
auto last_dot = url.find_last_of('.');
if (last_dot != std::string::npos && (last_slash == std::string::npos || last_dot > last_slash)) {
auto ext = url.substr(last_dot);
if (ext.size() <= 10) {
filename += ext;
}
}
return get_cache_dir() / filename;
}
std::chrono::system_clock::time_point to_system_clock(std::filesystem::file_time_type time) {
return std::chrono::time_point_cast<std::chrono::system_clock::duration>(
time - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now()
);
}
size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) { size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
auto *buffer = static_cast<std::vector<unsigned char> *>(userp); auto *buffer = static_cast<std::vector<unsigned char> *>(userp);
auto total = size * nmemb; auto total = size * nmemb;
@@ -14,7 +78,20 @@ size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
return total; return total;
} }
Glib::RefPtr<Gdk::Texture> download_texture_from_url(const std::string &url) { Glib::RefPtr<Gdk::Texture> load_texture_from_file(const std::filesystem::path &path) {
if (!std::filesystem::exists(path)) {
return {};
}
auto file = Gio::File::create_for_path(path.string());
try {
return Gdk::Texture::create_from_file(file);
} catch (...) {
return {};
}
}
Glib::RefPtr<Gdk::Texture> download_texture_from_url(const std::string &url, const std::filesystem::path &path) {
if (url.empty()) { if (url.empty()) {
return {}; return {};
} }
@@ -38,6 +115,14 @@ Glib::RefPtr<Gdk::Texture> download_texture_from_url(const std::string &url) {
return {}; return {};
} }
std::error_code error;
std::filesystem::create_directories(path.parent_path(), error);
std::ofstream output(path, std::ios::binary | std::ios::trunc);
if (output) {
output.write(reinterpret_cast<const char *>(buffer.data()), static_cast<std::streamsize>(buffer.size()));
}
auto bytes = Glib::Bytes::create(buffer.data(), buffer.size()); auto bytes = Glib::Bytes::create(buffer.data(), buffer.size());
return Gdk::Texture::create_from_bytes(bytes); return Gdk::Texture::create_from_bytes(bytes);
} }
@@ -58,10 +143,81 @@ Glib::RefPtr<Gdk::Texture> TextureCacheService::getTexture(const std::string &ur
return it->second; return it->second;
} }
auto texture = download_texture_from_url(url); auto cache_path = get_cache_path_for_url(url);
auto texture = load_texture_from_file(cache_path);
if (!texture) {
texture = download_texture_from_url(url, cache_path);
}
if (texture) { if (texture) {
cache.emplace(url, texture); cache.emplace(url, texture);
std::error_code error;
std::filesystem::last_write_time(cache_path, std::filesystem::file_time_type::clock::now(), error);
} }
return texture; return texture;
} }
void TextureCacheService::pruneCache() {
std::error_code error;
auto cache_dir = get_cache_dir();
std::filesystem::create_directories(cache_dir, error);
std::vector<std::pair<std::filesystem::path, std::uintmax_t>> files;
std::uintmax_t total_size = 0;
auto now = std::chrono::system_clock::now();
auto max_age = std::chrono::hours(CACHE_AGE);
for (const auto &entry : std::filesystem::directory_iterator(cache_dir, error)) {
if (error || !entry.is_regular_file()) {
continue;
}
auto path = entry.path();
auto last_write = entry.last_write_time(error);
if (!error) {
auto age = now - to_system_clock(last_write);
if (age > max_age) {
std::filesystem::remove(path, error);
continue;
}
}
auto size = entry.file_size(error);
if (!error) {
total_size += size;
files.emplace_back(path, size);
}
}
auto max_size = static_cast<std::uintmax_t>(CACHE_SIZE) * 1024ull * 1024ull;
if (total_size <= max_size) {
return;
}
std::sort(files.begin(), files.end(), [&](const auto &left, const auto &right) {
std::error_code left_error;
std::error_code right_error;
auto left_time = std::filesystem::last_write_time(left.first, left_error);
auto right_time = std::filesystem::last_write_time(right.first, right_error);
if (left_error || right_error) {
return left.first.string() < right.first.string();
}
return left_time < right_time;
});
for (const auto &item : files) {
if (total_size <= max_size) {
break;
}
std::filesystem::remove(item.first, error);
if (!error) {
if (total_size >= item.second) {
total_size -= item.second;
} else {
total_size = 0;
}
}
}
}

View File

@@ -1,34 +0,0 @@
#include "widgets/notification.hpp"
#include "gtk4-layer-shell.h"
NotificationWidget::NotificationWidget(std::shared_ptr<Gdk::Monitor> monitor, const Glib::ustring &title, const Glib::ustring &message) {
if (!monitor) return;
auto win = new Gtk::Window();
win->set_title(title);
win->set_default_size(300, 100);
auto label = Gtk::make_managed<Gtk::Label>(message);
label->set_use_markup(true);
win->set_child(*label);
gtk_layer_init_for_window(win->gobj());
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
win->add_css_class("notification-popup");
win->show();
// Auto close after 3 seconds for demo purposes
Glib::signal_timeout().connect([win]() {
win->close();
delete win;
return false; // Don't repeat
},
3000);
}

View File

@@ -0,0 +1,51 @@
#include "widgets/notification/baseNotification.hpp"
#include <filesystem>
#include <string>
#include "helpers/system.hpp"
#include "gdkmm/monitor.h"
#include "gtk4-layer-shell.h"
#include "gtkmm/cssprovider.h"
BaseNotification::BaseNotification(std::shared_ptr<Gdk::Monitor> monitor) {
ensure_notification_css_loaded();
set_default_size(300, 100);
gtk_layer_init_for_window(gobj());
gtk_layer_set_monitor(gobj(), monitor->gobj());
gtk_layer_set_layer(gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
gtk_layer_set_anchor(gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
gtk_layer_set_anchor(gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
gtk_layer_set_margin(gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
gtk_layer_set_margin(gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, 2);
add_css_class("notification-popup");
}
void BaseNotification::ensure_notification_css_loaded() {
static bool loaded = false;
if (loaded) {
return;
}
auto css_provider = Gtk::CssProvider::create();
std::string css_path = "resources/notification.css";
const char *home = std::getenv("HOME");
if (home) {
std::filesystem::path config_path =
std::filesystem::path(home) / ".config/bar/notification.css";
if (std::filesystem::exists(config_path)) {
css_path = config_path.string();
}
}
const std::string css = SystemHelper::read_file_to_string(css_path);
css_provider->load_from_data(css);
Gtk::StyleContext::add_provider_for_display(
Gdk::Display::get_default(), css_provider,
GTK_STYLE_PROVIDER_PRIORITY_USER + 2);
loaded = true;
}

View File

@@ -0,0 +1,48 @@
#include "widgets/notification/notification.hpp"
#include "gtkmm/box.h"
#include "gtkmm/button.h"
#include "gtkmm/label.h"
NotificationWindow::NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage notify) : BaseNotification(monitor) {
set_title(notify.summary);
// Main vertical box
auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 8);
// Summary label
auto summary_label = Gtk::make_managed<Gtk::Label>("<b>" + notify.summary + "</b>");
summary_label->set_use_markup(true);
summary_label->set_halign(Gtk::Align::START);
vbox->append(*summary_label);
// Body label
auto body_label = Gtk::make_managed<Gtk::Label>(notify.body);
body_label->set_use_markup(true);
body_label->set_halign(Gtk::Align::START);
vbox->append(*body_label);
// If actions exist, add buttons
if (!notify.actions.empty()) {
auto actions_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 6);
for (size_t i = 0; i + 1 < notify.actions.size(); i += 2) {
std::string action_id = notify.actions[i];
std::string action_label = notify.actions[i + 1];
auto btn = Gtk::make_managed<Gtk::Button>();
btn->set_label(action_label);
btn->add_css_class("notification-button");
btn->signal_clicked().connect([this, action_id, cb = notify.on_action]() {
if (cb) {
cb(action_id);
this->close();
}
});
actions_box->append(*btn);
}
vbox->append(*actions_box);
}
set_child(*vbox);
}

View File

@@ -0,0 +1,95 @@
#include "widgets/notification/spotifyNotification.hpp"
#include "services/textureCache.hpp"
#include "gtkmm/box.h"
#include "gtkmm/button.h"
#include "gtkmm/centerbox.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
SpotifyNotification::SpotifyNotification(std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message mpris) : BaseNotification(monitor) {
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
container->set_hexpand(true);
if (auto texture = TextureCacheService::getInstance()->getTexture(mpris.artwork_url)) {
auto img = Gtk::make_managed<Gtk::Image>(texture);
img->set_pixel_size(64);
container->append(*img);
}
auto rightArea = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
rightArea->set_halign(Gtk::Align::FILL);
rightArea->set_valign(Gtk::Align::CENTER);
rightArea->set_hexpand(true);
rightArea->set_size_request(220, -1);
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + mpris.title + "</b>");
title_label->set_use_markup(true);
title_label->set_hexpand(true);
title_label->set_halign(Gtk::Align::CENTER);
title_label->set_ellipsize(Pango::EllipsizeMode::END);
auto artistLabel = Gtk::make_managed<Gtk::Label>(mpris.artist);
artistLabel->set_hexpand(true);
artistLabel->set_halign(Gtk::Align::CENTER);
auto buttonBox = createButtonBox(mpris);
rightArea->append(*artistLabel);
rightArea->append(*title_label);
rightArea->append(*buttonBox);
container->append(*rightArea);
set_child(*container);
}
std::unique_ptr<Gtk::CenterBox> SpotifyNotification::createButtonBox(MprisPlayer2Message mpris) {
auto buttonBox = std::make_unique<Gtk::CenterBox>();
buttonBox->add_css_class("notification-button-box");
buttonBox->set_hexpand(true);
buttonBox->set_halign(Gtk::Align::CENTER);
auto backButton = Gtk::make_managed<Gtk::Button>("\ue045");
backButton->add_css_class("notification-icon-button");
backButton->add_css_class("notification-button");
backButton->signal_clicked().connect([this, mpris]() {
if (mpris.previous) {
mpris.previous();
this->close();
}
});
auto playPauseButton = Gtk::make_managed<Gtk::Button>("\ue037");
playPauseButton->add_css_class("notification-icon-button");
playPauseButton->add_css_class("notification-button");
playPauseButton->signal_clicked().connect([playPauseButton, mpris]() {
if (mpris.play_pause) {
mpris.play_pause();
static bool isPlaying = false;
if (isPlaying) {
playPauseButton->set_label("\ue037");
} else {
playPauseButton->set_label("\ue034");
}
isPlaying = !isPlaying;
}
});
auto nextButton = Gtk::make_managed<Gtk::Button>("\ue044");
nextButton->add_css_class("notification-icon-button");
nextButton->add_css_class("notification-button");
nextButton->signal_clicked().connect([this, mpris]() {
if (mpris.next) {
mpris.next();
this->close();
}
});
buttonBox->set_start_widget(*backButton);
buttonBox->set_center_widget(*playPauseButton);
buttonBox->set_end_widget(*nextButton);
return buttonBox;
}