mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Merge pull request #1397 from JakeStanger/feat/image-module
Resolves https://github.com/Alexays/Waybar/issues/1191
This commit is contained in:
@ -148,6 +148,8 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
||||
}
|
||||
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
|
||||
return new waybar::modules::Custom(ref.substr(7), id, config_[name]);
|
||||
} else if (ref.compare(0, 6, "image/") == 0 && ref.size() > 6) {
|
||||
return new waybar::modules::Image(ref.substr(6), id, config_[name]);
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
auto err = fmt::format("Disabling module \"{}\", {}", name, e.what());
|
||||
|
59
src/modules/image.cpp
Normal file
59
src/modules/image.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "modules/image.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
waybar::modules::Image::Image(const std::string& name, const std::string& id,
|
||||
const Json::Value& config)
|
||||
: AModule(config, "image-" + name, id, "{}") {
|
||||
event_box_.add(image_);
|
||||
|
||||
dp.emit();
|
||||
|
||||
path_ = config["path"].asString();
|
||||
size_ = config["size"].asInt();
|
||||
|
||||
interval_ = config_["interval"].asInt();
|
||||
|
||||
if (size_ == 0) {
|
||||
size_ = 16;
|
||||
}
|
||||
|
||||
if (interval_ == 0) {
|
||||
interval_ = INT_MAX;
|
||||
}
|
||||
|
||||
delayWorker();
|
||||
}
|
||||
|
||||
void waybar::modules::Image::delayWorker() {
|
||||
thread_ = [this] {
|
||||
dp.emit();
|
||||
auto interval = std::chrono::seconds(interval_);
|
||||
thread_.sleep_for(interval);
|
||||
};
|
||||
}
|
||||
|
||||
void waybar::modules::Image::refresh(int sig) {
|
||||
if (sig == SIGRTMIN + config_["signal"].asInt()) {
|
||||
thread_.wake_up();
|
||||
}
|
||||
}
|
||||
|
||||
auto waybar::modules::Image::update() -> void {
|
||||
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
|
||||
|
||||
if (Glib::file_test(path_, Glib::FILE_TEST_EXISTS))
|
||||
pixbuf = Gdk::Pixbuf::create_from_file(path_, size_, size_);
|
||||
else
|
||||
pixbuf = {};
|
||||
|
||||
if (pixbuf) {
|
||||
image_.set(pixbuf);
|
||||
image_.show();
|
||||
} else {
|
||||
image_.clear();
|
||||
image_.hide();
|
||||
}
|
||||
|
||||
AModule::update();
|
||||
}
|
Reference in New Issue
Block a user