vibed a cached downloader for images

This commit is contained in:
2026-02-01 00:57:16 +01:00
parent 13278d518a
commit caca94bc6a
10 changed files with 376 additions and 24 deletions

View File

@@ -2,13 +2,15 @@
#include <sigc++/sigc++.h>
#include <vector>
#include "services/notification.hpp"
#include "services/dbus/notification.hpp"
#include "services/dbus/mpris.hpp"
App::App() {
this->app = Gtk::Application::create("org.example.mybar");
this->setupServices();
this->hyprlandService = HyprlandService::getInstance();
this->notificationService = std::make_shared<NotificationService>();
this->mprisController = std::make_shared<MprisController>();
app->signal_activate().connect([&]() {
auto display = Gdk::Display::get_default();

View File

@@ -1,8 +1,8 @@
#include "services/notification.hpp"
#include "services/dbus/notification.hpp"
#include <iostream>
#include "services/notificationController.hpp"
#include "widgets/notification.hpp"
#include "gtkmm/object.h"
void NotificationService::onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name) {
std::cout << "Acquired bus name: " << name << std::endl;
@@ -68,22 +68,7 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase &parame
}
void NotificationService::createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message) {
auto display = Gdk::Display::get_default();
if (!display) {
std::cerr << "Error: No default display found" << std::endl;
return;
}
auto monitors = display->get_monitors();
if (!monitors) {
std::cerr << "Error: No monitors found" << std::endl;
return;
}
for (guint i = 0; i < monitors->get_n_items(); ++i) {
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(monitors->get_object(i));
if (!monitor) continue;
auto widget = std::make_shared<NotificationWidget>(monitor, title, message);
}
auto controller = NotificationController::getInstance();
controller->showNotificationOnAllMonitors(title, message);
}

View 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);
}
}

View File

@@ -0,0 +1,67 @@
#include "services/textureCache.hpp"
#include <curl/curl.h>
#include <vector>
#include "glibmm/bytes.h"
namespace {
size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
auto *buffer = static_cast<std::vector<unsigned char> *>(userp);
auto total = size * nmemb;
auto *bytes = static_cast<unsigned char *>(contents);
buffer->insert(buffer->end(), bytes, bytes + total);
return total;
}
Glib::RefPtr<Gdk::Texture> download_texture_from_url(const std::string &url) {
if (url.empty()) {
return {};
}
CURL *curl = curl_easy_init();
if (!curl) {
return {};
}
std::vector<unsigned char> buffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_buffer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "bar/1.0");
auto res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK || buffer.empty()) {
return {};
}
auto bytes = Glib::Bytes::create(buffer.data(), buffer.size());
return Gdk::Texture::create_from_bytes(bytes);
}
} // namespace
TextureCacheService *TextureCacheService::getInstance() {
static TextureCacheService instance;
return &instance;
}
Glib::RefPtr<Gdk::Texture> TextureCacheService::getTexture(const std::string &url) {
if (url.empty()) {
return {};
}
auto it = cache.find(url);
if (it != cache.end()) {
return it->second;
}
auto texture = download_texture_from_url(url);
if (texture) {
cache.emplace(url, texture);
}
return texture;
}