2024-03-09 12:55:29 +01:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
class SwitchToKeybindListener : public MainWindowAwareKeyListener {
|
2024-03-09 12:55:29 +01:00
|
|
|
public:
|
2024-03-26 18:01:53 +01:00
|
|
|
explicit SwitchToKeybindListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
|
2024-03-09 12:55:29 +01:00
|
|
|
|
|
|
|
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
|
2024-03-26 18:01:53 +01:00
|
|
|
auto *root = getModel()->getRoot();
|
2024-03-09 12:55:29 +01:00
|
|
|
if (root == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Formatter cmd;
|
2024-03-26 18:01:53 +01:00
|
|
|
auto const selectedNodes = getModel()->getSelectedNodes(getTreeView());
|
2024-03-09 12:55:29 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
auto newWorkspace = createNewWorkspaceId(selectedNodes);
|
2024-03-09 12:55:29 +01:00
|
|
|
|
|
|
|
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) {
|
2024-03-11 13:33:26 +01:00
|
|
|
cmd << "[con_id=" << id << "] move container workspace " << newWorkspace << " ; ";
|
2024-03-09 12:55:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
cmd << "workspace " << newWorkspace;
|
2024-03-09 12:55:29 +01:00
|
|
|
|
|
|
|
std::cout << cmd.str() << "\n";
|
|
|
|
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
|
|
|
|
std::cout << resp.msg << "\n";
|
|
|
|
QApplication::quit();
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:43:57 +01:00
|
|
|
const std::string getDescription() override {
|
2024-03-09 17:58:15 +01:00
|
|
|
return "Switch to selected container(s)";
|
2024-03-09 12:55:29 +01:00
|
|
|
}
|
|
|
|
|
2024-03-13 21:43:57 +01:00
|
|
|
const std::string getKeyText() override {
|
2024-03-12 11:48:43 +01:00
|
|
|
return "Enter";
|
2024-03-09 12:55:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool canAcceptKey(int key) const override {
|
|
|
|
return key == Qt::Key_Enter || key == Qt::Key_Return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //SWAYMUX_SWITCHTOKEYBINDLISTENER_H
|