swaymux/Keys/CreateWorkspaceKeyListener.h

61 lines
1.9 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"
#include "../sway_bindings/swayoutputs.h"
2024-03-08 10:53:30 +01:00
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)) {
2024-03-08 10:53:30 +01:00
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().c_str(), cmd.str().length()));
std::cout << resp.msg << "\n";
QApplication::quit();
2024-03-08 10:53:30 +01:00
}
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