From c087452a8fdcc1f0968a79ea9ad59bfb386bfe61 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Wed, 28 Jun 2023 12:58:31 -0700 Subject: [PATCH] Add focusTab index mode --- src/Hy3Layout.cpp | 47 ++++++++++++++++++++++++++++++++--------------- src/Hy3Layout.hpp | 3 ++- src/main.cpp | 13 ++++++++++--- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/Hy3Layout.cpp b/src/Hy3Layout.cpp index ac106b0..e9dcbc8 100644 --- a/src/Hy3Layout.cpp +++ b/src/Hy3Layout.cpp @@ -1804,7 +1804,8 @@ void Hy3Layout::focusTab( int workspace, TabFocus target, TabFocusMousePriority mouse, - bool wrap_scroll + bool wrap_scroll, + int index ) { auto* node = this->getWorkspaceRootGroup(workspace); if (node == nullptr) return; @@ -1842,23 +1843,39 @@ hastab: return; auto& children = tab_node->data.as_group.children; - auto node_iter - = std::find(children.begin(), children.end(), tab_node->data.as_group.focused_child); - if (node_iter == children.end()) return; - if (target == TabFocus::Left) { - if (node_iter == children.begin()) { - if (wrap_scroll) node_iter = std::prev(children.end()); - else return; - } else node_iter = std::prev(node_iter); + if (target == TabFocus::Index) { + int i = 1; - tab_focused_node = *node_iter; + for (auto* node: children) { + if (i == index) { + tab_focused_node = node; + goto cont; + } + + i++; + } + + return; + cont:; } else { - if (node_iter == std::prev(children.end())) { - if (wrap_scroll) node_iter = children.begin(); - else return; - } else node_iter = std::next(node_iter); + auto node_iter + = std::find(children.begin(), children.end(), tab_node->data.as_group.focused_child); + if (node_iter == children.end()) return; + if (target == TabFocus::Left) { + if (node_iter == children.begin()) { + if (wrap_scroll) node_iter = std::prev(children.end()); + else return; + } else node_iter = std::prev(node_iter); - tab_focused_node = *node_iter; + tab_focused_node = *node_iter; + } else { + if (node_iter == std::prev(children.end())) { + if (wrap_scroll) node_iter = children.begin(); + else return; + } else node_iter = std::next(node_iter); + + tab_focused_node = *node_iter; + } } } diff --git a/src/Hy3Layout.hpp b/src/Hy3Layout.hpp index ac01478..944b2f0 100644 --- a/src/Hy3Layout.hpp +++ b/src/Hy3Layout.hpp @@ -35,6 +35,7 @@ enum class TabFocus { MouseLocation, Left, Right, + Index, }; enum class TabFocusMousePriority { @@ -159,7 +160,7 @@ public: void shiftWindow(int, ShiftDirection, bool); void shiftFocus(int, ShiftDirection, bool); void changeFocus(int, FocusShift); - void focusTab(int, TabFocus, TabFocusMousePriority, bool); + void focusTab(int, TabFocus, TabFocusMousePriority, bool, int); void killFocusedNode(int); bool shouldRenderSelected(CWindow*); diff --git a/src/main.cpp b/src/main.cpp index 1aa5bbd..3b5b322 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,11 +103,18 @@ void dispatch_focustab(std::string value) { TabFocus focus; auto mouse = TabFocusMousePriority::Ignore; bool wrap_scroll = false; + int index = 0; if (args[i] == "l" || args[i] == "left") focus = TabFocus::Left; else if (args[i] == "r" || args[i] == "right") focus = TabFocus::Right; - else if (args[i] == "mouse") { - g_Hy3Layout->focusTab(workspace, TabFocus::MouseLocation, mouse, false); + else if (args[i] == "index") { + i++; + focus = TabFocus::Index; + if (!isNumber(args[i])) return; + index = std::stoi(args[i]); + Debug::log(LOG, "Focus index '%s' -> %d, errno: %d", args[i].c_str(), index, errno); + } else if (args[i] == "mouse") { + g_Hy3Layout->focusTab(workspace, TabFocus::MouseLocation, mouse, false, 0); return; } else return; @@ -123,7 +130,7 @@ void dispatch_focustab(std::string value) { if (args[i++] == "wrap") wrap_scroll = true; - g_Hy3Layout->focusTab(workspace, focus, mouse, wrap_scroll); + g_Hy3Layout->focusTab(workspace, focus, mouse, wrap_scroll, index); } void dispatch_killactive(std::string value) {