swaymux/Keys/CreateWorkspaceKeyListener.h

61 lines
1.9 KiB
C++

//
// Created by grimmauld on 07.03.24.
//
#ifndef SWAYMUX_CREATEWORKSPACEKEYLISTENER_H
#define SWAYMUX_CREATEWORKSPACEKEYLISTENER_H
#include <QTreeView>
#include <iostream>
#include "AbstractKeyListener.h"
#include "../tree/SwayTreeModel.h"
#include "../sway_bindings/swayoutputs.h"
class CreateWorkspaceKeyListener : public AbstractKeyListener {
public:
explicit CreateWorkspaceKeyListener(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);
auto availableOutputs = SwayOutputList(SwayTreeModel::sway);
for (auto node: selectedNodes) {
auto output = node->findOutput();
if (availableOutputs.isOutputValid(output)) {
cmd << "focus output " << output->node.name << " , ";
break;
}
}
auto used_workspaces = root->accumulateWorkspaces();
int i = 1; // workspace 1 is the first one. We don't expect our users to be programmers.
for (; used_workspaces.contains(std::to_string(i)); ++i) {}
cmd << "workspace number " << i;
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
QApplication::quit();
}
std::string getDescription() override {
return "Create and switch to new empty workspace on selected output";
}
std::string getKeyText() override {
return "C";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_C;
}
private:
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
};
#endif //SWAYMUX_CREATEWORKSPACEKEYLISTENER_H