swaymux/Keys/MainWindowAwareKeyListener.h

70 lines
2.1 KiB
C++

//
// Created by grimmauld on 11.03.24.
//
#ifndef SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H
#define SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H
#include "AbstractKeyListener.h"
#include "../mainwindow.h"
#include "../sway_bindings/swayoutputs.h"
class MainWindowAwareKeyListener : public AbstractKeyListener {
protected:
explicit MainWindowAwareKeyListener(MainWindow *pWindow) : window(pWindow) {}
void updateMainWindow() const {
usleep(100000); // give sway time to react. Not great but works i guess.
window->updateModel();
}
[[nodiscard]] std::set<int> getSelectedContainerIds() const {
std::set<int> containerIds;
auto selectedNodes = getModel()->getSelectedNodes(getTreeView());
for (auto *container: selectedNodes) {
auto containers = container->accumulateContainers();
for (auto *containerToMove: container->accumulateContainers()) {
containerIds.insert(containerToMove->node.id);
}
}
return containerIds;
}
[[nodiscard]] int createNewWorkspaceId() const {
auto used_workspaces = getModel()->getRoot()->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) {}
return newWorkspace;
}
[[nodiscard]] const SwayTreeNode* findSelectedValidOutput() const {
auto availableOutputs = SwayOutputList(SwayTreeModel::sway);
auto selectedNodes = getModel()->getSelectedNodes(getTreeView());
for (auto node: selectedNodes) {
auto output = node->findOutput();
if (availableOutputs.isOutputValid(output)) {
return output;
}
}
return nullptr;
}
private:
MainWindow *window;
[[nodiscard]] SwayTreeModel* getModel() const {
return this->window->model;
}
[[nodiscard]] QTreeView* getTreeView() const {
return window->ui->treeView;
}
};
#endif //SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H