1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00

refactor(i3): Output based wrapping

This commit is contained in:
Michael Carlberg 2016-12-14 04:42:21 +01:00
parent 80a5985344
commit 0ed173b96e
3 changed files with 43 additions and 30 deletions

View file

@ -13,10 +13,22 @@ namespace i3_util {
using connection_t = i3ipc::connection; using connection_t = i3ipc::connection;
using workspace_t = i3ipc::workspace_t; using workspace_t = i3ipc::workspace_t;
const auto ws_numsort = [](shared_ptr<workspace_t> a, shared_ptr<workspace_t> b) { return a->num < b->num; };
vector<shared_ptr<workspace_t>> workspaces(const connection_t& conn, const string& output = "");
shared_ptr<workspace_t> focused_workspace(const connection_t&); shared_ptr<workspace_t> focused_workspace(const connection_t&);
vector<xcb_window_t> root_windows(connection& conn, const string& output_name = ""); vector<xcb_window_t> root_windows(connection& conn, const string& output_name = "");
bool restack_above_root(connection& conn, const monitor_t& mon, const xcb_window_t win); bool restack_above_root(connection& conn, const monitor_t& mon, const xcb_window_t win);
} }
namespace {
inline bool operator==(i3_util::workspace_t& a, i3_util::workspace_t& b) {
return a.num == b.num && a.output == b.output;
}
inline bool operator!=(i3_util::workspace_t& a, i3_util::workspace_t& b) {
return !(a == b);
}
}
POLYBAR_NS_END POLYBAR_NS_END

View file

