swaymux/Keys/JumpOutputKeyListener.h

56 lines
1.9 KiB
C++

//
// Created by grimmauld on 09.03.24.
//
#ifndef SWAYMUX_JUMPOUTPUTKEYLISTENER_H
#define SWAYMUX_JUMPOUTPUTKEYLISTENER_H
#include <QTreeView>
#include <iostream>
#include <QGuiApplication>
#include "AbstractKeyListener.h"
#include "../tree/SwayTreeModel.h"
#include "../sway_bindings/swayoutputs.h"
class JumpOutputKeyListener : public AbstractKeyListener {
public:
explicit JumpOutputKeyListener(SwayTreeModel *swayTreeModel, QTreeView *treeView) : swayTreeModel(
swayTreeModel), treeView(treeView) {};
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto current = treeView->selectionModel()->currentIndex();
auto *selected = swayTreeModel->getSelectedNode(current);
if (selected == nullptr)
return;
auto *output = selected->findOutput();
if (output == nullptr) {
if (selected->node.type == NodeType::root)
output = selected->child(0);
else
return;
}
if (output->parentItem() == nullptr)
return;
auto *toFocus = output->parentItem()->child(output->row() + getOffset(selected));
if (toFocus == nullptr) // happens if index out of bounds
return;
auto focusIndex = swayTreeModel->findIndexByNode(toFocus);
auto modifiers = QGuiApplication::queryKeyboardModifiers();
treeView->selectionModel()->setCurrentIndex(focusIndex, modifiers.testFlag(Qt::ControlModifier)
? QItemSelectionModel::Current
: QItemSelectionModel::ClearAndSelect);
}
[[nodiscard]] inline virtual int getOffset(const SwayTreeNode* selected) const = 0;
private:
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
};
#endif //SWAYMUX_JUMPOUTPUTKEYLISTENER_H