ce93188a4a
* add units support (POINT, PIXEL, SPACE) for polybar - add a size_with_unit struct - add a geometry_format_values struct - move dpi initialisation from renderer.cpp to bar.cpp - add a string to size_with_unit converter - add point support (with pt) - add pixel support (with px) * Fix unit test compilation * clang-format * Better names The old names didn't really capture the purpose of the structs and function. space_type -> spacing_type space_size -> spacing_val size_type -> extent_type geometry -> extent_val geometry_format_values -> percentage_with_offset * Remove parse_size_with_unit No longer needed. The convert<spacing_val> function in config.cpp already does all the work for us and always setting the type to pixel was wrong. In addition, line-size should not be of type spacing_val but extent_val. * Cleanup I tried to address most of my comments on the old PR * Fix renderer width calculation We can't just blindly add the x difference to the width because for example the width should increase if x < width and the increase keeps x < width. Similarly, we can't just add the offset to the width. * Rename geom_format_to_pixels to percentage_with_offset_to_pixel * Cleanup * Apply suggested changes from Patrick on GitHub Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> * Update src/components/bar.cpp Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> * Update src/components/config.cpp Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> * Update src/components/builder.cpp Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> * Update src/components/builder.cpp Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> * config: Use stod for parsing percentage * Use stof instead of strtof * units: Fix test edge cases * Remove unnecessary clang-format toggle * Use percentage_with_offset for margin-{top,bottom} * Support negative extent values * Rename unit to units and create a cpp file * Move percentage_with_offset_to_pixel unit test to units * Add unit tests for units_utils * Clarify when and how negative spacing/extent is allowed Negative spacing is never allowed and produces a config error. Extents allow negative values in theory, but only a few use-cases accept it. Only the extent value used for the `%{O}` tag and the offset value in percentage_with_offset can be negative. Everything else is capped below at 0. The final pixel value of percentage_with_offset also caps below at 0. * Fix parsing errors not being caught in config * Print a proper error message for uncaught exceptions * Cleanup module::get_output All changes preserve the existing semantics * Stop using remove_trailing_space in module::get_output Instead, we first check if the current tag is built, and only if it is, the spacing is prepended. * Remove unused imports * Restore old behavior If there are two tags and the second one isn't built (module::build returns false), the space in between them is removed. For example in the mpd module: format-online = <toggle> <label-song> foo If mpd is not running, the mpd module will return false when trying to build the `<label-song>` tag. If we don't remove the space between `<toggle>` and `<label-song>`, we end up with two spaces between `<toggle>` and `foo`. This change is to match the old behavior where at least one trailing space character was removed from the builder. * Add changelog entry * Remove unused setting * Use percentage with offset for tray-offset Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr> Co-authored-by: Joe Groocock <github@frebib.net>
147 lines
5.9 KiB
C++
147 lines
5.9 KiB
C++
#include "utils/units.hpp"
|
|
|
|
#include "common/test.hpp"
|
|
#include "utils/units.hpp"
|
|
|
|
using namespace polybar;
|
|
using namespace units_utils;
|
|
|
|
|
|
namespace polybar {
|
|
bool operator==(const extent_val lhs, const extent_val rhs) {
|
|
return lhs.type == rhs.type && lhs.value == rhs.value;
|
|
}
|
|
|
|
bool operator==(const spacing_val lhs, const spacing_val rhs) {
|
|
return lhs.type == rhs.type && lhs.value == rhs.value;
|
|
}
|
|
} // namespace polybar
|
|
|
|
/**
|
|
* \brief Class for parameterized tests on geom_format_to_pixels
|
|
*
|
|
* The first element in the tuple is the expected return value, the second
|
|
* value represents the format string. The max value is always 1000 and dpi is always 96
|
|
*/
|
|
class GeomFormatToPixelsTest : public ::testing::Test,
|
|
public ::testing::WithParamInterface<pair<unsigned, percentage_with_offset>> {};
|
|
|
|
vector<pair<unsigned, percentage_with_offset>> to_pixels_no_offset_list = {
|
|
{1000, percentage_with_offset{100.}},
|
|
{0, percentage_with_offset{0.}},
|
|
{1000, percentage_with_offset{150.}},
|
|
{100, percentage_with_offset{10.}},
|
|
{0, percentage_with_offset{0., ZERO_PX_EXTENT}},
|
|
{1234, percentage_with_offset{0., extent_val{extent_type::PIXEL, 1234}}},
|
|
{1, percentage_with_offset{0., extent_val{extent_type::PIXEL, 1}}},
|
|
};
|
|
|
|
vector<pair<unsigned, percentage_with_offset>> to_pixels_with_offset_list = {
|
|
{1000, percentage_with_offset{100., ZERO_PX_EXTENT}},
|
|
{1010, percentage_with_offset{100., extent_val{extent_type::PIXEL, 10}}},
|
|
{990, percentage_with_offset{100., extent_val{extent_type::PIXEL, -10}}},
|
|
{10, percentage_with_offset{0., extent_val{extent_type::PIXEL, 10}}},
|
|
{1000, percentage_with_offset{99., extent_val{extent_type::PIXEL, 10}}},
|
|
{0, percentage_with_offset{1., extent_val{extent_type::PIXEL, -100}}},
|
|
};
|
|
|
|
vector<pair<unsigned, percentage_with_offset>> to_pixels_with_units_list = {
|
|
{1013, percentage_with_offset{100., extent_val{extent_type::POINT, 10}}},
|
|
{987, percentage_with_offset{100., extent_val{extent_type::POINT, -10}}},
|
|
{1003, percentage_with_offset{99., extent_val{extent_type::POINT, 10}}},
|
|
{13, percentage_with_offset{0., extent_val{extent_type::POINT, 10}}},
|
|
{0, percentage_with_offset{0, extent_val{extent_type::POINT, -10}}},
|
|
};
|
|
|
|
INSTANTIATE_TEST_SUITE_P(NoOffset, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_no_offset_list));
|
|
|
|
INSTANTIATE_TEST_SUITE_P(WithOffset, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_with_offset_list));
|
|
|
|
INSTANTIATE_TEST_SUITE_P(WithUnits, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_with_units_list));
|
|
|
|
static constexpr int MAX_WIDTH = 1000;
|
|
static constexpr int DPI = 96;
|
|
|
|
TEST_P(GeomFormatToPixelsTest, correctness) {
|
|
unsigned exp = GetParam().first;
|
|
percentage_with_offset geometry = GetParam().second;
|
|
EXPECT_DOUBLE_EQ(exp, percentage_with_offset_to_pixel(geometry, MAX_WIDTH, DPI));
|
|
}
|
|
|
|
TEST(UnitsUtils, point_to_pixel) {
|
|
EXPECT_EQ(72, point_to_pixel(72, 72));
|
|
EXPECT_EQ(96, point_to_pixel(72, 96));
|
|
EXPECT_EQ(48, point_to_pixel(36, 96));
|
|
EXPECT_EQ(-48, point_to_pixel(-36, 96));
|
|
}
|
|
|
|
TEST(UnitsUtils, extent_to_pixel) {
|
|
EXPECT_EQ(100, extent_to_pixel_nonnegative({extent_type::PIXEL, 100}, 0));
|
|
EXPECT_EQ(48, extent_to_pixel_nonnegative({extent_type::POINT, 36}, 96));
|
|
|
|
EXPECT_EQ(0, extent_to_pixel_nonnegative({extent_type::PIXEL, -100}, 0));
|
|
EXPECT_EQ(0, extent_to_pixel_nonnegative({extent_type::POINT, -36}, 96));
|
|
}
|
|
|
|
TEST(UnitsUtils, percentage_with_offset_to_pixel) {
|
|
EXPECT_EQ(1100, percentage_with_offset_to_pixel({100, {extent_type::PIXEL, 100}}, 1000, 0));
|
|
EXPECT_EQ(1048, percentage_with_offset_to_pixel({100, {extent_type::POINT, 36}}, 1000, 96));
|
|
|
|
EXPECT_EQ(900, percentage_with_offset_to_pixel({100, {extent_type::PIXEL, -100}}, 1000, 0));
|
|
EXPECT_EQ(952, percentage_with_offset_to_pixel({100, {extent_type::POINT, -36}}, 1000, 96));
|
|
|
|
EXPECT_EQ(0, percentage_with_offset_to_pixel({0, {extent_type::PIXEL, -100}}, 1000, 0));
|
|
EXPECT_EQ(100, percentage_with_offset_to_pixel({0, {extent_type::PIXEL, 100}}, 1000, 0));
|
|
}
|
|
|
|
TEST(UnitsUtils, parse_extent_unit) {
|
|
EXPECT_EQ(extent_type::PIXEL, parse_extent_unit("px"));
|
|
EXPECT_EQ(extent_type::POINT, parse_extent_unit("pt"));
|
|
|
|
EXPECT_EQ(extent_type::PIXEL, parse_extent_unit(""));
|
|
|
|
EXPECT_THROW(parse_extent_unit("foo"), std::runtime_error);
|
|
}
|
|
|
|
TEST(UnitsUtils, parse_extent) {
|
|
EXPECT_EQ((extent_val{extent_type::PIXEL, 100}), parse_extent("100px"));
|
|
EXPECT_EQ((extent_val{extent_type::POINT, 36}), parse_extent("36pt"));
|
|
|
|
EXPECT_EQ((extent_val{extent_type::PIXEL, -100}), parse_extent("-100px"));
|
|
EXPECT_EQ((extent_val{extent_type::POINT, -36}), parse_extent("-36pt"));
|
|
|
|
EXPECT_EQ((extent_val{extent_type::PIXEL, 100}), parse_extent("100"));
|
|
EXPECT_EQ((extent_val{extent_type::PIXEL, -100}), parse_extent("-100"));
|
|
|
|
EXPECT_THROW(parse_extent("100foo"), std::runtime_error);
|
|
}
|
|
|
|
TEST(UnitsUtils, extent_to_string) {
|
|
EXPECT_EQ("100px", extent_to_string({extent_type::PIXEL, 100}));
|
|
EXPECT_EQ("36pt", extent_to_string({extent_type::POINT, 36}));
|
|
|
|
EXPECT_EQ("-100px", extent_to_string({extent_type::PIXEL, -100}));
|
|
EXPECT_EQ("-36pt", extent_to_string({extent_type::POINT, -36}));
|
|
}
|
|
|
|
TEST(UnitsUtils, parse_spacing_unit) {
|
|
EXPECT_EQ(spacing_type::PIXEL, parse_spacing_unit("px"));
|
|
EXPECT_EQ(spacing_type::POINT, parse_spacing_unit("pt"));
|
|
|
|
EXPECT_EQ(spacing_type::SPACE, parse_spacing_unit(""));
|
|
|
|
EXPECT_THROW(parse_spacing_unit("foo"), std::runtime_error);
|
|
}
|
|
|
|
TEST(UnitsUtils, parse_spacing) {
|
|
EXPECT_EQ((spacing_val{spacing_type::PIXEL, 100}), parse_spacing("100px"));
|
|
EXPECT_EQ((spacing_val{spacing_type::POINT, 36}), parse_spacing("36pt"));
|
|
|
|
EXPECT_EQ((spacing_val{spacing_type::SPACE, 100}), parse_spacing("100"));
|
|
|
|
EXPECT_THROW(parse_spacing("-100px"), std::runtime_error);
|
|
EXPECT_THROW(parse_spacing("-36pt"), std::runtime_error);
|
|
EXPECT_THROW(parse_spacing("-100"), std::runtime_error);
|
|
EXPECT_THROW(parse_spacing("100foo"), std::runtime_error);
|
|
}
|