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

refactor(builder): Action wrapped labels

This commit is contained in:
Michael Carlberg 2017-01-13 11:22:23 +01:00
parent e1dbd98c40
commit 22140f7db9
4 changed files with 38 additions and 49 deletions

View file

@ -52,6 +52,7 @@ class builder {
void underline(const string& color = "");
void underline_close();
void cmd(mousebtn index, string action, bool condition = true);
void cmd(mousebtn index, string action, const label_t& label);
void cmd_close(bool condition = true);
protected:

View file

@ -83,6 +83,10 @@ void builder::append(string text) {
* This will also parse raw syntax tags
*/
void builder::node(string str, bool add_space) {
if (str.empty()) {
return;
}
string::size_type n, m;
string s(move(str));
@ -506,15 +510,22 @@ void builder::underline_close() {
* Open command tag
*/
void builder::cmd(mousebtn index, string action, bool condition) {
int button = static_cast<int>(index);
if (!condition || action.empty()) {
return;
if (condition && !action.empty()) {
action = string_util::replace_all(action, ":", "\\:");
tag_open(syntaxtag::A, to_string(static_cast<int>(index)) + ":" + action + ":");
}
}
action = string_util::replace_all(action, ":", "\\:");
tag_open(syntaxtag::A, to_string(button) + ":" + action + ":");
/**
* Wrap label in command block
*/
void builder::cmd(mousebtn index, string action, const label_t& label) {
if (!action.empty() && label && *label) {
action = string_util::replace_all(action, ":", "\\:");
tag_open(syntaxtag::A, to_string(static_cast<int>(index)) + ":" + action + ":");
node(label);
tag_close(syntaxtag::A);
}
}
/**

View file

@ -5,6 +5,7 @@
#include "modules/bspwm.hpp"
#include "utils/factory.hpp"
#include "utils/file.hpp"
#include "utils/string.hpp"
#include "modules/meta/base.inl"
@ -393,18 +394,14 @@ namespace modules {
workspace_n++;
if (m_click) {
builder->cmd(mousebtn::LEFT, EVENT_CLICK + to_string(m_index) + "+" + to_string(workspace_n));
builder->node(ws.second);
builder->cmd_close();
builder->cmd(mousebtn::LEFT, stringstream() << EVENT_CLICK << m_index << "+" << workspace_n, ws.second);
} else {
builder->node(ws.second);
}
if (m_inlinemode && m_monitors[m_index]->focused && check_mask(ws.first, bspwm_state::FOCUSED)) {
for (auto&& mode : m_monitors[m_index]->modes) {
if (*mode.get()) {
builder->node(mode);
}
builder->node(mode);
}
}
}
@ -421,7 +418,7 @@ namespace modules {
int modes_n = 0;
for (auto&& mode : m_monitors[m_index]->modes) {
if (*mode.get()) {
if (mode && *mode) {
builder->node(mode);
modes_n++;
}

View file

@ -257,62 +257,42 @@ namespace modules {
}
bool mpd_module::build(builder* builder, const string& tag) const {
bool is_playing = false;
bool is_paused = false;
bool is_stopped = true;
int elapsed_percentage = 0;
if (m_status) {
elapsed_percentage = m_status->get_elapsed_percentage();
if (m_status->match_state(mpdstate::PLAYING)) {
is_playing = true;
}
if (m_status->match_state(mpdstate::PAUSED)) {
is_paused = true;
}
if (!(m_status->match_state(mpdstate::STOPPED))) {
is_stopped = false;
}
}
auto icon_cmd = [&builder](string cmd, icon_t icon) {
builder->cmd(mousebtn::LEFT, cmd);
builder->node(icon);
builder->cmd_close();
};
bool is_playing = m_status && m_status->match_state(mpdstate::PLAYING);
bool is_paused = m_status && m_status->match_state(mpdstate::PAUSED);
bool is_stopped = m_status && m_status->match_state(mpdstate::STOPPED);
if (tag == TAG_LABEL_SONG && !is_stopped) {
builder->node(m_label_song);
} else if (tag == TAG_LABEL_TIME && !is_stopped) {
builder->node(m_label_time);
} else if (tag == TAG_BAR_PROGRESS && !is_stopped) {
builder->node(m_bar_progress->output(elapsed_percentage));
builder->node(m_bar_progress->output(!m_status ? 0 : m_status->get_elapsed_percentage()));
} else if (tag == TAG_LABEL_OFFLINE) {
builder->node(m_label_offline);
} else if (tag == TAG_ICON_RANDOM) {
icon_cmd(EVENT_RANDOM, m_icons->get("random"));
builder->cmd(mousebtn::LEFT, EVENT_RANDOM, m_icons->get("random"));
} else if (tag == TAG_ICON_REPEAT) {
icon_cmd(EVENT_REPEAT, m_icons->get("repeat"));
builder->cmd(mousebtn::LEFT, EVENT_REPEAT, m_icons->get("repeat"));
} else if (tag == TAG_ICON_REPEAT_ONE) {
icon_cmd(EVENT_REPEAT_ONE, m_icons->get("repeat_one"));
builder->cmd(mousebtn::LEFT, EVENT_REPEAT_ONE, m_icons->get("repeat_one"));
} else if (tag == TAG_ICON_PREV) {
icon_cmd(EVENT_PREV, m_icons->get("prev"));
builder->cmd(mousebtn::LEFT, EVENT_PREV, m_icons->get("prev"));
} else if ((tag == TAG_ICON_STOP || tag == TAG_TOGGLE_STOP) && (is_playing || is_paused)) {
icon_cmd(EVENT_STOP, m_icons->get("stop"));
builder->cmd(mousebtn::LEFT, EVENT_STOP, m_icons->get("stop"));
} else if ((tag == TAG_ICON_PAUSE || tag == TAG_TOGGLE) && is_playing) {
icon_cmd(EVENT_PAUSE, m_icons->get("pause"));
builder->cmd(mousebtn::LEFT, EVENT_PAUSE, m_icons->get("pause"));
} else if ((tag == TAG_ICON_PLAY || tag == TAG_TOGGLE || tag == TAG_TOGGLE_STOP) && !is_playing) {
icon_cmd(EVENT_PLAY, m_icons->get("play"));
builder->cmd(mousebtn::LEFT, EVENT_PLAY, m_icons->get("play"));
} else if (tag == TAG_ICON_NEXT) {
icon_cmd(EVENT_NEXT, m_icons->get("next"));
builder->cmd(mousebtn::LEFT, EVENT_NEXT, m_icons->get("next"));
} else if (tag == TAG_ICON_SEEKB) {
icon_cmd(string(EVENT_SEEK).append("-5"), m_icons->get("seekb"));
builder->cmd(mousebtn::LEFT, EVENT_SEEK + "-5"s, m_icons->get("seekb"));
} else if (tag == TAG_ICON_SEEKF) {
icon_cmd(string(EVENT_SEEK).append("+5"), m_icons->get("seekf"));
builder->cmd(mousebtn::LEFT, EVENT_SEEK + "+5"s, m_icons->get("seekf"));
} else {
return false;
}
return true;
}