2018-08-15 18:17:17 +00:00
|
|
|
#include "modules/sway/workspaces.hpp"
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2019-04-18 15:43:16 +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();
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
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_);
|
2019-04-18 15:43:16 +00:00
|
|
|
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
|
2019-01-13 21:23:09 +00:00
|
|
|
if (thread_.isRunning()) {
|
|
|
|
workspaces_ = parser_.parse(res.payload);
|
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
}
|
|
|
|
dp.emit();
|
2019-04-18 15:43:16 +00:00
|
|
|
} catch (const std::exception &e) {
|
2018-12-09 09:49:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +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();) {
|
2019-04-18 15:43:16 +00:00
|
|
|
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), [it](auto node) -> bool {
|
|
|
|
return node["name"].asString() == it->first;
|
|
|
|
});
|
2018-11-05 19:59:28 +00:00
|
|
|
if (ws == workspaces_.end() ||
|
2019-04-18 15:43:16 +00:00
|
|
|
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output->name)) {
|
2019-04-18 15:52:00 +00:00
|
|
|
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
|
|
|
}
|
2019-04-18 15:43:16 +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
|
|
|
}
|
2018-11-05 19:16:19 +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()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->remove_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["visible"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->remove_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["urgent"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("urgent");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
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
|
|
|
}
|
2019-04-18 15:52:00 +00:00
|
|
|
auto icon = getIcon(node["name"].asString(), node);
|
2019-04-15 08:55:44 +00:00
|
|
|
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();
|
2019-04-18 15:52:00 +00:00
|
|
|
output = fmt::format(format,
|
2019-04-18 15:43:16 +00:00
|
|
|
fmt::arg("icon", icon),
|
|
|
|
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
|
|
|
fmt::arg("index", node["num"].asString()));
|
2019-04-15 08:55:44 +00:00
|
|
|
}
|
|
|
|
if (!config_["disable-markup"].asBool()) {
|
2019-04-18 15:43:16 +00:00
|
|
|
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(output);
|
2018-09-18 18:58:11 +00:00
|
|
|
} else {
|
2019-04-15 08:55:44 +00:00
|
|
|
button.set_label(output);
|
2018-09-18 18:58:11 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +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
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) {
|
2019-04-18 15:52:00 +00:00
|
|
|
auto icon = getIcon(node["name"].asString(), node);
|
2018-10-26 07:27:16 +00:00
|
|
|
auto format = config_["format"].isString()
|
2019-04-18 15:43:16 +00:00
|
|
|
? fmt::format(config_["format"].asString(),
|
|
|
|
fmt::arg("icon", icon),
|
|
|
|
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
|
|
|
fmt::arg("index", node["num"].asString()))
|
|
|
|
: icon;
|
2019-04-18 15:52:00 +00:00
|
|
|
auto pair = buttons_.emplace(node["name"].asString(), format);
|
2018-08-08 21:54:58 +00:00
|
|
|
auto &button = pair.first->second;
|
2019-04-15 08:55:44 +00:00
|
|
|
if (!config_["disable-markup"].asBool()) {
|
2019-04-18 15:43:16 +00:00
|
|
|
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(format);
|
2019-04-15 08:55:44 +00:00
|
|
|
}
|
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_);
|
2019-04-18 15:43:16 +00:00
|
|
|
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
|
2018-08-20 12:50:45 +00:00
|
|
|
ipc_.sendCmd(IPC_COMMAND, cmd);
|
2019-04-18 15:43:16 +00:00
|
|
|
} 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
|
|
|
});
|
2018-08-16 15:12:45 +00:00
|
|
|
if (!config_["disable-scroll"].asBool()) {
|
2019-03-18 13:44:07 +00:00
|
|
|
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
2019-04-18 15:43:16 +00:00
|
|
|
button.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
|
2018-08-16 15:12:45 +00:00
|
|
|
}
|
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()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["visible"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["urgent"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("urgent");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2019-03-03 10:35:32 +00:00
|
|
|
|
2019-04-18 15:43:16 +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,
|
2019-04-18 15:43:16 +00:00
|
|
|
const Json::Value &node) {
|
|
|
|
std::vector<std::string> keys = {name, "urgent", "focused", "visible", "default"};
|
|
|
|
for (auto const &key : keys) {
|
2018-09-04 22:16:56 +00:00
|
|
|
if (key == "focused" || key == "visible" || key == "urgent") {
|
2018-10-26 07:27:16 +00:00
|
|
|
if (config_["format-icons"][key].isString() && node[key].asBool()) {
|
2018-09-04 22:16:56 +00:00
|
|
|
return config_["format-icons"][key].asString();
|
|
|
|
}
|
2018-10-26 07:27:16 +00:00
|
|
|
} else if (config_["format-icons"][key].isString()) {
|
2018-09-04 22:16:56 +00:00
|
|
|
return config_["format-icons"][key].asString();
|
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-15 12:48:08 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
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
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2019-04-18 15:43:16 +00:00
|
|
|
uint8_t idx;
|
2018-08-16 12:29:41 +00:00
|
|
|
scrolling_ = true;
|
2019-03-18 13:44:07 +00:00
|
|
|
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
|
|
|
}
|
2019-03-18 13:44:07 +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;
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
std::string name;
|
2018-08-16 12:29:41 +00:00
|
|
|
if (e->direction == GDK_SCROLL_UP) {
|
2019-04-18 15:43:16 +00:00
|
|
|
name = getCycleWorkspace(idx, true);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (e->direction == GDK_SCROLL_DOWN) {
|
2019-04-18 15:43:16 +00:00
|
|
|
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;
|
2019-04-18 15:43:16 +00:00
|
|
|
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) {
|
2019-03-18 13:44:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
if (name.empty() || name == workspaces_[idx]["name"].asString()) {
|
|
|
|
scrolling_ = false;
|
|
|
|
return false;
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +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;
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(uint8_t focused_workspace,
|
|
|
|
bool prev) const {
|
2019-04-18 15:52:00 +00:00
|
|
|
auto inc = prev ? -1 : 1;
|
2019-04-18 15:43:16 +00:00
|
|
|
int size = workspaces_.size();
|
2019-04-18 15:52:00 +00:00
|
|
|
uint8_t idx = 0;
|
2019-03-18 13:44:07 +00:00
|
|
|
for (int i = focused_workspace; i < size && i >= 0; i += inc) {
|
2019-04-18 15:43:16 +00:00
|
|
|
bool same_output = (workspaces_[i]["output"].asString() == bar_.output->name &&
|
|
|
|
!config_["all-outputs"].asBool()) ||
|
|
|
|
config_["all-outputs"].asBool();
|
2019-03-18 13:44:07 +00:00
|
|
|
bool same_name =
|
2019-04-18 15:43:16 +00:00
|
|
|
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
|
2019-03-18 13:44:07 +00:00
|
|
|
if (same_output && !same_name) {
|
|
|
|
return workspaces_[i]["name"].asString();
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +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
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
idx += 1;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-05 19:16:19 +00:00
|
|
|
return "";
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +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;
|
|
|
|
}
|
2019-04-18 15:43:16 +00:00
|
|
|
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
|
|
|
|
2019-04-18 15:43:16 +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(":");
|
2019-04-18 15:43:16 +00:00
|
|
|
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
|
|
|
|
2019-04-18 15:43:16 +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
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
waybar::modules::sway::Workspaces::operator Gtk::Widget &() { return box_; }
|