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