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/MoveToScratchpadKeyListener.h Keys/MoveToScratchpadKeyListener.h
Keys/RestoreKeyListener.h Keys/RestoreKeyListener.h
Keys/MainWindowAwareKeyListener.h
) )
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

View File

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

View File

@ -10,32 +10,20 @@
#include "AbstractKeyListener.h" #include "AbstractKeyListener.h"
#include "../tree/SwayTreeModel.h" #include "../tree/SwayTreeModel.h"
#include "../sway_bindings/swayoutputs.h" #include "../sway_bindings/swayoutputs.h"
#include "MainWindowAwareKeyListener.h"
class CreateWorkspaceKeyListener : public AbstractKeyListener { class CreateWorkspaceKeyListener : public MainWindowAwareKeyListener {
public: public:
explicit CreateWorkspaceKeyListener(SwayTreeModel *swayTreeModel, QTreeView *treeView) : swayTreeModel( explicit CreateWorkspaceKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
swayTreeModel), treeView(treeView) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override { void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto *root = swayTreeModel->getRoot();
if (root == nullptr)
return;
Formatter cmd; Formatter cmd;
auto selectedNodes = swayTreeModel->getSelectedNodes(treeView); const auto* selectedOutput = findSelectedValidOutput();
auto availableOutputs = SwayOutputList(SwayTreeModel::sway); if (selectedOutput != nullptr)
for (auto node: selectedNodes) { cmd << "focus output " << selectedOutput->node.name << " , ";
auto output = node->findOutput();
if (availableOutputs.isOutputValid(output)) {
cmd << "focus output " << output->node.name << " , ";
break;
}
}
auto used_workspaces = root->accumulateWorkspaces(); cmd << "workspace number " << createNewWorkspaceId();
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;
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str())); auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n"; std::cout << resp.msg << "\n";
QApplication::quit(); QApplication::quit();
@ -52,9 +40,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override { [[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_C; return key == Qt::Key_C;
} }
private:
SwayTreeModel *swayTreeModel;
QTreeView *treeView;
}; };
#endif //SWAYMUX_CREATEWORKSPACEKEYLISTENER_H #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 "../sway_bindings/Formatter.h"
#include "../tree/SwayTreeModel.h" #include "../tree/SwayTreeModel.h"
class MoveToScratchpadKeyListener : public AbstractKeyListener { class MoveToScratchpadKeyListener : public MainWindowAwareKeyListener {
public: public:
explicit MoveToScratchpadKeyListener(MainWindow *pWindow) : window(pWindow) {} explicit MoveToScratchpadKeyListener(MainWindow *pWindow) : MainWindowAwareKeyListener(pWindow) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override { void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto selectedNodes = window->model->getSelectedNodes(window->ui->treeView); std::set<int> containersToScratch = 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 (containersToScratch.empty())
return; return;
@ -38,8 +30,7 @@ public:
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str())); auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str()));
std::cout << resp.msg << "\n"; std::cout << resp.msg << "\n";
usleep(100000); // give sway time to react. Not great but works i guess. updateMainWindow();
window->updateModel();
} }
std::string getDescription() override { std::string getDescription() override {
@ -53,8 +44,6 @@ public:
[[nodiscard]] bool canAcceptKey(int key) const override { [[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Minus; return key == Qt::Key_Minus;
} }
private:
MainWindow *window;
}; };

View File

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

View File

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