From: Orestis Floros Date: Thu, 23 Aug 2018 12:36:50 +0000 (+0300) Subject: Introduce parse_direction X-Git-Tag: 4.16~54^2~9 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=9522b46f1b78d1020285f53d2053ccd6c800c62e;p=i3%2Fi3 Introduce parse_direction Also fixes the following bug: in the fix for #1011 in cmd_resize_floating direction "width" is not considered. Influenced by #3240. --- diff --git a/include/commands.h b/include/commands.h index aaa0875f..b4b3da38 100644 --- a/include/commands.h +++ b/include/commands.h @@ -216,7 +216,7 @@ void cmd_sticky(I3_CMD, const char *action); * Implementation of 'move [ [px]]'. * */ -void cmd_move_direction(I3_CMD, const char *direction, long move_px); +void cmd_move_direction(I3_CMD, const char *direction_str, long move_px); /** * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'. diff --git a/src/commands.c b/src/commands.c index ad7d9770..0a2f3424 100644 --- a/src/commands.c +++ b/src/commands.c @@ -419,71 +419,89 @@ void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no ysuccess(true); } -static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) { - LOG("floating resize\n"); +/* + * Convert a string direction ("left", "right", etc.) to a direction_t. Assumes + * valid direction string. + */ +static direction_t parse_direction(const char *str) { + if (strcmp(str, "left") == 0) { + return D_LEFT; + } else if (strcmp(str, "right") == 0) { + return D_RIGHT; + } else if (strcmp(str, "up") == 0) { + return D_UP; + } else if (strcmp(str, "down") == 0) { + return D_DOWN; + } else { + ELOG("Invalid direction. This is a parser bug.\n"); + assert(false); + } +} + +static void cmd_resize_floating(I3_CMD, const char *way, const char *direction_str, Con *floating_con, int px) { Rect old_rect = floating_con->rect; Con *focused_con = con_descend_focused(floating_con); + direction_t direction; + if (strcmp(direction_str, "height") == 0) { + direction = D_DOWN; + } else if (strcmp(direction_str, "width") == 0) { + direction = D_RIGHT; + } else { + direction = parse_direction(direction_str); + } + orientation_t orientation = orientation_from_direction(direction); + /* ensure that resize will take place even if pixel increment is smaller than * height increment or width increment. * fixes #1011 */ const i3Window *window = focused_con->window; if (window != NULL) { - if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 || - strcmp(direction, "height") == 0) { - if (px < 0) + if (orientation == VERT) { + if (px < 0) { px = (-px < window->height_increment) ? -window->height_increment : px; - else + } else { px = (px < window->height_increment) ? window->height_increment : px; - } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) { - if (px < 0) + } + } else { + if (px < 0) { px = (-px < window->width_increment) ? -window->width_increment : px; - else + } else { px = (px < window->width_increment) ? window->width_increment : px; + } } } - if (strcmp(direction, "up") == 0) { + if (orientation == VERT) { floating_con->rect.height += px; - } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) { - floating_con->rect.height += px; - } else if (strcmp(direction, "left") == 0) { - floating_con->rect.width += px; } else { floating_con->rect.width += px; } - floating_check_size(floating_con); /* Did we actually resize anything or did the size constraints prevent us? * If we could not resize, exit now to not move the window. */ - if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0) + if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0) { return; + } - if (strcmp(direction, "up") == 0) { + if (direction == D_UP) { floating_con->rect.y -= (floating_con->rect.height - old_rect.height); - } else if (strcmp(direction, "left") == 0) { + } else if (direction == D_LEFT) { floating_con->rect.x -= (floating_con->rect.width - old_rect.width); } /* If this is a scratchpad window, don't auto center it from now on. */ - if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) + if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) { floating_con->scratchpad_state = SCRATCHPAD_CHANGED; + } } static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) { LOG("tiling resize\n"); Con *second = NULL; Con *first = current; - direction_t search_direction; - if (!strcmp(direction, "left")) - search_direction = D_LEFT; - else if (!strcmp(direction, "right")) - search_direction = D_RIGHT; - else if (!strcmp(direction, "up")) - search_direction = D_UP; - else - search_direction = D_DOWN; + direction_t search_direction = parse_direction(direction); bool res = resize_find_tiling_participants(&first, &second, search_direction, false); if (!res) { @@ -1254,20 +1272,19 @@ void cmd_exec(I3_CMD, const char *nosn, const char *command) { * */ void cmd_focus_direction(I3_CMD, const char *direction) { - DLOG("direction = *%s*\n", direction); - - if (strcmp(direction, "left") == 0) - tree_next('p', HORIZ); - else if (strcmp(direction, "right") == 0) - tree_next('n', HORIZ); - else if (strcmp(direction, "up") == 0) - tree_next('p', VERT); - else if (strcmp(direction, "down") == 0) - tree_next('n', VERT); - else { - ELOG("Invalid focus direction (%s)\n", direction); - ysuccess(false); - return; + switch (parse_direction(direction)) { + case D_LEFT: + tree_next('p', HORIZ); + break; + case D_RIGHT: + tree_next('n', HORIZ); + break; + case D_UP: + tree_next('p', VERT); + break; + case D_DOWN: + tree_next('n', VERT); + break; } cmd_output->needs_tree_render = true; @@ -1492,29 +1509,37 @@ void cmd_sticky(I3_CMD, const char *action) { * Implementation of 'move [ [px]]'. * */ -void cmd_move_direction(I3_CMD, const char *direction, long move_px) { +void cmd_move_direction(I3_CMD, const char *direction_str, long move_px) { owindow *current; HANDLE_EMPTY_MATCH; Con *initially_focused = focused; + direction_t direction = parse_direction(direction_str); TAILQ_FOREACH(current, &owindows, owindows) { - DLOG("moving in direction %s, px %ld\n", direction, move_px); + DLOG("moving in direction %s, px %ld\n", direction_str, move_px); if (con_is_floating(current->con)) { DLOG("floating move with %ld pixels\n", move_px); Rect newrect = current->con->parent->rect; - if (strcmp(direction, "left") == 0) { - newrect.x -= move_px; - } else if (strcmp(direction, "right") == 0) { - newrect.x += move_px; - } else if (strcmp(direction, "up") == 0) { - newrect.y -= move_px; - } else if (strcmp(direction, "down") == 0) { - newrect.y += move_px; + + switch (direction) { + case D_LEFT: + newrect.x -= move_px; + break; + case D_RIGHT: + newrect.x += move_px; + break; + case D_UP: + newrect.y -= move_px; + break; + case D_DOWN: + newrect.y += move_px; + break; } + floating_reposition(current->con->parent, newrect); } else { - tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN)))); + tree_move(current->con, direction); cmd_output->needs_tree_render = true; } }