Merge branch 'dsp' of https://github.com/kennypm/Waybar into dsp

pull/1653/head
kennypm 2022-08-11 17:26:45 -04:00
commit 4336f10b29
3 changed files with 45 additions and 13 deletions

View File

@ -41,6 +41,7 @@ class MPD : public ALabel {
private:
std::string getTag(mpd_tag_type type, unsigned idx = 0) const;
std::string getFilename() const;
void setLabel();
std::string getStateIcon() const;
std::string getOptionIcon(std::string optionName, bool activated) const;

View File

@ -73,6 +73,16 @@ std::string waybar::modules::MPD::getTag(mpd_tag_type type, unsigned idx) const
return result;
}
std::string waybar::modules::MPD::getFilename() const {
std::string path = mpd_song_get_uri(song_.get());
size_t position = path.find_last_of("/");
if (position == std::string::npos) {
return path;
} else {
return path.substr(position + 1);
}
}
void waybar::modules::MPD::setLabel() {
if (connection_ == nullptr) {
label_.get_style_context()->add_class("disconnected");
@ -100,7 +110,7 @@ void waybar::modules::MPD::setLabel() {
auto format = format_;
Glib::ustring artist, album_artist, album, title;
std::string date;
std::string date, filename;
int song_pos = 0, queue_length = 0, volume = 0;
std::chrono::seconds elapsedTime, totalTime;
@ -130,6 +140,7 @@ void waybar::modules::MPD::setLabel() {
album = getTag(MPD_TAG_ALBUM);
title = getTag(MPD_TAG_TITLE);
date = getTag(MPD_TAG_DATE);
filename = getFilename();
song_pos = mpd_status_get_song_pos(status_.get()) + 1;
volume = mpd_status_get_volume(status_.get());
if (volume < 0) {
@ -165,7 +176,8 @@ void waybar::modules::MPD::setLabel() {
fmt::arg("totalTime", totalTime), fmt::arg("songPosition", song_pos),
fmt::arg("queueLength", queue_length), fmt::arg("stateIcon", stateIcon),
fmt::arg("consumeIcon", consumeIcon), fmt::arg("randomIcon", randomIcon),
fmt::arg("repeatIcon", repeatIcon), fmt::arg("singleIcon", singleIcon)));
fmt::arg("repeatIcon", repeatIcon), fmt::arg("singleIcon", singleIcon),
fmt::arg("filename", filename)));
} catch (fmt::format_error const& e) {
spdlog::warn("mpd: format error: {}", e.what());
}

View File

@ -300,10 +300,6 @@ void Item::updateImage() {
auto pixbuf = getIconPixbuf();
auto scaled_icon_size = getScaledIconSize();
if (!pixbuf) {
pixbuf = getIconByName("image-missing", getScaledIconSize());
}
// If the loaded icon is not square, assume that the icon height should match the
// requested icon size, but the width is allowed to be different. As such, if the
// height of the image does not match the requested icon size, resize the icon such that
@ -318,19 +314,42 @@ void Item::updateImage() {
}
Glib::RefPtr<Gdk::Pixbuf> Item::getIconPixbuf() {
try {
if (!icon_name.empty()) {
try {
std::ifstream temp(icon_name);
if (temp.is_open()) {
return Gdk::Pixbuf::create_from_file(icon_name);
}
} catch (Glib::Error& e) {
// Ignore because we want to also try different methods of getting an icon.
//
// But a warning is logged, as the file apparently exists, but there was
// a failure in creating a pixbuf out of it.
spdlog::warn("Item '{}': {}", id, static_cast<std::string>(e.what()));
}
try {
// Will throw if it can not find an icon.
return getIconByName(icon_name, getScaledIconSize());
} else if (icon_pixmap) {
} catch (Glib::Error& e) {
spdlog::trace("Item '{}': {}", id, static_cast<std::string>(e.what()));
}
}
// Return the pixmap only if an icon for the given name could not be found.
if (icon_pixmap) {
return icon_pixmap;
}
} catch (Glib::Error& e) {
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
if (icon_name.empty()) {
spdlog::error("Item '{}': No icon name or pixmap given.", id);
} else {
spdlog::error("Item '{}': Could not find an icon named '{}' and no pixmap given.", id, icon_name);
}
return getIconByName("image-missing", getScaledIconSize());
}