From 46b8bb84edce683e75672b2c0c9ff58cfb3f9f68 Mon Sep 17 00:00:00 2001 From: Ddone Date: Sat, 12 Jan 2019 12:48:09 +0200 Subject: [PATCH] feat(border): Percentage and pixel offset (#1592) Uses the same X%:Z format as width, height and offset-* Resolves #1567 --- include/components/bar.hpp | 6 ++---- src/components/bar.cpp | 19 +++++++++++++------ tests/unit_tests/components/bar.cpp | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/components/bar.hpp b/include/components/bar.hpp index 9c83a039..2d556e4f 100644 --- a/include/components/bar.hpp +++ b/include/components/bar.hpp @@ -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 * 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) { size_t i; if ((i = str.find(':')) != std::string::npos) { std::string a = str.substr(0, i - 1); std::string b = str.substr(i + 1); - return math_util::percentage_to_value(strtod(a.c_str(), nullptr), max) + strtod(b.c_str(), nullptr); + return math_util::max(0,math_util::percentage_to_value(strtod(a.c_str(), nullptr), max) + strtod(b.c_str(), nullptr)); } else { if (str.find('%') != std::string::npos) { return math_util::percentage_to_value(strtod(str.c_str(), nullptr), max); diff --git a/src/components/bar.cpp b/src/components/bar.cpp index f315dd57..79e1321a 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -219,19 +219,23 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const // Load border settings 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[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.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.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.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); // 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 += 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_connection.attach_sink(this, SINK_PRIORITY_BAR); diff --git a/tests/unit_tests/components/bar.cpp b/tests/unit_tests/components/bar.cpp index 8b98b262..839a0088 100644 --- a/tests/unit_tests/components/bar.cpp +++ b/tests/unit_tests/components/bar.cpp @@ -32,6 +32,7 @@ vector> to_pixels_with_offset_list = { {990, "100%:-10"}, {10, "0%:+10"}, {1000, "99%:+10"}, + {0, "1%:-100"}, }; INSTANTIATE_TEST_CASE_P(NoOffset, GeomFormatToPixelsTest,