A new logic to calculate the percentages.

It's slower, but this way we make sure that the resulting
percentages *ALWAYS* sum up to 1.0 (or as close to that as we get
with double math).
This commit is contained in:
Fernando Tarlá Cardoso Lemos 2011-01-25 21:50:23 -02:00 committed by Michael Stapelberg
parent a93f4643ec
commit 45227fba54
1 changed files with 30 additions and 9 deletions

View File

@ -385,17 +385,38 @@ int con_num_children(Con *con) {
void con_fix_percent(Con *con, int action) {
Con *child;
int children = con_num_children(con);
/* TODO: better document why this math works */
double fix;
if (action == WINDOW_ADD)
fix = (1.0 - (1.0 / (children+1)));
else
fix = 1.0 / (1.0 - (1.0 / (children+1)));
// calculate how much we have distributed and how many containers
// with a percentage set we have
double total = 0.0;
int children_with_percent = 0;
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
if (child->percent <= 0.0)
continue;
child->percent *= fix;
if (child->percent > 0.0) {
total += child->percent;
++children_with_percent;
}
}
// if there were children without a percentage set, set to a value that
// will make those children proportional to all others
if (children_with_percent != children) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
if (child->percent <= 0.0) {
if (children_with_percent == 0)
total += (child->percent = 1.0);
else total += (child->percent = total / children_with_percent);
}
}
}
// if we got a zero, just distribute the space equally, otherwise
// distribute according to the proportions we got
if (total == 0.0) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes)
child->percent = 1.0 / children;
} else if (total != 1.0) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes)
child->percent /= total;
}
}