Skip to content

Commit e25a7c9

Browse files
committedFeb 28, 2023
ISSUE#1977. AModule implements module actions call
Signed-off-by: Viktar Lukashonak <[email protected]>
1 parent 09142fa commit e25a7c9

File tree

5 files changed

+96
-74
lines changed

5 files changed

+96
-74
lines changed
 

‎include/AModule.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ namespace waybar {
1111

1212
class AModule : public IModule {
1313
public:
14-
AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
15-
bool enable_scroll = false);
1614
virtual ~AModule();
1715
virtual auto update() -> void;
1816
virtual auto refresh(int) -> void{};
1917
virtual operator Gtk::Widget &();
18+
virtual auto doAction(const std::string& name) -> void;
2019

2120
Glib::Dispatcher dp;
2221

2322
protected:
23+
// Don't need to make an object directly
24+
// Derived classes are able to use it
25+
AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
26+
bool enable_scroll = false);
27+
2428
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
2529

2630
SCROLL_DIR getScrollDir(GdkEventScroll *e);
@@ -37,6 +41,7 @@ class AModule : public IModule {
3741
std::vector<int> pid_;
3842
gdouble distance_scrolled_y_;
3943
gdouble distance_scrolled_x_;
44+
std::map<std::string, std::string> eventActionMap_;
4045
static const inline std::map<std::pair<uint, GdkEventType>, std::string> eventMap_{
4146
{std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"},
4247
{std::make_pair(1, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click"},

‎include/IModule.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class IModule {
99
virtual ~IModule() = default;
1010
virtual auto update() -> void = 0;
1111
virtual operator Gtk::Widget&() = 0;
12+
virtual auto doAction(const std::string& name) -> void = 0;
1213
};
1314

1415
} // namespace waybar

‎include/modules/clock.hpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ enum class WeeksSide {
1515
HIDDEN,
1616
};
1717

18-
enum class CldMode { MONTH, YEAR };
18+
enum class CldMode {
19+
MONTH,
20+
YEAR
21+
};
1922

20-
class Clock : public ALabel {
23+
class Clock final : public ALabel {
2124
public:
2225
Clock(const std::string&, const Json::Value&);
2326
~Clock() = default;
2427
auto update() -> void;
28+
auto doAction(const std::string& name) -> void override;
2529

2630
private:
2731
util::SleeperThread thread_;
28-
std::map<std::pair<uint, GdkEventType>, void (waybar::modules::Clock::*)()> eventMap_;
2932
std::locale locale_;
3033
std::vector<const date::time_zone*> time_zones_;
3134
int current_time_zone_idx_;
3235
bool is_calendar_in_tooltip_;
3336
bool is_timezoned_list_in_tooltip_;
3437

35-
bool handleScroll(GdkEventScroll* e);
36-
bool handleToggle(GdkEventButton* const& e);
37-
3838
auto first_day_of_week() -> date::weekday;
3939
const date::time_zone* current_timezone();
4040
bool is_timezone_fixed();
@@ -56,6 +56,20 @@ class Clock : public ALabel {
5656
/*Calendar functions*/
5757
auto get_calendar(const date::zoned_seconds& now, const date::zoned_seconds& wtime)
5858
-> std::string;
59+
/*Clock actions*/
5960
void cldModeSwitch();
61+
void cldShift_up();
62+
void cldShift_down();
63+
void tz_up();
64+
void tz_down();
65+
66+
// ModuleActionMap
67+
static inline std::map<const std::string, void(waybar::modules::Clock::* const)()> actionMap_{
68+
{"mode", &waybar::modules::Clock::cldModeSwitch},
69+
{"shift_up", &waybar::modules::Clock::cldShift_up},
70+
{"shift_down", &waybar::modules::Clock::cldShift_down},
71+
{"tz_up", &waybar::modules::Clock::tz_up},
72+
{"tz_down", &waybar::modules::Clock::tz_down}
73+
};
6074
};
6175
} // namespace waybar::modules

‎src/AModule.cpp

+41-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
3232
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
3333
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
3434
}
35+
36+
// Configure module action Map
37+
const Json::Value actions{config_["actions"]};
38+
for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) {
39+
if (it.key().isString() && it->isString())
40+
if (eventActionMap_.count(it.key().asString()) == 0)
41+
eventActionMap_.insert({it.key().asString(), it->asString()});
42+
else
43+
spdlog::warn("Dublicate action is ignored: {0}", it.key().asString());
44+
else
45+
spdlog::warn("Wrong actions section configuration. See config by index: {}", it.index());
46+
}
3547
}
3648

3749
AModule::~AModule() {
@@ -48,19 +60,33 @@ auto AModule::update() -> void {
4860
pid_.push_back(util::command::forkExec(config_["on-update"].asString()));
4961
}
5062
}
63+
// Get mapping between event name and module action name
64+
// Then call overrided doAction in order to call appropriate module action
65+
auto AModule::doAction(const std::string& name) -> void {
66+
if (!name.empty()) {
67+
const std::map<std::string, std::string>::const_iterator& recA{eventActionMap_.find(name)};
68+
// Call overrided action if derrived class has implemented it
69+
if (recA != eventActionMap_.cend() && name != recA->second) this->doAction(recA->second);
70+
}
71+
}
5172

5273
bool AModule::handleToggle(GdkEventButton* const& e) {
74+
std::string format{};
5375
const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{
5476
eventMap_.find(std::pair(e->button, e->type))};
55-
std::string format{(rec != eventMap_.cend()) ? rec->second : std::string{""}};
77+
if (rec != eventMap_.cend()) {
78+
// First call module actions
79+
this->AModule::doAction(rec->second);
5680

81+
format = rec->second;
82+
}
83+
// Second call user scripts
5784
if (!format.empty()) {
5885
if (config_[format].isString())
5986
format = config_[format].asString();
6087
else
6188
format.clear();
6289
}
63-
6490
if (!format.empty()) {
6591
pid_.push_back(util::command::forkExec(format));
6692
}
@@ -122,11 +148,19 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) {
122148

123149
bool AModule::handleScroll(GdkEventScroll* e) {
124150
auto dir = getScrollDir(e);
125-
if (dir == SCROLL_DIR::UP && config_["on-scroll-up"].isString()) {
126-
pid_.push_back(util::command::forkExec(config_["on-scroll-up"].asString()));
127-
} else if (dir == SCROLL_DIR::DOWN && config_["on-scroll-down"].isString()) {
128-
pid_.push_back(util::command::forkExec(config_["on-scroll-down"].asString()));
129-
}
151+
std::string eventName{};
152+
153+
if (dir == SCROLL_DIR::UP)
154+
eventName = "on-scroll-up";
155+
else if (dir == SCROLL_DIR::DOWN)
156+
eventName = "on-scroll-down";
157+
158+
// First call module actions
159+
this->AModule::doAction(eventName);
160+
// Second call user scripts
161+
if (config_[eventName].isString())
162+
pid_.push_back(util::command::forkExec(config_[eventName].asString()));
163+
130164
dp.emit();
131165
return true;
132166
}

‎src/modules/clock.cpp

+27-59
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,6 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
125125
return false;
126126
});
127127
}
128-
if (config_[kCalendarPlaceholder]["on-click-left"].isString()) {
129-
if (config_[kCalendarPlaceholder]["on-click-left"].asString() == "mode")
130-
eventMap_.insert({std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS),
131-
&waybar::modules::Clock::cldModeSwitch});
132-
}
133-
if (config_[kCalendarPlaceholder]["on-click-right"].isString()) {
134-
if (config_[kCalendarPlaceholder]["on-click-right"].asString() == "mode")
135-
eventMap_.insert({std::make_pair(3, GdkEventType::GDK_BUTTON_PRESS),
136-
&waybar::modules::Clock::cldModeSwitch});
137-
}
138128
}
139129

