swaymux/Keys/MainWindowAwareKeyListener.h

99 lines
3.0 KiB
C
Raw Normal View History

//
// 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<const SwayTreeNode *> getSelectedContainers() const {
std::set<const SwayTreeNode *> containerIds;
auto selectedNodes = getModel()->getSelectedNodes(getTreeView());
for (auto *container: selectedNodes) {
auto containers = container->accumulateContainers();
for (auto *containerToMove: container->accumulateContainers()) {
containerIds.insert(containerToMove);
}
}
return containerIds;
}
[[nodiscard]] std::string createNewWorkspaceId(const std::set<const SwayTreeNode *> &allowedContainers) const {
std::set<std::string> candidates;
for (auto *con: allowedContainers) {
const auto *workspace = con->findWorkspace();
if (workspace == nullptr)
continue;
if (allowedContainers.contains(workspace)) {
candidates.insert(workspace->node.name);
continue;
}
auto containers = workspace->accumulateContainers();
if (std::all_of(containers.begin(), containers.end(), [allowedContainers](const SwayTreeNode *container) {
return allowedContainers.contains(container);
})) {
candidates.insert(workspace->node.name);
}
}
if (!candidates.empty()) {
auto res = std::min_element(candidates.begin(), candidates.end());
return *res;
}
int newWorkspace = 0;
SwayTreeNode* workspaceNode;
do {
newWorkspace++;
workspaceNode = getModel()->getRoot()->findWorkspaceByName(std::to_string(newWorkspace));
} while (workspaceNode != nullptr && workspaceNode->childCount() != 0);
return std::to_string(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;
}
2024-03-12 11:48:43 +01:00
protected:
MainWindow *window;
[[nodiscard]] SwayTreeModel *getModel() const {
return this->window->model;
}
[[nodiscard]] QTreeView *getTreeView() const {
return window->ui->treeView;
}
};
#endif //SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H