refactor notifications
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "services/dbus/mpris.hpp"
|
||||
#include "services/dbus/messages.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
@@ -94,7 +95,7 @@ void MprisController::launchNotification() {
|
||||
|
||||
auto notifactionController = NotificationController::getInstance();
|
||||
|
||||
MprisController::MprisPlayer2Message mpris;
|
||||
MprisPlayer2Message mpris;
|
||||
mpris.title = StringHelper::trimToSize(title, 30);
|
||||
mpris.artist = StringHelper::trimToSize(artist, 30);
|
||||
mpris.artwork_url = artwork_url;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "services/dbus/notification.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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) {
|
||||
std::cout << "Acquired bus name: " << name << std::endl;
|
||||
@@ -52,23 +55,61 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase ¶me
|
||||
|
||||
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(replaces_id_var, 1);
|
||||
parameters.get_child(app_icon_var, 2);
|
||||
parameters.get_child(summary_var, 3);
|
||||
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 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 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 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;
|
||||
|
||||
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++;
|
||||
// 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(
|
||||
Glib::Variant<guint>::create(id)));
|
||||
}
|
||||
|
||||
void NotificationService::createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message) {
|
||||
|
||||
auto controller = NotificationController::getInstance();
|
||||
controller->showNotificationOnAllMonitors(title, message);
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,15 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "services/dbus/mpris.hpp"
|
||||
#include "services/textureCache.hpp"
|
||||
#include "services/dbus/messages.hpp"
|
||||
#include "widgets/notification/notification.hpp"
|
||||
#include "widgets/notification/spotifyNotification.hpp"
|
||||
|
||||
#include "gdkmm/display.h"
|
||||
#include "giomm/listmodel.h"
|
||||
#include "glibmm/main.h"
|
||||
#include "gtk4-layer-shell.h"
|
||||
#include "gtkmm/box.h"
|
||||
#include "gtkmm/button.h"
|
||||
#include "gtkmm/centerbox.h"
|
||||
#include "gtkmm/image.h"
|
||||
#include "gtkmm/label.h"
|
||||
#include "gtkmm/window.h"
|
||||
|
||||
|
||||
#define DEFAULT_NOTIFICATION_TIMEOUT 4000
|
||||
|
||||
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) {
|
||||
auto notification = std::make_shared<SpotifyNotification>(monitor, mpris);
|
||||
notification->show();
|
||||
|
||||
auto win = std::make_shared<Gtk::Window>();
|
||||
win->set_title(mpris.title);
|
||||
this->baseWindowSetup(win, monitor);
|
||||
Glib::signal_timeout().connect([notification]() {
|
||||
notification->close();
|
||||
return false; // Don't repeat
|
||||
},
|
||||
DEFAULT_NOTIFICATION_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
|
||||
void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) {
|
||||
for (const auto &monitor : this->activeMonitors) {
|
||||
auto notification = std::make_shared<NotificationWindow>(monitor, notify);
|
||||
|
||||
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 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 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();
|
||||
Glib::signal_timeout().connect([notification]() {
|
||||
notification->close();
|
||||
return false; // Don't repeat
|
||||
},
|
||||
3000);
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationController::baseWindowSetup(std::shared_ptr<Gtk::Window> win, std::shared_ptr<Gdk::Monitor> monitor) {
|
||||
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) {
|
||||
auto win = std::make_shared<Gtk::Window>();
|
||||
win->set_title(title);
|
||||
|
||||
this->baseWindowSetup(win, monitor);
|
||||
|
||||
auto label = Gtk::make_managed<Gtk::Label>(message);
|
||||
label->set_use_markup(true);
|
||||
win->set_child(*label);
|
||||
|
||||
win->show();
|
||||
|
||||
Glib::signal_timeout().connect([win]() {
|
||||
win->close();
|
||||
return false; // Don't repeat
|
||||
},
|
||||
3000);
|
||||
timeout);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
51
src/widgets/notification/baseNotification.cpp
Normal file
51
src/widgets/notification/baseNotification.cpp
Normal 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;
|
||||
}
|
||||
48
src/widgets/notification/notification.cpp
Normal file
48
src/widgets/notification/notification.cpp
Normal 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);
|
||||
}
|
||||
95
src/widgets/notification/spotifyNotification.cpp
Normal file
95
src/widgets/notification/spotifyNotification.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user