// // Created by grimmauld on 11.03.24. // #ifndef SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H #define SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H #include "AbstractKeyListener.h" #include "../mainwindow.h" #include "../sway_bindings/swayoutputs.h" class MainWindowAwareKeyListener : public AbstractKeyListener { protected: explicit MainWindowAwareKeyListener(MainWindow *pWindow) : window(pWindow) {} void updateMainWindow() const { usleep(100000); // give sway time to react. Not great but works i guess. window->updateModel(); } [[nodiscard]] std::set getSelectedContainers() const { std::set containerIds; auto selectedNodes = getModel()->getSelectedNodes(getTreeView()); for (auto *container: selectedNodes) { auto containers = container->accumulateContainers(); for (auto *containerToMove: container->accumulateContainers()) { containerIds.insert(containerToMove); } } return containerIds; } [[nodiscard]] std::string createNewWorkspaceId(const std::set &allowedContainers, const SwayTreeNode* preferredOutput = nullptr) const { std::set candidates; for (auto *con: allowedContainers) { const auto *workspace = con->findWorkspace(); if (workspace == nullptr) continue; if (allowedContainers.contains(workspace) && (preferredOutput == nullptr || preferredOutput == workspace->findOutput())) { candidates.insert(workspace->node.name); continue; } auto containers = workspace->accumulateContainers(); if (std::all_of(containers.begin(), containers.end(), [allowedContainers, preferredOutput](const SwayTreeNode *container) { return allowedContainers.contains(container) && (preferredOutput == nullptr || preferredOutput == container->findOutput()); })) { candidates.insert(workspace->node.name); } } if (!candidates.empty()) { auto res = std::min_element(candidates.begin(), candidates.end()); return *res; } int newWorkspace = 0; SwayTreeNode* workspaceNode; do { newWorkspace++; workspaceNode = getModel()->getRoot()->findWorkspaceByName(std::to_string(newWorkspace)); } while (workspaceNode != nullptr && workspaceNode->childCount() != 0 && (preferredOutput != nullptr && preferredOutput == workspaceNode->findOutput())); return std::to_string(newWorkspace); } [[nodiscard]] const SwayTreeNode *findSelectedValidOutput() const { auto availableOutputs = SwayOutputList(SwayTreeModel::sway); auto selectedNodes = getModel()->getSelectedNodes(getTreeView()); for (auto node: selectedNodes) { auto output = node->findOutput(); if (availableOutputs.isOutputValid(output)) { return output; } } return nullptr; } protected: MainWindow *window; [[nodiscard]] SwayTreeModel *getModel() const { return this->window->model; } [[nodiscard]] QTreeView *getTreeView() const { return window->ui->treeView; } }; #endif //SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H