swaymux/Keys/SwitchToKeybindListener.h

102 lines
3.4 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 AbstractKeyListener {
public:
explicit SwitchToKeybindListener(SwayTreeModel *swayTreeModel, QTreeView *treeView) : swayTreeModel(
swayTreeModel), treeView(treeView) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto *root = swayTreeModel->getRoot();
if (root == nullptr)
return;
Formatter cmd;
auto selectedNodes = swayTreeModel->getSelectedNodes(treeView);
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 used_workspaces = root->accumulateWorkspaces();
int newWorkspace = 1; // workspace 1 is the first one. We don't expect our users to be programmers.
for (; used_workspaces.contains(std::to_string(newWorkspace)); ++newWorkspace) {}
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 << "] focus , move container workspace " << newWorkspace << " ; ";
}
cmd << "workspace number " << newWorkspace;
std::cout << cmd.str() << "\n";
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
QApplication::quit();
}
std::string getDescription() override {
2024-03-09 17:58:15 +01:00
return "Switch to selected container(s)";
}
std::string getKeyText() override {
return "ENTER";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Enter || key == Qt::Key_Return;
}
private:
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
};
#endif //SWAYMUX_SWITCHTOKEYBINDLISTENER_H