]> git.sur5r.net Git - i3/i3/blobdiff - src/scratchpad.c
Merge pull request #1613 from Airblader/feature-1426
[i3/i3] / src / scratchpad.c
index 8b96521704eb80dc8c87f2c52f94df86eb7f7c25..75b8e56c2bbbfe6064189e563eccc72ebee7acba 100644 (file)
@@ -21,7 +21,8 @@
 void scratchpad_move(Con *con) {
     if (con->type == CT_WORKSPACE) {
         LOG("'move scratchpad' used on a workspace \"%s\". Calling it "
-            "recursively on all windows on this workspace.\n", con->name);
+            "recursively on all windows on this workspace.\n",
+            con->name);
         Con *current;
         current = TAILQ_FIRST(&(con->focus_head));
         while (current) {
@@ -39,6 +40,12 @@ void scratchpad_move(Con *con) {
         return;
     }
 
+    /* If the current con is in fullscreen mode, we need to disable that,
+     *  as a scratchpad window should never be in fullscreen mode */
+    if (focused && focused->type != CT_WORKSPACE && focused->fullscreen_mode != CF_NONE) {
+        con_toggle_fullscreen(focused, CF_OUTPUT);
+    }
+
     /* 1: Ensure the window or any parent is floating. From now on, we deal
      * with the CT_FLOATING_CON. We use automatic == false because the user
      * made the choice that this window should be a scratchpad (and floating).
@@ -60,7 +67,13 @@ void scratchpad_move(Con *con) {
      * adjusted in size according to what the user specifies. */
     if (con->scratchpad_state == SCRATCHPAD_NONE) {
         DLOG("This window was never used as a scratchpad before.\n");
-        con->scratchpad_state = SCRATCHPAD_FRESH;
+        if (con == maybe_floating_con) {
+            DLOG("It was in floating mode before, set scratchpad state to changed.\n");
+            con->scratchpad_state = SCRATCHPAD_CHANGED;
+        } else {
+            DLOG("It was in tiling mode before, set scratchpad state to fresh.\n");
+            con->scratchpad_state = SCRATCHPAD_FRESH;
+        }
     }
 }
 
@@ -78,14 +91,25 @@ void scratchpad_show(Con *con) {
     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
     Con *floating;
 
+    /* If this was 'scratchpad show' without criteria, we check if the
+     * currently focused window is a scratchpad window and should be hidden
+     * again. */
+    if (!con &&
+        (floating = con_inside_floating(focused)) &&
+        floating->scratchpad_state != SCRATCHPAD_NONE) {
+        DLOG("Focused window is a scratchpad window, hiding it.\n");
+        scratchpad_move(focused);
+        return;
+    }
+
     /* If the current con or any of its parents are in fullscreen mode, we
      * first need to disable it before showing the scratchpad con. */
     Con *fs = focused;
     while (fs && fs->fullscreen_mode == CF_NONE)
         fs = fs->parent;
 
-    if (fs->type != CT_WORKSPACE) {
-        con_toggle_fullscreen(focused, CF_OUTPUT);
+    if (fs && fs->type != CT_WORKSPACE) {
+        con_toggle_fullscreen(fs, CF_OUTPUT);
     }
 
     /* If this was 'scratchpad show' without criteria, we check if there is a
@@ -93,17 +117,17 @@ void scratchpad_show(Con *con) {
     Con *walk_con;
     Con *focused_ws = con_get_workspace(focused);
     TAILQ_FOREACH(walk_con, &(focused_ws->floating_head), floating_windows) {
-        if ((floating = con_inside_floating(walk_con)) &&
+        if (!con && (floating = con_inside_floating(walk_con)) &&
             floating->scratchpad_state != SCRATCHPAD_NONE &&
             floating != con_inside_floating(focused)) {
-                DLOG("Found an unfocused scratchpad window on this workspace\n");
-                DLOG("Focusing it: %p\n", walk_con);
-                /* use con_descend_tiling_focused to get the last focused
+            DLOG("Found an unfocused scratchpad window on this workspace\n");
+            DLOG("Focusing it: %p\n", walk_con);
+            /* use con_descend_tiling_focused to get the last focused
                  * window inside this scratch container in order to
                  * keep the focus the same within this container */
-                con_focus(con_descend_tiling_focused(walk_con));
-                return;
-            }
+            con_focus(con_descend_tiling_focused(walk_con));
+            return;
+        }
     }
 
     /* If this was 'scratchpad show' without criteria, we check if there is a
@@ -112,7 +136,7 @@ void scratchpad_show(Con *con) {
     focused_ws = con_get_workspace(focused);
     TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
         Con *walk_ws = con_get_workspace(walk_con);
-        if (walk_ws &&
+        if (!con && walk_ws &&
             !con_is_internal(walk_ws) && focused_ws != walk_ws &&
             (floating = con_inside_floating(walk_con)) &&
             floating->scratchpad_state != SCRATCHPAD_NONE) {
@@ -123,14 +147,10 @@ void scratchpad_show(Con *con) {
         }
     }
 
-    /* If this was 'scratchpad show' without criteria, we check if the
-     * currently focused window is a scratchpad window and should be hidden
-     * again. */
-    if (!con &&
-        (floating = con_inside_floating(focused)) &&
-        floating->scratchpad_state != SCRATCHPAD_NONE) {
-        DLOG("Focused window is a scratchpad window, hiding it.\n");
-        scratchpad_move(focused);
+    /* If this was 'scratchpad show' with criteria, we check if the window
+     * is actually in the scratchpad */
+    if (con && con->parent->scratchpad_state == SCRATCHPAD_NONE) {
+        DLOG("Window is not in the scratchpad, doing nothing.\n");
         return;
     }
 
@@ -162,6 +182,10 @@ void scratchpad_show(Con *con) {
             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
             return;
         }
+    } else {
+        /* We used a criterion, so we need to do what follows (moving,
+         * resizing) on the floating parent. */
+        con = con_inside_floating(con);
     }
 
     /* 1: Move the window from __i3_scratch to the current workspace. */
@@ -173,11 +197,8 @@ void scratchpad_show(Con *con) {
         Con *output = con_get_output(con);
         con->rect.width = output->rect.width * 0.5;
         con->rect.height = output->rect.height * 0.75;
-        con->rect.x = output->rect.x +
-                      ((output->rect.width / 2.0) - (con->rect.width / 2.0));
-        con->rect.y = output->rect.y +
-                      ((output->rect.height / 2.0) - (con->rect.height / 2.0));
-        con->scratchpad_state = SCRATCHPAD_CHANGED;
+        floating_check_size(con);
+        floating_center(con, con_get_workspace(con)->rect);
     }
 
     /* Activate active workspace if window is from another workspace to ensure