Skip to content

Commit

Permalink
refactor(group): moving group stuff out of bar
Browse files Browse the repository at this point in the history
  • Loading branch information
haug1 committed Apr 30, 2024
1 parent f41458e commit f679d30
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 50 deletions.
7 changes: 4 additions & 3 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Bar {
Json::Value config;
struct wl_surface *surface;
bool visible = true;
Gtk::Box box_;
Gtk::Window window;
Gtk::Orientation orientation = Gtk::ORIENTATION_HORIZONTAL;
Gtk::PositionType position = Gtk::POS_TOP;
Expand All @@ -87,7 +88,7 @@ class Bar {
private:
void onMap(GdkEventAny *);
auto setupWidgets() -> void;
void getModules(const Factory &, const std::string &, waybar::Group *);
void getModules(const std::string &);
void setupAltFormatKeyForModule(const std::string &module_name);
void setupAltFormatKeyForModuleList(const char *module_list_name);
void setMode(const bar_mode &);
Expand All @@ -105,18 +106,18 @@ class Bar {
uint32_t width_, height_;
bool passthrough_;

Factory factory_;

Gtk::Box left_;
Gtk::Box center_;
Gtk::Box right_;
Gtk::Box box_;
std::vector<std::shared_ptr<waybar::AModule>> modules_left_;
std::vector<std::shared_ptr<waybar::AModule>> modules_center_;
std::vector<std::shared_ptr<waybar::AModule>> modules_right_;
#ifdef HAVE_SWAY
using BarIpcClient = modules::sway::BarIpcClient;
std::unique_ptr<BarIpcClient> _ipc_client;
#endif
std::vector<std::shared_ptr<waybar::AModule>> modules_all_;
};

} // namespace waybar
4 changes: 3 additions & 1 deletion include/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class Bar;
class Factory {
public:
Factory(const Bar& bar, const Json::Value& config);
AModule* makeModule(const std::string& name, const std::string& pos) const;
AModule* addModule(const std::string& name, const std::string& pos);
std::vector<std::shared_ptr<waybar::AModule>> modules_all_;

private:
const Bar& bar_;
const Json::Value& config_;
AModule* makeModule(const std::string& name, const std::string& pos, waybar::Factory& factory) const;
};

} // namespace waybar
3 changes: 2 additions & 1 deletion include/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include <json/json.h>

#include "AModule.hpp"
#include "factory.hpp"
#include "gtkmm/revealer.h"

