From c5b726ae5aa629cadaaddcad46d347d31ac05af6 Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Sat, 24 Feb 2024 20:32:14 +0300 Subject: [PATCH] GTK4. First success run win clock module Signed-off-by: Viktar Lukashonak --- include/ALabel.hpp | 13 ++++------ include/AModule.hpp | 6 ++--- include/bar.hpp | 4 ++-- include/factory.hpp | 2 -- include/group.hpp | 17 ++++++------- meson.build | 3 ++- src/ALabel.cpp | 34 ++++++++++++-------------- src/AModule.cpp | 30 +++++++++++------------ src/bar.cpp | 18 +++++++------- src/group.cpp | 56 +++++++++++++++++-------------------------- src/modules/clock.cpp | 4 ++-- 11 files changed, 82 insertions(+), 105 deletions(-) diff --git a/include/ALabel.hpp b/include/ALabel.hpp index aec0745c..816fd4f5 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -1,32 +1,29 @@ #pragma once #include -#include #include -#include #include "AModule.hpp" namespace waybar { -class ALabel : public AModule { +class ALabel : public AModule, public Gtk::Label { public: - ALabel(const Json::Value &, const std::string &, const std::string &, const std::string &format, - uint16_t interval = 0, bool ellipsize = false, bool enable_click = false, - bool enable_scroll = false); virtual ~ALabel() = default; auto update() -> void override; virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0); virtual std::string getIcon(uint16_t, const std::vector &alts, uint16_t max = 0); protected: - Gtk::Label label_; + ALabel(const Json::Value &, const std::string &, const std::string &, const std::string &format, + uint16_t interval = 0, bool ellipsize = false, bool enable_click = false, + bool enable_scroll = false); + std::string format_; const std::chrono::seconds interval_; bool alt_ = false; std::string default_format_; -//todo bool handleToggle(GdkEventButton *const &e) override; void handleToggle(int n_press, double dx, double dy); virtual std::string getState(uint8_t value, bool lesser = false); }; diff --git a/include/AModule.hpp b/include/AModule.hpp index ba074acc..0274f1ea 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -35,8 +35,8 @@ class AModule : public IModule { const std::string name_; const Json::Value &config_; - Glib::RefPtr handleClick_; - Glib::RefPtr handleScroll_; + Glib::RefPtr controllClick_; + Glib::RefPtr controllScroll_; 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); @@ -44,8 +44,8 @@ class AModule : public IModule { private: const bool isTooltip; std::vector pid_; - double distance_scrolled_y_{0.0}; double distance_scrolled_x_{0.0}; + double distance_scrolled_y_{0.0}; Glib::RefPtr currEvent_; std::map eventActionMap_; static const inline std::map,Gdk::Event::Type>, std::string> eventMap_ { diff --git a/include/bar.hpp b/include/bar.hpp index e4257b93..ff1b989d 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -7,7 +7,7 @@ #include #include "AModule.hpp" -//#include "group.hpp" +#include "group.hpp" #include "xdg-output-unstable-v1-client-protocol.h" namespace waybar { @@ -83,7 +83,7 @@ class Bar { private: void onMap(); auto setupWidgets() -> void; -// void getModules(const Factory &, const std::string &, waybar::Group *); + void getModules(const Factory &, const std::string &, waybar::Group *); void setupAltFormatKeyForModule(const std::string &module_name); void setupAltFormatKeyForModuleList(const char *module_list_name); void setMode(const bar_mode &); diff --git a/include/factory.hpp b/include/factory.hpp index f805aab5..81530db0 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include namespace waybar { diff --git a/include/group.hpp b/include/group.hpp index 87a57b81..e42655da 100644 --- a/include/group.hpp +++ b/include/group.hpp @@ -1,10 +1,9 @@ #pragma once -#include -#include -#include - #include "AModule.hpp" + +#include +#include #include "gtkmm/revealer.h" namespace waybar { @@ -19,17 +18,19 @@ class Group : public AModule { virtual Gtk::Box& getBox(); void addWidget(Gtk::Widget& widget); -// bool handleMouseHover(GdkEventCrossing* const& e); - protected: Gtk::Box box; Gtk::Box revealer_box; Gtk::Revealer revealer; - bool is_first_widget = true; - bool is_drawer = false; + bool is_first_widget{true}; + bool is_drawer{false}; std::string add_class_to_drawer_children; + private: + Glib::RefPtr controllMotion_; void addHoverHandlerTo(Gtk::Widget& widget); + void onMotionEnter(double x, double y); + void onMotionLeave(); }; } // namespace waybar diff --git a/meson.build b/meson.build index 60c610da..fbd1762f 100644 --- a/meson.build +++ b/meson.build @@ -136,7 +136,8 @@ src_files = files( 'src/AModule.cpp', 'src/ALabel.cpp', 'src/factory.cpp', - 'src/util/ustring_clen.cpp' + 'src/util/ustring_clen.cpp', + 'src/group.cpp' ) man_files = files( diff --git a/src/ALabel.cpp b/src/ALabel.cpp index 1fb38e48..7156851e 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -1,9 +1,5 @@ #include "ALabel.hpp" -#include - -#include - namespace waybar { ALabel::ALabel(const Json::Value& config, const std::string& name, const std::string& id, @@ -16,22 +12,22 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st : std::chrono::seconds( config_["interval"].isUInt() ? config_["interval"].asUInt() : interval)), default_format_(format_) { - label_.set_name(name); + Gtk::Label::set_name(name); if (!id.empty()) { - label_.get_style_context()->add_class(id); + Gtk::Label::get_style_context()->add_class(id); } - label_.get_style_context()->add_class(MODULE_CLASS); + Gtk::Label::get_style_context()->add_class(MODULE_CLASS); if (config_["max-length"].isUInt()) { - label_.set_max_width_chars(config_["max-length"].asInt()); - label_.set_ellipsize(Pango::EllipsizeMode::END); - label_.set_single_line_mode(true); - } else if (ellipsize && label_.get_max_width_chars() == -1) { - label_.set_ellipsize(Pango::EllipsizeMode::END); - label_.set_single_line_mode(true); + Gtk::Label::set_max_width_chars(config_["max-length"].asInt()); + Gtk::Label::set_ellipsize(Pango::EllipsizeMode::END); + Gtk::Label::set_single_line_mode(true); + } else if (ellipsize && Gtk::Label::get_max_width_chars() == -1) { + Gtk::Label::set_ellipsize(Pango::EllipsizeMode::END); + Gtk::Label::set_single_line_mode(true); } if (config_["min-length"].isUInt()) { - label_.set_width_chars(config_["min-length"].asUInt()); + Gtk::Label::set_width_chars(config_["min-length"].asUInt()); } uint rotate = 0; @@ -45,9 +41,9 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st if (config_["align"].isDouble()) { auto align = config_["align"].asFloat(); if (rotate == 90 || rotate == 270) { - label_.set_yalign(align); + Gtk::Label::set_yalign(align); } else { - label_.set_xalign(align); + Gtk::Label::set_xalign(align); } } } @@ -103,7 +99,7 @@ std::string ALabel::getIcon(uint16_t percentage, const std::vector& } void waybar::ALabel::handleToggle(int n_press, double dx, double dy) { - if (config_["format-alt-click"].isUInt() && handleClick_->get_button() == config_["format-alt-click"].asUInt()) { + if (config_["format-alt-click"].isUInt() && controllClick_->get_button() == config_["format-alt-click"].asUInt()) { alt_ = !alt_; if (alt_ && config_["format-alt"].isString()) { format_ = config_["format-alt"].asString(); @@ -135,10 +131,10 @@ std::string ALabel::getState(uint8_t value, bool lesser) { std::string valid_state; for (auto const& state : states) { if ((lesser ? value <= state.second : value >= state.second) && valid_state.empty()) { - label_.get_style_context()->add_class(state.first); + Gtk::Label::get_style_context()->add_class(state.first); valid_state = state.first; } else { - label_.get_style_context()->remove_class(state.first); + Gtk::Label::get_style_context()->remove_class(state.first); } } return valid_state; diff --git a/src/AModule.cpp b/src/AModule.cpp index 528991a9..0c02a435 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -8,11 +8,9 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: bool enable_click, bool enable_scroll) : name_(std::move(name)), config_(std::move(config)), - isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true}, - distance_scrolled_y_(0.0), - distance_scrolled_x_(0.0), - handleClick_(Gtk::GestureClick::create()), - handleScroll_(Gtk::EventControllerScroll::create()){ + controllClick_{Gtk::GestureClick::create()}, + controllScroll_{Gtk::EventControllerScroll::create()}, + isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true} { // Configure module action Map const Json::Value actions{config_["actions"]}; for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) { @@ -42,19 +40,19 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: }) != eventMap_.cend()}; if (enable_click || hasUserPressEvent || hasUserReleaseEvent) { - handleClick_->set_propagation_phase(Gtk::PropagationPhase::TARGET); - ((Gtk::Widget&)*this).add_controller(handleClick_); + controllClick_->set_propagation_phase(Gtk::PropagationPhase::TARGET); +// (this->operator Gtk::Widget&()).add_controller(controllClick_); if (enable_click || hasUserPressEvent) - handleClick_->signal_pressed().connect(sigc::mem_fun(*this, &AModule::handleToggle), after); + controllClick_->signal_pressed().connect(sigc::mem_fun(*this, &AModule::handleToggle), after); if (hasUserReleaseEvent) - handleClick_->signal_released().connect(sigc::mem_fun(*this, &AModule::handleRelease), after); + controllClick_->signal_released().connect(sigc::mem_fun(*this, &AModule::handleRelease), after); } if (enable_scroll || config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) { - handleScroll_->set_propagation_phase(Gtk::PropagationPhase::TARGET); - ((Gtk::Widget&)*this).add_controller(handleScroll_); - handleScroll_->signal_scroll().connect(sigc::mem_fun(*this, &AModule::handleScroll), after); + controllScroll_->set_propagation_phase(Gtk::PropagationPhase::TARGET); +// ((Gtk::Widget&)*this).add_controller(controllScroll_); + controllScroll_->signal_scroll().connect(sigc::mem_fun(*this, &AModule::handleScroll), after); } } @@ -83,10 +81,10 @@ auto AModule::doAction(const std::string& name) -> void { } void AModule::handleToggle(int n_press, double dx, double dy) { - handleClickEvent(handleClick_->get_current_button(), n_press, Gdk::Event::Type::BUTTON_PRESS); + handleClickEvent(controllClick_->get_current_button(), n_press, Gdk::Event::Type::BUTTON_PRESS); } void AModule::handleRelease(int n_press, double dx, double dy) { - handleClickEvent(handleClick_->get_current_button(), n_press, Gdk::Event::Type::BUTTON_RELEASE); + handleClickEvent(controllClick_->get_current_button(), n_press, Gdk::Event::Type::BUTTON_RELEASE); } void AModule::handleClickEvent(uint n_button, int n_press, Gdk::Event::Type n_evtype) { @@ -176,7 +174,7 @@ const AModule::SCROLL_DIR AModule::getScrollDir(Glib::RefPtr e } bool AModule::handleScroll(double dx, double dy) { - currEvent_ = handleScroll_->get_current_event(); + currEvent_ = controllScroll_->get_current_event(); if (currEvent_) { std::string format{}; @@ -201,6 +199,6 @@ bool AModule::handleScroll(double dx, double dy) { bool AModule::tooltipEnabled() { return isTooltip; } -AModule::operator Gtk::Widget&() { return *this; } +AModule::operator Gtk::Widget&() { return dynamic_cast(*this); } } // namespace waybar diff --git a/src/bar.cpp b/src/bar.cpp index 5ef759b3..6e7de108 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -4,8 +4,7 @@ #include #include "client.hpp" -//#include "factory.hpp" -//#include "group.hpp" +#include "factory.hpp" #ifdef HAVE_SWAY #include "modules/sway/bar.hpp" @@ -108,6 +107,8 @@ Glib::ustring to_string(Gtk::PositionType pos) { return "top"; case Gtk::PositionType::BOTTOM: return "bottom"; + default: + return ""; } } @@ -458,8 +459,6 @@ void waybar::Bar::handleSignal(int signal) { } } -// todo gtkmm4 -/* void waybar::Bar::getModules(const Factory& factory, const std::string& pos, waybar::Group* group = nullptr) { auto module_list = group ? config[pos]["modules"] : config[pos]; @@ -512,7 +511,7 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos, } } } -*/ + auto waybar::Bar::setupWidgets() -> void { box_.set_start_widget(left_); if (config["fixed-center"].isBool() ? config["fixed-center"].asBool() : true) { @@ -526,11 +525,10 @@ auto waybar::Bar::setupWidgets() -> void { setupAltFormatKeyForModuleList("modules-right"); setupAltFormatKeyForModuleList("modules-center"); -// todo gtkmm4 -// Factory factory(*this, config); -// getModules(factory, "modules-left"); -// getModules(factory, "modules-center"); -// getModules(factory, "modules-right"); + Factory factory(*this, config); + getModules(factory, "modules-left"); + getModules(factory, "modules-center"); + getModules(factory, "modules-right"); for (auto const& module : modules_left_) { left_.prepend(*module); } diff --git a/src/group.cpp b/src/group.cpp index cad36e51..05f4a972 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -1,26 +1,19 @@ #include "group.hpp" -#include - -#include - -#include "gdkmm/device.h" -#include "gtkmm/widget.h" - namespace waybar { const Gtk::RevealerTransitionType getPreferredTransitionType(bool is_vertical, bool left_to_right) { if (is_vertical) { if (left_to_right) { - return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_DOWN; + return Gtk::RevealerTransitionType::SLIDE_DOWN; } else { - return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_UP; + return Gtk::RevealerTransitionType::SLIDE_UP; } } else { if (left_to_right) { - return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_RIGHT; + return Gtk::RevealerTransitionType::SLIDE_RIGHT; } else { - return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_LEFT; + return Gtk::RevealerTransitionType::SLIDE_LEFT; } } } @@ -28,8 +21,9 @@ const Gtk::RevealerTransitionType getPreferredTransitionType(bool is_vertical, b Group::Group(const std::string& name, const std::string& id, const Json::Value& config, bool vertical) : AModule(config, name, id, true, true), - box{vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0}, - revealer_box{vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0} { + box{vertical ? Gtk::Orientation::VERTICAL : Gtk::Orientation::HORIZONTAL, 0}, + revealer_box{vertical ? Gtk::Orientation::VERTICAL : Gtk::Orientation::HORIZONTAL, 0}, + controllMotion_{Gtk::EventControllerMotion::create()} { box.set_name(name_); if (!id.empty()) { box.get_style_context()->add_class(id); @@ -41,11 +35,11 @@ Group::Group(const std::string& name, const std::string& id, const Json::Value& if (orientation == "inherit") { // keep orientation passed } else if (orientation == "orthogonal") { - box.set_orientation(vertical ? Gtk::ORIENTATION_HORIZONTAL : Gtk::ORIENTATION_VERTICAL); + box.set_orientation(vertical ? Gtk::Orientation::HORIZONTAL : Gtk::Orientation::VERTICAL); } else if (orientation == "vertical") { - box.set_orientation(Gtk::ORIENTATION_VERTICAL); + box.set_orientation(Gtk::Orientation::VERTICAL); } else if (orientation == "horizontal") { - box.set_orientation(Gtk::ORIENTATION_HORIZONTAL); + box.set_orientation(Gtk::Orientation::HORIZONTAL); } else { throw std::runtime_error("Invalid orientation value: " + orientation); } @@ -72,32 +66,26 @@ Group::Group(const std::string& name, const std::string& id, const Json::Value& revealer.get_style_context()->add_class("drawer"); - revealer.add(revealer_box); - box.pack_start(revealer); + revealer.set_child(revealer_box); + box.prepend(revealer); addHoverHandlerTo(revealer); } } -bool Group::handleMouseHover(GdkEventCrossing* const& e) { - switch (e->type) { - case GDK_ENTER_NOTIFY: - revealer.set_reveal_child(true); - break; - case GDK_LEAVE_NOTIFY: - revealer.set_reveal_child(false); - break; - default: - break; - } +void Group::onMotionEnter(double x, double y) { + revealer.set_reveal_child(true); +} - return true; +void Group::onMotionLeave() { + revealer.set_reveal_child(false); } void Group::addHoverHandlerTo(Gtk::Widget& widget) { - widget.add_events(Gdk::EventMask::ENTER_NOTIFY_MASK | Gdk::EventMask::LEAVE_NOTIFY_MASK); - widget.signal_enter_notify_event().connect(sigc::mem_fun(*this, &Group::handleMouseHover)); - widget.signal_leave_notify_event().connect(sigc::mem_fun(*this, &Group::handleMouseHover)); + controllMotion_->set_propagation_phase(Gtk::PropagationPhase::TARGET); + box.add_controller(controllMotion_); + controllMotion_->signal_enter().connect(sigc::mem_fun(*this, &Group::onMotionEnter)); + controllMotion_->signal_leave().connect(sigc::mem_fun(*this, &Group::onMotionLeave)); } auto Group::update() -> void { @@ -107,7 +95,7 @@ auto Group::update() -> void { Gtk::Box& Group::getBox() { return is_drawer ? (is_first_widget ? box : revealer_box) : box; } void Group::addWidget(Gtk::Widget& widget) { - getBox().pack_start(widget, false, false); + getBox().prepend(widget); if (is_drawer) { // Necessary because of GTK's hitbox detection diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 6f011b9f..e2890d9d 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -132,7 +132,7 @@ auto waybar::modules::Clock::update() -> void { auto tz{tzList_[tzCurrIdx_]}; const zoned_time now{tz, floor(system_clock::now())}; - label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now))); + Gtk::Label::set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now))); if (tooltipEnabled()) { const year_month_day today{floor(now.get_local_time())}; @@ -151,7 +151,7 @@ auto waybar::modules::Clock::update() -> void { tlpText_ = fmt_lib::vformat(locale_, tlpText_, fmt_lib::make_format_args(shiftedNow)); - label_.set_tooltip_markup(tlpText_); + Gtk::Label::set_tooltip_markup(tlpText_); } ALabel::update();