nice spotify notification

This commit is contained in:
2026-02-01 01:53:47 +01:00
parent caca94bc6a
commit dc325834c7
7 changed files with 256 additions and 167 deletions

View File

@@ -2,19 +2,27 @@
#include <memory>
#include "services/dbus/mpris.hpp"
#include "services/textureCache.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"
#include "services/textureCache.hpp"
std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
NotificationController::NotificationController() {
if (NotificationController::instance) {
throw std::runtime_error("use getInstance()!");
}
auto display = Gdk::Display::get_default();
if (!display) {
return;
@@ -33,82 +41,110 @@ NotificationController::NotificationController() {
}
}
void NotificationController::showSpotifyNotification(const std::string &title, const std::string &message, const std::string &artwork_url) {
void NotificationController::showSpotifyNotification(MprisController::MprisPlayer2Message mpris) {
for (const auto &monitor : this->activeMonitors) {
auto win = new Gtk::Window();
win->set_title(title);
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");
auto win = std::make_shared<Gtk::Window>();
win->set_title(mpris.title);
this->baseWindowSetup(win, monitor);
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
if (auto texture = TextureCacheService::getInstance()->getTexture(artwork_url)) {
if (auto texture = TextureCacheService::getInstance()->getTexture(mpris.artwork_url)) {
auto img = Gtk::make_managed<Gtk::Image>(texture);
// make it larger
img->set_pixel_size(64);
img->set_pixel_size(64);
container->append(*img);
}
auto text_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
text_box->set_halign(Gtk::Align::CENTER);
text_box->set_valign(Gtk::Align::CENTER);
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + title + "</b>");
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);
text_box->append(*title_label);
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 message_label = Gtk::make_managed<Gtk::Label>(message);
message_label->set_halign(Gtk::Align::START);
text_box->append(*message_label);
auto buttonBox = Gtk::make_managed<Gtk::CenterBox>();
buttonBox->add_css_class("notification-button-box");
buttonBox->set_hexpand(true);
container->append(*text_box);
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();
// Auto close after 3 seconds for demo purposes
Glib::signal_timeout().connect([win]() {
win->close();
delete win;
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 = new Gtk::Window();
auto win = std::make_shared<Gtk::Window>();
win->set_title(title);
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);
this->baseWindowSetup(win, monitor);
win->add_css_class("notification-popup");
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();
delete win;
return false; // Don't repeat
},
3000);