Fix keyboard and mouse resize in nested containers

fixes #1084
fixes #1085
This commit is contained in:
jj 2013-09-24 15:46:58 +02:00 committed by Michael Stapelberg
parent 5baada6532
commit dbec5eb905
4 changed files with 114 additions and 95 deletions

View File

@ -10,6 +10,8 @@
#ifndef I3_RESIZE_H #ifndef I3_RESIZE_H
#define I3_RESIZE_H #define I3_RESIZE_H
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction);
int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event); int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event);
#endif #endif

View File

@ -28,45 +28,43 @@ typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_
*/ */
static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) { static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
DLOG("border = %d, con = %p\n", border, con); DLOG("border = %d, con = %p\n", border, con);
char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n'); Con *second = NULL;
orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ); Con *first = con;
direction_t search_direction;
/* look for a parent container with the right orientation */ switch (border) {
Con *first = NULL, *second = NULL; case BORDER_LEFT:
Con *resize_con = con; search_direction = D_LEFT;
while (resize_con->type != CT_WORKSPACE && break;
resize_con->type != CT_FLOATING_CON && case BORDER_RIGHT:
con_orientation(resize_con->parent) != orientation) search_direction = D_RIGHT;
resize_con = resize_con->parent; break;
case BORDER_TOP:
DLOG("resize_con = %p\n", resize_con); search_direction = D_UP;
if (resize_con->type != CT_WORKSPACE && break;
resize_con->type != CT_FLOATING_CON && case BORDER_BOTTOM:
con_orientation(resize_con->parent) == orientation) { search_direction = D_DOWN;
first = resize_con; break;
second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
if (second == TAILQ_END(&(first->nodes_head))) {
second = NULL;
}
else if (way == 'p') {
Con *tmp = first;
first = second;
second = tmp;
}
DLOG("first = %p, second = %p, resize_con = %p\n",
first, second, resize_con);
} }
if (first == NULL || second == NULL) { bool res = resize_find_tiling_participants(&first, &second, search_direction);
DLOG("Resize not possible\n"); if (!res) {
LOG("No second container in this direction found.\n");
return false; return false;
} }
assert(first != second); assert(first != second);
assert(first->parent == second->parent); assert(first->parent == second->parent);
/* The first container should always be in front of the second container */
if (search_direction == D_UP || search_direction == D_LEFT) {
Con *tmp = first;
first = second;
second = tmp;
}
/* We modify the X/Y position in the event so that the divider line is at /* We modify the X/Y position in the event so that the divider line is at
* the actual position of the border, not at the position of the click. */ * the actual position of the border, not at the position of the click. */
const orientation_t orientation = ((border == BORDER_LEFT || border == BORDER_RIGHT) ? HORIZ : VERT);
if (orientation == HORIZ) if (orientation == HORIZ)
event->root_x = second->rect.x; event->root_x = second->rect.x;
else event->root_y = second->rect.y; else event->root_y = second->rect.y;

View File

