8a12cb4b64d2129c58d0f82d3b8ce1d712ea4540

Author
outfoxxed <outfoxxed@outfoxxed.me>
Committer
outfoxxed <outfoxxed@outfoxxed.me>
Date

Message

Refactor and deduplicate recursive group distruction

Diff

This diff is truncated to protect this page.

  1diff --git a/src/Hy3Layout.cpp b/src/Hy3Layout.cpp
  2index 0ac8eae3333e2ade46c7b5589f43be41b23fd8ac..16434827df3f44949f3b39aa80c4d368a9911fdd 100644
  3--- a/src/Hy3Layout.cpp
  4+++ b/src/Hy3Layout.cpp
  5@@ -213,6 +213,51 @@ bool swallowGroup(Hy3Node* into) {
  6 	return true;
  7 }
  8 
  9+Hy3Node* removeFromParentRecursive(Hy3Node* node) {
 10+	Hy3Node* parent = node;
 11+
 12+	Debug::log(LOG, "Recursively removing parent nodes of %p", node);
 13+
 14+	while (parent->parent != nullptr) {
 15+		auto* child = parent;
 16+		parent = parent->parent;
 17+		auto& group = parent->data.as_group;
 18+
 19+
 20+		if (group.children.size() > 2) {
 21+			auto iter = std::find(group.children.begin(), group.children.end(), child);
 22+
 23+			if (iter == group.children.begin()) {
 24+				group.lastFocusedChild = *std::next(iter);
 25+			} else {
 26+				group.lastFocusedChild = *std::prev(iter);
 27+			}
 28+		}
 29+
 30+		group.children.remove(child);
 31+
 32+		if (group.children.size() == 1) {
 33+			group.lastFocusedChild = group.children.front();
 34+		}
 35+
 36+		if (child != node) {
 37+			parent->layout->nodes.remove(*child);
 38+		}
 39+
 40+		if (!group.children.empty()) {
 41+			auto splitmod = group.children.empty() ? 0.0 : -((1.0 - child->size_ratio) / group.children.size());
 42+
 43+			for (auto* child: group.children) {
 44+				child->size_ratio += splitmod;
 45+			}
 46+
 47+			break;
 48+		}
 49+	}
 50+
 51+	return parent;
 52+}
 53+
 54 bool Hy3GroupData::hasChild(Hy3Node* node) {
 55 	Debug::log(LOG, "Searching for child %p of %p", this, node);
 56 	for (auto child: this->children) {
 57@@ -471,59 +516,9 @@ void Hy3Layout::onWindowRemovedTiling(CWindow* window) {
 58 		g_pCompositor->setWindowFullscreen(window, false, FULLSCREEN_FULL);
 59 	}
 60 
 61-	auto* parent = node->parent;
 62-	auto* group = &parent->data.as_group;
 63-
 64-	if (group->children.size() > 2) {
 65-		auto iter = std::find(group->children.begin(), group->children.end(), node);
 66-		if (iter == group->children.begin()) {
 67-			group->lastFocusedChild = *std::next(iter);
 68-		} else {
 69-			group->lastFocusedChild = *std::prev(iter);
 70-		}
 71-	}
 72-
 73-	group->children.remove(node);
 74-
 75-	auto splitmod = group->children.empty() ? 0.0 : (1.0 - node->size_ratio) / group->children.size();
 76-	for (auto child: group->children) {
 77-		child->size_ratio -= splitmod;
 78-	}
 79-
 80+	auto* parent = removeFromParentRecursive(node);
 81 	this->nodes.remove(*node);
 82 
 83-	if (group->children.size() == 1) {
 84-		group->lastFocusedChild = group->children.front();
 85-	}
 86-
 87-	while (parent->parent != nullptr && group->children.empty()) {
 88-		auto* child = parent;
 89-		parent = parent->parent;
 90-		group = &parent->data.as_group;
 91-
 92-		if (group->children.size() > 2) {
 93-			auto iter = std::find(group->children.begin(), group->children.end(), child);
 94-			if (iter == group->children.begin()) {
 95-				group->lastFocusedChild = *std::next(iter);
 96-			} else {
 97-				group->lastFocusedChild = *std::prev(iter);
 98-			}
 99-		}
100-
101-		group->children.remove(child);
102-
103-		auto splitmod = group->children.empty() ? 0.0 : (1.0 - child->size_ratio) / group->children.size();
104-		for (auto child: group->children) {
105diff --git a/src/Hy3Layout.hpp b/src/Hy3Layout.hpp
106index 0220c6037759e6bc56f4504e8ea104ee1129096f..6e88f9b5143422775508a457f469312ec1498ee7 100644
107--- a/src/Hy3Layout.hpp
108+++ b/src/Hy3Layout.hpp
109@@ -81,7 +81,9 @@ struct Hy3Node {
110 
111 // Attempt to swallow a group. returns true if swallowed
112 Hy3Node* swallowGroups(Hy3Node*);
113-
114+// Remove this node from its parent, deleting the parent if it was
115+// the only child and recursing if the parent was the only child of it's parent.
116+Hy3Node* removeFromParentRecursive(Hy3Node*);
117 
118 class Hy3Layout: public IHyprLayout {
119 public: