ISSUE#1977. AModule implements module actions call

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
Viktar Lukashonak 2023-02-28 15:32:28 +03:00
parent 09142fa322
commit e25a7c9719
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
5 changed files with 96 additions and 74 deletions

View File

@ -11,16 +11,20 @@ namespace waybar {
class AModule : public IModule { class AModule : public IModule {
public: public:
AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
bool enable_scroll = false);
virtual ~AModule(); virtual ~AModule();
virtual auto update() -> void; virtual auto update() -> void;
virtual auto refresh(int) -> void{}; virtual auto refresh(int) -> void{};
virtual operator Gtk::Widget &(); virtual operator Gtk::Widget &();
virtual auto doAction(const std::string& name) -> void;
Glib::Dispatcher dp; Glib::Dispatcher dp;
protected: protected:
// Don't need to make an object directly
// Derived classes are able to use it
AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
bool enable_scroll = false);
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT }; enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
SCROLL_DIR getScrollDir(GdkEventScroll *e); SCROLL_DIR getScrollDir(GdkEventScroll *e);
@ -37,6 +41,7 @@ class AModule : public IModule {
std::vector<int> pid_; std::vector<int> pid_;
gdouble distance_scrolled_y_; gdouble distance_scrolled_y_;
gdouble distance_scrolled_x_; gdouble distance_scrolled_x_;
std::map<std::string, std::string> eventActionMap_;
static const inline std::map<std::pair<uint, GdkEventType>, std::string> eventMap_{ static const inline std::map<std::pair<uint, GdkEventType>, std::string> eventMap_{
{std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"}, {std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"},
{std::make_pair(1, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click"}, {std::make_pair(1, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click"},

View File

@ -9,6 +9,7 @@ class IModule {
virtual ~IModule() = default; virtual ~IModule() = default;
virtual auto update() -> void = 0; virtual auto update() -> void = 0;
virtual operator Gtk::Widget&() = 0; virtual operator Gtk::Widget&() = 0;
virtual auto doAction(const std::string& name) -> void = 0;
}; };
} // namespace waybar } // namespace waybar

View File

@ -15,26 +15,26 @@ enum class WeeksSide {
HIDDEN, HIDDEN,
}; };
enum class CldMode { MONTH, YEAR }; enum class CldMode {
MONTH,
YEAR
};
class Clock : public ALabel { class Clock final : public ALabel {
public: public:
Clock(const std::string&, const Json::Value&); Clock(const std::string&, const Json::Value&);
~Clock() = default; ~Clock() = default;
auto update() -> void; auto update() -> void;
auto doAction(const std::string& name) -> void override;
private: private:
util::SleeperThread thread_; util::SleeperThread thread_;
std::map<std::pair<uint, GdkEventType>, void (waybar::modules::Clock::*)()> eventMap_;
std::locale locale_; std::locale locale_;
std::vector<const date::time_zone*> time_zones_; std::vector<const date::time_zone*> time_zones_;
int current_time_zone_idx_; int current_time_zone_idx_;
bool is_calendar_in_tooltip_; bool is_calendar_in_tooltip_;
bool is_timezoned_list_in_tooltip_; bool is_timezoned_list_in_tooltip_;
bool handleScroll(GdkEventScroll* e);
bool handleToggle(GdkEventButton* const& e);
auto first_day_of_week() -> date::weekday; auto first_day_of_week() -> date::weekday;
const date::time_zone* current_timezone(); const date::time_zone* current_timezone();
bool is_timezone_fixed(); bool is_timezone_fixed();
@ -56,6 +56,20 @@ class Clock : public ALabel {
/*Calendar functions*/ /*Calendar functions*/
auto get_calendar(const date::zoned_seconds& now, const date::zoned_seconds& wtime) auto get_calendar(const date::zoned_seconds& now, const date::zoned_seconds& wtime)
-> std::string; -> std::string;
/*Clock actions*/
void cldModeSwitch(); void cldModeSwitch();
void cldShift_up();
void cldShift_down();
void tz_up();
void tz_down();
// ModuleActionMap
static inline std::map<const std::string, void(waybar::modules::Clock::* const)()> actionMap_{
{"mode", &waybar::modules::Clock::cldModeSwitch},
{"shift_up", &waybar::modules::Clock::cldShift_up},
{"shift_down", &waybar::modules::Clock::cldShift_down},
{"tz_up", &waybar::modules::Clock::tz_up},
{"tz_down", &waybar::modules::Clock::tz_down}
};
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -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_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll)); 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() { AModule::~AModule() {
@ -48,19 +60,33 @@ auto AModule::update() -> void {
pid_.push_back(util::command::forkExec(config_["on-update"].asString())); 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) { bool AModule::handleToggle(GdkEventButton* const& e) {
std::string format{};
const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{ const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{
eventMap_.find(std::pair(e->button, e->type))}; 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 (!format.empty()) {
if (config_[format].isString()) if (config_[format].isString())
format = config_[format].asString(); format = config_[format].asString();
else else
format.clear(); format.clear();
} }
if (!format.empty()) { if (!format.empty()) {
pid_.push_back(util::command::forkExec(format)); pid_.push_back(util::command::forkExec(format));
} }
@ -122,11 +148,19 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) {
bool AModule::handleScroll(GdkEventScroll* e) { bool AModule::handleScroll(GdkEventScroll* e) {
auto dir = getScrollDir(e); auto dir = getScrollDir(e);
if (dir == SCROLL_DIR::UP && config_["on-scroll-up"].isString()) { std::string eventName{};
pid_.push_back(util::command::forkExec(config_["on-scroll-up"].asString()));
} else if (dir == SCROLL_DIR::DOWN && config_["on-scroll-down"].isString()) { if (dir == SCROLL_DIR::UP)
pid_.push_back(util::command::forkExec(config_["on-scroll-down"].asString())); 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(); dp.emit();
return true; return true;
} }

View File

@ -125,16 +125,6 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
return false; 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()) if (config_["locale"].isString())
@ -203,56 +193,12 @@ auto waybar::modules::Clock::update() -> void {
ALabel::update(); ALabel::update();
} }
bool waybar::modules::Clock::handleToggle(GdkEventButton* const& e) { auto waybar::modules::Clock::doAction(const std::string& name) -> void {
const std::map<std::pair<uint, GdkEventType>, void (waybar::modules::Clock::*)()>::const_iterator& if ((actionMap_[name])) {
rec{eventMap_.find(std::pair(e->button, e->type))}; (this->*actionMap_[name])();
update();
const auto callMethod{(rec != eventMap_.cend()) ? rec->second : nullptr};
if (callMethod) {
(this->*callMethod)();
} else } else
return ALabel::handleToggle(e); spdlog::error("Clock. Unsupported action \"{0}\"", name);
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;
} }
// The number of weeks in calendar month layout plus 1 more for calendar titles // 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(); return os.str();
} }
/*Clock actions*/
void waybar::modules::Clock::cldModeSwitch() { void waybar::modules::Clock::cldModeSwitch() {
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR; 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) auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point* now)
-> std::string { -> std::string {