some refactore

This commit is contained in:
2026-02-05 14:33:19 +01:00
parent ce0643b6ac
commit 64b3babd3d
27 changed files with 239 additions and 141 deletions

View File

@@ -1,32 +1,40 @@
#include "widgets/controlCenter/controlCenter.hpp"
#include "components/button/iconButton.hpp"
#include "components/button/tabButton.hpp"
ControlCenter::ControlCenter(std::string icon, std::string name)
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->popover->set_hexpand(false);
this->popover->set_size_request(240, -1);
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);
this->scrollview.set_vexpand(true);
this->scrollview.set_propagate_natural_height(true);
set_popover_child(this->container);
set_popover_child(this->scrollview);
this->tabRow.set_orientation(Gtk::Orientation::HORIZONTAL);
this->tabRow.set_spacing(4);
this->tabRow.set_margin_bottom(4);
this->tabRow.add_css_class("control-center-tab-row");
this->mediaControl.set_label("\uf5d3"); // control icon
this->mediaControl.add_css_class("tab-icon");
this->testTabButton.set_label("\uE5CA"); // test icon
this->testTabButton.add_css_class("tab-icon");
this->mediaControl = std::make_unique<TabButton>(Icon::PLAY_CIRCLE);
this->testTabButton = std::make_unique<TabButton>(Icon::EMPTY_DASHBOARD);
this->tabRow.append(this->mediaControl);
this->tabRow.append(this->testTabButton);
this->tabRow.append(*this->mediaControl);
this->tabRow.append(*this->testTabButton);
this->container.append(this->tabRow);
this->contentStack.set_hhomogeneous(false);
this->contentStack.set_hhomogeneous(true);
this->contentStack.set_vhomogeneous(false);
this->contentStack.set_transition_type(Gtk::StackTransitionType::CROSSFADE);
this->contentStack.set_transition_duration(150);
@@ -41,11 +49,11 @@ ControlCenter::ControlCenter(std::string icon, std::string name)
this->container.append(this->contentStack);
this->mediaControl.signal_clicked().connect([this]() {
this->mediaControl->signal_clicked().connect([this]() {
this->setActiveTab("controls");
});
this->testTabButton.signal_clicked().connect([this]() {
this->testTabButton->signal_clicked().connect([this]() {
this->setActiveTab("test");
});
@@ -68,13 +76,13 @@ ControlCenter::ControlCenter(std::string icon, std::string name)
void ControlCenter::setActiveTab(const std::string &tab_name) {
this->contentStack.set_visible_child(tab_name);
this->mediaControl.remove_css_class("active-button");
this->testTabButton.remove_css_class("active-button");
this->mediaControl->setActive(false);
this->testTabButton->setActive(false);
if (tab_name == "controls") {
this->mediaControl.add_css_class("active-button");
this->mediaControl->setActive(true);
} else if (tab_name == "test") {
this->testTabButton.add_css_class("active-button");
this->testTabButton->setActive(true);
}
}

View File

@@ -1,15 +1,34 @@
#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_size_request(200, -1);
this->set_hexpand(false);
this->set_vexpand(false);
this->add_css_class("control-center-player-container");
@@ -88,6 +107,10 @@ MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controll
}
});
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);
@@ -95,26 +118,17 @@ MediaControlWidget::MediaControlWidget(std::shared_ptr<MprisController> controll
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->bottomContainer.append(*this->previousButton);
this->bottomContainer.append(*this->playPauseButton);
this->bottomContainer.append(*this->nextButton);
this->previousButton.set_label("\u23EE"); // Previous track symbol
this->previousButton.add_css_class("button");
this->previousButton.add_css_class("material-icons");
this->playPauseButton.set_label("\u23EF"); // Play/Pause symbol
this->playPauseButton.add_css_class("button");
this->playPauseButton.add_css_class("material-icons");
this->nextButton.set_label("\u23ED"); // Next track symbol
this->nextButton.add_css_class("button");
this->nextButton.add_css_class("material-icons");
this->previousButton.signal_clicked().connect([this]() {
this->previousButton->signal_clicked().connect([this]() {
this->mprisController->previous_song();
});
this->playPauseButton.signal_clicked().connect([this]() {
this->playPauseButton->signal_clicked().connect([this]() {
this->mprisController->toggle_play();
});
this->nextButton.signal_clicked().connect([this]() {
this->nextButton->signal_clicked().connect([this]() {
this->mprisController->next_song();
});
@@ -179,10 +193,7 @@ void MediaControlWidget::onSpotifyMprisUpdated(const MprisPlayer2Message &messag
void MediaControlWidget::setCurrentPosition(int64_t position_us) {
this->currentPositionUs = position_us;
int64_t seconds = (position_us / 1000000) % 60;
int64_t minutes = (position_us / (1000000 * 60)) % 60;
this->currentTimeLabel.set_text(
std::to_string(minutes) + ":" + (seconds < 10 ? "0" : "") + std::to_string(seconds));
this->currentTimeLabel.set_text(formatTimeUs(position_us));
if (totalLengthUs > 0) {
double fraction = static_cast<double>(currentPositionUs) / static_cast<double>(totalLengthUs);
this->suppressSeekSignal = true;
@@ -193,10 +204,7 @@ void MediaControlWidget::setCurrentPosition(int64_t position_us) {
void MediaControlWidget::setTotalLength(int64_t length_us) {
this->totalLengthUs = length_us;
int64_t seconds = (length_us / 1000000) % 60;
int64_t minutes = (length_us / (1000000 * 60)) % 60;
this->totalTimeLabel.set_text(
std::to_string(minutes) + ":" + (seconds < 10 ? "0" : "") + std::to_string(seconds));
this->totalTimeLabel.set_text(formatTimeUs(length_us));
}
void MediaControlWidget::resetSeekTimer(int64_t start_position_us) {
@@ -248,20 +256,21 @@ void MediaControlWidget::onRunningStateChanged(MprisController::PlaybackStatus s
}
void MediaControlWidget::onPlay() {
this->playPauseButton.set_label("\u23F8"); // Pause symbol
// strart seek timer if not already running
this->playPauseButton->setIcon(Icon::PAUSE);
this->resetSeekTimer(currentPositionUs);
}
void MediaControlWidget::onPause() {
this->playPauseButton.set_label("\u23F5"); // Play symbol
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}
}
void MediaControlWidget::onStop() {
this->playPauseButton.set_label("\u23F9"); // stop symbol
this->playPauseButton->setIcon(Icon::PLAY_ARROW);
if (seekTimerConnection.connected()) {
seekTimerConnection.disconnect();
}