From f3ab895916ca1a0f004b5ceaefa90eee90676532 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Fri, 4 May 2018 08:24:25 -0400
Subject: Implement `floating enable`

---
 sway/tree/layout.c    | 43 +++++++++++++++++++++++++++++++++++++++----
 sway/tree/workspace.c |  2 ++
 2 files changed, 41 insertions(+), 4 deletions(-)

(limited to 'sway/tree')

diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 2f4ae667..7bbeb4b1 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -142,11 +142,26 @@ struct sway_container *container_remove_child(struct sway_container *child) {
 	}
 
 	struct sway_container *parent = child->parent;
-	for (int i = 0; i < parent->children->length; ++i) {
-		if (parent->children->items[i] == child) {
-			list_del(parent->children, i);
-			break;
+	if (!child->is_floating) {
+		for (int i = 0; i < parent->children->length; ++i) {
+			if (parent->children->items[i] == child) {
+				list_del(parent->children, i);
+				break;
+			}
+		}
+	} else {
+		if (!sway_assert(parent->type == C_WORKSPACE && child->type == C_VIEW,
+					"Found floating non-view and/or in non-workspace")) {
+			return parent;
+		}
+		struct sway_workspace *ws = parent->sway_workspace;
+		for (int i = 0; i < ws->floating->length; ++i) {
+			if (ws->floating->items[i] == child) {
+				list_del(ws->floating, i);
+				break;
+			}
 		}
+		child->is_floating = false;
 	}
 	child->parent = NULL;
 	container_notify_subtree_changed(parent);
@@ -154,6 +169,26 @@ struct sway_container *container_remove_child(struct sway_container *child) {
 	return parent;
 }
 
+void container_add_floating(struct sway_container *workspace,
+		struct sway_container *child) {
+	if (!sway_assert(workspace->type == C_WORKSPACE && child->type == C_VIEW,
+				"Attempted to float non-view and/or in non-workspace")) {
+		return;
+	}
+	if (!sway_assert(!child->parent,
+				"child already has a parent (invalid call)")) {
+		return;
+	}
+	if (!sway_assert(!child->is_floating,
+				"child is already floating (invalid state)")) {
+		return;
+	}
+	struct sway_workspace *ws = workspace->sway_workspace;
+	list_add(ws->floating, child);
+	child->parent = workspace;
+	child->is_floating = true;
+}
+
 void container_move_to(struct sway_container *container,
 		struct sway_container *destination) {
 	if (container == destination
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index f34baa9e..c4f8ac5e 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -12,6 +12,7 @@
 #include "sway/tree/arrange.h"
 #include "sway/tree/container.h"
 #include "sway/tree/workspace.h"
+#include "list.h"
 #include "log.h"
 #include "util.h"
 
@@ -64,6 +65,7 @@ struct sway_container *workspace_create(struct sway_container *output,
 		return NULL;
 	}
 	swayws->swayc = workspace;
+	swayws->floating = create_list();
 	workspace->sway_workspace = swayws;
 
 	container_add_child(output, workspace);
-- 
cgit v1.2.3