2024-03-11 17:11:26 +01:00
|
|
|
//
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
[[nodiscard]] std::set<const SwayTreeNode *> getSelectedContainers() const {
|
|
|
|
std::set<const SwayTreeNode *> containerIds;
|
2024-03-11 17:11:26 +01:00
|
|
|
auto selectedNodes = getModel()->getSelectedNodes(getTreeView());
|
|
|
|
|
|
|
|
for (auto *container: selectedNodes) {
|
|
|
|
auto containers = container->accumulateContainers();
|
|
|
|
for (auto *containerToMove: container->accumulateContainers()) {
|
2024-03-26 18:01:53 +01:00
|
|
|
containerIds.insert(containerToMove);
|
2024-03-11 17:11:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return containerIds;
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
[[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);
|
2024-03-11 17:11:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
[[nodiscard]] const SwayTreeNode *findSelectedValidOutput() const {
|
2024-03-11 17:11:26 +01:00
|
|
|
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:
|
2024-03-11 17:11:26 +01:00
|
|
|
MainWindow *window;
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
[[nodiscard]] SwayTreeModel *getModel() const {
|
2024-03-11 17:11:26 +01:00
|
|
|
return this->window->model;
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:01:53 +01:00
|
|
|
[[nodiscard]] QTreeView *getTreeView() const {
|
2024-03-11 17:11:26 +01:00
|
|
|
return window->ui->treeView;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //SWAYMUX_MAINWINDOWAWAREKEYLISTENER_H
|