@ -107,55 +107,44 @@ namespace modules {
i3_util::connection_t ipc; i3_util::connection_t ipc;
try { try {
auto workspaces = ipc.get_workspaces(); vector<shared_ptr<i3_util::workspace_t>> workspaces;
vector<shared_ptr<i3ipc::workspace_t>> sorted = workspaces;
string focused_output;
for (auto&& ws : workspaces) { if (m_pinworkspaces) {
if (ws->focused) { workspaces = i3_util::workspaces(ipc, m_bar.monitor->name);
focused_output = ws->output; } else {
break; workspaces = i3_util::workspaces(ipc);
}
} }
if (m_indexsort) { if (m_indexsort) {
using ws_t = shared_ptr<i3ipc::workspace_t>; sort(workspaces.begin(), workspaces.end(), i3_util::ws_numsort);
// clang-format off
sort(sorted.begin(), sorted.end(), [](ws_t ws1, ws_t ws2){
return ws1->num < ws2->num;
});
// clang-format on
} }
for (auto&& ws : sorted) { for (auto&& ws : workspaces) {
if (m_pinworkspaces && ws->output != m_bar.monitor->name) { state ws_state{state::NONE};
continue;
}
auto ws_state = state::NONE;
if (ws->focused) { if (ws->focused) {
ws_state = state::FOCUSED; ws_state = state::FOCUSED;
} else if (ws->urgent) { } else if (ws->urgent) {
ws_state = state::URGENT; ws_state = state::URGENT;
} else if (!ws->visible || (ws->visible && ws->output != focused_output)) { } else if (!ws->visible || (ws->visible && ws->output != m_bar.monitor->name)) {
ws_state = state::UNFOCUSED; ws_state = state::UNFOCUSED;
} else { } else {
ws_state = state::VISIBLE; ws_state = state::VISIBLE;
} }
string wsname{ws->name}; string ws_name{ws->name};
// Remove workspace numbers "0:" // Remove workspace numbers "0:"
if (m_strip_wsnumbers) { if (m_strip_wsnumbers) {
wsname.erase(0, string_util::find_nth(wsname, 0, ":", 1) + 1); ws_name.erase(0, string_util::find_nth(ws_name, 0, ":", 1) + 1);
} }
// Trim leading and trailing whitespace // Trim leading and trailing whitespace
wsname = string_util::trim(wsname, ' '); ws_name = string_util::trim(ws_name, ' ');
// Cap at configured max length // Cap at configured max length
if (m_wsname_maxlen > 0 && wsname.length() > m_wsname_maxlen) { if (m_wsname_maxlen > 0 && ws_name.length() > m_wsname_maxlen) {
wsname.erase(m_wsname_maxlen); ws_name.erase(m_wsname_maxlen);
} }
auto icon = m_icons->get(ws->name, DEFAULT_WS_ICON); auto icon = m_icons->get(ws->name, DEFAULT_WS_ICON);
@ -163,7 +152,7 @@ namespace modules {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%output%", ws->output); label->replace_token("%output%", ws->output);
label->replace_token("%name%", wsname); label->replace_token("%name%", ws_name);
label->replace_token("%icon%", icon->get()); label->replace_token("%icon%", icon->get());
label->replace_token("%index%", to_string(ws->num)); label->replace_token("%index%", to_string(ws->num));
m_workspaces.emplace_back(make_unique<workspace>(ws->num, ws_state, move(label))); m_workspaces.emplace_back(make_unique<workspace>(ws->num, ws_state, move(label)));
@ -208,7 +197,6 @@ namespace modules {
bool i3_module::handle_event(string cmd) { bool i3_module::handle_event(string cmd) {
try { try {
if (cmd.compare(0, strlen(EVENT_CLICK), EVENT_CLICK) == 0) { if (cmd.compare(0, strlen(EVENT_CLICK), EVENT_CLICK) == 0) {
const i3_util::connection_t conn{}; const i3_util::connection_t conn{};
const string workspace_num{cmd.substr(strlen(EVENT_CLICK))}; const string workspace_num{cmd.substr(strlen(EVENT_CLICK))};
@ -222,16 +210,16 @@ namespace modules {
} else if (cmd.compare(0, strlen(EVENT_SCROLL_DOWN), EVENT_SCROLL_DOWN) == 0) { } else if (cmd.compare(0, strlen(EVENT_SCROLL_DOWN), EVENT_SCROLL_DOWN) == 0) {
const i3_util::connection_t conn{}; const i3_util::connection_t conn{};
if (m_wrap || conn.get_workspaces().back()->num != i3_util::focused_workspace(conn)->num) { if (m_wrap || *i3_util::workspaces(conn, m_bar.monitor->name).back() != *i3_util::focused_workspace(conn)) {
m_log.info("%s: Sending workspace next command to ipc handler", name()); m_log.info("%s: Sending workspace next command to ipc handler", name());
conn.send_command("workspace next_on_output"); conn.send_command("workspace next_on_output");
} else { } else {
m_log.warn("%s: Ignoring workspace next command (reason: `wrap = false`)", name()); m_log.warn("%s: Ignoring workspace next command (reason: `wrapping-scroll = false`)", name());
} }
} else if (cmd.compare(0, strlen(EVENT_SCROLL_UP), EVENT_SCROLL_UP) == 0) { } else if (cmd.compare(0, strlen(EVENT_SCROLL_UP), EVENT_SCROLL_UP) == 0) {
const i3_util::connection_t conn{}; const i3_util::connection_t conn{};
if (m_wrap || conn.get_workspaces().front()->num != i3_util::focused_workspace(conn)->num) { if (m_wrap || *i3_util::workspaces(conn, m_bar.monitor->name).front() != *i3_util::focused_workspace(conn)) {
m_log.info("%s: Sending workspace prev command to ipc handler", name()); m_log.info("%s: Sending workspace prev command to ipc handler", name());
conn.send_command("workspace prev_on_output"); conn.send_command("workspace prev_on_output");
} else { } else {

View file

@ -12,6 +12,19 @@
POLYBAR_NS POLYBAR_NS
namespace i3_util { namespace i3_util {
/**
* Get all workspaces for given output
*/
vector<shared_ptr<workspace_t>> workspaces(const connection_t& conn, const string& output) {
vector<shared_ptr<workspace_t>> result;
for (auto&& ws : conn.get_workspaces()) {
if (output.empty() || ws->output == output) {
result.emplace_back(forward<decltype(ws)>(ws));
}
}
return result;
}
/** /**
* Get currently focused workspace * Get currently focused workspace
*/ */