Don't Repeat Yourself: pull out some common functionality

This commit is contained in:
LordGrimmauld 2024-03-11 17:11:26 +01:00
parent f3b60a4a2a
commit db6d7c66b8
7 changed files with 95 additions and 74 deletions

View File

@ -47,6 +47,7 @@ set(PROJECT_SOURCES
Keys/MoveToScratchpadKeyListener.h
Keys/MoveToScratchpadKeyListener.h
Keys/RestoreKeyListener.h
Keys/MainWindowAwareKeyListener.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

View File

@ -7,21 +7,14 @@
#include "AbstractKeyListener.h"
#include "MainWindowAwareKeyListener.h"
class CloseKeyListener : public AbstractKeyListener {
class CloseKeyListener : public MainWindowAwareKeyListener {
public:
explicit CloseKeyListener(MainWindow *pWindow) : window(pWindow) {}
explicit CloseKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto selectedNodes = window->model->getSelectedNodes(window->ui->treeView);
std::set<int> containersToKill;
for (auto* container: selectedNodes) {
auto containers = container->accumulateContainers();
for (auto* containerToMove : container->accumulateContainers()) {
containersToKill.insert(containerToMove->node.id);
}
}
auto containersToKill = getSelectedContainerIds();
if (containersToKill.empty())
return;
@ -35,8 +28,7 @@ public:
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
usleep(100000); // give sway time to react. Not great but works i guess.
window->updateModel();
updateMainWindow();
}
std::string getDescription() override {
@ -50,8 +42,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Q || key == Qt::Key_Delete;
}
private:
MainWindow *window;
};

View File

@ -10,32 +10,20 @@
#include "AbstractKeyListener.h"
#include "../tree/SwayTreeModel.h"
#include "../sway_bindings/swayoutputs.h"
#include "MainWindowAwareKeyListener.h"
class CreateWorkspaceKeyListener : public AbstractKeyListener {
class CreateWorkspaceKeyListener : public MainWindowAwareKeyListener {
public:
explicit CreateWorkspaceKeyListener(SwayTreeModel *swayTreeModel, QTreeView *treeView) : swayTreeModel(
swayTreeModel), treeView(treeView) {}
explicit CreateWorkspaceKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
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)) {
cmd << "focus output " << output->node.name << " , ";
break;
}
}
const auto* selectedOutput = findSelectedValidOutput();
if (selectedOutput != nullptr)
cmd << "focus output " << selectedOutput->node.name << " , ";
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;
cmd << "workspace number " << createNewWorkspaceId();
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
QApplication::quit();
@ -52,9 +40,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_C;
}
private:
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
};
#endif //SWAYMUX_CREATEWORKSPACEKEYLISTENER_H

View File

@ -0,0 +1,69 @@
//
// 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

View File

@ -11,20 +11,12 @@
#include "../sway_bindings/Formatter.h"
#include "../tree/SwayTreeModel.h"
class MoveToScratchpadKeyListener : public AbstractKeyListener {
class MoveToScratchpadKeyListener : public MainWindowAwareKeyListener {
public:
explicit MoveToScratchpadKeyListener(MainWindow *pWindow) : window(pWindow) {}
explicit MoveToScratchpadKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto selectedNodes = window->model->getSelectedNodes(window->ui->treeView);
std::set<int> containersToScratch;
for (auto* container: selectedNodes) {
auto containers = container->accumulateContainers();
for (auto* containerToMove : container->accumulateContainers()) {
containersToScratch.insert(containerToMove->node.id);
}
}
std::set<int> containersToScratch = getSelectedContainerIds();
if (containersToScratch.empty())
return;
@ -38,8 +30,7 @@ public:
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
usleep(100000); // give sway time to react. Not great but works i guess.
window->updateModel();
updateMainWindow();
}
std::string getDescription() override {
@ -53,8 +44,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Minus;
}
private:
MainWindow *window;
};

View File

@ -11,39 +11,28 @@
#include "../sway_bindings/Formatter.h"
#include "../tree/SwayTreeModel.h"
class RestoreKeyListener : public AbstractKeyListener {
class RestoreKeyListener : public MainWindowAwareKeyListener {
public:
explicit RestoreKeyListener(MainWindow *pWindow) : window(pWindow) {}
explicit RestoreKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto selectedNodes = window->model->getSelectedNodes(window->ui->treeView);
std::set<int> containersToRestore = getSelectedContainerIds();
std::set<int> containersToScratch;
for (auto* container: selectedNodes) {
auto containers = container->accumulateContainers();
for (auto* containerToMove : container->accumulateContainers()) {
containersToScratch.insert(containerToMove->node.id);
}
}
if (containersToScratch.empty())
if (containersToRestore.empty())
return;
Formatter cmd;
auto used_workspaces = window->model->getRoot()->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) {}
int newWorkspaceId = createNewWorkspaceId();
for (auto id: containersToScratch) {
cmd << "[con_id=" << id << "] move workspace " << i << " ; ";
for (auto id: containersToRestore) {
cmd << "[con_id=" << id << "] move workspace " << newWorkspaceId << " ; ";
}
std::cout << cmd.str() << "\n";
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n";
usleep(100000); // give sway time to react. Not great but works i guess.
window->updateModel();
updateMainWindow();
}
std::string getDescription() override {
@ -57,8 +46,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Plus;
}
private:
MainWindow *window;
};

View File

@ -27,7 +27,7 @@ MainWindow::MainWindow(QWidget *parent)
swayTreeKeyHandler = new KeyHandler();
swayTreeKeyHandler->addListener(new HelpKeyListener(ui->help_page));
swayTreeKeyHandler->addListener(new CloseSwaymuxKeyListener());
swayTreeKeyHandler->addListener(new CreateWorkspaceKeyListener(model, ui->treeView));
swayTreeKeyHandler->addListener(new CreateWorkspaceKeyListener(this));
swayTreeKeyHandler->addListener(new SwitchToKeybindListener(model, ui->treeView));
swayTreeKeyHandler->addListener(new PrevOutputKeyListener(model, ui->treeView));
swayTreeKeyHandler->addListener(new NextOutputKeyListener(model, ui->treeView));