0a0177d0190537f46d39b01b25ebd16c7534c91a

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

Message

Fix wrong window selection on close

Diff

 1diff --git a/src/Hy3Layout.cpp b/src/Hy3Layout.cpp
 2index bbdc85d73b58566d6635443046fdb1a7f07c6df6..de95f6a1ce1faba3eb18a7116e131692a7f0a471 100644
 3--- a/src/Hy3Layout.cpp
 4+++ b/src/Hy3Layout.cpp
 5@@ -416,6 +416,7 @@ void Hy3Layout::onWindowCreatedTiling(CWindow* window) {
 6 	}
 7 	Debug::log(LOG, "open new window %p(node: %p:%p) on winodow %p in %p", window, &node, node.data.as_window, opening_after, opening_into);
 8 
 9+	opening_into->data.as_group.lastFocusedChild = &node;
10 	opening_into->recalcSizePosRecursive();
11 	Debug::log(LOG, "opening_into (%p) contains new child (%p)? %d", opening_into, &node, opening_into->data.as_group.hasChild(&node));
12 }
13@@ -440,9 +441,22 @@ void Hy3Layout::onWindowRemovedTiling(CWindow* window) {
14 	auto* parent = node->parent;
15 	auto* group = &parent->data.as_group;
16 
17+	if (group->children.size() > 2) {
18+		auto iter = std::find(group->children.begin(), group->children.end(), node);
19+		if (iter == group->children.begin()) {
20+			group->lastFocusedChild = *std::next(iter);
21+		} else {
22+			group->lastFocusedChild = *std::prev(iter);
23+		}
24+	}
25+
26 	group->children.remove(node);
27 	this->nodes.remove(*node);
28 
29+	if (group->children.size() == 1) {
30+		group->lastFocusedChild = group->children.front();
31+	}
32+
33 	while (parent->parent != nullptr && group->children.empty()) {
34 		auto* child = parent;
35 		parent = parent->parent;
36@@ -457,23 +471,26 @@ void Hy3Layout::onWindowRemovedTiling(CWindow* window) {
37 			}
38 		}
39 
40-		parent->data.as_group.children.remove(child);
41+		group->children.remove(child);
42+		this->nodes.remove(*child);
43 
44 		if (group->children.size() == 1) {
45 			group->lastFocusedChild = group->children.front();
46 		}
47-
48-		this->nodes.remove(*child);
49 	}
50 
51 	if (parent != nullptr) {
52 		parent->recalcSizePosRecursive();
53-		auto* focus = group->lastFocusedChild;
54-		while (focus->data.type == Hy3NodeData::Group) focus = focus->data.as_group.lastFocusedChild;
55-		g_pCompositor->focusWindow(focus->data.as_window);
56 	}
57 }
58 
59+CWindow* Hy3Layout::getNextWindowCandidate(CWindow* window) {
60+	auto* node = this->getWorkspaceRootGroup(window->m_iWorkspaceID);
61+	if (node == nullptr) return nullptr;
62+	while (node->data.type == Hy3NodeData::Group) node = node->data.as_group.lastFocusedChild;
63+	return node->data.as_window;
64+}
65+
66 void Hy3Layout::onWindowFocusChange(CWindow* window) {
67 	Debug::log(LOG, "Switched windows from %p to %p", this->lastActiveWindow, window);
68 	auto* node = this->getNodeFromWindow(this->lastActiveWindow);
69diff --git a/src/Hy3Layout.hpp b/src/Hy3Layout.hpp
70index 6e606539465f26580d5e886d3107883101d777a0..bbc5c33f2ff9e48487903c03f6bec91f198a69c4 100644
71--- a/src/Hy3Layout.hpp
72+++ b/src/Hy3Layout.hpp
73@@ -93,7 +93,8 @@ public:
74 	virtual void switchWindows(CWindow*, CWindow*);
75 	virtual void alterSplitRatio(CWindow*, float, bool);
76 	virtual std::string getLayoutName();
77-	virtual void replaceWindowDataWith(CWindow* from, CWindow* to);
78+	virtual CWindow* getNextWindowCandidate(CWindow*);
79+	virtual void replaceWindowDataWith(CWindow*, CWindow*);
80 
81 	virtual void onEnable();
82 	virtual void onDisable();