Merge pull request #2203 from rodrgz/mprisImprovement

mpris: dynamic tag ordering and separator customization
This commit is contained in:
Alexis Rouillard 2023-07-04 22:44:21 +02:00 committed by GitHub
commit 0d1016d4d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 32 deletions

View File

@ -66,6 +66,8 @@ class Mpris : public ALabel {
int album_len_; int album_len_;
int title_len_; int title_len_;
int dynamic_len_; int dynamic_len_;
std::string dynamic_separator_;
std::vector<std::string> dynamic_order_;
std::vector<std::string> dynamic_prio_; std::vector<std::string> dynamic_prio_;
bool truncate_hours_; bool truncate_hours_;
bool tooltip_len_limits_; bool tooltip_len_limits_;

View File

@ -71,11 +71,27 @@ The *mpris* module displays currently playing media via libplayerctl.
something less than or equal to this value, so the title will always be ++ something less than or equal to this value, so the title will always be ++
displayed. displayed.
*dynamic-priority*: ++ *dynamic-order*: ++
typeof: []string ++ typeof: []string ++
default: ["title", "length", "position", "artist", "album"] ++ default: ["title", "artist", "album", "position", "length"] ++
Priority of the tags when truncating the Dynamic tag (absence in this Order of the tags shown by Dynamic tag. The position and length tags ++
list means force inclusion). will always be combined in the format [{position}/{length}]. The order ++
of these tags in relation to other tags will be determined based on the ++
declaration of the first among the two tags. Absence in this list means ++
force exclusion.
*dynamic-importance-order*: ++
typeof: []string ++
default: ["title", "artist", "album", "position", "length"] ++
Priority of the tags when truncating the Dynamic tag. The final ones ++
will be the first to be truncated. Absence in this list means force ++
inclusion.
*dynamic-separator*: ++
typeof: string ++
default: " - " ++
These characters will be used to separate two different tags, except ++
when one of these tags is position and length.
*truncate-hours*: ++ *truncate-hours*: ++
typeof: bool ++ typeof: bool ++

View File

@ -24,7 +24,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
album_len_(-1), album_len_(-1),
title_len_(-1), title_len_(-1),
dynamic_len_(-1), dynamic_len_(-1),
dynamic_prio_({"title", "length", "position", "artist", "album"}), dynamic_prio_({"title", "artist", "album", "position", "length"}),
dynamic_order_({"title", "artist", "album", "position", "length"}),
dynamic_separator_(" - "),
truncate_hours_(true), truncate_hours_(true),
tooltip_len_limits_(false), tooltip_len_limits_(false),
// this character is used in Gnome so it's fine to use it here // this character is used in Gnome so it's fine to use it here
@ -45,6 +47,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
if (config_["ellipsis"].isString()) { if (config_["ellipsis"].isString()) {
ellipsis_ = config_["ellipsis"].asString(); ellipsis_ = config_["ellipsis"].asString();
} }
if (config_["dynamic-separator"].isString()) {
dynamic_separator_ = config_["dynamic-separator"].asString();
}
if (tooltipEnabled()) { if (tooltipEnabled()) {
if (config_["tooltip-format"].isString()) { if (config_["tooltip-format"].isString()) {
tooltip_ = config_["tooltip-format"].asString(); tooltip_ = config_["tooltip-format"].asString();
@ -75,12 +80,21 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
if (config["dynamic-len"].isUInt()) { if (config["dynamic-len"].isUInt()) {
dynamic_len_ = config["dynamic-len"].asUInt(); dynamic_len_ = config["dynamic-len"].asUInt();
} }
if (config_["dynamic-priority"].isArray()) { // "dynamic-priority" has been kept for backward compatibility
if (config_["dynamic-importance-order"].isArray() || config_["dynamic-priority"].isArray()) {
dynamic_prio_.clear(); dynamic_prio_.clear();
for (auto it = config_["dynamic-priority"].begin(); it != config_["dynamic-priority"].end(); const auto& dynamic_priority = config_["dynamic-importance-order"].isArray() ? config_["dynamic-importance-order"] : config_["dynamic-priority"];
++it) { for (const auto& value : dynamic_priority) {
if (value.isString()) {
dynamic_prio_.push_back(value.asString());
}
}
}
if (config_["dynamic-order"].isArray()) {
dynamic_order_.clear();
for (auto it = config_["dynamic-order"].begin(); it != config_["dynamic-order"].end(); ++it) {
if (it->isString()) { if (it->isString()) {
dynamic_prio_.push_back(it->asString()); dynamic_order_.push_back(it->asString());
} }
} }
} }
@ -273,18 +287,28 @@ auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) ->
size_t lengthLen = length.length(); size_t lengthLen = length.length();
size_t posLen = position.length(); size_t posLen = position.length();
bool showArtist = artistLen != 0; bool showArtist = (artistLen != 0) && (std::find(dynamic_order_.begin(), dynamic_order_.end(),
bool showAlbum = albumLen != 0; "artist") != dynamic_order_.end());
bool showTitle = titleLen != 0; bool showAlbum = (albumLen != 0) && (std::find(dynamic_order_.begin(), dynamic_order_.end(),
bool showLength = lengthLen != 0; "album") != dynamic_order_.end());
bool showPos = posLen != 0; bool showTitle = (titleLen != 0) && (std::find(dynamic_order_.begin(), dynamic_order_.end(),
"title") != dynamic_order_.end());
bool showLength = (lengthLen != 0) && (std::find(dynamic_order_.begin(), dynamic_order_.end(),
"length") != dynamic_order_.end());
bool showPos = (posLen != 0) && (std::find(dynamic_order_.begin(), dynamic_order_.end(),
"position") != dynamic_order_.end());
if (truncated && dynamic_len_ >= 0) { if (truncated && dynamic_len_ >= 0) {
size_t dynamicLen = dynamic_len_; //Since the first element doesn't present a separator and we don't know a priori which one
if (showArtist) artistLen += 3; //it will be, we add a "virtual separatorLen" to the dynamicLen, since we are adding the
if (showAlbum) albumLen += 3; //separatorLen to all the other lengths.
if (showLength) lengthLen += 3; size_t separatorLen = utf8_width(dynamic_separator_);
if (showPos) posLen += 3; size_t dynamicLen = dynamic_len_ + separatorLen;
if (showArtist) artistLen += separatorLen;
if (showAlbum) albumLen += separatorLen;
if (showTitle) albumLen += separatorLen;
if (showLength) lengthLen += separatorLen;
if (showPos) posLen += separatorLen;
size_t totalLen = 0; size_t totalLen = 0;
@ -331,12 +355,34 @@ auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) ->
album = Glib::Markup::escape_text(album); album = Glib::Markup::escape_text(album);
title = Glib::Markup::escape_text(title); title = Glib::Markup::escape_text(title);
} }
if (showArtist) dynamic << artist << " - ";
if (showAlbum) dynamic << album << " - "; bool lengthOrPositionShown = false;
if (showTitle) dynamic << title; bool previousShown = false;
if (showLength || showPos) { std::string previousOrder = "";
dynamic << ' ';
for (const std::string& order : dynamic_order_) {
if ((order == "artist" && showArtist) ||
(order == "album" && showAlbum) ||
(order == "title" && showTitle)) {
if (previousShown &&
previousOrder != "length" &&
previousOrder != "position") {
dynamic << dynamic_separator_;
}
if (order == "artist") {
dynamic << artist;
} else if (order == "album") {
dynamic << album;
} else if (order == "title") {
dynamic << title;
}
previousShown = true;
} else if (order == "length" || order == "position") {
if (!lengthOrPositionShown && (showLength || showPos)) {
if (html) dynamic << "<small>"; if (html) dynamic << "<small>";
if (previousShown) dynamic << ' ';
dynamic << '['; dynamic << '[';
if (showPos) { if (showPos) {
dynamic << position; dynamic << position;
@ -344,7 +390,12 @@ auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) ->
} }
if (showLength) dynamic << length; if (showLength) dynamic << length;
dynamic << ']'; dynamic << ']';
if (!dynamic.str().empty()) dynamic << ' ';
if (html) dynamic << "</small>"; if (html) dynamic << "</small>";
lengthOrPositionShown = true;
}
}
previousOrder = order;
} }
return dynamic.str(); return dynamic.str();
} }