namespace waybar {

class Group : public AModule {
public:
Group(const std::string&, const std::string&, const Json::Value&, bool);
Group(const std::string&, const std::string&, const Json::Value&, bool, const std::string&, Factory&);
virtual ~Group() = default;

Check warning on line 16 in include/group.hpp

View workflow job for this annotation

GitHub Actions / build

include/group.hpp:16:11 [modernize-use-override]

prefer using 'override' or (rarely) 'final' instead of 'virtual'
auto update() -> void override;
operator Gtk::Widget&() override;
Expand Down
57 changes: 17 additions & 40 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "client.hpp"
#include "factory.hpp"
#include "group.hpp"

#ifdef HAVE_SWAY
#include "modules/sway/bar.hpp"
Expand Down Expand Up @@ -139,7 +138,8 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
center_(Gtk::ORIENTATION_HORIZONTAL, 0),
right_(Gtk::ORIENTATION_HORIZONTAL, 0),
box_(Gtk::ORIENTATION_HORIZONTAL, 0) {
box_(Gtk::ORIENTATION_HORIZONTAL, 0),
factory_(*this, config) {
window.set_title("waybar");
window.set_name("waybar");
window.set_decorated(false);
Expand Down Expand Up @@ -466,49 +466,27 @@ void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) {
}

void waybar::Bar::handleSignal(int signal) {
for (auto& module : modules_all_) {
for (auto& module : factory_.modules_all_) {
module->refresh(signal);
}
}

void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
waybar::Group* group = nullptr) {
auto module_list = group ? config[pos]["modules"] : config[pos];
void waybar::Bar::getModules(const std::string& pos) {
auto module_list = config[pos];

Check warning on line 475 in src/bar.cpp

View workflow job for this annotation

GitHub Actions / build

src/bar.cpp:475:8 [readability-identifier-naming]

invalid case style for variable 'module_list'
if (module_list.isArray()) {
for (const auto& name : module_list) {
try {
auto ref = name.asString();
AModule* module;

if (ref.compare(0, 6, "group/") == 0 && ref.size() > 6) {
auto hash_pos = ref.find('#');
auto id_name = ref.substr(6, hash_pos - 6);
auto class_name = hash_pos != std::string::npos ? ref.substr(hash_pos + 1) : "";

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);
module = group_module;
} else {
module = factory.makeModule(ref, pos);
}

AModule* module = factory_.addModule(ref, pos);
std::shared_ptr<AModule> module_sp(module);

Check warning on line 481 in src/bar.cpp

View workflow job for this annotation

GitHub Actions / build

src/bar.cpp:481:34 [readability-identifier-naming]

invalid case style for variable 'module_sp'
modules_all_.emplace_back(module_sp);
if (group) {
group->addWidget(*module);
} else {
if (pos == "modules-left") {
modules_left_.emplace_back(module_sp);
}
if (pos == "modules-center") {
modules_center_.emplace_back(module_sp);
}
if (pos == "modules-right") {
modules_right_.emplace_back(module_sp);
}
if (pos == "modules-left") {
modules_left_.emplace_back(module_sp);
}
if (pos == "modules-center") {
modules_center_.emplace_back(module_sp);
}
if (pos == "modules-right") {
modules_right_.emplace_back(module_sp);
}
module->dp.connect([module, ref] {
try {
Expand Down Expand Up @@ -539,10 +517,9 @@ auto waybar::Bar::setupWidgets() -> void {
setupAltFormatKeyForModuleList("modules-right");
setupAltFormatKeyForModuleList("modules-center");

Factory factory(*this, config);
getModules(factory, "modules-left");
getModules(factory, "modules-center");
getModules(factory, "modules-right");
getModules("modules-left");
getModules("modules-center");
getModules("modules-right");
for (auto const& module : modules_left_) {
left_.pack_start(*module, false, false);
}
Expand Down
24 changes: 22 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "factory.hpp"

#include "bar.hpp"
#include "gtkmm/enums.h"

#if defined(HAVE_CHRONO_TIMEZONES) || defined(HAVE_LIBDATE)
#include "modules/clock.hpp"
Expand Down Expand Up @@ -110,10 +111,19 @@
#include "modules/temperature.hpp"
#include "modules/user.hpp"

waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), config_(config) {}
waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), config_(config) {
}

waybar::AModule* waybar::Factory::addModule(const std::string& name,
const std::string& pos) {
auto *module = makeModule(name, pos, *this);
modules_all_.emplace_back(module);
return module;
}

waybar::AModule* waybar::Factory::makeModule(const std::string& name,

Check warning on line 124 in src/factory.cpp

View workflow job for this annotation

GitHub Actions / build

src/factory.cpp:124:35 [readability-function-cognitive-complexity]

function 'makeModule' has cognitive complexity of 57 (threshold 25)
const std::string& pos) const {
const std::string& pos,
waybar::Factory& factory) const {
try {
auto hash_pos = name.find('#');
auto ref = name.substr(0, hash_pos);
Expand Down Expand Up @@ -325,6 +335,16 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
if (ref.compare(0, 5, "cffi/") == 0 && ref.size() > 5) {
return new waybar::modules::CFFI(ref.substr(5), id, config_[name]);
}
if (ref.compare(0, 6, "group/") == 0 && ref.size() > 6) {
return new waybar::Group(
ref.substr(6),
hash_pos != std::string::npos ? ref.substr(hash_pos + 1) : "",
config_[name],
bar_.box_.get_orientation() == Gtk::ORIENTATION_VERTICAL,
pos,
factory
);
}
} catch (const std::exception& e) {
auto err = fmt::format("Disabling module \"{}\", {}", name, e.what());
throw std::runtime_error(err);
Expand Down
26 changes: 23 additions & 3 deletions src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ const Gtk::RevealerTransitionType getPreferredTransitionType(bool is_vertical) {

if (is_vertical) {
return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_UP;
} else {
return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_LEFT;
}
return Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_LEFT;
}

Group::Group(const std::string& name, const std::string& id, const Json::Value& config,

Check warning on line 26 in src/group.cpp

View workflow job for this annotation

GitHub Actions / build

src/group.cpp:26:8 [readability-function-cognitive-complexity]

function 'Group' has cognitive complexity of 31 (threshold 25)
bool vertical)
bool vertical, const std::string& pos, Factory& factory)
: 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} {
Expand Down Expand Up @@ -81,6 +80,27 @@ Group::Group(const std::string& name, const std::string& id, const Json::Value&

addHoverHandlerTo(revealer);
}

auto module_list = config_["modules"];

Check warning on line 84 in src/group.cpp

View workflow job for this annotation

GitHub Actions / build

src/group.cpp:84:8 [readability-identifier-naming]

invalid case style for variable 'module_list'
if (module_list.isArray()) {
for (const auto& name : module_list) {
try {
auto ref = name.asString();
AModule* module = factory.addModule(ref, pos);
std::shared_ptr<AModule> module_sp(module);

Check warning on line 90 in src/group.cpp

View workflow job for this annotation

GitHub Actions / build

src/group.cpp:90:34 [readability-identifier-naming]

invalid case style for variable 'module_sp'
addWidget(*module_sp);
module->dp.connect([module, ref] {
try {
module->update();
} catch (const std::exception& e) {
spdlog::error("{}: {}", ref, e.what());
}
});
} catch (const std::exception& e) {
spdlog::warn("module {}: {}", name.asString(), e.what());
}
}
}
}

bool Group::handleMouseHover(GdkEventCrossing* const& e) {
Expand Down

0 comments on commit f679d30

Please sign in to comment.