Merge pull request #5069 from stlkr/master

feat(custom): add image
This commit is contained in:
Alexis Rouillard
2026-07-04 00:14:00 +02:00
committed by GitHub
2 changed files with 31 additions and 5 deletions
+26 -3
View File
@@ -8,7 +8,7 @@
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
const Json::Value& config, const std::string& output_name)
: ALabel(config, "custom-" + name, id, "{}"),
: AIconLabel(config, "custom-" + name, id, "{}"),
name_(name),
output_name_(output_name),
id_(id),
@@ -28,6 +28,15 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
} else if (config_["exec"].isString()) {
continuousWorker();
}
if (config_["image-path"].isString()) {
image_path_ = config_["image-path"].asString();
}
if (config_["image-name"].isString()) {
image_name_ = config_["image-name"].asString();
}
if (config["icon-size"].isUInt()) {
app_icon_size_ = config["icon-size"].asUInt();
}
}
waybar::modules::Custom::~Custom() {
@@ -184,7 +193,8 @@ auto waybar::modules::Custom::update() -> void {
auto str = fmt::format(fmt::runtime(format_), fmt::arg("text", text_), fmt::arg("alt", alt_),
fmt::arg("icon", getIcon(percentage_, alt_)),
fmt::arg("percentage", percentage_));
if ((config_["hide-empty-text"].asBool() && text_.empty()) || str.empty()) {
if ((config_["hide-empty-text"].asBool() && text_.empty()) ||
(str.empty() && image_path_.empty() && image_name_.empty())) {
event_box_.hide();
} else {
label_.set_markup(str);
@@ -219,7 +229,19 @@ auto waybar::modules::Custom::update() -> void {
style->add_class("flat");
style->add_class("text-button");
style->add_class(MODULE_CLASS);
auto image_style = image_.get_style_context();
image_style->add_class("image-button");
event_box_.show();
if (!image_path_.empty()) {
auto pixbuf = Gdk::Pixbuf::create_from_file(image_path_, app_icon_size_, app_icon_size_);
image_.set(pixbuf);
} else if (!image_name_.empty()) {
image_.set_from_icon_name(image_name_, Gtk::ICON_SIZE_INVALID);
image_.set_pixel_size(app_icon_size_);
}
image_.set_visible(!image_name_.empty() || !image_path_.empty());
label_.set_visible(!str.empty());
}
} catch (const fmt::format_error& e) {
if (std::strcmp(e.what(), "cannot switch from manual to automatic argument indexing") != 0)
@@ -231,7 +253,7 @@ auto waybar::modules::Custom::update() -> void {
}
}
// Call parent update
ALabel::update();
AIconLabel::update();
}
void waybar::modules::Custom::parseOutputRaw() {
@@ -297,6 +319,7 @@ void waybar::modules::Custom::parseOutputJson() {
class_.push_back(c.asString());
}
}
if (!parsed["percentage"].asString().empty() && parsed["percentage"].isNumeric()) {
percentage_ = (int)lround(parsed["percentage"].asFloat());
} else {