Skip to content

Commit

Permalink
Adjust nearby ratios when moving a fence
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Jan 21, 2019
1 parent ab7e5ab commit 644b200
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/tree.c
Expand Up @@ -1173,6 +1173,46 @@ int balance_tree(node_t *n)
}
}

/* Adjust the split ratios so that they keep their position
* despite the potential alteration of their rectangle. */
void adjust_ratios(node_t *n, xcb_rectangle_t rect)
{
if (n == NULL) {
return;
}

double ratio;

if (n->split_type == TYPE_VERTICAL) {
double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
ratio = (position - (double) rect.x) / (double) rect.width;
} else {
double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
ratio = (position - (double) rect.y) / (double) rect.height;
}

ratio = MAX(0.0, ratio);
ratio = MIN(1.0, ratio);
n->split_ratio = ratio;

xcb_rectangle_t first_rect;
xcb_rectangle_t second_rect;
unsigned int fence;

if (n->split_type == TYPE_VERTICAL) {
fence = rect.width * n->split_ratio;
first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
} else {
fence = rect.height * n->split_ratio;
first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
}

adjust_ratios(n->first_child, first_rect);
adjust_ratios(n->second_child, second_rect);
}

void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
{
if (d == NULL || n == NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/tree.h
Expand Up @@ -77,6 +77,7 @@ void rotate_tree_rec(node_t *n, int deg);
void flip_tree(node_t *n, flip_t flp);
void equalize_tree(node_t *n);
int balance_tree(node_t *n);
void adjust_ratios(node_t *n, xcb_rectangle_t rect);
void unlink_node(monitor_t *m, desktop_t *d, node_t *n);
void close_node(node_t *n);
void kill_node(monitor_t *m, desktop_t *d, node_t *n);
Expand Down
2 changes: 2 additions & 0 deletions src/window.c
Expand Up @@ -582,6 +582,8 @@ bool resize_client(coordinates_t *loc, resize_handle_t rh, int dx, int dy, bool
sr = MIN(1, sr);
horizontal_fence->split_ratio = sr;
}
node_t *target_fence = horizontal_fence != NULL ? horizontal_fence : vertical_fence;
adjust_ratios(target_fence, target_fence->rectangle);
arrange(loc->monitor, loc->desktop);
} else {
int w = width, h = height;
Expand Down

0 comments on commit 644b200

Please sign in to comment.