Waybar/src/modules/sway/workspaces.cpp

264 lines
8.8 KiB
C++
Raw Normal View History

2018-08-15 18:17:17 +00:00
#include "modules/sway/workspaces.hpp"
2018-08-08 21:54:58 +00:00
waybar::modules::sway::Workspaces::Workspaces(const std::string &id, const Bar &bar,
const Json::Value &config)
: bar_(bar),
config_(config),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
scrolling_(false) {
2018-08-16 12:29:41 +00:00
box_.set_name("workspaces");
2018-12-18 16:30:54 +00:00
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
2018-08-20 12:50:45 +00:00
ipc_.subscribe("[ \"workspace\" ]");
// Launch worker
worker();
}
void waybar::modules::sway::Workspaces::worker() {
2018-08-16 12:29:41 +00:00
thread_ = [this] {
2018-08-13 19:23:43 +00:00
try {
2019-02-01 23:36:52 +00:00
if (!workspaces_.empty()) {
2018-08-20 12:50:45 +00:00
ipc_.handleEvent();
2018-08-16 12:29:41 +00:00
}
2018-08-20 12:50:45 +00:00
{
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
if (thread_.isRunning()) {
workspaces_ = parser_.parse(res.payload);
}
2018-08-20 12:50:45 +00:00
}
dp.emit();
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
2018-08-13 19:23:43 +00:00
}
2018-08-09 18:22:01 +00:00
};
2018-08-08 21:54:58 +00:00
}
auto waybar::modules::sway::Workspaces::update() -> void {
bool needReorder = false;
2018-08-20 12:50:45 +00:00
std::lock_guard<std::mutex> lock(mutex_);
2018-08-16 12:29:41 +00:00
for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), [it](auto node) -> bool {
return node["name"].asString() == it->first;
});
if (ws == workspaces_.end() ||
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output->name)) {
it = buttons_.erase(it);
2018-08-10 16:02:12 +00:00
needReorder = true;
2018-08-16 12:29:41 +00:00
} else {
2018-08-15 12:30:01 +00:00
++it;
2018-08-16 12:29:41 +00:00
}
2018-08-08 21:54:58 +00:00
}
for (auto const &node : workspaces_) {
if (!config_["all-outputs"].asBool() && bar_.output->name != node["output"].asString()) {
2018-08-13 19:23:43 +00:00
continue;
2018-08-16 12:29:41 +00:00
}
auto it = buttons_.find(node["name"].asString());
2018-08-16 12:29:41 +00:00
if (it == buttons_.end()) {
addWorkspace(node);
2018-08-10 16:02:12 +00:00
needReorder = true;
2018-08-08 21:54:58 +00:00
} else {
2018-08-13 19:23:43 +00:00
auto &button = it->second;
2018-08-16 12:29:41 +00:00
if (node["focused"].asBool()) {
button.get_style_context()->add_class("focused");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("focused");
2018-08-16 12:29:41 +00:00
}
if (node["visible"].asBool()) {
button.get_style_context()->add_class("visible");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("visible");
2018-08-16 12:29:41 +00:00
}
if (node["urgent"].asBool()) {
button.get_style_context()->add_class("urgent");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("urgent");
2018-08-16 12:29:41 +00:00
}
if (needReorder) {
2019-02-01 23:36:52 +00:00
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
2018-08-16 12:29:41 +00:00
}
auto icon = getIcon(node["name"].asString(), node);
std::string output = icon;
2018-10-26 07:27:16 +00:00
if (config_["format"].isString()) {
2018-09-18 18:58:11 +00:00
auto format = config_["format"].asString();
output = fmt::format(format,
fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()));
}
if (!config_["disable-markup"].asBool()) {
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(output);
2018-09-18 18:58:11 +00:00
} else {
button.set_label(output);
2018-09-18 18:58:11 +00:00
}
onButtonReady(node, button);
2018-08-08 21:54:58 +00:00
}
}
2018-08-16 12:29:41 +00:00
if (scrolling_) {
scrolling_ = false;
}
2018-08-08 21:54:58 +00:00
}
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) {
auto icon = getIcon(node["name"].asString(), node);
2018-10-26 07:27:16 +00:00
auto format = config_["format"].isString()
? fmt::format(config_["format"].asString(),
fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()))
: icon;
auto pair = buttons_.emplace(node["name"].asString(), format);
2018-08-08 21:54:58 +00:00
auto &button = pair.first->second;
if (!config_["disable-markup"].asBool()) {
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(format);
}
2018-08-16 12:29:41 +00:00
box_.pack_start(button, false, false, 0);
2018-08-08 21:54:58 +00:00
button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] {
2018-08-11 00:09:39 +00:00
try {
2018-08-16 12:29:41 +00:00
std::lock_guard<std::mutex> lock(mutex_);
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
2018-08-20 12:50:45 +00:00
ipc_.sendCmd(IPC_COMMAND, cmd);
} catch (const std::exception &e) {
2018-08-11 00:09:39 +00:00
std::cerr << e.what() << std::endl;
}
2018-08-08 21:54:58 +00:00
});
if (!config_["disable-scroll"].asBool()) {
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
}
2019-02-01 23:36:52 +00:00
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
2018-08-16 12:29:41 +00:00
if (node["focused"].asBool()) {
button.get_style_context()->add_class("focused");
2018-08-16 12:29:41 +00:00
}
if (node["visible"].asBool()) {
button.get_style_context()->add_class("visible");
2018-08-16 12:29:41 +00:00
}
if (node["urgent"].asBool()) {
button.get_style_context()->add_class("urgent");
2018-08-16 12:29:41 +00:00
}
2019-03-03 10:35:32 +00:00
onButtonReady(node, button);
2018-08-08 21:54:58 +00:00
}
2019-02-01 23:36:52 +00:00
std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name,
const Json::Value &node) {
std::vector<std::string> keys = {name, "urgent", "focused", "visible", "default"};
for (auto const &key : keys) {
if (key == "focused" || key == "visible" || key == "urgent") {
2018-10-26 07:27:16 +00:00
if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString();
}
2018-10-26 07:27:16 +00:00
} else if (config_["format-icons"][key].isString()) {
return config_["format-icons"][key].asString();
}
2018-08-16 12:29:41 +00:00
}
2018-08-15 12:48:08 +00:00
return name;
}
bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) {
2018-08-14 09:26:06 +00:00
// Avoid concurrent scroll event
2018-08-16 12:29:41 +00:00
if (scrolling_) {
2018-08-14 09:26:06 +00:00
return false;
2018-08-16 12:29:41 +00:00
}
std::lock_guard<std::mutex> lock(mutex_);
uint8_t idx;
2018-08-16 12:29:41 +00:00
scrolling_ = true;
for (idx = 0; idx < workspaces_.size(); idx += 1) {
if (workspaces_[idx]["focused"].asBool()) {
break;
2018-08-14 09:26:06 +00:00
}
2018-08-16 12:29:41 +00:00
}
if (idx == workspaces_.size()) {
2018-08-16 12:29:41 +00:00
scrolling_ = false;
2018-08-14 09:26:06 +00:00
return false;
}
std::string name;
2018-08-16 12:29:41 +00:00
if (e->direction == GDK_SCROLL_UP) {
name = getCycleWorkspace(idx, true);
2018-08-16 12:29:41 +00:00
}
if (e->direction == GDK_SCROLL_DOWN) {
name = getCycleWorkspace(idx, false);
2018-08-16 12:29:41 +00:00
}
2018-08-14 09:26:06 +00:00
if (e->direction == GDK_SCROLL_SMOOTH) {
gdouble delta_x, delta_y;
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e), &delta_x, &delta_y);
2018-08-16 12:29:41 +00:00
if (delta_y < 0) {
name = getCycleWorkspace(idx, true);
2019-03-25 10:54:41 +00:00
} else if (delta_y > 0) {
name = getCycleWorkspace(idx, false);
2018-08-16 12:29:41 +00:00
}
}
if (name.empty() || name == workspaces_[idx]["name"].asString()) {
scrolling_ = false;
return false;
2018-08-14 09:26:06 +00:00
}
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
std::this_thread::sleep_for(std::chrono::milliseconds(150));
2018-08-14 09:26:06 +00:00
return true;
}
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(uint8_t focused_workspace,
bool prev) const {
auto inc = prev ? -1 : 1;
int size = workspaces_.size();
uint8_t idx = 0;
for (int i = focused_workspace; i < size && i >= 0; i += inc) {
bool same_output = (workspaces_[i]["output"].asString() == bar_.output->name &&
!config_["all-outputs"].asBool()) ||
config_["all-outputs"].asBool();
bool same_name =
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
if (same_output && !same_name) {
return workspaces_[i]["name"].asString();
2018-08-14 09:26:06 +00:00
}
if (prev && i - 1 < 0) {
i = size;
} else if (!prev && i + 1 >= size) {
i = -1;
} else if (idx >= workspaces_.size()) {
return "";
2018-08-14 09:26:06 +00:00
}
idx += 1;
2018-08-16 12:29:41 +00:00
}
return "";
2018-08-14 09:26:06 +00:00
}
uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const {
2019-02-02 11:07:59 +00:00
uint16_t idx = 0;
for (const auto &workspace : workspaces_) {
if (workspace["name"].asString() == name) {
return idx;
}
if (!(!config_["all-outputs"].asBool() &&
workspace["output"].asString() != bar_.output->name)) {
2019-02-02 11:07:59 +00:00
idx += 1;
2019-02-01 23:36:52 +00:00
}
}
return workspaces_.size();
2018-08-08 21:54:58 +00:00
}
2018-12-28 17:35:21 +00:00
std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name) {
2018-12-28 17:35:21 +00:00
std::size_t found = name.find(":");
if (found != std::string::npos) {
return name.substr(found + 1);
2018-12-28 17:35:21 +00:00
}
return name;
}
2019-02-01 23:36:52 +00:00
void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value &node,
Gtk::Button & button) {
if (config_["current-only"].asBool()) {
if (node["focused"].asBool()) {
button.show();
} else {
button.hide();
}
} else {
button.show();
}
2019-03-03 10:35:32 +00:00
}
waybar::modules::sway::Workspaces::operator Gtk::Widget &() { return box_; }