refactor notifications
This commit is contained in:
@@ -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