Skip to content
Open
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
6 changes: 4 additions & 2 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AModule : public IModule {
virtual auto refresh(int shouldRefresh) -> void {};
operator Gtk::Widget&() override;
auto doAction(const std::string& name) -> void override;

void init();
/// Emitting on this dispatcher triggers a update() call
Glib::Dispatcher dp;

Expand All @@ -37,12 +37,14 @@ class AModule : public IModule {
bool tooltipEnabled() const;

std::vector<int> pid_children_;
bool enable_scroll_;
bool enable_click_;
const std::string name_;
const Json::Value& config_;
Gtk::EventBox event_box_;

virtual void setCursor(Gdk::CursorType const& c);

virtual void do_init() {}; // implemented by derived class if needed
virtual bool handleToggle(GdkEventButton* const& ev);
virtual bool handleMouseEnter(GdkEventCrossing* const& ev);
virtual bool handleMouseLeave(GdkEventCrossing* const& ev);
Expand Down
3 changes: 3 additions & 0 deletions include/modules/hyprland/workspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Workspaces : public AModule, public EventHandler {
bool windowRewriteConfigUsesTitle() const { return m_anyWindowRewriteRuleUsesTitle; }
const IconLoader& iconLoader() const { return m_iconLoader; }

protected:
void do_init() override;

private:
void onEvent(const std::string& e) override;
void updateWindowCount();
Expand Down
45 changes: 26 additions & 19 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,40 @@ namespace waybar {

AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
bool enable_click, bool enable_scroll)
: name_(name),
: enable_scroll_(enable_scroll),
enable_click_(enable_click),
name_(name),
config_(config),
isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true},
isExpand{config_["expand"].isBool() ? config_["expand"].asBool() : false},
distance_scrolled_y_(0.0),
distance_scrolled_x_(0.0) {
distance_scrolled_x_(0.0) {}

AModule::~AModule() {
for (const auto& pid : pid_children_) {
if (pid != -1) {
killpg(pid, SIGTERM);
}
}
if (menu_ != nullptr) {
g_object_unref(menu_);
menu_ = nullptr;
}
}

void AModule::init() {
// here are the point where obj is fully constructed
// we can do sth like connect signal that requires well constructed object
do_init();
// Configure module action Map
const Json::Value actions{config_["actions"]};

const auto& config = config_;
for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) {
if (it.key().isString() && it->isString())
if (!eventActionMap_.contains(it.key().asString())) {
eventActionMap_.insert({it.key().asString(), it->asString()});
enable_click = true;
enable_scroll = true;
enable_click_ = true;
enable_scroll_ = true;
} else
spdlog::warn("Duplicate action is ignored: {0}", it.key().asString());
else
Expand All @@ -45,7 +64,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
config[eventEntry.second].isString();
}) != eventMap_.cend();

if (enable_click || hasUserEvents) {
if (enable_click_ || hasUserEvents) {
hasUserEvents_ = true;
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle));
Expand All @@ -65,7 +84,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
}
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString() ||
config_["on-scroll-left"].isString() || config_["on-scroll-right"].isString() ||
enable_scroll) {
enable_scroll_) {
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
}
Expand All @@ -82,18 +101,6 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
}
}

AModule::~AModule() {
for (const auto& pid : pid_children_) {
if (pid != -1) {
killpg(pid, SIGTERM);
}
}
if (menu_ != nullptr) {
g_object_unref(menu_);
menu_ = nullptr;
}
}

auto AModule::update() -> void {
// Run user-provided update handler if configured
if (config_["on-update"].isString()) {
Expand Down
1 change: 1 addition & 0 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
module = group_module.release();
} else {
module = factory.makeModule(ref, pos);
module->init();
}

std::shared_ptr<AModule> module_sp(module);
Expand Down
10 changes: 6 additions & 4 deletions src/modules/hyprland/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ Workspaces::Workspaces(const std::string& id, const Bar& bar, const Json::Value&
}
m_box.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(m_box);

setCurrentMonitorId();
init();
registerIpc();
}

Workspaces::~Workspaces() {
Expand All @@ -42,6 +38,12 @@ Workspaces::~Workspaces() {
std::lock_guard<std::mutex> lg(m_mutex);
}

void Workspaces::do_init() {
setCurrentMonitorId();
init();
registerIpc();
}

void Workspaces::init() {
m_activeWorkspaceId = m_ipc.getSocket1JsonReply("activeworkspace")["id"].asInt();

Expand Down
Loading