swaymux/Keys/CreateWorkspaceKeyListener.h

72 lines
2.2 KiB
C
Raw Normal View History

2024-03-08 10:53:30 +01:00
//
// 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"
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 indexes = treeView->selectionModel()->selectedIndexes();
for (auto i: indexes) {
std::vector<int> indices;
for (auto j = i; j.isValid(); j = j.parent()) {
indices.insert(indices.cbegin(), j.row());
}
auto *head = root;
const SwayTreeNode *output;
for (auto j: indices) {
head = head->child(j);
if (head == nullptr)
goto hell;
}
output = head->findOutput();
if (output != nullptr) {
cmd << "focus output " << output->node.name << " , ";
break;
}
hell:;
}
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().c_str(), cmd.str().length()));
std::cout << resp.msg << "\n";
}
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;
}
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
};
#endif //SWAYMUX_CREATEWORKSPACEKEYLISTENER_H