swaymux/Keys/SwitchToKeybindListener.h

94 lines
3.1 KiB
C
Raw Normal View History

//
// Created by grimmauld on 08.03.24.
//
#ifndef SWAYMUX_SWITCHTOKEYBINDLISTENER_H
#define SWAYMUX_SWITCHTOKEYBINDLISTENER_H
#include <QTreeView>
#include <iostream>
#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<int> 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 {
2024-03-09 17:58:15 +01:00
return "Switch to selected container(s)";
}
const std::string getKeyText() override {
2024-03-12 11:48:43 +01:00
return "Enter";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Enter || key == Qt::Key_Return;
}
};
#endif //SWAYMUX_SWITCHTOKEYBINDLISTENER_H