vibed a cached downloader for images
This commit is contained in:
116
src/services/notificationController.cpp
Normal file
116
src/services/notificationController.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "services/notificationController.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "gdkmm/display.h"
|
||||
#include "giomm/listmodel.h"
|
||||
#include "glibmm/main.h"
|
||||
#include "gtk4-layer-shell.h"
|
||||
#include "gtkmm/box.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() {
|
||||
auto display = Gdk::Display::get_default();
|
||||
if (!display) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto monitors = display->get_monitors();
|
||||
if (!monitors) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (guint i = 0; i < monitors->get_n_items(); ++i) {
|
||||
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
|
||||
monitors->get_object(i));
|
||||
|
||||
this->activeMonitors.push_back(monitor);
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationController::showSpotifyNotification(const std::string &title, const std::string &message, const std::string &artwork_url) {
|
||||
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 container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
|
||||
|
||||
if (auto texture = TextureCacheService::getInstance()->getTexture(artwork_url)) {
|
||||
auto img = Gtk::make_managed<Gtk::Image>(texture);
|
||||
// make it larger
|
||||
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>");
|
||||
title_label->set_use_markup(true);
|
||||
title_label->set_halign(Gtk::Align::START);
|
||||
text_box->append(*title_label);
|
||||
|
||||
|
||||
auto message_label = Gtk::make_managed<Gtk::Label>(message);
|
||||
message_label->set_halign(Gtk::Align::START);
|
||||
text_box->append(*message_label);
|
||||
|
||||
container->append(*text_box);
|
||||
|
||||
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::showNotificationOnAllMonitors(const std::string &title, const std::string &message) {
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user