nice control center

This commit is contained in:
2026-02-03 23:03:20 +01:00
parent cddcc96aa9
commit 0463c37543
7 changed files with 235 additions and 32 deletions

View File

@@ -14,12 +14,27 @@ std::shared_ptr<MprisController> MprisController::getInstance() {
return instance;
}
std::shared_ptr<MprisController> MprisController::createForPlayer(const std::string &bus_name) {
return std::shared_ptr<MprisController>(new MprisController(bus_name));
}
MprisController::MprisController() {
Gio::DBus::Connection::get(
Gio::DBus::BusType::SESSION,
sigc::mem_fun(*this, &MprisController::on_bus_connected));
}
MprisController::MprisController(const std::string &bus_name)
: m_player_bus_name(bus_name) {
Gio::DBus::Connection::get(
Gio::DBus::BusType::SESSION,
sigc::mem_fun(*this, &MprisController::on_bus_connected));
}
std::vector<std::string> MprisController::get_registered_players() const {
return std::vector<std::string>(registeredPlayers.begin(), registeredPlayers.end());
}
sigc::signal<void(const MprisPlayer2Message &)> &MprisController::signal_mpris_updated() {
return mprisUpdatedSignal;
}
@@ -32,6 +47,10 @@ sigc::signal<void(int64_t)> &MprisController::signal_playback_position_changed()
return playbackPositionChangedSignal;
}
sigc::signal<void(bool)> &MprisController::signal_can_seek_changed() {
return canSeekChangedSignal;
}
sigc::signal<void(const std::string &)> &MprisController::signal_player_registered() {
return playerRegisteredSignal;
}
@@ -110,6 +129,8 @@ void MprisController::on_dbus_signal(const Glib::ustring &,
void MprisController::handle_player_registered(const std::string &bus_name) {
spdlog::info("MPRIS player registered: {}", bus_name);
registeredPlayers.insert(bus_name);
if (bus_name == m_player_bus_name && !m_proxy && m_connection) {
try {
m_proxy = Gio::DBus::Proxy::create_sync(
@@ -124,6 +145,7 @@ void MprisController::handle_player_registered(const std::string &bus_name) {
signalNotification();
emit_cached_playback_status();
emit_cached_position();
emit_cached_can_seek();
}
} catch (const Glib::Error &ex) {
spdlog::error("DBus Connection Error: {}", ex.what());
@@ -136,6 +158,8 @@ void MprisController::handle_player_registered(const std::string &bus_name) {
void MprisController::handle_player_deregistered(const std::string &bus_name) {
spdlog::info("MPRIS player deregistered: {}", bus_name);
registeredPlayers.erase(bus_name);
if (bus_name == m_player_bus_name) {
m_proxy.reset();
}
@@ -275,6 +299,21 @@ void MprisController::emit_cached_position() {
playbackPositionChangedSignal.emit(static_cast<int64_t>(position));
}
void MprisController::emit_cached_can_seek() {
if (!m_proxy) {
return;
}
Glib::VariantBase can_seek_var;
m_proxy->get_cached_property(can_seek_var, "CanSeek");
if (!can_seek_var || !can_seek_var.is_of_type(Glib::VariantType("b"))) {
return;
}
auto can_seek = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(can_seek_var).get();
canSeekChangedSignal.emit(can_seek);
}
void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedProperties &changed_properties,
const std::vector<Glib::ustring> &) {
@@ -306,6 +345,14 @@ void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedPr
playbackPositionChangedSignal.emit(static_cast<int64_t>(position));
}
}
if (changed_properties.find("CanSeek") != changed_properties.end()) {
auto can_seek_var = changed_properties.at("CanSeek");
if (can_seek_var.is_of_type(Glib::VariantType("b"))) {
auto can_seek = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(can_seek_var).get();
canSeekChangedSignal.emit(can_seek);
}
}
}
void MprisController::previous_song() {

View File

@@ -5,16 +5,99 @@ ControlCenter::ControlCenter(std::string 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(0);
this->container.set_margin_top(0);
this->container.set_margin_bottom(0);
this->container.set_margin_start(0);
this->container.set_margin_end(0);
this->container.set_spacing(10);
this->popover->set_hexpand(false);
this->popover->set_size_request(240, -1);
set_popover_child(this->container);
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->container.append(this->mediaControlWidget);
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->tabRow.append(this->mediaControl);
this->tabRow.append(this->testTabButton);
this->container.append(this->tabRow);
this->contentStack.set_hhomogeneous(false);
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->testLabel.set_text("Test tab");
this->contentStack.add(this->controlCenterContainer, "controls", "Controls");
this->contentStack.add(this->testLabel, "test", "Test");
this->contentStack.set_visible_child("controls");
this->setActiveTab("controls");
this->container.append(this->contentStack);
this->mediaControl.signal_clicked().connect([this]() {
this->setActiveTab("controls");
});
this->testTabButton.signal_clicked().connect([this]() {
this->setActiveTab("test");
});
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 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");
if (tab_name == "controls") {
this->mediaControl.add_css_class("active-button");
} else if (tab_name == "test") {
this->testTabButton.add_css_class("active-button");
}
}
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

@@ -3,14 +3,16 @@
#include "helpers/string.hpp"
#include "services/textureCache.hpp"
MediaControlWidget::MediaControlWidget()
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, 240);
this->set_size_request(200, -1);
this->set_hexpand(false);
this->set_vexpand(false);
this->add_css_class("control-center-spotify-container");
this->add_css_class("control-center-player-container");
this->append(this->topContainer);
this->append(this->seekBarContainer);
@@ -24,7 +26,7 @@ MediaControlWidget::MediaControlWidget()
this->topContainer.set_child(this->imageWrapper);
this->topContainer.set_size_request(200, 100);
this->topContainer.set_size_request(-1, 120);
this->topContainer.set_vexpand(false);
this->topContainer.set_hexpand(true);
@@ -45,6 +47,7 @@ MediaControlWidget::MediaControlWidget()
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);
@@ -54,7 +57,6 @@ MediaControlWidget::MediaControlWidget()
this->seekBar.set_value(0);
this->seekBar.set_orientation(Gtk::Orientation::HORIZONTAL);
this->seekBar.set_draw_value(false);
this->seekBar.set_size_request(120, -1);
this->seekBar.set_hexpand(true);
this->seekBar.set_halign(Gtk::Align::CENTER);
this->seekBar.add_css_class("control-center-seek-bar");
@@ -98,15 +100,14 @@ MediaControlWidget::MediaControlWidget()
this->bottomContainer.append(this->nextButton);
this->previousButton.set_label("\u23EE"); // Previous track symbol
this->previousButton.add_css_class("notification-button");
this->previousButton.add_css_class("notification-icon-button");
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("notification-button");
this->playPauseButton.add_css_class("notification-icon-button");
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("notification-button");
this->nextButton.add_css_class("notification-icon-button");
this->nextButton.add_css_class("button");
this->nextButton.add_css_class("material-icons");
this->previousButton.signal_clicked().connect([this]() {
this->mprisController->previous_song();
});
@@ -130,14 +131,24 @@ MediaControlWidget::MediaControlWidget()
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-spotify-artist-label");
this->artistLabel.add_css_class("control-center-player-artist-label");
this->titleLabel.set_text("Song Title");
this->titleLabel.add_css_class("control-center-spotify-title-label");
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()) {