From 719022d80a30774480ba6e5b619d9b5e9188318a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 2 Aug 2011 22:31:45 +0200 Subject: [PATCH] Bugfix: Ensure that the percentage is > 0.05 when using the 'resize' cmd (Thanks rogutes) Fixes #437 --- src/cmdparse.y | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cmdparse.y b/src/cmdparse.y index d84023c3..ec8e6c2c 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -14,6 +14,7 @@ #include #include #include +#include #include "all.h" @@ -106,6 +107,14 @@ char *parse_cmd(const char *new) { return json_output; } +/* + * Returns true if a is definitely greater than b (using the given epsilon) + * + */ +bool definitelyGreaterThan(float a, float b, float epsilon) { + return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); +} + %} %error-verbose @@ -807,10 +816,21 @@ resize: focused->percent = percentage; if (other->percent == 0.0) other->percent = percentage; - focused->percent += ((double)ppt / 100.0); - other->percent -= ((double)ppt / 100.0); - LOG("focused->percent after = %f\n", focused->percent); - LOG("other->percent after = %f\n", other->percent); + double new_focused_percent = focused->percent + ((double)ppt / 100.0); + double new_other_percent = other->percent - ((double)ppt / 100.0); + LOG("new_focused_percent = %f\n", new_focused_percent); + LOG("new_other_percent = %f\n", new_other_percent); + /* Ensure that the new percentages are positive and greater than + * 0.05 to have a reasonable minimum size. */ + if (definitelyGreaterThan(new_focused_percent, 0.05, DBL_EPSILON) && + definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) { + focused->percent += ((double)ppt / 100.0); + other->percent -= ((double)ppt / 100.0); + LOG("focused->percent after = %f\n", focused->percent); + LOG("other->percent after = %f\n", other->percent); + } else { + LOG("Not resizing, already at minimum size\n"); + } } tree_render();