Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(group): moving group stuff out of bar #3188

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
std::shared_ptr<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_;
waybar::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,14 +5,15 @@
#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
56 changes: 16 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 @@
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,26 @@
}

void waybar::Bar::handleSignal(int signal) {
for (auto& module : modules_all_) {
for (const 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);
auto module = factory_.addModule(ref, pos);
if (pos == "modules-left") {
modules_left_.emplace_back(module);
}

std::shared_ptr<AModule> module_sp(module);
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-center") {
modules_center_.emplace_back(module);
}
if (pos == "modules-right") {
modules_right_.emplace_back(module);
}
module->dp.connect([module, ref] {
try {
Expand Down Expand Up @@ -539,10 +516,9 @@
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
26 changes: 24 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "factory.hpp"
#include <memory>

#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 +112,20 @@
#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) {
}

std::shared_ptr<waybar::AModule> waybar::Factory::addModule(const std::string& name,
const std::string& pos) {
auto module = makeModule(name, pos, *this);

Check warning on line 120 in src/factory.cpp

View workflow job for this annotation

GitHub Actions / build

src/factory.cpp:120:3 [readability-qualified-auto]

'auto module' can be declared as 'auto *module'
std::shared_ptr<AModule> module_sp(module);

Check warning on line 121 in src/factory.cpp

View workflow job for this annotation

GitHub Actions / build

src/factory.cpp:121:28 [readability-identifier-naming]

invalid case style for variable 'module_sp'
modules_all_.emplace_back(module_sp);
return module_sp;
}

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

Check warning on line 126 in src/factory.cpp

View workflow job for this annotation

GitHub Actions / build

src/factory.cpp:126: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 +337,16 @@
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
25 changes: 22 additions & 3 deletions src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

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,26 @@

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();
auto module = factory.addModule(ref, pos);
addWidget(*module);
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
Loading