mirror of
https://github.com/polybar/polybar.git
synced 2024-11-25 13:55:47 -05:00
feat(border): Percentage and pixel offset (#1592)
Uses the same X%:Z format as width, height and offset-* Resolves #1567
This commit is contained in:
parent
b728fea5be
commit
46b8bb84ed
3 changed files with 16 additions and 10 deletions
|
@ -28,19 +28,17 @@ class tray_manager;
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows a new format for width, height, offset-x and offset-y in the bar section
|
* Allows a new format for pixel sizes (like width in the bar section)
|
||||||
*
|
*
|
||||||
* The new format is X%:Z, where X is in [0, 100], and Z is any real value
|
* The new format is X%:Z, where X is in [0, 100], and Z is any real value
|
||||||
* describing a pixel offset. The actual value is calculated by X% * max + Z
|
* describing a pixel offset. The actual value is calculated by X% * max + Z
|
||||||
* The max is the monitor width for width and offset-x and monitor height for
|
|
||||||
* the other two
|
|
||||||
*/
|
*/
|
||||||
inline double geom_format_to_pixels(std::string str, double max) {
|
inline double geom_format_to_pixels(std::string str, double max) {
|
||||||
size_t i;
|
size_t i;
|
||||||
if ((i = str.find(':')) != std::string::npos) {
|
if ((i = str.find(':')) != std::string::npos) {
|
||||||
std::string a = str.substr(0, i - 1);
|
std::string a = str.substr(0, i - 1);
|
||||||
std::string b = str.substr(i + 1);
|
std::string b = str.substr(i + 1);
|
||||||
return math_util::percentage_to_value<double>(strtod(a.c_str(), nullptr), max) + strtod(b.c_str(), nullptr);
|
return math_util::max<double>(0,math_util::percentage_to_value<double>(strtod(a.c_str(), nullptr), max) + strtod(b.c_str(), nullptr));
|
||||||
} else {
|
} else {
|
||||||
if (str.find('%') != std::string::npos) {
|
if (str.find('%') != std::string::npos) {
|
||||||
return math_util::percentage_to_value<double>(strtod(str.c_str(), nullptr), max);
|
return math_util::percentage_to_value<double>(strtod(str.c_str(), nullptr), max);
|
||||||
|
|
|
@ -219,19 +219,23 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
|
||||||
|
|
||||||
// Load border settings
|
// Load border settings
|
||||||
auto border_color = m_conf.get(bs, "border-color", rgba{0x00000000});
|
auto border_color = m_conf.get(bs, "border-color", rgba{0x00000000});
|
||||||
auto border_size = m_conf.get(bs, "border-size", 0);
|
auto border_size = m_conf.get(bs, "border-size", ""s);
|
||||||
|
auto border_top = m_conf.deprecated(bs, "border-top", "border-top-size", border_size);
|
||||||
|
auto border_bottom = m_conf.deprecated(bs, "border-bottom", "border-bottom-size", border_size);
|
||||||
|
auto border_left = m_conf.deprecated(bs, "border-left", "border-left-size", border_size);
|
||||||
|
auto border_right = m_conf.deprecated(bs, "border-right", "border-right-size", border_size);
|
||||||
|
|
||||||
m_opts.borders.emplace(edge::TOP, border_settings{});
|
m_opts.borders.emplace(edge::TOP, border_settings{});
|
||||||
m_opts.borders[edge::TOP].size = m_conf.deprecated(bs, "border-top", "border-top-size", border_size);
|
m_opts.borders[edge::TOP].size = geom_format_to_pixels(border_top, m_opts.monitor->h);
|
||||||
m_opts.borders[edge::TOP].color = parse_or_throw("border-top-color", border_color);
|
m_opts.borders[edge::TOP].color = parse_or_throw("border-top-color", border_color);
|
||||||
m_opts.borders.emplace(edge::BOTTOM, border_settings{});
|
m_opts.borders.emplace(edge::BOTTOM, border_settings{});
|
||||||
m_opts.borders[edge::BOTTOM].size = m_conf.deprecated(bs, "border-bottom", "border-bottom-size", border_size);
|
m_opts.borders[edge::BOTTOM].size = geom_format_to_pixels(border_bottom, m_opts.monitor->h);
|
||||||
m_opts.borders[edge::BOTTOM].color = parse_or_throw("border-bottom-color", border_color);
|
m_opts.borders[edge::BOTTOM].color = parse_or_throw("border-bottom-color", border_color);
|
||||||
m_opts.borders.emplace(edge::LEFT, border_settings{});
|
m_opts.borders.emplace(edge::LEFT, border_settings{});
|
||||||
m_opts.borders[edge::LEFT].size = m_conf.deprecated(bs, "border-left", "border-left-size", border_size);
|
m_opts.borders[edge::LEFT].size = geom_format_to_pixels(border_left, m_opts.monitor->w);
|
||||||
m_opts.borders[edge::LEFT].color = parse_or_throw("border-left-color", border_color);
|
m_opts.borders[edge::LEFT].color = parse_or_throw("border-left-color", border_color);
|
||||||
m_opts.borders.emplace(edge::RIGHT, border_settings{});
|
m_opts.borders.emplace(edge::RIGHT, border_settings{});
|
||||||
m_opts.borders[edge::RIGHT].size = m_conf.deprecated(bs, "border-right", "border-right-size", border_size);
|
m_opts.borders[edge::RIGHT].size = geom_format_to_pixels(border_right, m_opts.monitor->w);
|
||||||
m_opts.borders[edge::RIGHT].color = parse_or_throw("border-right-color", border_color);
|
m_opts.borders[edge::RIGHT].color = parse_or_throw("border-right-color", border_color);
|
||||||
|
|
||||||
// Load geometry values
|
// Load geometry values
|
||||||
|
@ -274,7 +278,10 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
|
||||||
m_opts.center.x /= 2;
|
m_opts.center.x /= 2;
|
||||||
m_opts.center.x += m_opts.borders[edge::LEFT].size;
|
m_opts.center.x += m_opts.borders[edge::LEFT].size;
|
||||||
|
|
||||||
m_log.info("Bar geometry: %ix%i+%i+%i", m_opts.size.w, m_opts.size.h, m_opts.pos.x, m_opts.pos.y);
|
m_log.info("Bar geometry: %ix%i+%i+%i; Borders: %d,%d,%d,%d", m_opts.size.w,
|
||||||
|
m_opts.size.h, m_opts.pos.x, m_opts.pos.y,
|
||||||
|
m_opts.borders[edge::TOP].size, m_opts.borders[edge::RIGHT].size,
|
||||||
|
m_opts.borders[edge::BOTTOM].size, m_opts.borders[edge::LEFT].size);
|
||||||
|
|
||||||
m_log.trace("bar: Attach X event sink");
|
m_log.trace("bar: Attach X event sink");
|
||||||
m_connection.attach_sink(this, SINK_PRIORITY_BAR);
|
m_connection.attach_sink(this, SINK_PRIORITY_BAR);
|
||||||
|
|
|
@ -32,6 +32,7 @@ vector<pair<double, string>> to_pixels_with_offset_list = {
|
||||||
{990, "100%:-10"},
|
{990, "100%:-10"},
|
||||||
{10, "0%:+10"},
|
{10, "0%:+10"},
|
||||||
{1000, "99%:+10"},
|
{1000, "99%:+10"},
|
||||||
|
{0, "1%:-100"},
|
||||||
};
|
};
|
||||||
|
|
||||||
INSTANTIATE_TEST_CASE_P(NoOffset, GeomFormatToPixelsTest,
|
INSTANTIATE_TEST_CASE_P(NoOffset, GeomFormatToPixelsTest,
|
||||||
|
|
Loading…
Reference in a new issue