From db6d7c66b8e089cf4fa4650126703e3d34a3eaba Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Mon, 11 Mar 2024 17:11:26 +0100 Subject: [PATCH] Don't Repeat Yourself: pull out some common functionality --- CMakeLists.txt | 1 + Keys/CloseKeyListener.h | 20 +++------ Keys/CreateWorkspaceKeyListener.h | 29 +++---------- Keys/MainWindowAwareKeyListener.h | 69 ++++++++++++++++++++++++++++++ Keys/MoveToScratchpadKeyListener.h | 19 ++------ Keys/RestoreKeyListener.h | 29 ++++--------- mainwindow.cpp | 2 +- 7 files changed, 95 insertions(+), 74 deletions(-) create mode 100644 Keys/MainWindowAwareKeyListener.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 783b700..da1d46d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Keys/CloseKeyListener.h b/Keys/CloseKeyListener.h index 14be07a..84de6ef 100644 --- a/Keys/CloseKeyListener.h +++ b/Keys/CloseKeyListener.h @@ -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 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; }; diff --git a/Keys/CreateWorkspaceKeyListener.h b/Keys/CreateWorkspaceKeyListener.h index 962e444..e5b2401 100644 --- a/Keys/CreateWorkspaceKeyListener.h +++ b/Keys/CreateWorkspaceKeyListener.h @@ -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 diff --git a/Keys/MainWindowAwareKeyListener.h b/Keys/MainWindowAwareKeyListener.h new file mode 100644 index 0000000..bc316d2 --- /dev/null +++ b/Keys/MainWindowAwareKeyListener.h @@ -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 getSelectedContainerIds() const { + std::set 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 diff --git a/Keys/MoveToScratchpadKeyListener.h b/Keys/MoveToScratchpadKeyListener.h index 9fbcce4..92e5ff6 100644 --- a/Keys/MoveToScratchpadKeyListener.h +++ b/Keys/MoveToScratchpadKeyListener.h @@ -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 containersToScratch; - for (auto* container: selectedNodes) { - auto containers = container->accumulateContainers(); - for (auto* containerToMove : container->accumulateContainers()) { - containersToScratch.insert(containerToMove->node.id); - } - } + std::set 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; }; diff --git a/Keys/RestoreKeyListener.h b/Keys/RestoreKeyListener.h index 2c2be58..00116fd 100644 --- a/Keys/RestoreKeyListener.h +++ b/Keys/RestoreKeyListener.h @@ -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 containersToRestore = getSelectedContainerIds(); - std::set 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; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index aba575b..7d541bc 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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));