Skip to content

Commit

Permalink
GTK4. AModule, ALabel handle events
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
  • Loading branch information
LukashonakV committed Feb 26, 2024
1 parent c5b726a commit 84cdd91
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 5 additions & 6 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ class AModule : public IModule {
// 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 };

bool tooltipEnabled();

const std::string name_;
const Json::Value &config_;

Glib::RefPtr<Gtk::GestureClick> controllClick_;
Glib::RefPtr<Gtk::EventControllerScroll> controllScroll_;

void bindEvents(Gtk::Widget& wg);
bool tooltipEnabled();

virtual void handleToggle(int n_press, double dx, double dy);
virtual void handleRelease(int n_press, double dx, double dy);
virtual bool handleScroll(double dx, double dy);

private:
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
const bool isTooltip;
std::vector<int> pid_;
double distance_scrolled_x_{0.0};
Expand Down
4 changes: 3 additions & 1 deletion src/ALabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
Gtk::Label::set_xalign(align);
}
}

AModule::bindEvents(*this);
}

auto ALabel::update() -> void { AModule::update(); }
Expand Down Expand Up @@ -99,7 +101,7 @@ std::string ALabel::getIcon(uint16_t percentage, const std::vector<std::string>&
}

void waybar::ALabel::handleToggle(int n_press, double dx, double dy) {
if (config_["format-alt-click"].isUInt() && controllClick_->get_button() == config_["format-alt-click"].asUInt()) {
if (config_["format-alt-click"].isUInt() && controllClick_->get_current_button() == config_["format-alt-click"].asUInt()) {
alt_ = !alt_;
if (alt_ && config_["format-alt"].isString()) {
format_ = config_["format-alt"].asString();
Expand Down
12 changes: 10 additions & 2 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::

// configure events' user commands
// hasUserEvent is true if any element from eventMap_ is satisfying the condition in the lambda

const bool after{true};
const bool hasUserPressEvent{std::find_if(eventMap_.cbegin(), eventMap_.cend(), [&config](const auto& eventEntry) {
// True if there is any non-release type event
Expand All @@ -41,7 +42,8 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::

if (enable_click || hasUserPressEvent || hasUserReleaseEvent) {
controllClick_->set_propagation_phase(Gtk::PropagationPhase::TARGET);
// (this->operator Gtk::Widget&()).add_controller(controllClick_);
controllClick_->set_button(0);


if (enable_click || hasUserPressEvent)
controllClick_->signal_pressed().connect(sigc::mem_fun(*this, &AModule::handleToggle), after);
Expand All @@ -51,7 +53,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::

if (enable_scroll || config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
controllScroll_->set_propagation_phase(Gtk::PropagationPhase::TARGET);
// ((Gtk::Widget&)*this).add_controller(controllScroll_);
controllScroll_->set_flags(Gtk::EventControllerScroll::Flags::BOTH_AXES);
controllScroll_->signal_scroll().connect(sigc::mem_fun(*this, &AModule::handleScroll), after);
}
}
Expand Down Expand Up @@ -174,6 +176,7 @@ const AModule::SCROLL_DIR AModule::getScrollDir(Glib::RefPtr<const Gdk::Event> e
}

bool AModule::handleScroll(double dx, double dy) {
spdlog::info("handleScroll");
currEvent_ = controllScroll_->get_current_event();

if (currEvent_) {
Expand Down Expand Up @@ -201,4 +204,9 @@ bool AModule::tooltipEnabled() { return isTooltip; }

AModule::operator Gtk::Widget&() { return dynamic_cast<Gtk::Widget&>(*this); }

void AModule::bindEvents(Gtk::Widget& wg) {
wg.add_controller(controllClick_);
wg.add_controller(controllScroll_);
}

} // namespace waybar

0 comments on commit 84cdd91

Please sign in to comment.