From 5246ab15cbbdb92bf490b70d1c326465f6910d38 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sat, 14 Oct 2023 17:17:19 -0300 Subject: [PATCH] feat: add drawer bool option to group --- include/group.hpp | 10 +++++++- src/bar.cpp | 9 +++---- src/group.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/include/group.hpp b/include/group.hpp index ee9d282b..a9ecf066 100644 --- a/include/group.hpp +++ b/include/group.hpp @@ -5,20 +5,28 @@ #include #include "AModule.hpp" +#include "gtkmm/revealer.h" namespace waybar { class Group : public AModule { public: Group(const std::string&, const std::string&, const Json::Value&, bool); - ~Group() = default; + virtual ~Group() = default; auto update() -> void override; operator Gtk::Widget&() override; virtual Gtk::Box& getBox(); + void addWidget(Gtk::Widget& widget); + + bool hangleMouseHover(GdkEventCrossing* const& e); protected: Gtk::Box box; + Gtk::Box revealer_box; + Gtk::Revealer revealer; + bool is_first_widget = true; + bool is_drawer = false; }; } // namespace waybar diff --git a/src/bar.cpp b/src/bar.cpp index 9b3a12f3..415466b0 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -756,11 +756,8 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos, // auto parent = group ? group : &this->box_; // auto vertical = parent->get_orientation() == Gtk::ORIENTATION_VERTICAL; - auto vertical = ( - group ? - group->getBox().get_orientation() : - box_.get_orientation() - ) == Gtk::ORIENTATION_VERTICAL; + auto vertical = (group ? group->getBox().get_orientation() : box_.get_orientation()) == + Gtk::ORIENTATION_VERTICAL; auto group_module = new waybar::Group(id_name, class_name, config[ref], vertical); getModules(factory, ref, group_module); @@ -772,7 +769,7 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos, std::shared_ptr module_sp(module); modules_all_.emplace_back(module_sp); if (group) { - group->getBox().pack_start(*module, false, false); + group->addWidget(*module); } else { if (pos == "modules-left") { modules_left_.emplace_back(module_sp); diff --git a/src/group.cpp b/src/group.cpp index 54d53e7e..300d59fb 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -1,15 +1,20 @@ #include "group.hpp" #include +#include #include +#include "gdkmm/device.h" +#include "gtkmm/widget.h" + namespace waybar { Group::Group(const std::string& name, const std::string& id, const Json::Value& config, bool vertical) - : AModule(config, name, id, false, false), - box{vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0} { + : 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.set_name(name_); if (!id.empty()) { box.get_style_context()->add_class(id); @@ -29,14 +34,62 @@ Group::Group(const std::string& name, const std::string& id, const Json::Value& } else { throw std::runtime_error("Invalid orientation value: " + orientation); } + + if (!config_["drawer"].empty() && config_["drawer"].asBool()) { + is_drawer = true; + revealer.set_transition_type(Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_UP); + revealer.set_transition_duration(500); + revealer.set_reveal_child(false); + + revealer.get_style_context()->add_class("drawer"); + + revealer.add(revealer_box); + box.pack_start(revealer); + + revealer.add_events(Gdk::EventMask::ENTER_NOTIFY_MASK | Gdk::EventMask::LEAVE_NOTIFY_MASK); + revealer.signal_enter_notify_event().connect(sigc::mem_fun(*this, &Group::hangleMouseHover)); + revealer.signal_leave_notify_event().connect(sigc::mem_fun(*this, &Group::hangleMouseHover)); + } +} + +bool Group::hangleMouseHover(GdkEventCrossing* const& e) { + spdlog::info("Mouse hover event"); + + switch (e->type) { + case GDK_ENTER_NOTIFY: + spdlog::info("Mouse enter event"); + revealer.set_reveal_child(true); + break; + case GDK_LEAVE_NOTIFY: + spdlog::info("Mouse leave event"); + revealer.set_reveal_child(false); + break; + default: + spdlog::warn("Unhandled mouse hover event type: {}", (int)e->type); + break; + } + + return true; } auto Group::update() -> void { // noop } -Gtk::Box& Group::getBox() { return box; } +Gtk::Box& Group::getBox() { return is_drawer ? (is_first_widget ? box : revealer_box) : box; } -Group::operator Gtk::Widget&() { return getBox(); } +void Group::addWidget(Gtk::Widget& widget) { + widget.set_has_tooltip(false); + spdlog::info("Adding widget to group {}. Is first? {}", name_, is_first_widget); + getBox().pack_start(widget, false, false); + if (is_first_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::hangleMouseHover)); + widget.signal_leave_notify_event().connect(sigc::mem_fun(*this, &Group::hangleMouseHover)); + } + is_first_widget = false; +} + +Group::operator Gtk::Widget&() { return box; } } // namespace waybar