// // Created by grimmauld on 08.03.24. // #ifndef SWAYMUX_SWITCHTOKEYBINDLISTENER_H #define SWAYMUX_SWITCHTOKEYBINDLISTENER_H #include #include #include "AbstractKeyListener.h" #include "../tree/SwayTreeModel.h" #include "../sway_bindings/swayoutputs.h" class SwitchToKeybindListener : public MainWindowAwareKeyListener { public: explicit SwitchToKeybindListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {} void handleKeyEvent(const QKeyEvent *keyEvent) const override { auto *root = getModel()->getRoot(); if (root == nullptr) return; Formatter cmd; auto const selectedNodes = getModel()->getSelectedNodes(getTreeView()); if (selectedNodes.empty()) return; auto availableOutputs = SwayOutputList(SwayTreeModel::sway); if (selectedNodes.size() == 1) { auto *selected = *selectedNodes.begin(); if (availableOutputs.isOutputValid(selected->findOutput())) { switch (selected->node.type) { case NodeType::output: cmd << "focus output " << selected->node.name; break; case NodeType::workspace: cmd << "workspace " << selected->node.name; break; case NodeType::con: case NodeType::floating_con: cmd << "[con_id=" << selected->node.id << "] focus"; break; case NodeType::root: return; // this should never happen! isOutputValid should guard against this. } std::cout << cmd.str() << "\n"; auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str())); std::cout << resp.msg << "\n"; QApplication::quit(); return; } } auto newWorkspace = createNewWorkspaceId(selectedNodes); std::set containersToMove; for (auto* container: selectedNodes) { auto containers = container->accumulateContainers(); for (auto* containerToMove : container->accumulateContainers()) { containersToMove.insert(containerToMove->node.id); } } if (containersToMove.empty()) return; for (auto id: containersToMove) { cmd << "[con_id=" << id << "] move container workspace " << newWorkspace << " ; "; } cmd << "workspace " << newWorkspace; std::cout << cmd.str() << "\n"; auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str())); std::cout << resp.msg << "\n"; QApplication::quit(); } const std::string getDescription() override { return "Switch to selected container(s)"; } const std::string getKeyText() override { return "Enter"; } [[nodiscard]] bool canAcceptKey(int key) const override { return key == Qt::Key_Enter || key == Qt::Key_Return; } }; #endif //SWAYMUX_SWITCHTOKEYBINDLISTENER_H