fix(tray): fix offset for negative percentage (#1669)

tray-offset-{x,y} were clamped between [0, max_value] rather than [-max_value, max_value]. Therefore negative percentage were ignored.

Fixes #1666
This commit is contained in:
Jérôme BOULMIER 2019-03-22 04:55:08 -04:00 committed by Patrick Ziegler
parent 82b9ea8a09
commit 31a25af3d3
2 changed files with 16 additions and 5 deletions

View File

@ -56,7 +56,18 @@ namespace math_util {
} }
/** /**
* Get value for percentage of `max_value` * Get value for signed percentage of `max_value` (cap between -max_value and max_value)
*/
template <typename ValueType, typename ReturnType = int>
ReturnType signed_percentage_to_value(ValueType signed_percentage, ValueType max_value) {
if (std::is_integral<ReturnType>())
return cap<ReturnType>(signed_percentage * max_value / 100.0f + 0.5f, -max_value, max_value);
else
return cap<ReturnType>(signed_percentage * max_value / 100.0f, -max_value, max_value);
}
/**
* Get value for percentage of `max_value` (cap between 0 and max_value)
*/ */
template <typename ValueType, typename ReturnType = int> template <typename ValueType, typename ReturnType = int>
ReturnType percentage_to_value(ValueType percentage, ValueType max_value) { ReturnType percentage_to_value(ValueType percentage, ValueType max_value) {

View File

@ -150,17 +150,17 @@ void tray_manager::setup(const bar_settings& bar_opts) {
if (offset_x != 0 && offset_x_def.find('%') != string::npos) { if (offset_x != 0 && offset_x_def.find('%') != string::npos) {
if (m_opts.detached) { if (m_opts.detached) {
offset_x = math_util::percentage_to_value<int>(offset_x, bar_opts.monitor->w); offset_x = math_util::signed_percentage_to_value<int>(offset_x, bar_opts.monitor->w);
} else { } else {
offset_x = math_util::percentage_to_value<int>(offset_x, inner_area.width); offset_x = math_util::signed_percentage_to_value<int>(offset_x, inner_area.width);
} }
} }
if (offset_y != 0 && offset_y_def.find('%') != string::npos) { if (offset_y != 0 && offset_y_def.find('%') != string::npos) {
if (m_opts.detached) { if (m_opts.detached) {
offset_y = math_util::percentage_to_value<int>(offset_y, bar_opts.monitor->h); offset_y = math_util::signed_percentage_to_value<int>(offset_y, bar_opts.monitor->h);
} else { } else {
offset_y = math_util::percentage_to_value<int>(offset_y, inner_area.height); offset_y = math_util::signed_percentage_to_value<int>(offset_y, inner_area.height);
} }
} }