pull/805/head
dmitry 2021-11-23 03:10:44 +03:00
parent ef4c6a9ba3
commit 23991b6543
4 changed files with 183 additions and 85 deletions

View File

@ -5,6 +5,7 @@
#include <gtkmm/image.h> #include <gtkmm/image.h>
#include <gtkmm/label.h> #include <gtkmm/label.h>
#include <functional>
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -37,25 +38,24 @@ class Workspace {
auto handle_done() -> void; auto handle_done() -> void;
auto handle_clicked(GdkEventButton *bt) -> bool; auto handle_clicked(GdkEventButton *bt) -> bool;
auto show() -> void { button_.show(); } auto show() -> void;
auto hide() -> void { button_.hide(); } auto hide() -> void;
auto get_button_ref() -> Gtk::Button & { return button_; } auto get_button_ref() -> Gtk::Button & { return button_; }
auto get_name() -> std::string & { return name_; } auto get_name() -> std::string & { return name_; }
auto get_coords() -> std::vector<uint32_t> & { return coordinates_; } auto get_coords() -> std::vector<uint32_t> & { return coordinates_; }
enum class State enum class State {
{ ACTIVE = (1 << 0),
ACTIVE = 1 << 0, URGENT = (1 << 1),
URGENT = 1 << 1, HIDDEN = (1 << 2),
HIDDEN = 1 << 2,
}; };
private: private:
auto get_icon() -> std::string; auto get_icon() -> std::string;
const Bar & bar_; const Bar &bar_;
const Json::Value &config_; const Json::Value &config_;
WorkspaceGroup & workspace_group_; WorkspaceGroup &workspace_group_;
// wlr stuff // wlr stuff
zext_workspace_handle_v1 *workspace_handle_; zext_workspace_handle_v1 *workspace_handle_;
@ -84,35 +84,36 @@ class WorkspaceGroup {
auto id() const -> uint32_t { return id_; } auto id() const -> uint32_t { return id_; }
auto is_visible() const -> bool; auto is_visible() const -> bool;
auto remove_workspace(uint32_t id_) -> void; auto remove_workspace(uint32_t id_) -> void;
auto active_only() const -> bool;
auto creation_delayed() const -> bool;
auto workspaces() -> std::vector<std::unique_ptr<Workspace>> & { return workspaces_; }
auto sort_workspaces() -> void;
auto set_need_to_sort() -> void { need_to_sort = true; }
auto add_button(Gtk::Button &button) -> void;
auto remove_button(Gtk::Button &button) -> void;
// wlr stuff // wlr stuff
auto handle_workspace_create(zext_workspace_handle_v1 *workspace_handle) -> void; auto handle_workspace_create(zext_workspace_handle_v1 *workspace_handle) -> void;
auto handle_remove() -> void; auto handle_remove() -> void;
auto handle_output_enter(wl_output *output) -> void; auto handle_output_enter(wl_output *output) -> void;
auto handle_output_leave() -> void; auto handle_output_leave() -> void;
auto add_button(Gtk::Button &button) -> void;
auto remove_button(Gtk::Button &button) -> void;
auto handle_done() -> void; auto handle_done() -> void;
auto commit() -> void; auto commit() -> void;
auto sort_workspaces() -> void;
auto set_need_to_sort() -> void { need_to_sort = true; }
private: private:
static uint32_t workspace_global_id; static uint32_t workspace_global_id;
const waybar::Bar &bar_; const waybar::Bar &bar_;
Gtk::Box & box_; Gtk::Box &box_;
const Json::Value &config_; const Json::Value &config_;
WorkspaceManager & workspace_manager_; WorkspaceManager &workspace_manager_;
// wlr stuff // wlr stuff
zext_workspace_group_handle_v1 *workspace_group_handle_; zext_workspace_group_handle_v1 *workspace_group_handle_;
wl_output * output_ = nullptr; wl_output *output_ = nullptr;
uint32_t id_; uint32_t id_;
std::vector<std::unique_ptr<Workspace>> workspaces_; std::vector<std::unique_ptr<Workspace>> workspaces_;
bool sort_by_name = true;
bool sort_by_coordinates = true;
bool need_to_sort = false; bool need_to_sort = false;
}; };
@ -122,6 +123,13 @@ class WorkspaceManager : public AModule {
~WorkspaceManager() override; ~WorkspaceManager() override;
auto update() -> void override; auto update() -> void override;
auto all_outputs() const -> bool { return all_outputs_; }
auto active_only() const -> bool { return active_only_; }
auto workspace_comparator() const
-> std::function<bool(std::unique_ptr<Workspace> &, std::unique_ptr<Workspace> &)>;
auto creation_delayed() const -> bool { return creation_delayed_; }
auto sort_workspaces() -> void;
auto remove_workspace_group(uint32_t id_) -> void; auto remove_workspace_group(uint32_t id_) -> void;
// wlr stuff // wlr stuff
@ -130,11 +138,10 @@ class WorkspaceManager : public AModule {
-> void; -> void;
auto handle_done() -> void; auto handle_done() -> void;
auto handle_finished() -> void; auto handle_finished() -> void;
auto commit() -> void; auto commit() -> void;
private: private:
const waybar::Bar & bar_; const waybar::Bar &bar_;
Gtk::Box box_; Gtk::Box box_;
std::vector<std::unique_ptr<WorkspaceGroup>> groups_; std::vector<std::unique_ptr<WorkspaceGroup>> groups_;
@ -142,6 +149,12 @@ class WorkspaceManager : public AModule {
zext_workspace_manager_v1 *workspace_manager_ = nullptr; zext_workspace_manager_v1 *workspace_manager_ = nullptr;
static uint32_t group_global_id; static uint32_t group_global_id;
bool sort_by_name_ = true;
bool sort_by_coordinates_ = true;
bool all_outputs_ = false;
bool active_only_ = false;
bool creation_delayed_ = false;
}; };
} // namespace waybar::modules::wlr } // namespace waybar::modules::wlr

