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

@@ -33,8 +33,10 @@ target_sources(bar_lib
src/widgets/webWidget.cpp
src/services/hyprland.cpp
src/services/notification.cpp
src/services/notificationController.cpp
src/services/textureCache.cpp
src/services/tray.cpp
src/services/dbus/notification.cpp
src/widgets/tray.cpp
src/widgets/controlCenter.cpp

View File

@@ -4,11 +4,13 @@
#include "bar/bar.hpp"
#include "services/hyprland.hpp"
#include "services/notification.hpp"
#include "services/dbus/notification.hpp"
#include "glibmm/refptr.h"
#include "gtkmm/application.h"
class MprisController;
class App {
public:
App();
@@ -19,6 +21,7 @@ class App {
Glib::RefPtr<Gtk::Application> app;
std::vector<std::shared_ptr<Bar>> bars;
std::shared_ptr<NotificationService> notificationService = nullptr;
std::shared_ptr<MprisController> mprisController = nullptr;
HyprlandService *hyprlandService = nullptr;
TrayService *trayService = TrayService::getInstance();

View File

@@ -0,0 +1,136 @@
#include <gtkmm.h>
#include <giomm.h>
#include <iostream>
#include <vector>
#include "services/notificationController.hpp"
#include "giomm/dbusconnection.h"
#include "giomm/dbusproxy.h"
class MprisController {
public:
MprisController() {
// 1. Connect to the Session Bus
Gio::DBus::Connection::get(
Gio::DBus::BusType::SESSION,
sigc::mem_fun(*this, &MprisController::on_bus_connected)
);
}
private:
Glib::RefPtr<Gio::DBus::Connection> m_connection;
Glib::RefPtr<Gio::DBus::Proxy> m_proxy;
void on_bus_connected(const Glib::RefPtr<Gio::AsyncResult>& result) {
if (!result) {
std::cerr << "DBus Connection Error: null async result" << std::endl;
return;
}
try {
m_connection = Gio::DBus::Connection::get_finish(result);
// 2. Create a Proxy to the media player
// NOTE: In a real app, you must find the name dynamically (see Step 2 below).
// For now, ensure a player like Spotify or VLC is running.
// Try "org.mpris.MediaPlayer2.spotify" or "org.mpris.MediaPlayer2.vlc"
std::string player_bus_name = "org.mpris.MediaPlayer2.spotify";
m_proxy = Gio::DBus::Proxy::create_sync(
m_connection,
player_bus_name, // The Bus Name
"/org/mpris/MediaPlayer2", // The Object Path
"org.mpris.MediaPlayer2.Player" // The Interface
);
if (m_proxy) {
std::cout << "Connected to: " << player_bus_name << std::endl;
// Get initial state
print_metadata();
// 3. Listen for changes (Song change, Pause/Play)
m_proxy->signal_properties_changed().connect(
sigc::mem_fun(*this, &MprisController::on_properties_changed)
);
}
} catch (const Glib::Error& ex) {
std::cerr << "DBus Connection Error: " << ex.what() << std::endl;
}
}
void print_metadata() {
// Retrieve the cached property "Metadata"
Glib::VariantBase metadata_var;
m_proxy->get_cached_property(metadata_var, "Metadata");
if (!metadata_var) {
std::cout << "No metadata available." << std::endl;
return;
}
// 4. Unpack Metadata (Type: a{sv})
// This is a dictionary of string -> variant
using MetadataMap = std::map<Glib::ustring, Glib::VariantBase>;
MetadataMap metadata_map;
// Cast the variant to a container and iterate
Glib::Variant<MetadataMap> variant_dict =
Glib::VariantBase::cast_dynamic<Glib::Variant<MetadataMap>>(metadata_var);
metadata_map = variant_dict.get();
std::string title, artist, artwork_url;
if (metadata_map.count("xesam:title")) {
auto title_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["xesam:title"]);
title = title_var.get();
}
if (metadata_map.count("xesam:artist")) {
auto artist_var = metadata_map["xesam:artist"];
if (artist_var.is_of_type(Glib::VariantType("as"))) {
auto artists = Glib::VariantBase::cast_dynamic<Glib::Variant<std::vector<Glib::ustring>>>(artist_var).get();
if (!artists.empty()) {
artist = artists[0]; // Take the first artist
}
}
}
if (metadata_map.count("mpris:artUrl")) {
auto art_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["mpris:artUrl"]);
artwork_url = art_var.get();
}
auto notifactionController = NotificationController::getInstance();
notifactionController->showSpotifyNotification(title, artist, artwork_url);
}
// Called when the song changes
void on_properties_changed(const Gio::DBus::Proxy::MapChangedProperties& changed_properties,
const std::vector<Glib::ustring>& invalidated_properties) {
(void)invalidated_properties;
// Only refresh when Metadata is updated
if (changed_properties.find("Metadata") != changed_properties.end()) {
// You could parse 'changed_properties' directly, but it's easier to just pull the new cache
print_metadata();
}
}
public:
// Call this to toggle play/pause
void toggle_play() {
if(m_proxy) {
m_proxy->call("PlayPause");
}
}
// Call this to skip
void next_song() {
if(m_proxy) {
m_proxy->call("Next");
}
}
};

View File

@@ -7,7 +7,6 @@
#include <sigc++/sigc++.h>
#include <vector>
#include "gdkmm/monitor.h"
#include "giomm/dbusconnection.h"
#include "giomm/dbusownname.h"
@@ -47,7 +46,6 @@ class NotificationService {
public:
NotificationService() : notificationIdCounter(1) {
Gio::DBus::own_name(
Gio::DBus::BusType::SESSION,
"org.freedesktop.Notifications",
@@ -56,6 +54,7 @@ class NotificationService {
{}, // Name lost slot (optional)
Gio::DBus::BusNameOwnerFlags::REPLACE);
}
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
private:

View File

@@ -0,0 +1,23 @@
#pragma once
#include <memory>
#include <vector>
#include "gdkmm/monitor.h"
class NotificationController {
static std::shared_ptr<NotificationController> instance;
public:
static std::shared_ptr<NotificationController> getInstance() {
if (!NotificationController::instance) {
NotificationController::instance = std::shared_ptr<NotificationController>(new NotificationController());
}
return NotificationController::instance;
}
void showSpotifyNotification(const std::string &title, const std::string &message, const std::string &artwork_url);
void showNotificationOnAllMonitors(const std::string &title, const std::string &message);
private:
NotificationController();
std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors;
};

View File

@@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <unordered_map>
#include "gdkmm/texture.h"
#include "glibmm/refptr.h"
class TextureCacheService {
public:
static TextureCacheService *getInstance();
Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url);
private:
TextureCacheService() = default;
std::unordered_map<std::string, Glib::RefPtr<Gdk::Texture>> cache;
};

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