140130
if (config_["locale"].isString())
@@ -203,56 +193,12 @@ auto waybar::modules::Clock::update() -> void {
203193
ALabel::update();
204194
}
205195

206-
bool waybar::modules::Clock::handleToggle(GdkEventButton* const& e) {
207-
const std::map<std::pair<uint, GdkEventType>, void (waybar::modules::Clock::*)()>::const_iterator&
208-
rec{eventMap_.find(std::pair(e->button, e->type))};
209-
210-
const auto callMethod{(rec != eventMap_.cend()) ? rec->second : nullptr};
211-
212-
if (callMethod) {
213-
(this->*callMethod)();
196+
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
197+
if ((actionMap_[name])) {
198+
(this->*actionMap_[name])();
199+
update();
214200
} else
215-
return ALabel::handleToggle(e);
216-
217-
update();
218-
return true;
219-
}
220-
221-
bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
222-
// defer to user commands if set
223-
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
224-
return AModule::handleScroll(e);
225-
}
226-
227-
auto dir = AModule::getScrollDir(e);
228-
229-
// Shift calendar date
230-
if (cldShift_.count() != 0) {
231-
if (dir == SCROLL_DIR::UP)
232-
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
233-
else
234-
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
235-
} else {
236-
// Change time zone
237-
if (dir != SCROLL_DIR::UP && dir != SCROLL_DIR::DOWN) {
238-
return true;
239-
}
240-
if (time_zones_.size() == 1) {
241-
return true;
242-
}
243-
244-
auto nr_zones = time_zones_.size();
245-
if (dir == SCROLL_DIR::UP) {
246-
size_t new_idx = current_time_zone_idx_ + 1;
247-
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
248-
} else {
249-
current_time_zone_idx_ =
250-
current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
251-
}
252-
}
253-
254-
update();
255-
return true;
201+
spdlog::error("Clock. Unsupported action \"{0}\"", name);
256202
}
257203

258204
// 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,
461407
return os.str();
462408
}
463409

410+
/*Clock actions*/
464411
void waybar::modules::Clock::cldModeSwitch() {
465412
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR;
466413
}
414+
void waybar::modules::Clock::cldShift_up() {
415+
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
416+
}
417+
void waybar::modules::Clock::cldShift_down() {
418+
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
419+
}
420+
void waybar::modules::Clock::tz_up() {
421+
auto nr_zones = time_zones_.size();
422+
423+
if (nr_zones == 1) return;
424+
425+
size_t new_idx = current_time_zone_idx_ + 1;
426+
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
427+
}
428+
void waybar::modules::Clock::tz_down() {
429+
auto nr_zones = time_zones_.size();
430+
431+
if (nr_zones == 1) return;
432+
433+
current_time_zone_idx_ = current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
434+
}
467435

468436
auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point* now)
469437
-> std::string {

0 commit comments

Comments
 (0)
Please sign in to comment.