Merge remote-tracking branch 'origin/master' into pr-3151
# Conflicts: # include/modules/image.hpp # src/modules/image.cpp
This commit is contained in:
+42
-21
@@ -2,22 +2,36 @@
|
||||
|
||||
#include <json/value.h>
|
||||
|
||||
#include <config.hpp>
|
||||
|
||||
waybar::modules::Image::Image(const std::string& id, const Json::Value& config)
|
||||
: AModule(config, "image", id) {
|
||||
strategy_ = getStrategy(id, config, MODULE_CLASS, event_box_, tooltipEnabled());
|
||||
|
||||
interval_ = config_["interval"].asInt();
|
||||
|
||||
if (interval_ == 0) {
|
||||
interval_ = INT_MAX;
|
||||
const auto once = std::chrono::milliseconds::max();
|
||||
if (!config_.isMember("interval") || config_["interval"].isNull() ||
|
||||
config_["interval"] == "once") {
|
||||
interval_ = once;
|
||||
} else if (config_["interval"].isNumeric()) {
|
||||
const auto interval_seconds = config_["interval"].asDouble();
|
||||
if (interval_seconds <= 0) {
|
||||
interval_ = once;
|
||||
} else {
|
||||
interval_ =
|
||||
std::chrono::milliseconds(std::max(1L, // Minimum 1ms due to millisecond precision
|
||||
static_cast<long>(interval_seconds * 1000)));
|
||||
}
|
||||
} else {
|
||||
interval_ = once;
|
||||
}
|
||||
|
||||
delayWorker();
|
||||
}
|
||||
|
||||
auto waybar::modules::Image::getStrategy(
|
||||
const std::string& id, const Json::Value& cfg, const std::string& module, Gtk::EventBox& evbox,
|
||||
bool hasTooltip) -> std::unique_ptr<waybar::modules::image::IStrategy> {
|
||||
auto waybar::modules::Image::getStrategy(const std::string& id, const Json::Value& cfg,
|
||||
const std::string& module, Gtk::EventBox& evbox,
|
||||
bool hasTooltip)
|
||||
-> std::unique_ptr<waybar::modules::image::IStrategy> {
|
||||
std::unique_ptr<waybar::modules::image::IStrategy> strat;
|
||||
if (!cfg["multiple"].empty() && cfg["multiple"].asBool()) {
|
||||
strat = std::make_unique<waybar::modules::image::MultipleImageStrategy>(id, cfg, module, evbox);
|
||||
@@ -32,15 +46,16 @@ auto waybar::modules::Image::getStrategy(
|
||||
void waybar::modules::Image::delayWorker() {
|
||||
thread_ = [this] {
|
||||
dp.emit();
|
||||
auto interval = std::chrono::seconds(interval_);
|
||||
thread_.sleep_for(interval);
|
||||
thread_.sleep_for(interval_);
|
||||
};
|
||||
}
|
||||
|
||||
void waybar::modules::Image::refresh(int sig) {
|
||||
if (sig == SIGRTMIN + config_["signal"].asInt()) {
|
||||
#ifdef SIGRTMIN
|
||||
if (config_["signal"].isInt() && sig == SIGRTMIN + config_["signal"].asInt()) {
|
||||
thread_.wake_up();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
auto waybar::modules::Image::update() -> void {
|
||||
@@ -218,28 +233,34 @@ SingleImageStrategy::SingleImageStrategy(const std::string& id, const Json::Valu
|
||||
}
|
||||
|
||||
void SingleImageStrategy::update() {
|
||||
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
|
||||
if (config_["path"].isString()) {
|
||||
path_ = config_["path"].asString();
|
||||
auto result = Config::tryExpandPath(config_["path"].asString(), "");
|
||||
path_ = result.empty() ? "" : result.front();
|
||||
} else if (config_["exec"].isString()) {
|
||||
output_ = util::command::exec(config_["exec"].asString(), "");
|
||||
parseOutputRaw();
|
||||
} else {
|
||||
path_ = "";
|
||||
// expand path if "~" or "$HOME" is present in original path
|
||||
auto result = Config::tryExpandPath(path_, "");
|
||||
path_ = result.empty() ? "" : result.front();
|
||||
}
|
||||
if (Glib::file_test(path_, Glib::FILE_TEST_EXISTS))
|
||||
pixbuf = Gdk::Pixbuf::create_from_file(path_, size_, size_);
|
||||
else
|
||||
pixbuf = {};
|
||||
|
||||
if (pixbuf) {
|
||||
if (Glib::file_test(path_, Glib::FILE_TEST_EXISTS)) {
|
||||
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
|
||||
|
||||
int scaled_icon_size = size_ * image_.get_scale_factor();
|
||||
pixbuf = Gdk::Pixbuf::create_from_file(path_, scaled_icon_size, scaled_icon_size);
|
||||
|
||||
auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image_.get_scale_factor(),
|
||||
image_.get_window());
|
||||
image_.set(surface);
|
||||
image_.show();
|
||||
|
||||
if (hasTooltip_ && !tooltip_.empty()) {
|
||||
if (box_.get_tooltip_markup() != tooltip_) {
|
||||
box_.set_tooltip_markup(tooltip_);
|
||||
}
|
||||
}
|
||||
image_.set(pixbuf);
|
||||
image_.show();
|
||||
|
||||
box_.get_style_context()->remove_class("empty");
|
||||
} else {
|
||||
image_.clear();
|
||||
|
||||
Reference in New Issue
Block a user