From 17b60bc73783f8f32a75b779ca0a867e9730b352 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 1 Jul 2022 15:35:25 +0200 Subject: [PATCH] minor changes --- include/modules/hyprland/workspaces.hpp | 2 ++ src/modules/hyprland/workspaces.cpp | 35 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 8d182d5..93e9770 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -32,6 +32,8 @@ class Workspaces : public AModule, public sigc::trackable { bool isNumber(const std::string&); + bool needReorder = false; + Gtk::Box box_; const Bar& bar_; std::deque workspaces; diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 0dad13a..92dabc9 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -118,6 +118,9 @@ void Workspaces::updateButtons() { for (auto it = ws.begin(); it != ws.end(); ++it) { auto bit = buttons_.find(*it); + if (bit == buttons_.end()) + needReorder = true; + auto &button = bit == buttons_.end() ? addButton(*it) : bit->second; if (focusedWorkspace == *it) { @@ -132,6 +135,10 @@ void Workspaces::updateButtons() { label = fmt::format(format, fmt::arg("icon", getIcon(*it)), fmt::arg("name", *it)); } + if (needReorder) { + box_.reorder_child(button, it - workspaces.begin()); + } + button.set_label(label); button.show(); @@ -139,6 +146,8 @@ void Workspaces::updateButtons() { AModule::update(); + needReorder = false; + mutex_.unlock(); } @@ -183,22 +192,28 @@ void Workspaces::onEvent(const std::string& ev) { focusedWorkspace = WORKSPACE; } else if (EVENT == "createworkspace") { workspaces.emplace_back(WORKSPACE); - - // remove the buttons for reorder - buttons_.clear(); } else { - const auto it = std::remove(workspaces.begin(), workspaces.end(), WORKSPACE); + for (auto it = workspaces.begin(); it != workspaces.end(); it++) { + if (*it == WORKSPACE) { + workspaces.erase(it); + break; + } + } - if (it != workspaces.end()) - workspaces.erase(it); + // also remove the button + for (auto it = buttons_.begin(); it != buttons_.end(); it++) { + if (it->second.get_name() == WORKSPACE) + it = buttons_.erase(it); + + if (it == buttons_.end()) + break; + } - // also remove the buttons - buttons_.clear(); } - dp.emit(); - mutex_.unlock(); + + dp.emit(); } bool Workspaces::handleScroll(GdkEventScroll *e) {