@ -616,79 +616,50 @@ static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floatin
static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) { static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
LOG("tiling resize\n"); LOG("tiling resize\n");
/* get the appropriate current container (skip stacked/tabbed cons) */ Con *second = NULL;
Con *other = NULL; Con *first = current;
double percentage = 0; direction_t search_direction;
while (current->parent->layout == L_STACKED || if (!strcmp(direction, "left"))
current->parent->layout == L_TABBED) search_direction = D_LEFT;
current = current->parent; else if (!strcmp(direction, "right"))
search_direction = D_RIGHT;
else if (!strcmp(direction, "up"))
search_direction = D_UP;
else
search_direction = D_DOWN;
/* Then further go up until we find one with the matching orientation. */ bool res = resize_find_tiling_participants(&first, &second, search_direction);
orientation_t search_orientation = if (!res) {
(strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT); LOG("No second container in this direction found.\n");
do {
if (con_orientation(current->parent) != search_orientation) {
current = current->parent;
continue;
}
/* get the default percentage */
int children = con_num_children(current->parent);
LOG("ins. %d children\n", children);
percentage = 1.0 / children;
LOG("default percentage = %f\n", percentage);
orientation_t orientation = con_orientation(current->parent);
if ((orientation == HORIZ &&
(strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
(orientation == VERT &&
(strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
(orientation == HORIZ ? "horizontal" : "vertical"));
ysuccess(false);
return false;
}
if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
other = TAILQ_PREV(current, nodes_head, nodes);
} else {
other = TAILQ_NEXT(current, nodes);
}
if (other == TAILQ_END(workspaces)) {
LOG("No other container in this direction found, trying to look further up in the tree...\n");
current = current->parent;
continue;
}
break;
} while (current->type != CT_WORKSPACE &&
current->type != CT_FLOATING_CON);
if (other == NULL) {
LOG("No other container in this direction found, trying to look further up in the tree...\n");
ysuccess(false); ysuccess(false);
return false; return false;
} }
LOG("other->percent = %f\n", other->percent); /* get the default percentage */
LOG("current->percent before = %f\n", current->percent); int children = con_num_children(first->parent);
if (current->percent == 0.0) LOG("ins. %d children\n", children);
current->percent = percentage; double percentage = 1.0 / children;
if (other->percent == 0.0) LOG("default percentage = %f\n", percentage);
other->percent = percentage;
double new_current_percent = current->percent + ((double)ppt / 100.0); /* resize */
double new_other_percent = other->percent - ((double)ppt / 100.0); LOG("second->percent = %f\n", second->percent);
LOG("new_current_percent = %f\n", new_current_percent); LOG("first->percent before = %f\n", first->percent);
LOG("new_other_percent = %f\n", new_other_percent); if (first->percent == 0.0)
first->percent = percentage;
if (second->percent == 0.0)
second->percent = percentage;
double new_first_percent = first->percent + ((double)ppt / 100.0);
double new_second_percent = second->percent - ((double)ppt / 100.0);
LOG("new_first_percent = %f\n", new_first_percent);
LOG("new_second_percent = %f\n", new_second_percent);
/* Ensure that the new percentages are positive and greater than /* Ensure that the new percentages are positive and greater than
* 0.05 to have a reasonable minimum size. */ * 0.05 to have a reasonable minimum size. */
if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) && if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) { definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
current->percent += ((double)ppt / 100.0); first->percent += ((double)ppt / 100.0);
other->percent -= ((double)ppt / 100.0); second->percent -= ((double)ppt / 100.0);
LOG("current->percent after = %f\n", current->percent); LOG("first->percent after = %f\n", first->percent);
LOG("other->percent after = %f\n", other->percent); LOG("second->percent after = %f\n", second->percent);
} else { } else {
LOG("Not resizing, already at minimum size\n"); LOG("Not resizing, already at minimum size\n");
} }

View File

@ -51,6 +51,54 @@ DRAGGING_CB(resize_callback) {
xcb_flush(conn); xcb_flush(conn);
} }
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction) {
DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
Con *first = *current;
Con *second = NULL;
if (first == NULL) {
DLOG("Current container is NULL, aborting.\n");
return false;
}
/* Go up in the tree and search for a container to resize */
const orientation_t search_orientation = ((direction == D_LEFT || direction == D_RIGHT) ? HORIZ : VERT);
const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
while (first->type != CT_WORKSPACE &&
first->type != CT_FLOATING_CON &&
second == NULL) {
/* get the appropriate first container with the matching
* orientation (skip stacked/tabbed cons) */
if ((con_orientation(first->parent) != search_orientation) ||
(first->parent->layout == L_STACKED) ||
(first->parent->layout == L_TABBED)) {
first = first->parent;
continue;
}
/* get the counterpart for this resizement */
if (dir_backwards) {
second = TAILQ_PREV(first, nodes_head, nodes);
} else {
second = TAILQ_NEXT(first, nodes);
}
if (second == NULL) {
DLOG("No second container in this direction found, trying to look further up in the tree...\n");
first = first->parent;
}
}
DLOG("Found participants: first=%p and second=%p.", first, second);
*current = first;
*other = second;
if (first == NULL || second == NULL) {
DLOG("Could not find two participants for this resize request.\n");
return false;
}
return true;
}
int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) { int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
DLOG("resize handler\n"); DLOG("resize handler\n");