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

@@ -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

@@ -1,278 +0,0 @@
#include "widgets/controlCenter/mediaControl.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
MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controller)
: Gtk::Box(Gtk::Orientation::VERTICAL) {
this->mprisController = std::move(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, &MediaControlWidget::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 MediaControlWidget::setCanSeek(bool can_seek) {
this->canSeek = can_seek;
this->seekBarContainer.set_visible(can_seek);
}
void MediaControlWidget::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 MediaControlWidget::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 MediaControlWidget::setTotalLength(int64_t length_us) {
this->totalLengthUs = length_us;
this->totalTimeLabel.set_text(formatTimeUs(length_us));
}
void MediaControlWidget::resetSeekTimer(int64_t start_position_us) {
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
setCurrentPosition(start_position_us);
seekTimerConnection = Glib::signal_timeout().connect(
sigc::mem_fun(*this, &MediaControlWidget::onSeekTick),
1000);
}
void MediaControlWidget::schedulePauseAfterSeek() {
Glib::signal_timeout().connect_once([this]() {
if (this->playbackStatus != MprisController::PlaybackStatus::Playing) {
this->mprisController->pause();
}
}, 100);
}
bool MediaControlWidget::onSeekTick() {
if (totalLengthUs <= 0) {
return true;
}
int64_t nextPosition = currentPositionUs + 1000000;
if (nextPosition > totalLengthUs) {
nextPosition = totalLengthUs;
}
setCurrentPosition(nextPosition);
return true;
}
void MediaControlWidget::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 MediaControlWidget::onPlay() {
this->playPauseButton->setIcon(Icon::PAUSE);
this->resetSeekTimer(currentPositionUs);
}
void MediaControlWidget::onPause() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
}
void MediaControlWidget::onStop() {
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
this->setCurrentPosition(0);
}

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