View File

@ -33,6 +33,16 @@ Addressed by *wlr/workspaces*
Note that if both *sort-by-name* and *sort-by-coordinates* are true sort by name will be first. Note that if both *sort-by-name* and *sort-by-coordinates* are true sort by name will be first.
If both are false - sort by id will be performed. If both are false - sort by id will be performed.
*all-outputs*: ++
typeof: bool ++
default: false ++
If set to false workspaces group will be shown only in assigned output. Otherwise all workspace groups are shown.
*active-only*: ++
typeof: bool ++
default: false ++
If set to true only active or urgent workspaces will be shown.
# FORMAT REPLACEMENTS # FORMAT REPLACEMENTS
*{name}*: Name of workspace assigned by compositor *{name}*: Name of workspace assigned by compositor

View File

@ -5,7 +5,10 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <algorithm> #include <algorithm>
#include <iterator>
#include <vector>
#include "gtkmm/widget.h"
#include "modules/wlr/workspace_manager_binding.hpp" #include "modules/wlr/workspace_manager_binding.hpp"
namespace waybar::modules::wlr { namespace waybar::modules::wlr {
@ -19,6 +22,27 @@ WorkspaceManager::WorkspaceManager(const std::string &id, const waybar::Bar &bar
: waybar::AModule(config, "workspaces", id, false, false), : waybar::AModule(config, "workspaces", id, false, false),
bar_(bar), bar_(bar),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) { box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) {
auto config_sort_by_name = config_["sort-by-name"];
if (config_sort_by_name.isBool()) {
sort_by_name_ = config_sort_by_name.asBool();
}
auto config_sort_by_coordinates = config_["sort-by-coordinates"];
if (config_sort_by_coordinates.isBool()) {
sort_by_coordinates_ = config_sort_by_coordinates.asBool();
}
auto config_all_outputs = config_["all-outputs"];
if (config_all_outputs.isBool()) {
all_outputs_ = config_all_outputs.asBool();
}
auto config_active_only = config_["active-only"];
if (config_active_only.isBool()) {
active_only_ = config_active_only.asBool();
creation_delayed_ = active_only_;
}
box_.set_name("workspaces"); box_.set_name("workspaces");
if (!id.empty()) { if (!id.empty()) {
box_.get_style_context()->add_class(id); box_.get_style_context()->add_class(id);
@ -31,6 +55,54 @@ WorkspaceManager::WorkspaceManager(const std::string &id, const waybar::Bar &bar
} }
} }
auto WorkspaceManager::workspace_comparator() const
-> std::function<bool(std::unique_ptr<Workspace> &, std::unique_ptr<Workspace> &)> {
return [=](std::unique_ptr<Workspace> &lhs, std::unique_ptr<Workspace> &rhs) {
auto is_name_less = lhs->get_name() < rhs->get_name();
auto is_name_eq = lhs->get_name() == rhs->get_name();
auto is_coords_less = lhs->get_coords() < rhs->get_coords();
if (sort_by_name_) {
if (sort_by_coordinates_) {
return is_name_eq ? is_coords_less : is_name_less;
} else {
return is_name_less;
}
}
if (sort_by_coordinates_) {
return is_coords_less;
}
return lhs->id() < rhs->id();
};
}
auto WorkspaceManager::sort_workspaces() -> void {
std::vector<std::reference_wrapper<std::unique_ptr<Workspace>>> all_workspaces;
for (auto &group : groups_) {
auto &group_workspaces = group->workspaces();
all_workspaces.reserve(all_workspaces.size() +
std::distance(group_workspaces.begin(), group_workspaces.end()));
if (!active_only()) {
all_workspaces.insert(all_workspaces.end(), group_workspaces.begin(), group_workspaces.end());
continue;
}
for (auto &workspace : group_workspaces) {
if (!workspace->is_active()) {
continue;
}
all_workspaces.push_back(workspace);
}
}
std::sort(all_workspaces.begin(), all_workspaces.end(), workspace_comparator());
for (size_t i = 0; i < all_workspaces.size(); ++i) {
box_.reorder_child(all_workspaces[i].get()->get_button_ref(), i);
}
}
auto WorkspaceManager::register_manager(wl_registry *registry, uint32_t name, uint32_t version) auto WorkspaceManager::register_manager(wl_registry *registry, uint32_t name, uint32_t version)
-> void { -> void {
if (workspace_manager_) { if (workspace_manager_) {
@ -67,6 +139,9 @@ auto WorkspaceManager::update() -> void {
for (auto &group : groups_) { for (auto &group : groups_) {
group->update(); group->update();
} }
if (creation_delayed()) {
creation_delayed_ = false;
}
AModule::update(); AModule::update();
} }
@ -94,7 +169,7 @@ auto WorkspaceManager::remove_workspace_group(uint32_t id) -> void {
auto WorkspaceManager::commit() -> void { zext_workspace_manager_v1_commit(workspace_manager_); } auto WorkspaceManager::commit() -> void { zext_workspace_manager_v1_commit(workspace_manager_); }
WorkspaceGroup::WorkspaceGroup(const Bar &bar, Gtk::Box &box, const Json::Value &config, WorkspaceGroup::WorkspaceGroup(const Bar &bar, Gtk::Box &box, const Json::Value &config,
WorkspaceManager & manager, WorkspaceManager &manager,
zext_workspace_group_handle_v1 *workspace_group_handle, uint32_t id) zext_workspace_group_handle_v1 *workspace_group_handle, uint32_t id)
: bar_(bar), : bar_(bar),
box_(box), box_(box),
@ -103,15 +178,11 @@ WorkspaceGroup::WorkspaceGroup(const Bar &bar, Gtk::Box &box, const Json::Value
workspace_group_handle_(workspace_group_handle), workspace_group_handle_(workspace_group_handle),
id_(id) { id_(id) {
add_workspace_group_listener(workspace_group_handle, this); add_workspace_group_listener(workspace_group_handle, this);
auto config_sort_by_name = config_["sort-by-name"]; }
if (config_sort_by_name.isBool()) {
sort_by_name = config_sort_by_name.asBool();
}
auto config_sort_by_coordinates = config_["sort-by-coordinates"]; auto WorkspaceGroup::active_only() const -> bool { return workspace_manager_.active_only(); }
if (config_sort_by_coordinates.isBool()) { auto WorkspaceGroup::creation_delayed() const -> bool {
sort_by_coordinates = config_sort_by_coordinates.asBool(); return workspace_manager_.creation_delayed();
}
} }
auto WorkspaceGroup::add_button(Gtk::Button &button) -> void { auto WorkspaceGroup::add_button(Gtk::Button &button) -> void {
@ -143,7 +214,7 @@ auto WorkspaceGroup::handle_output_enter(wl_output *output) -> void {
spdlog::debug("Output {} assigned to {} group", (void *)output, id_); spdlog::debug("Output {} assigned to {} group", (void *)output, id_);
output_ = output; output_ = output;
if (!is_visible()) { if (!is_visible() || workspace_manager_.creation_delayed()) {
return; return;
} }
@ -153,7 +224,9 @@ auto WorkspaceGroup::handle_output_enter(wl_output *output) -> void {
} }
auto WorkspaceGroup::is_visible() const -> bool { auto WorkspaceGroup::is_visible() const -> bool {
return output_ != nullptr && output_ == gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj()); return output_ != nullptr &&
(workspace_manager_.all_outputs() ||
output_ == gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj()));
} }
auto WorkspaceGroup::handle_output_leave() -> void { auto WorkspaceGroup::handle_output_leave() -> void {
@ -171,6 +244,13 @@ auto WorkspaceGroup::handle_output_leave() -> void {
auto WorkspaceGroup::update() -> void { auto WorkspaceGroup::update() -> void {
for (auto &workspace : workspaces_) { for (auto &workspace : workspaces_) {
if (workspace_manager_.creation_delayed()) {
add_button(workspace->get_button_ref());
if (is_visible() && (workspace->is_active() || workspace->is_urgent())) {
workspace->show();
}
}
workspace->update(); workspace->update();
} }
} }
@ -198,41 +278,27 @@ auto WorkspaceGroup::handle_done() -> void {
workspace->handle_done(); workspace->handle_done();
} }
sort_workspaces(); if (creation_delayed()) {
return;
}
if (!workspace_manager_.all_outputs()) {
//sort_workspaces();
} else {
workspace_manager_.sort_workspaces();
}
} }
auto WorkspaceGroup::commit() -> void { workspace_manager_.commit(); } auto WorkspaceGroup::commit() -> void { workspace_manager_.commit(); }
auto WorkspaceGroup::sort_workspaces() -> void { auto WorkspaceGroup::sort_workspaces() -> void {
auto cmp = [=](std::unique_ptr<Workspace> &lhs, std::unique_ptr<Workspace> &rhs) { std::sort(workspaces_.begin(), workspaces_.end(), workspace_manager_.workspace_comparator());
auto is_name_less = lhs->get_name() < rhs->get_name();
auto is_name_eq = lhs->get_name() == rhs->get_name();
auto is_coords_less = lhs->get_coords() < rhs->get_coords();
if (sort_by_name) {
if (sort_by_coordinates) {
return is_name_eq ? is_coords_less : is_name_less;
}
else {
return is_name_less;
}
}
if (sort_by_coordinates) {
return is_coords_less;
}
return lhs->id() < rhs->id();
};
std::sort(workspaces_.begin(), workspaces_.end(), cmp);
for (size_t i = 0; i < workspaces_.size(); ++i) { for (size_t i = 0; i < workspaces_.size(); ++i) {
box_.reorder_child(workspaces_[i]->get_button_ref(), i); box_.reorder_child(workspaces_[i]->get_button_ref(), i);
} }
} }
auto WorkspaceGroup::remove_button(Gtk::Button &button) -> void { auto WorkspaceGroup::remove_button(Gtk::Button &button) -> void { box_.remove(button); }
box_.remove(button);
}
Workspace::Workspace(const Bar &bar, const Json::Value &config, WorkspaceGroup &workspace_group, Workspace::Workspace(const Bar &bar, const Json::Value &config, WorkspaceGroup &workspace_group,
zext_workspace_handle_v1 *workspace, uint32_t id) zext_workspace_handle_v1 *workspace, uint32_t id)
@ -256,11 +322,11 @@ Workspace::Workspace(const Bar &bar, const Json::Value &config, WorkspaceGroup &
} }
/* Handle click events if configured */ /* Handle click events if configured */
if (config_["on-click"].isString() || config_["on-click-middle"].isString() if (config_["on-click"].isString() || config_["on-click-middle"].isString() ||
|| config_["on-click-right"].isString()) { config_["on-click-right"].isString()) {
button_.add_events(Gdk::BUTTON_PRESS_MASK); button_.add_events(Gdk::BUTTON_PRESS_MASK);
button_.signal_button_press_event().connect( button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handle_clicked),
sigc::mem_fun(*this, &Workspace::handle_clicked), false); false);
} }
button_.set_relief(Gtk::RELIEF_NONE); button_.set_relief(Gtk::RELIEF_NONE);
@ -272,9 +338,7 @@ Workspace::Workspace(const Bar &bar, const Json::Value &config, WorkspaceGroup &
} }
workspace_group.add_button(button_); workspace_group.add_button(button_);
button_.show(); button_.show_all();
label_.show();
content_.show();
} }
Workspace::~Workspace() { Workspace::~Workspace() {
@ -303,7 +367,7 @@ auto Workspace::handle_state(const std::vector<uint32_t> &state) -> void {
state_ |= (uint32_t)State::URGENT; state_ |= (uint32_t)State::URGENT;
break; break;
case ZEXT_WORKSPACE_HANDLE_V1_STATE_HIDDEN: case ZEXT_WORKSPACE_HANDLE_V1_STATE_HIDDEN:
state_ |= (uint32_t)State::URGENT; state_ |= (uint32_t)State::HIDDEN;
break; break;
} }
} }
@ -315,7 +379,8 @@ auto Workspace::handle_remove() -> void {
workspace_group_.remove_workspace(id_); workspace_group_.remove_workspace(id_);
} }
auto add_or_remove_class(Glib::RefPtr<Gtk::StyleContext> context, bool condition, const std::string& class_name) { auto add_or_remove_class(Glib::RefPtr<Gtk::StyleContext> context, bool condition,
const std::string &class_name) {
if (condition) { if (condition) {
context->add_class(class_name); context->add_class(class_name);
} else { } else {
@ -329,6 +394,16 @@ auto Workspace::handle_done() -> void {
add_or_remove_class(style_context, is_active(), "active"); add_or_remove_class(style_context, is_active(), "active");
add_or_remove_class(style_context, is_urgent(), "urgent"); add_or_remove_class(style_context, is_urgent(), "urgent");
add_or_remove_class(style_context, is_hidden(), "hidden"); add_or_remove_class(style_context, is_hidden(), "hidden");
if (workspace_group_.creation_delayed()) {
return;
}
if (workspace_group_.active_only() && (is_active() || is_urgent())) {
button_.show_all();
} else if (workspace_group_.active_only() && !(is_active() || is_urgent())) {
button_.hide();
}
} }
auto Workspace::get_icon() -> std::string { auto Workspace::get_icon() -> std::string {
@ -356,11 +431,9 @@ auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
std::string action; std::string action;
if (config_["on-click"].isString() && bt->button == 1) { if (config_["on-click"].isString() && bt->button == 1) {
action = config_["on-click"].asString(); action = config_["on-click"].asString();
} } else if (config_["on-click-middle"].isString() && bt->button == 2) {
else if (config_["on-click-middle"].isString() && bt->button == 2) {
action = config_["on-click-middle"].asString(); action = config_["on-click-middle"].asString();
} } else if (config_["on-click-right"].isString() && bt->button == 3) {
else if (config_["on-click-right"].isString() && bt->button == 3) {
action = config_["on-click-right"].asString(); action = config_["on-click-right"].asString();
} }
@ -368,11 +441,9 @@ auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
return true; return true;
else if (action == "activate") { else if (action == "activate") {
zext_workspace_handle_v1_activate(workspace_handle_); zext_workspace_handle_v1_activate(workspace_handle_);
} } else if (action == "close") {
else if (action == "close") {
zext_workspace_handle_v1_remove(workspace_handle_); zext_workspace_handle_v1_remove(workspace_handle_);
} } else {
else {
spdlog::warn("Unknown action {}", action); spdlog::warn("Unknown action {}", action);
} }
@ -381,6 +452,9 @@ auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
return true; return true;
} }
auto Workspace::show() -> void { button_.show_all(); }
auto Workspace::hide() -> void { button_.hide(); }
auto Workspace::handle_name(const std::string &name) -> void { auto Workspace::handle_name(const std::string &name) -> void {
if (name_ != name) { if (name_ != name) {
workspace_group_.set_need_to_sort(); workspace_group_.set_need_to_sort();

View File

@ -1,6 +1,7 @@
#include "modules/wlr/workspace_manager_binding.hpp" #include "modules/wlr/workspace_manager_binding.hpp"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <cstdint>
#include "client.hpp" #include "client.hpp"
#include "modules/wlr/workspace_manager.hpp" #include "modules/wlr/workspace_manager.hpp"
@ -100,7 +101,7 @@ void workspace_handle_coordinates(void *data, struct zext_workspace_handle_v1 *_
struct wl_array *coordinates) { struct wl_array *coordinates) {
std::vector<uint32_t> coords_vec; std::vector<uint32_t> coords_vec;
auto coords = static_cast<uint32_t *>(coordinates->data); auto coords = static_cast<uint32_t *>(coordinates->data);
for (size_t i = 0; i < coordinates->size; ++i) { for (size_t i = 0; i < coordinates->size / sizeof(uint32_t); ++i) {
coords_vec.push_back(coords[i]); coords_vec.push_back(coords[i]);
} }
@ -111,7 +112,7 @@ void workspace_handle_state(void *data, struct zext_workspace_handle_v1 *workspa
struct wl_array *state) { struct wl_array *state) {
std::vector<uint32_t> state_vec; std::vector<uint32_t> state_vec;
auto states = static_cast<uint32_t *>(state->data); auto states = static_cast<uint32_t *>(state->data);
for (size_t i = 0; i < state->size; ++i) { for (size_t i = 0; i < state->size / sizeof(uint32_t); ++i) {
state_vec.push_back(states[i]); state_vec.push_back(states[i]);
} }