GTK4. First success run win clock module

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
pull/2956/head
Viktar Lukashonak 2024-02-24 20:32:14 +03:00
parent 54ef531501
commit c5b726ae5a
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
11 changed files with 82 additions and 105 deletions

View File

@ -1,32 +1,29 @@
#pragma once
#include <chrono>
#include <glibmm/markup.h>
#include <gtkmm/label.h>
#include <json/json.h>
#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<std::string> &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);
};

View File

@ -35,8 +35,8 @@ class AModule : public IModule {
const std::string name_;
const Json::Value &config_;
Glib::RefPtr<Gtk::GestureClick> handleClick_;
Glib::RefPtr<Gtk::EventControllerScroll> handleScroll_;
Glib::RefPtr<Gtk::GestureClick> controllClick_;
Glib::RefPtr<Gtk::EventControllerScroll> 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<int> pid_;
double distance_scrolled_y_{0.0};
double distance_scrolled_x_{0.0};
double distance_scrolled_y_{0.0};
Glib::RefPtr<const Gdk::Event> currEvent_;
std::map<std::string, std::string> eventActionMap_;
static const inline std::map<std::pair<std::pair<uint, int>,Gdk::Event::Type>, std::string> eventMap_ {

View File

@ -7,7 +7,7 @@
#include <gtkmm/window.h>
#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 &);

View File

@ -1,7 +1,5 @@
#pragma once
#include <json/json.h>
#include <AModule.hpp>
namespace waybar {

View File

@ -1,10 +1,9 @@
#pragma once
#include <gtkmm/box.h>
#include <gtkmm/widget.h>
#include <json/json.h>
#include "AModule.hpp"
#include <gtkmm/box.h>
#include <gtkmm/eventcontrollermotion.h>
#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<Gtk::EventControllerMotion> controllMotion_;
void addHoverHandlerTo(Gtk::Widget& widget);
void onMotionEnter(double x, double y);
void onMotionLeave();
};
} // namespace waybar

View File

@ -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(

View File

@ -1,9 +1,5 @@
#include "ALabel.hpp"
#include <fmt/format.h>
#include <util/command.hpp>
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<std::string>&
}
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;

View File

@ -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<const Gdk::Event> 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<Gtk::Widget&>(*this); }
} // namespace waybar

View File

@ -4,8 +4,7 @@
#include <spdlog/spdlog.h>
#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);
}

View File

@ -1,26 +1,19 @@
#include "group.hpp"
#include <fmt/format.h>
#include <util/command.hpp>
#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

View File

@ -132,7 +132,7 @@ auto waybar::modules::Clock::update() -> void {
auto tz{tzList_[tzCurrIdx_]};
const zoned_time now{tz, floor<seconds>(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<days>(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();