refacor media widget, apply clang format rule

This commit is contained in:
2026-02-07 14:14:50 +01:00
parent 64b3babd3d
commit d9ac353a0d
54 changed files with 642 additions and 878 deletions

View File

@@ -2,17 +2,18 @@
#include <sigc++/sigc++.h>
#include <vector>
#include "connection/dbus/notification.hpp"
#include "connection/dbus/mpris.hpp"
#include "connection/dbus/notification.hpp"
#include "services/notificationController.hpp"
#include "services/textureCache.hpp"
App::App() {
this->app = Gtk::Application::create("org.example.mybar");
this->setupServices();
this->hyprlandService = HyprlandService::getInstance();
this->hyprlandService = HyprlandService::getInstance();
this->notificationService = std::make_shared<NotificationService>();
this->mprisController = MprisController::getInstance();
this->mprisController = MprisController::getInstance();
auto notificationController = NotificationController::getInstance();
this->mprisController->signal_mpris_updated().connect(
@@ -26,8 +27,7 @@ App::App() {
for (guint i = 0; i < monitors->get_n_items(); ++i) {
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
monitors->get_object(i)
);
monitors->get_object(i));
if (monitor) {
auto bar = std::make_shared<Bar>(monitor->gobj());
@@ -44,7 +44,6 @@ App::App() {
}
});
app->signal_shutdown().connect([&]() {
this->trayService->stop();
});

View File

@@ -33,8 +33,8 @@ Bar::Bar(GdkMonitor *monitor) {
set_child(main_box);
this->volumeWidget = std::make_shared<VolumeWidget>();
this->trayWidget = std::make_shared<TrayWidget>();
this->volumeWidget = std::make_shared<VolumeWidget>();
this->trayWidget = std::make_shared<TrayWidget>();
load_css();
setup_ui();
@@ -43,7 +43,7 @@ Bar::Bar(GdkMonitor *monitor) {
date.onUpdate();
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate), 1000);
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
}
void Bar::setup_ui() {

View File

@@ -1,4 +1,4 @@
#include "widgets/controlCenter/mediaControl.hpp"
#include "components/mediaPlayer.hpp"
#include "components/button/iconButton.hpp"
#include "helpers/string.hpp"
@@ -23,10 +23,8 @@ std::string formatTimeUs(int64_t time_us) {
}
} // namespace
MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controller)
: Gtk::Box(Gtk::Orientation::VERTICAL) {
this->mprisController = std::move(controller);
MediaPlayer::MediaPlayer(std::shared_ptr<MprisController> controller)
: Gtk::Box(Gtk::Orientation::VERTICAL), mprisController(controller) {
this->set_orientation(Gtk::Orientation::VERTICAL);
this->set_hexpand(false);
@@ -107,9 +105,9 @@ MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controll
}
});
this->previousButton = std::make_unique<IconButton>(Icon::SKIP_PREVIOUS);
this->previousButton = std::make_unique<IconButton>(Icon::SKIP_PREVIOUS);
this->playPauseButton = std::make_unique<IconButton>(Icon::PLAY_ARROW);
this->nextButton = std::make_unique<IconButton>(Icon::SKIP_NEXT);
this->nextButton = std::make_unique<IconButton>(Icon::SKIP_NEXT);
this->bottomContainer.set_orientation(Gtk::Orientation::HORIZONTAL);
this->bottomContainer.set_vexpand(false);
@@ -133,7 +131,7 @@ MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controll
});
this->mprisController->signal_mpris_updated().connect(
sigc::mem_fun(*this, &MediaControlWidget::onSpotifyMprisUpdated));
sigc::mem_fun(*this, &MediaPlayer::onSpotifyMprisUpdated));
this->mprisController->signal_playback_status_changed().connect(
[this](MprisController::PlaybackStatus status) {
@@ -158,12 +156,12 @@ MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controll
this->resetSeekTimer(0);
}
void MediaControlWidget::setCanSeek(bool can_seek) {
void MediaPlayer::setCanSeek(bool can_seek) {
this->canSeek = can_seek;
this->seekBarContainer.set_visible(can_seek);
}
void MediaControlWidget::onSpotifyMprisUpdated(const MprisPlayer2Message &message) {
void MediaPlayer::onSpotifyMprisUpdated(const MprisPlayer2Message &message) {
std::string artistText = "Unknown Artist";
if (!message.artist.empty()) {
artistText = StringHelper::trimToSize(message.artist[0], 30);
@@ -171,7 +169,7 @@ void MediaControlWidget::onSpotifyMprisUpdated(const MprisPlayer2Message &messag
this->artistLabel.set_text(artistText);
this->titleLabel.set_text(message.title);
const bool trackChanged = !this->currentTrackId.empty() && this->currentTrackId != message.track_id;
this->currentTrackId = message.track_id;
this->currentTrackId = message.track_id;
if (trackChanged) {
this->currentPositionUs = 0;
@@ -191,23 +189,23 @@ void MediaControlWidget::onSpotifyMprisUpdated(const MprisPlayer2Message &messag
}
}
void MediaControlWidget::setCurrentPosition(int64_t position_us) {
void MediaPlayer::setCurrentPosition(int64_t position_us) {
this->currentPositionUs = position_us;
this->currentTimeLabel.set_text(formatTimeUs(position_us));
if (totalLengthUs > 0) {
double fraction = static_cast<double>(currentPositionUs) / static_cast<double>(totalLengthUs);
double fraction = static_cast<double>(currentPositionUs) / static_cast<double>(totalLengthUs);
this->suppressSeekSignal = true;
this->seekBar.set_value(fraction * 100);
this->suppressSeekSignal = false;
}
}
void MediaControlWidget::setTotalLength(int64_t length_us) {
void MediaPlayer::setTotalLength(int64_t length_us) {
this->totalLengthUs = length_us;
this->totalTimeLabel.set_text(formatTimeUs(length_us));
}
void MediaControlWidget::resetSeekTimer(int64_t start_position_us) {
void MediaPlayer::resetSeekTimer(int64_t start_position_us) {
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
@@ -215,19 +213,20 @@ void MediaControlWidget::resetSeekTimer(int64_t start_position_us) {
setCurrentPosition(start_position_us);
seekTimerConnection = Glib::signal_timeout().connect(
sigc::mem_fun(*this, &MediaControlWidget::onSeekTick),
sigc::mem_fun(*this, &MediaPlayer::onSeekTick),
1000);
}
void MediaControlWidget::schedulePauseAfterSeek() {
void MediaPlayer::schedulePauseAfterSeek() {
Glib::signal_timeout().connect_once([this]() {
if (this->playbackStatus != MprisController::PlaybackStatus::Playing) {
this->mprisController->pause();
}
}, 100);
},
100);
}
bool MediaControlWidget::onSeekTick() {
bool MediaPlayer::onSeekTick() {
if (totalLengthUs <= 0) {
return true;
}
@@ -240,7 +239,7 @@ bool MediaControlWidget::onSeekTick() {
return true;
}
void MediaControlWidget::onRunningStateChanged(MprisController::PlaybackStatus status) {
void MediaPlayer::onRunningStateChanged(MprisController::PlaybackStatus status) {
this->playbackStatus = status;
switch (status) {
case MprisController::PlaybackStatus::Playing:
@@ -255,20 +254,20 @@ void MediaControlWidget::onRunningStateChanged(MprisController::PlaybackStatus s
}
}
void MediaControlWidget::onPlay() {
void MediaPlayer::onPlay() {
this->playPauseButton->setIcon(Icon::PAUSE);
this->resetSeekTimer(currentPositionUs);
}
void MediaControlWidget::onPause() {
void MediaPlayer::onPause() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
}
void MediaControlWidget::onStop() {
void MediaPlayer::onStop() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {

View File

@@ -0,0 +1,277 @@
#include "components/mediaPlayer.hpp"
#include "components/button/iconButton.hpp"
#include "helpers/string.hpp"
#include "services/textureCache.hpp"
namespace {
std::string formatTimeUs(int64_t time_us) {
if (time_us < 0) {
time_us = 0;
}
int64_t totalSeconds = time_us / 1000000;
int64_t hours = totalSeconds / 3600;
int64_t minutes = (totalSeconds / 60) % 60;
int64_t seconds = totalSeconds % 60;
if (hours > 0) {
return std::to_string(hours) + ":" +
(minutes < 10 ? "0" : "") + std::to_string(minutes) + ":" +
(seconds < 10 ? "0" : "") + std::to_string(seconds);
}
return std::to_string(minutes) + ":" + (seconds < 10 ? "0" : "") + std::to_string(seconds);
}
} // namespace
MediaPlayer::MediaPlayer(std::shared_ptr<MprisController> controller)
: Gtk::Box(Gtk::Orientation::VERTICAL), mprisController(controller) {
this->set_orientation(Gtk::Orientation::VERTICAL);
this->set_hexpand(false);
this->set_vexpand(false);
this->add_css_class("control-center-player-container");
this->append(this->topContainer);
this->append(this->seekBarContainer);
this->append(this->bottomContainer);
this->backgroundImage.set_content_fit(Gtk::ContentFit::COVER);
this->backgroundImage.set_can_shrink(true);
this->imageWrapper.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::NEVER);
this->imageWrapper.set_child(this->backgroundImage);
this->topContainer.set_child(this->imageWrapper);
this->topContainer.set_size_request(-1, 120);
this->topContainer.set_vexpand(false);
this->topContainer.set_hexpand(true);
this->infoContainer.set_orientation(Gtk::Orientation::VERTICAL);
this->infoContainer.set_valign(Gtk::Align::END);
this->infoContainer.set_halign(Gtk::Align::START);
this->infoContainer.append(this->artistLabel);
this->infoContainer.append(this->titleLabel);
this->topContainer.add_overlay(this->infoContainer);
this->artistLabel.set_halign(Gtk::Align::START);
this->titleLabel.set_halign(Gtk::Align::START);
this->seekBarContainer.set_orientation(Gtk::Orientation::HORIZONTAL);
this->seekBarContainer.set_vexpand(false);
this->seekBarContainer.set_hexpand(true);
this->seekBarContainer.set_halign(Gtk::Align::CENTER);
this->seekBarContainer.append(this->currentTimeLabel);
this->seekBarContainer.append(this->seekBar);
this->seekBarContainer.append(this->totalTimeLabel);
this->seekBarContainer.set_visible(true);
this->currentTimeLabel.set_text("0:00");
this->currentTimeLabel.set_halign(Gtk::Align::START);
this->totalTimeLabel.set_text("0:00");
this->totalTimeLabel.set_halign(Gtk::Align::END);
this->seekBar.set_range(0, 100);
this->seekBar.set_value(0);
this->seekBar.set_orientation(Gtk::Orientation::HORIZONTAL);
this->seekBar.set_draw_value(false);
this->seekBar.set_hexpand(true);
this->seekBar.set_halign(Gtk::Align::CENTER);
this->seekBar.add_css_class("control-center-seek-bar");
this->seekBar.signal_value_changed().connect([this]() {
if (this->suppressSeekSignal || this->totalLengthUs <= 0) {
return;
}
double fraction = this->seekBar.get_value() / 100.0;
int64_t new_position_us =
static_cast<int64_t>(fraction * static_cast<double>(this->totalLengthUs));
if (new_position_us == this->currentPositionUs) {
return;
}
if (!this->currentTrackId.empty()) {
this->mprisController->set_position(this->currentTrackId, new_position_us);
if (this->playbackStatus != MprisController::PlaybackStatus::Playing) {
this->schedulePauseAfterSeek();
}
} else if (this->playbackStatus == MprisController::PlaybackStatus::Playing) {
this->mprisController->emit_seeked(new_position_us - this->currentPositionUs); // in us
} else {
return;
}
if (this->playbackStatus == MprisController::PlaybackStatus::Playing) {
this->resetSeekTimer(new_position_us);
} else {
this->setCurrentPosition(new_position_us);
}
});
this->previousButton = std::make_unique<IconButton>(Icon::SKIP_PREVIOUS);
this->playPauseButton = std::make_unique<IconButton>(Icon::PLAY_ARROW);
this->nextButton = std::make_unique<IconButton>(Icon::SKIP_NEXT);
this->bottomContainer.set_orientation(Gtk::Orientation::HORIZONTAL);
this->bottomContainer.set_vexpand(false);
this->bottomContainer.set_hexpand(false);
this->bottomContainer.set_valign(Gtk::Align::START);
this->bottomContainer.set_homogeneous(true);
this->topContainer.set_vexpand(false);
this->topContainer.set_hexpand(true);
this->bottomContainer.append(*this->previousButton);
this->bottomContainer.append(*this->playPauseButton);
this->bottomContainer.append(*this->nextButton);
this->previousButton->signal_clicked().connect([this]() {
this->mprisController->previous_song();
});
this->playPauseButton->signal_clicked().connect([this]() {
this->mprisController->toggle_play();
});
this->nextButton->signal_clicked().connect([this]() {
this->mprisController->next_song();
});
this->mprisController->signal_mpris_updated().connect(
sigc::mem_fun(*this, &MediaPlayer::onSpotifyMprisUpdated));
this->mprisController->signal_playback_status_changed().connect(
[this](MprisController::PlaybackStatus status) {
this->onRunningStateChanged(status);
});
this->mprisController->signal_playback_position_changed().connect(
[this](int64_t position_us) {
this->setCurrentPosition(position_us);
});
this->mprisController->signal_can_seek_changed().connect(
[this](bool can_seek) {
this->setCanSeek(can_seek);
});
this->artistLabel.set_text("Artist Name");
this->artistLabel.add_css_class("control-center-player-artist-label");
this->titleLabel.set_text("Song Title");
this->titleLabel.add_css_class("control-center-player-title-label");
this->resetSeekTimer(0);
}
void MediaPlayer::setCanSeek(bool can_seek) {
this->canSeek = can_seek;
this->seekBarContainer.set_visible(can_seek);
}
void MediaPlayer::onSpotifyMprisUpdated(const MprisPlayer2Message &message) {
std::string artistText = "Unknown Artist";
if (!message.artist.empty()) {
artistText = StringHelper::trimToSize(message.artist[0], 30);
}
this->artistLabel.set_text(artistText);
this->titleLabel.set_text(message.title);
const bool trackChanged = !this->currentTrackId.empty() && this->currentTrackId != message.track_id;
this->currentTrackId = message.track_id;
if (trackChanged) {
this->currentPositionUs = 0;
}
if (auto texture = TextureCacheService::getInstance()->getTexture(message.artwork_url)) {
this->backgroundImage.set_paintable(texture);
}
this->setTotalLength(message.length_ms * 1000);
this->setCurrentPosition(this->currentPositionUs);
if (this->playbackStatus == MprisController::PlaybackStatus::Playing) {
this->resetSeekTimer(this->currentPositionUs);
} else if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
}
void MediaPlayer::setCurrentPosition(int64_t position_us) {
this->currentPositionUs = position_us;
this->currentTimeLabel.set_text(formatTimeUs(position_us));
if (totalLengthUs > 0) {
double fraction = static_cast<double>(currentPositionUs) / static_cast<double>(totalLengthUs);
this->suppressSeekSignal = true;
this->seekBar.set_value(fraction * 100);
this->suppressSeekSignal = false;
}
}
void MediaPlayer::setTotalLength(int64_t length_us) {
this->totalLengthUs = length_us;
this->totalTimeLabel.set_text(formatTimeUs(length_us));
}
void MediaPlayer::resetSeekTimer(int64_t start_position_us) {
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
setCurrentPosition(start_position_us);
seekTimerConnection = Glib::signal_timeout().connect(
sigc::mem_fun(*this, &MediaPlayer::onSeekTick),
1000);
}
void MediaPlayer::schedulePauseAfterSeek() {
Glib::signal_timeout().connect_once([this]() {
if (this->playbackStatus != MprisController::PlaybackStatus::Playing) {
this->mprisController->pause();
}
},
100);
}
bool MediaPlayer::onSeekTick() {
if (totalLengthUs <= 0) {
return true;
}
int64_t nextPosition = currentPositionUs + 1000000;
if (nextPosition > totalLengthUs) {
nextPosition = totalLengthUs;
}
setCurrentPosition(nextPosition);
return true;
}
void MediaPlayer::onRunningStateChanged(MprisController::PlaybackStatus status) {
this->playbackStatus = status;
switch (status) {
case MprisController::PlaybackStatus::Playing:
this->onPlay();
break;
case MprisController::PlaybackStatus::Paused:
this->onPause();
break;
case MprisController::PlaybackStatus::Stopped:
this->onStop();
break;
}
}
void MediaPlayer::onPlay() {
this->playPauseButton->setIcon(Icon::PAUSE);
this->resetSeekTimer(currentPositionUs);
}
void MediaPlayer::onPause() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
}
void MediaPlayer::onStop() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
this->setCurrentPosition(0);
}

View File

@@ -1,7 +1,8 @@
#include "components/popover.hpp"
#include "components/button/iconButton.hpp"
Popover::Popover(Icon::Type icon, std::string name): IconButton(icon) {
Popover::Popover(Icon::Type icon, std::string name) : IconButton(icon) {
signal_clicked().connect(sigc::mem_fun(*this, &Popover::on_toggle_window));
set_name(name);

0
src/components/tab.cpp Normal file
View File

View File

@@ -41,7 +41,7 @@ WorkspaceIndicator::WorkspaceIndicator(int id, std::string label, sigc::slot<voi
void WorkspaceIndicator::setIndicatorState(InidicatorState state) {
this->clearCssClass();
this->currentState = state;
auto cssClass = this->stateToCssClass[state];
auto cssClass = this->stateToCssClass[state];
this->overlay->add_css_class(cssClass);
}

View File

@@ -76,8 +76,8 @@ void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &res
try {
auto list_names_result = m_dbus_proxy->call_sync("ListNames");
auto names_variant = Glib::VariantBase::cast_dynamic<
Glib::Variant<std::vector<Glib::ustring>>>(list_names_result.get_child(0));
auto names_variant = Glib::VariantBase::cast_dynamic<
Glib::Variant<std::vector<Glib::ustring>>>(list_names_result.get_child(0));
for (const auto &name : names_variant.get()) {
const std::string bus_name = name;
@@ -103,7 +103,7 @@ void MprisController::on_dbus_signal(const Glib::ustring &,
return;
}
auto name_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(parameters.get_child(0));
auto name_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(parameters.get_child(0));
auto old_owner_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(parameters.get_child(1));
auto new_owner_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(parameters.get_child(2));
@@ -270,7 +270,7 @@ void MprisController::emit_cached_playback_status() {
return;
}
auto status = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(status_var).get();
auto status = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(status_var).get();
auto parsedStatusIt = playbackStatusMap.find(static_cast<std::string>(status));
if (parsedStatusIt != playbackStatusMap.end()) {
currentPlaybackStatus = parsedStatusIt->second;
@@ -398,10 +398,8 @@ void MprisController::set_position(const std::string &track_id, int64_t position
}
try {
Glib::VariantContainerBase params = Glib::VariantContainerBase::create_tuple({
Glib::Variant<Glib::DBusObjectPathString>::create(track_id),
Glib::Variant<gint64>::create(position_us)
});
Glib::VariantContainerBase params = Glib::VariantContainerBase::create_tuple({Glib::Variant<Glib::DBusObjectPathString>::create(track_id),
Glib::Variant<gint64>::create(position_us)});
m_proxy->call("SetPosition", params);
} catch (const Glib::Error &ex) {

View File

@@ -160,11 +160,11 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase &parame
notify.expire_timeout = expire_timeout;
guint id = notificationIdCounter++;
if (app_name == "image-copy" ) {
if (app_name == "image-copy") {
NotificationController::getInstance()->showCopyNotification(notify);
invocation->return_value(Glib::VariantContainerBase::create_tuple(
Glib::Variant<guint>::create(id)));
return;
}
@@ -173,7 +173,7 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase &parame
invocation->return_value(Glib::VariantContainerBase::create_tuple(
Glib::Variant<guint>::create(id)));
return;
}
}
if (app_name == "Thunderbird") {
notify.expire_timeout = 10000; // 10 seconds for email notifications

View File

@@ -9,8 +9,8 @@
#include <giomm/dbusactiongroup.h>
#include <giomm/dbusownname.h>
#include <giomm/menumodel.h>
#include <spdlog/spdlog.h>
#include <memory>
#include <spdlog/spdlog.h>
#include <tuple>
#include <vector>
@@ -128,7 +128,7 @@ void on_simple_call_finished(GObject *source, GAsyncResult *res,
std::unique_ptr<SimpleCallData> data(
static_cast<SimpleCallData *>(user_data));
GError *error = nullptr;
GError *error = nullptr;
GVariant *reply =
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
@@ -358,8 +358,8 @@ void TrayService::activate(const std::string &id, int32_t x, int32_t y) {
return;
}
auto data = new SimpleCallData();
data->debugLabel = "Activate(" + id + ")";
auto data = new SimpleCallData();
data->debugLabel = "Activate(" + id + ")";
data->ignoreUnknownMethod = false;
g_dbus_connection_call(
connection->gobj(), it->second->publicData.busName.c_str(),
@@ -375,8 +375,8 @@ void TrayService::secondaryActivate(const std::string &id, int32_t x,
return;
}
auto data = new SimpleCallData();
data->debugLabel = "SecondaryActivate(" + id + ")";
auto data = new SimpleCallData();
data->debugLabel = "SecondaryActivate(" + id + ")";
data->ignoreUnknownMethod = false;
g_dbus_connection_call(
connection->gobj(), it->second->publicData.busName.c_str(),
@@ -473,7 +473,7 @@ void on_menu_layout_finished(GObject *source, GAsyncResult *res,
return;
}
GError *error = nullptr;
GError *error = nullptr;
GVariant *reply =
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
@@ -823,7 +823,7 @@ struct RefreshCallData {
};
void TrayService::on_refresh_finished_static(GObject *source, GAsyncResult *res,
gpointer user_data) {
gpointer user_data) {
std::unique_ptr<RefreshCallData> data(
static_cast<RefreshCallData *>(user_data));
if (!data || !data->self) {
@@ -837,7 +837,7 @@ void TrayService::on_refresh_finished_static(GObject *source, GAsyncResult *res,
auto &tracked = *it->second;
GError *error = nullptr;
GError *error = nullptr;
GVariant *reply =
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
if (!reply) {
@@ -919,9 +919,9 @@ void TrayService::on_refresh_finished_static(GObject *source, GAsyncResult *res,
g_variant_unref(dictVariant);
const bool menuPathChanged = (tracked.publicData.menuPath != menuPath);
tracked.publicData.title = title;
tracked.publicData.status = status;
const bool menuPathChanged = (tracked.publicData.menuPath != menuPath);
tracked.publicData.title = title;
tracked.publicData.status = status;
tracked.publicData.menuPath = menuPath;
tracked.publicData.menuAvailable = !menuPath.empty();

View File

@@ -3,6 +3,7 @@
#include <algorithm>
#include <cctype>
#include <curl/curl.h>
#include <ranges>
#include <string>
#include <utility>
@@ -17,20 +18,20 @@ size_t write_to_string(void *contents, size_t size, size_t nmemb, void *userp) {
std::string trim(std::string value) {
auto not_space = [](unsigned char c) { return std::isspace(c) == 0; };
value.erase(value.begin(),
std::find_if(value.begin(), value.end(), not_space));
value.erase(std::find_if(value.rbegin(), value.rend(), not_space).base(),
std::ranges::find_if(value, not_space));
value.erase(std::ranges::find_if(std::ranges::reverse_view(value), not_space).base(),
value.end());
return value;
}
size_t header_to_map(char *buffer, size_t size, size_t nitems, void *userdata) {
size_t total = size * nitems;
size_t total = size * nitems;
auto *header_map = static_cast<std::map<std::string, std::string> *>(userdata);
std::string line(buffer, total);
auto colon = line.find(':');
if (colon != std::string::npos) {
auto key = trim(line.substr(0, colon));
auto key = trim(line.substr(0, colon));
auto value = trim(line.substr(colon + 1));
if (!key.empty()) {
header_map->insert_or_assign(std::move(key), std::move(value));
@@ -38,19 +39,19 @@ size_t header_to_map(char *buffer, size_t size, size_t nitems, void *userdata) {
}
return total;
}
}
} // namespace
HttpResponse HttpConnection::get(const std::string &url,
const std::map<std::string, std::string> &headers,
long timeout_ms) {
const std::map<std::string, std::string> &headers,
long timeout_ms) {
return performRequest("GET", url, std::string(), headers, std::string(), timeout_ms);
}
HttpResponse HttpConnection::post(const std::string &url,
const std::string &body,
const std::map<std::string, std::string> &headers,
const std::string &content_type,
long timeout_ms) {
const std::string &body,
const std::map<std::string, std::string> &headers,
const std::string &content_type,
long timeout_ms) {
return performRequest("POST", url, body, headers, content_type, timeout_ms);
}
@@ -91,12 +92,12 @@ HttpResponse HttpConnection::performRequest(const std::string &method,
struct curl_slist *header_list = nullptr;
for (const auto &pair : headers) {
std::string header = pair.first + ": " + pair.second;
header_list = curl_slist_append(header_list, header.c_str());
header_list = curl_slist_append(header_list, header.c_str());
}
if (method == "POST" && !content_type.empty()) {
std::string content_header = "Content-Type: " + content_type;
header_list = curl_slist_append(header_list, content_header.c_str());
header_list = curl_slist_append(header_list, content_header.c_str());
}
if (header_list) {

View File

@@ -14,7 +14,6 @@
#include "helpers/string.hpp"
#include "gtkmm/box.h"
#include "spdlog/spdlog.h"
HyprlandService::HyprlandService() {

View File

@@ -18,7 +18,7 @@ namespace {
#define CACHE_AGE 168
constexpr std::uint64_t kFnvOffsetBasis = 14695981039346656037ull;
constexpr std::uint64_t kFnvPrime = 1099511628211ull;
constexpr std::uint64_t kFnvPrime = 1099511628211ull;
std::string to_hex(std::uint64_t value) {
std::ostringstream stream;
@@ -53,7 +53,7 @@ std::filesystem::path get_cache_path_for_url(const std::string &url) {
auto filename = hash_url(url);
auto last_slash = url.find_last_of('/');
auto last_dot = url.find_last_of('.');
auto last_dot = url.find_last_of('.');
if (last_dot != std::string::npos && (last_slash == std::string::npos || last_dot > last_slash)) {
auto ext = url.substr(last_dot);
if (ext.size() <= 10) {
@@ -66,14 +66,13 @@ std::filesystem::path get_cache_path_for_url(const std::string &url) {
std::chrono::system_clock::time_point to_system_clock(std::filesystem::file_time_type time) {
return std::chrono::time_point_cast<std::chrono::system_clock::duration>(
time - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now()
);
time - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
}
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);
auto total = size * nmemb;
auto *bytes = static_cast<unsigned char *>(contents);
buffer->insert(buffer->end(), bytes, bytes + total);
return total;
}
@@ -144,7 +143,7 @@ Glib::RefPtr<Gdk::Texture> TextureCacheService::getTexture(const std::string &ur
}
auto cache_path = get_cache_path_for_url(url);
auto texture = load_texture_from_file(cache_path);
auto texture = load_texture_from_file(cache_path);
if (!texture) {
texture = download_texture_from_url(url, cache_path);
}
@@ -166,7 +165,7 @@ void TextureCacheService::pruneCache() {
std::vector<std::pair<std::filesystem::path, std::uintmax_t>> files;
std::uintmax_t total_size = 0;
auto now = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto max_age = std::chrono::hours(CACHE_AGE);
for (const auto &entry : std::filesystem::directory_iterator(cache_dir, error)) {
@@ -174,7 +173,7 @@ void TextureCacheService::pruneCache() {
continue;
}
auto path = entry.path();
auto path = entry.path();
auto last_write = entry.last_write_time(error);
if (!error) {
auto age = now - to_system_clock(last_write);
@@ -199,7 +198,7 @@ void TextureCacheService::pruneCache() {
std::sort(files.begin(), files.end(), [&](const auto &left, const auto &right) {
std::error_code left_error;
std::error_code right_error;
auto left_time = std::filesystem::last_write_time(left.first, left_error);
auto left_time = std::filesystem::last_write_time(left.first, left_error);
auto right_time = std::filesystem::last_write_time(right.first, right_error);
if (left_error || right_error) {
return left.first.string() < right.first.string();

View File

View File

@@ -1,18 +1,16 @@
#include "widgets/controlCenter/controlCenter.hpp"
#include "components/button/iconButton.hpp"
#include "components/button/tabButton.hpp"
#include "components/mediaPlayer.hpp"
ControlCenter::ControlCenter(Icon::Type icon, std::string name)
: Popover(icon, name) {
this->popover->add_css_class("control-center-popover");
this->container.set_orientation(Gtk::Orientation::VERTICAL);
this->container.set_spacing(10);
this->scrollview.set_child(this->container);
this->scrollview.set_min_content_width(220);
this->scrollview.set_max_content_width(220);
this->scrollview.set_size_request(220, -1);
this->scrollview.set_policy(
Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
this->scrollview.set_hexpand(false);
@@ -26,84 +24,60 @@ ControlCenter::ControlCenter(Icon::Type icon, std::string name)
this->tabRow.set_margin_bottom(4);
this->tabRow.add_css_class("control-center-tab-row");
this->mediaControl = std::make_unique<TabButton>(Icon::PLAY_CIRCLE);
this->testTabButton = std::make_unique<TabButton>(Icon::EMPTY_DASHBOARD);
this->mediaTabButton = std::make_unique<TabButton>(Icon::PLAY_CIRCLE);
this->infoTabButton = std::make_unique<TabButton>(Icon::EMPTY_DASHBOARD);
this->timerButton = std::make_unique<TabButton>(Icon::TOKEN);
this->tabRow.append(*this->mediaControl);
this->tabRow.append(*this->testTabButton);
this->tabRow.append(*this->mediaTabButton);
this->tabRow.append(*this->infoTabButton);
this->tabRow.append(*this->timerButton);
this->container.append(this->tabRow);
this->contentStack.set_hhomogeneous(true);
this->contentStack.set_vhomogeneous(false);
this->contentStack.set_transition_type(Gtk::StackTransitionType::CROSSFADE);
this->contentStack.set_transition_duration(150);
this->controlCenterContainer.set_orientation(Gtk::Orientation::VERTICAL);
this->controlCenterContainer.set_spacing(4);
this->mediaControlWidget = std::make_unique<MediaWidget>();
this->weatherWidget = std::make_unique<WeatherWidget>();
this->contentStack.add(*this->mediaControlWidget, "controls", "Controls");
this->contentStack.add(*this->weatherWidget, "info", "Info");
this->contentStack.add(*Gtk::make_managed<Gtk::Label>("Timer"), "timer", "Timer");
this->contentStack.add(this->controlCenterContainer, "controls", "Controls");
this->contentStack.add(this->weatherWidget, "test", "Test");
this->contentStack.set_visible_child("controls");
this->setActiveTab("controls");
this->setActiveTab("info");
this->container.append(this->contentStack);
this->mediaControl->signal_clicked().connect([this]() {
this->mediaTabButton->signal_clicked().connect([this]() {
this->setActiveTab("controls");
});
this->testTabButton->signal_clicked().connect([this]() {
this->setActiveTab("test");
this->infoTabButton->signal_clicked().connect([this]() {
this->setActiveTab("info");
});
this->mprisController->signal_player_registered().connect(
[this](const std::string &bus_name) {
this->addPlayerWidget(bus_name);
});
this->mprisController->signal_player_deregistered().connect(
[this](const std::string &bus_name) {
this->removePlayerWidget(bus_name);
});
for (const auto &bus_name : this->mprisController->get_registered_players()) {
this->addPlayerWidget(bus_name);
}
this->timerButton->signal_clicked().connect([this]() {
this->setActiveTab("timer");
});
}
void ControlCenter::setActiveTab(const std::string &tab_name) {
this->contentStack.set_visible_child(tab_name);
this->mediaControl->setActive(false);
this->testTabButton->setActive(false);
this->mediaTabButton->setActive(false);
this->infoTabButton->setActive(false);
this->timerButton->setActive(false);
if (tab_name == "controls") {
this->mediaControl->setActive(true);
} else if (tab_name == "test") {
this->testTabButton->setActive(true);
this->mediaTabButton->setActive(true);
} else if (tab_name == "info") {
this->infoTabButton->setActive(true);
} else if (tab_name == "timer") {
this->timerButton->setActive(true);
}
}
void ControlCenter::addPlayerWidget(const std::string &bus_name) {
if (this->mediaWidgets.find(bus_name) != this->mediaWidgets.end()) {
return;
}
auto controller = MprisController::createForPlayer(bus_name);
auto widget = Gtk::make_managed<MediaControlWidget>(controller);
this->mediaWidgets.emplace(bus_name, widget);
this->controlCenterContainer.append(*widget);
}
void ControlCenter::removePlayerWidget(const std::string &bus_name) {
auto it = this->mediaWidgets.find(bus_name);
if (it == this->mediaWidgets.end()) {
return;
}
this->controlCenterContainer.remove(*it->second);
this->mediaWidgets.erase(it);
}

View File

@@ -0,0 +1,50 @@
#include "widgets/controlCenter/mediaWidget.hpp"
#include <memory>
MediaWidget::MediaWidget() : Gtk::Box(Gtk::Orientation::VERTICAL) {
this->set_hexpand(true);
this->set_vexpand(false);
this->add_css_class("control-center-media-widget");
this->container.set_orientation(Gtk::Orientation::VERTICAL);
this->container.set_hexpand(true);
this->container.set_vexpand(false);
this->append(this->container);
this->mprisController->signal_player_registered().connect(
[this](const std::string &bus_name) {
this->addPlayerWidget(bus_name);
});
this->mprisController->signal_player_deregistered().connect(
[this](const std::string &bus_name) {
this->removePlayerWidget(bus_name);
});
for (const auto &bus_name : this->mprisController->get_registered_players()) {
this->addPlayerWidget(bus_name);
}
}
void MediaWidget::addPlayerWidget(const std::string &bus_name) {
if (this->mediaWidgets.find(bus_name) != this->mediaWidgets.end()) {
return;
}
auto controller = MprisController::createForPlayer(bus_name);
auto widget = std::make_unique<MediaPlayer>(controller);
this->mediaWidgets.emplace(bus_name, std::move(widget));
this->container.append(*this->mediaWidgets[bus_name]);
}
void MediaWidget::removePlayerWidget(const std::string &bus_name) {
auto it = this->mediaWidgets.find(bus_name);
if (it == this->mediaWidgets.end()) {
return;
}
this->container.remove(*it->second);
this->mediaWidgets.erase(it);
}

View File

@@ -2,6 +2,7 @@
#include <memory>
#include <spdlog/spdlog.h>
#include "components/button/iconButton.hpp"
#include "glibmm/datetime.h"
@@ -86,7 +87,6 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
auto buttonBox = Gtk::make_managed<Gtk::Box>();
buttonBox->set_spacing(10);
auto saveToClipboardButton = Gtk::make_managed<IconButton>(Icon::CONTENT_COPY);
saveToClipboardButton->signal_clicked().connect([this]() {
copyToClipboard(this->copiedImage);
@@ -103,8 +103,8 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
// xdg-pic/screenshot // use env
auto xdgPicturesDir = Glib::get_user_special_dir(Glib::UserDirectory::PICTURES);
auto dateStamp = Glib::DateTime::create_now_local().format("%Y%m%d_%H%M%S");
auto filepath = xdgPicturesDir + "/screenshot" ;
auto filename = dateStamp + ".png";
auto filepath = xdgPicturesDir + "/screenshot";
auto filename = dateStamp + ".png";
saveImageToFile(this->copiedImage, filepath, filename);
spdlog::info("Saved image to {}", filepath.c_str());
@@ -119,7 +119,6 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
contentBox->append(*buttonBox);
this->mainBox.append(*contentBox);
}
void CopyNotification::createTextNotification(NotifyMessage notify) {

View File

@@ -1,33 +1,32 @@
#include "widgets/notification/notificationWindow.hpp"
#include <cstdint>
#include <sys/types.h>
#include "components/button/iconButton.hpp"
#include "components/button/textButton.hpp"
#include "helpers/string.hpp"
#include "gtkmm/box.h"
#include "gtkmm/button.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage notify) : BaseNotification(notificationId, monitor) {
set_title(notify.summary);
// Main vertical box
auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 8);
vbox->set_halign(Gtk::Align::FILL);
switch (notify.urgency) {
case NotificationUrgency::CRITICAL:
add_css_class("notification-critical");
break;
case NotificationUrgency::NORMAL:
add_css_class("notification-normal");
break;
case NotificationUrgency::LOW:
add_css_class("notification-low");
break;
case NotificationUrgency::CRITICAL:
add_css_class("notification-critical");
break;
case NotificationUrgency::NORMAL:
add_css_class("notification-normal");
break;
case NotificationUrgency::LOW:
add_css_class("notification-low");
break;
}
auto header_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
@@ -77,16 +76,16 @@ NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<
btn->add_css_class("notification-button");
switch (notify.urgency) {
case NotificationUrgency::CRITICAL:
btn->add_css_class("notification-critical");
break;
case NotificationUrgency::NORMAL:
btn->add_css_class("notification-normal");
break;
case NotificationUrgency::LOW:
btn->add_css_class("notification-low");
break;
}
case NotificationUrgency::CRITICAL:
btn->add_css_class("notification-critical");
break;
case NotificationUrgency::NORMAL:
btn->add_css_class("notification-normal");
break;
case NotificationUrgency::LOW:
btn->add_css_class("notification-low");
break;
}
btn->signal_clicked().connect([this, action_id, cb = notify.on_action, guard = notify.actionInvoked]() {
if (cb && guard && !*guard) {

View File

@@ -1,4 +1,5 @@
#include "widgets/notification/spotifyNotification.hpp"
#include <sys/types.h>
#include "components/button/iconButton.hpp"

View File

@@ -1,12 +1,12 @@
#include "widgets/tray.hpp"
#include <cmath>
#include <gdkmm/rectangle.h>
#include <gio/gmenu.h>
#include <gtk/gtk.h>
#include <cmath>
#include <graphene.h>
#include <utility>
#include <gtk/gtk.h>
#include <spdlog/spdlog.h>
#include <utility>
namespace {
bool is_wayland_display(GtkWidget *widget) {
@@ -182,7 +182,7 @@ void log_menu_tree(const std::vector<TrayService::MenuNode> &nodes,
} // namespace
TrayIconWidget::TrayIconWidget(Icon::Type icon, std::string id) : IconButton(icon), id(std::move(id)),
container(Gtk::Orientation::HORIZONTAL) {
container(Gtk::Orientation::HORIZONTAL) {
aliveFlag = std::make_shared<bool>(true);
set_has_frame(false);
set_focusable(false);
@@ -243,8 +243,8 @@ TrayIconWidget::~TrayIconWidget() {
}
void TrayIconWidget::update(const TrayService::Item &item) {
hasRemoteMenu = item.menuAvailable;
menuPopupPending = false;
hasRemoteMenu = item.menuAvailable;
menuPopupPending = false;
menuRequestInFlight = false;
if (!item.menuAvailable) {
@@ -350,7 +350,7 @@ void TrayIconWidget::on_secondary_released(int /*n_press*/, double x,
}
menuRequestInFlight = true;
auto weak = std::weak_ptr<bool>(aliveFlag);
auto weak = std::weak_ptr<bool>(aliveFlag);
service.request_menu_layout(
id, [weak, this](std::optional<TrayService::MenuNode> layout) {
if (auto locked = weak.lock()) {
@@ -402,8 +402,8 @@ void TrayIconWidget::on_menu_layout_ready(
const auto &layout = *layoutOpt;
log_menu_tree(layout.children, 0);
auto menu = Gio::Menu::create();
auto actions = Gio::SimpleActionGroup::create();
auto menu = Gio::Menu::create();
auto actions = Gio::SimpleActionGroup::create();
populate_menu_items(layout.children, menu, actions);
@@ -529,8 +529,8 @@ void TrayIconWidget::on_menu_action(const Glib::VariantBase & /*parameter*/,
}
bool TrayIconWidget::try_get_pending_coords(int32_t &outX, int32_t &outY) const {
outX = -1;
outY = -1;
outX = -1;
outY = -1;
int32_t sendX = static_cast<int32_t>(std::lround(pendingX));
int32_t sendY = static_cast<int32_t>(std::lround(pendingY));
if (!try_get_global_click_coords(GTK_WIDGET(gobj()), pendingX, pendingY,

View File

@@ -1,9 +1,9 @@
#include "widgets/volumeWidget.hpp"
#include <cmath>
#include <spdlog/spdlog.h>
#include <regex>
#include <sigc++/functors/mem_fun.h>
#include <spdlog/spdlog.h>
#include "helpers/command.hpp"

View File

@@ -2,6 +2,7 @@
#include <gtkmm/label.h>
#include <webkit/webkit.h>
#include "components/button/iconButton.hpp"
WebWidget::WebWidget(Icon::Type icon, std::string name, std::string url) : Popover(icon, name) {