Commit 778af7f2d707ed8aa2590a49809e637205c380f7 Parent: 1b75f41f0bfd53ea1da26cae49cea94920ab5e26 Author: mcol <mcol@posteo.net> Date: 2021-01-22 11:31:12 +0000 Committer: mcol <mcol@posteo.net> Committed: 2021-01-22 11:31:12 +0000 Drop repeated code for focussing in direction
src/types.h Modified
@@ -142,11 +142,11 @@ }; enum direction { - NONE, - UP, - RIGHT, - DOWN, - LEFT, + NONE = 0, + UP = 1 << 0, + RIGHT = 1 << 1, + DOWN = 1 << 2, + LEFT = 1 << 3, }; #endif
src/action.c Modified
@@ -44,66 +44,37 @@ void focus_in_direction(struct server *server, void *data) { + if (wl_list_empty(&server->current_desk->views)) + return; + struct view *current = wl_container_of(server->current_desk->views.next, current, link); enum direction dir = *(enum direction*)data; + struct view *next = NULL; + struct view *view; + double vx, vy, vdist, dist; double x = current->x + current->surface->geometry.width / 2; double y = current->y + current->surface->geometry.height / 2; double c = y - x; // y = x + c / slope double n = y + x; // y = n - x \ slope - - struct view *next = NULL; - struct view *view; - double vx, vy, vdist, dist; - - switch (dir) { - case RIGHT: - wl_list_for_each(view, &server->current_desk->views, link) { - vx = view->x + view->surface->geometry.width / 2; - vy = view->y + view->surface->geometry.height / 2; - vdist = DIST(x - vx, y - vy); - if (vy - vx < c && vy + vx > n && (!next || vdist < dist)) { - next = view; - dist = vdist; - } - } - break; - case LEFT: - wl_list_for_each(view, &server->current_desk->views, link) { - vx = view->x + view->surface->geometry.width / 2; - vy = view->y + view->surface->geometry.height / 2; - vdist = DIST(x - vx, y - vy); - if (vy - vx > c && vy + vx < n && (!next || vdist < dist)) { - next = view; - dist = vdist; - } - } - break; - case DOWN: - wl_list_for_each(view, &server->current_desk->views, link) { - vx = view->x + view->surface->geometry.width / 2; - vy = view->y + view->surface->geometry.height / 2; - vdist = DIST(x - vx, y - vy); - if (vy - vx > c && vy + vx > n && (!next || vdist < dist)) { - next = view; - dist = vdist; - } - } - break; - case UP: - wl_list_for_each(view, &server->current_desk->views, link) { - vx = view->x + view->surface->geometry.width / 2; - vy = view->y + view->surface->geometry.height / 2; - vdist = DIST(x - vx, y - vy); - if (vy - vx < c && vy + vx < n && (!next || vdist < dist)) { - next = view; - dist = vdist; - } - } - break; - case NONE: - return; + bool above_c = dir & (DOWN | LEFT); + bool above_n = dir & (DOWN | RIGHT); + + wl_list_for_each(view, &server->current_desk->views, link) { + vx = view->x + view->surface->geometry.width / 2; + vy = view->y + view->surface->geometry.height / 2; + vdist = DIST(x - vx, y - vy); + if ( + (above_c ^ (vy - vx < c)) && + (above_n ^ (vy + vx < n)) && + (!next || vdist < dist) && + view != current + ) { + next = view; + dist = vdist; + } } + if (next) focus_view(next, next->surface->surface); }