Skip to content

Commit

Permalink
GTK4. First success run win clock module
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 24, 2024
1 parent 54ef531 commit c5b726a
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 105 deletions.
13 changes: 5 additions & 8 deletions include/ALabel.hpp
Original file line number Diff line number Diff line change
@@ -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);
};
Expand Down
6 changes: 3 additions & 3 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ 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);

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_ {
Expand Down
4 changes: 2 additions & 2 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 &);
Expand Down
2 changes: 0 additions & 2 deletions include/factory.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <json/json.h>

#include <AModule.hpp>

namespace waybar {
Expand Down
17 changes: 9 additions & 8 deletions include/group.hpp
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 15 additions & 19 deletions src/ALabel.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 14 additions & 16 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{};
Expand All @@ -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
18 changes: 8 additions & 10 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -108,6 +107,8 @@ Glib::ustring to_string(Gtk::PositionType pos) {
return "top";
case Gtk::PositionType::BOTTOM:
return "bottom";
default:
return "";
}
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit c5b726a

Please sign in to comment.