mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
ISSUE#1977. AModule implements module actions call
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
@ -32,6 +32,18 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
|
||||
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
||||
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
|
||||
}
|
||||
|
||||
// Configure module action Map
|
||||
const Json::Value actions{config_["actions"]};
|
||||
for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) {
|
||||
if (it.key().isString() && it->isString())
|
||||
if (eventActionMap_.count(it.key().asString()) == 0)
|
||||
eventActionMap_.insert({it.key().asString(), it->asString()});
|
||||
else
|
||||
spdlog::warn("Dublicate action is ignored: {0}", it.key().asString());
|
||||
else
|
||||
spdlog::warn("Wrong actions section configuration. See config by index: {}", it.index());
|
||||
}
|
||||
}
|
||||
|
||||
AModule::~AModule() {
|
||||
@ -48,19 +60,33 @@ auto AModule::update() -> void {
|
||||
pid_.push_back(util::command::forkExec(config_["on-update"].asString()));
|
||||
}
|
||||
}
|
||||
// Get mapping between event name and module action name
|
||||
// Then call overrided doAction in order to call appropriate module action
|
||||
auto AModule::doAction(const std::string& name) -> void {
|
||||
if (!name.empty()) {
|
||||
const std::map<std::string, std::string>::const_iterator& recA{eventActionMap_.find(name)};
|
||||
// Call overrided action if derrived class has implemented it
|
||||
if (recA != eventActionMap_.cend() && name != recA->second) this->doAction(recA->second);
|
||||
}
|
||||
}
|
||||
|
||||
bool AModule::handleToggle(GdkEventButton* const& e) {
|
||||
std::string format{};
|
||||
const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{
|
||||
eventMap_.find(std::pair(e->button, e->type))};
|
||||
std::string format{(rec != eventMap_.cend()) ? rec->second : std::string{""}};
|
||||
if (rec != eventMap_.cend()) {
|
||||
// First call module actions
|
||||
this->AModule::doAction(rec->second);
|
||||
|
||||
format = rec->second;
|
||||
}
|
||||
// Second call user scripts
|
||||
if (!format.empty()) {
|
||||
if (config_[format].isString())
|
||||
format = config_[format].asString();
|
||||
else
|
||||
format.clear();
|
||||
}
|
||||
|
||||
if (!format.empty()) {
|
||||
pid_.push_back(util::command::forkExec(format));
|
||||
}
|
||||
@ -122,11 +148,19 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) {
|
||||
|
||||
bool AModule::handleScroll(GdkEventScroll* e) {
|
||||
auto dir = getScrollDir(e);
|
||||
if (dir == SCROLL_DIR::UP && config_["on-scroll-up"].isString()) {
|
||||
pid_.push_back(util::command::forkExec(config_["on-scroll-up"].asString()));
|
||||
} else if (dir == SCROLL_DIR::DOWN && config_["on-scroll-down"].isString()) {
|
||||
pid_.push_back(util::command::forkExec(config_["on-scroll-down"].asString()));
|
||||
}
|
||||
std::string eventName{};
|
||||
|
||||
if (dir == SCROLL_DIR::UP)
|
||||
eventName = "on-scroll-up";
|
||||
else if (dir == SCROLL_DIR::DOWN)
|
||||
eventName = "on-scroll-down";
|
||||
|
||||
// First call module actions
|
||||
this->AModule::doAction(eventName);
|
||||
// Second call user scripts
|
||||
if (config_[eventName].isString())
|
||||
pid_.push_back(util::command::forkExec(config_[eventName].asString()));
|
||||
|
||||
dp.emit();
|
||||
return true;
|
||||
}
|
||||
|
@ -125,16 +125,6 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
||||
return false;
|
||||
});
|
||||
}
|
||||
if (config_[kCalendarPlaceholder]["on-click-left"].isString()) {
|
||||
if (config_[kCalendarPlaceholder]["on-click-left"].asString() == "mode")
|
||||
eventMap_.insert({std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS),
|
||||
&waybar::modules::Clock::cldModeSwitch});
|
||||
}
|
||||
if (config_[kCalendarPlaceholder]["on-click-right"].isString()) {
|
||||
if (config_[kCalendarPlaceholder]["on-click-right"].asString() == "mode")
|
||||
eventMap_.insert({std::make_pair(3, GdkEventType::GDK_BUTTON_PRESS),
|
||||
&waybar::modules::Clock::cldModeSwitch});
|
||||
}
|
||||
}
|
||||
|
||||
if (config_["locale"].isString())
|
||||
@ -203,56 +193,12 @@ auto waybar::modules::Clock::update() -> void {
|
||||
ALabel::update();
|
||||
}
|
||||
|
||||
bool waybar::modules::Clock::handleToggle(GdkEventButton* const& e) {
|
||||
const std::map<std::pair<uint, GdkEventType>, void (waybar::modules::Clock::*)()>::const_iterator&
|
||||
rec{eventMap_.find(std::pair(e->button, e->type))};
|
||||
|
||||
const auto callMethod{(rec != eventMap_.cend()) ? rec->second : nullptr};
|
||||
|
||||
if (callMethod) {
|
||||
(this->*callMethod)();
|
||||
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
|
||||
if ((actionMap_[name])) {
|
||||
(this->*actionMap_[name])();
|
||||
update();
|
||||
} else
|
||||
return ALabel::handleToggle(e);
|
||||
|
||||
update();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
|
||||
// defer to user commands if set
|
||||
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
|
||||
return AModule::handleScroll(e);
|
||||
}
|
||||
|
||||
auto dir = AModule::getScrollDir(e);
|
||||
|
||||
// Shift calendar date
|
||||
if (cldShift_.count() != 0) {
|
||||
if (dir == SCROLL_DIR::UP)
|
||||
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
||||
else
|
||||
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
||||
} else {
|
||||
// Change time zone
|
||||
if (dir != SCROLL_DIR::UP && dir != SCROLL_DIR::DOWN) {
|
||||
return true;
|
||||
}
|
||||
if (time_zones_.size() == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto nr_zones = time_zones_.size();
|
||||
if (dir == SCROLL_DIR::UP) {
|
||||
size_t new_idx = current_time_zone_idx_ + 1;
|
||||
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
|
||||
} else {
|
||||
current_time_zone_idx_ =
|
||||
current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
|
||||
}
|
||||
}
|
||||
|
||||
update();
|
||||
return true;
|
||||
spdlog::error("Clock. Unsupported action \"{0}\"", name);
|
||||
}
|
||||
|
||||
// The number of weeks in calendar month layout plus 1 more for calendar titles
|
||||
@ -461,9 +407,31 @@ auto waybar::modules::Clock::get_calendar(const date::zoned_seconds& now,
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/*Clock actions*/
|
||||
void waybar::modules::Clock::cldModeSwitch() {
|
||||
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR;
|
||||
}
|
||||
void waybar::modules::Clock::cldShift_up() {
|
||||
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
||||
}
|
||||
void waybar::modules::Clock::cldShift_down() {
|
||||
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
||||
}
|
||||
void waybar::modules::Clock::tz_up() {
|
||||
auto nr_zones = time_zones_.size();
|
||||
|
||||
if (nr_zones == 1) return;
|
||||
|
||||
size_t new_idx = current_time_zone_idx_ + 1;
|
||||
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
|
||||
}
|
||||
void waybar::modules::Clock::tz_down() {
|
||||
auto nr_zones = time_zones_.size();
|
||||
|
||||
if (nr_zones == 1) return;
|
||||
|
||||
current_time_zone_idx_ = current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
|
||||
}
|
||||
|
||||
auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point* now)
|
||||
-> std::string {
|
||||
|
Reference in New Issue
Block a user