From 6d10a98bd71cbb88bc5063bb5eee6e6db2749d61 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 22 May 2021 06:14:33 +0900 Subject: [PATCH] workspace: When the focused client is detached, focus on the next one When the focused client is detached from a workspace, the focus is moved to the first client of the workspace. However, from a user point-of-view, it seems better to move the focus to the next client. This commit changes the workspace_detach_client() function to shift the focus to the next client, if the focused client is removed. --- workspace.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/workspace.c b/workspace.c index 00fa0b5..53172b7 100644 --- a/workspace.c +++ b/workspace.c @@ -89,6 +89,7 @@ int workspace_attach_client(struct workspace *workspace, struct client *client) int workspace_detach_client(struct workspace *workspace, struct client *client) { + struct client *next; int err; #if MWM_DEBUG @@ -99,20 +100,42 @@ int workspace_detach_client(struct workspace *workspace, struct client *client) return(-EINVAL); } + /* before removing the client, we need to figure out who's getting the focus */ + err = loop_get_next(&workspace->clients, client, (void**)&next); + + if(err < 0) { + /* + * At the *very least* we should have gotten a pointer to client itself. + * If we didn't even get that, client wasn't on this workspace and we've + * encountered a bug. + */ + fprintf(stderr, "%s: BUG: Could not determine next client\n", __func__); + fprintf(stderr, "%s: loop_get_next: %s\n", __func__, strerror(-err)); + + return(err); + } + err = loop_remove(&workspace->clients, client); if(err < 0) { + /* + * If client wasn't on this workspace, the branch above should have been + * visited. So if we get here, something even nastier is going on. + */ + fprintf(stderr, "%s: BUG: Client %p was not on this workspace.\n", + __func__, (void*)client); + fprintf(stderr, "%s: loop_remove: %s\n", __func__, strerror(-err)); + return(err); } if(workspace_get_focused_client(workspace) == client) { - struct client *first; - - if(loop_get_first(&workspace->clients, (void**)&first) < 0) { - first = NULL; + if(next == client) { + /* nothing left to focus on */ + next = NULL; } - workspace_focus_client(workspace, first); + workspace_focus_client(workspace, next); } workspace_needs_redraw(workspace); -- 2.47.3