feat(xworkspaces): Change current desktop
This commit is contained in:
parent
0d4a24ef8e
commit
12ff82e913
4 changed files with 71 additions and 5 deletions
|
@ -51,6 +51,10 @@ namespace modules {
|
|||
void update();
|
||||
string get_output();
|
||||
bool build(builder* builder, const string& tag) const;
|
||||
bool handle_event(string cmd);
|
||||
bool receive_events() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
void rebuild_desktops();
|
||||
|
@ -64,6 +68,11 @@ namespace modules {
|
|||
static constexpr const char* TAG_LABEL_MONITOR{"<label-monitor>"};
|
||||
static constexpr const char* TAG_LABEL_STATE{"<label-state>"};
|
||||
|
||||
static constexpr const char* EVENT_PREFIX{"xworkspaces-"};
|
||||
static constexpr const char* EVENT_CLICK{"focus="};
|
||||
static constexpr const char* EVENT_SCROLL_UP{"next"};
|
||||
static constexpr const char* EVENT_SCROLL_DOWN{"prev"};
|
||||
|
||||
connection& m_connection;
|
||||
ewmh_connection_t m_ewmh;
|
||||
vector<monitor_t> m_monitors;
|
||||
|
@ -74,6 +83,8 @@ namespace modules {
|
|||
label_t m_monitorlabel;
|
||||
iconset_t m_icons;
|
||||
bool m_pinworkspaces{false};
|
||||
bool m_click{true};
|
||||
bool m_scroll{true};
|
||||
size_t m_index{0};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ namespace ewmh_util {
|
|||
vector<string> get_desktop_names(xcb_ewmh_connection_t* conn, int screen = 0);
|
||||
uint32_t get_current_desktop(xcb_ewmh_connection_t* conn, int screen = 0);
|
||||
xcb_window_t get_active_window(xcb_ewmh_connection_t* conn, int screen = 0);
|
||||
|
||||
void change_current_desktop(xcb_ewmh_connection_t* conn, uint32_t desktop);
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
|
|
@ -29,6 +29,8 @@ namespace modules {
|
|||
void xworkspaces_module::setup() {
|
||||
// Load config values
|
||||
m_pinworkspaces = m_conf.get<bool>(name(), "pin-workspaces", m_pinworkspaces);
|
||||
m_click = m_conf.get<bool>(name(), "enable-click", m_click);
|
||||
m_scroll = m_conf.get<bool>(name(), "enable-scroll", m_scroll);
|
||||
|
||||
// Initialize ewmh atoms
|
||||
if ((m_ewmh = ewmh_util::initialize()) == nullptr) {
|
||||
|
@ -157,9 +159,11 @@ namespace modules {
|
|||
}
|
||||
|
||||
if (current == n) {
|
||||
m_viewports.back()->desktops.emplace_back(make_pair(desktop_state::ACTIVE, m_labels[desktop_state::ACTIVE]->clone()));
|
||||
m_viewports.back()->desktops.emplace_back(
|
||||
make_pair(desktop_state::ACTIVE, m_labels[desktop_state::ACTIVE]->clone()));
|
||||
} else {
|
||||
m_viewports.back()->desktops.emplace_back(make_pair(desktop_state::EMPTY, m_labels[desktop_state::EMPTY]->clone()));
|
||||
m_viewports.back()->desktops.emplace_back(
|
||||
make_pair(desktop_state::EMPTY, m_labels[desktop_state::EMPTY]->clone()));
|
||||
}
|
||||
|
||||
auto& desktop = m_viewports.back()->desktops.back();
|
||||
|
@ -193,17 +197,62 @@ namespace modules {
|
|||
return true;
|
||||
} else if (tag == TAG_LABEL_STATE) {
|
||||
size_t num{0};
|
||||
for (auto&& d : m_viewports[m_index]->desktops) {
|
||||
if (d.second.get()) {
|
||||
|
||||
if (m_scroll) {
|
||||
builder->cmd(mousebtn::SCROLL_DOWN, string{EVENT_PREFIX} + string{EVENT_SCROLL_DOWN});
|
||||
builder->cmd(mousebtn::SCROLL_UP, string{EVENT_PREFIX} + string{EVENT_SCROLL_UP});
|
||||
}
|
||||
|
||||
for (auto&& desktop : m_viewports[m_index]->desktops) {
|
||||
if (!desktop.second.get()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_click) {
|
||||
builder->cmd(mousebtn::LEFT, string{EVENT_PREFIX} + string{EVENT_CLICK} + to_string(num++));
|
||||
builder->node(desktop.second);
|
||||
builder->cmd_close();
|
||||
} else {
|
||||
num++;
|
||||
builder->node(d.second);
|
||||
builder->node(desktop.second);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_scroll) {
|
||||
builder->cmd_close();
|
||||
builder->cmd_close();
|
||||
}
|
||||
|
||||
return num > 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool xworkspaces_module::handle_event(string cmd) {
|
||||
if (cmd.find(EVENT_PREFIX) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cmd.erase(0, strlen(EVENT_PREFIX));
|
||||
|
||||
if (cmd.compare(0, strlen(EVENT_CLICK), EVENT_CLICK) == 0) {
|
||||
cmd.erase(0, strlen(EVENT_CLICK));
|
||||
ewmh_util::change_current_desktop(m_ewmh.get(), atoi(cmd.c_str()));
|
||||
|
||||
} else if (cmd.compare(0, strlen(EVENT_SCROLL_UP), EVENT_SCROLL_UP) == 0) {
|
||||
auto current = ewmh_util::get_current_desktop(m_ewmh.get());
|
||||
ewmh_util::change_current_desktop(m_ewmh.get(), current + 1);
|
||||
|
||||
} else if (cmd.compare(0, strlen(EVENT_SCROLL_DOWN), EVENT_SCROLL_DOWN) == 0) {
|
||||
auto current = ewmh_util::get_current_desktop(m_ewmh.get());
|
||||
ewmh_util::change_current_desktop(m_ewmh.get(), current - 1);
|
||||
}
|
||||
|
||||
m_connection.flush();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
|
|
@ -131,6 +131,10 @@ namespace ewmh_util {
|
|||
}
|
||||
return win;
|
||||
}
|
||||
|
||||
void change_current_desktop(xcb_ewmh_connection_t* conn, uint32_t desktop) {
|
||||
xcb_ewmh_request_change_current_desktop(conn, 0, desktop, XCB_CURRENT_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
|
Loading…
Reference in a new issue