close keybind, output selection validation

This commit is contained in:
LordGrimmauld 2024-03-08 13:13:28 +01:00
parent 1339fb3148
commit 7a669fee4a
10 changed files with 136 additions and 19 deletions

View file

@ -36,6 +36,9 @@ set(PROJECT_SOURCES
Keys/HelpKeyListener.h
Keys/CloseHelpKeyListener.h
Keys/CreateWorkspaceKeyListener.h
Keys/CloseSwaymuxKeyListener.h
Keys/CloseSwaymuxKeyListener.h
sway_bindings/swayoutputs.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

View file

@ -0,0 +1,29 @@
//
// Created by grimmauld on 08.03.24.
//
#ifndef SWAYMUX_CLOSESWAYMUXKEYLISTENER_H
#define SWAYMUX_CLOSESWAYMUXKEYLISTENER_H
#include "AbstractKeyListener.h"
class CloseSwaymuxKeyListener : public AbstractKeyListener {
public:
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
QApplication::quit();
}
std::string getDescription() override {
return "Exit Swaymux";
}
std::string getKeyText() override {
return "ESC";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Escape;
}
};
#endif //SWAYMUX_CLOSESWAYMUXKEYLISTENER_H

View file

@ -9,6 +9,7 @@
#include <iostream>
#include "AbstractKeyListener.h"
#include "../tree/SwayTreeModel.h"
#include "../sway_bindings/swayoutputs.h"
class CreateWorkspaceKeyListener : public AbstractKeyListener {
public:
@ -21,27 +22,14 @@ public:
return;
Formatter cmd;
auto indexes = treeView->selectionModel()->selectedIndexes();
for (auto i: indexes) {
std::vector<int> indices;
for (auto j = i; j.isValid(); j = j.parent()) {
indices.insert(indices.cbegin(), j.row());
}
auto *head = root;
const SwayTreeNode *output;
for (auto j: indices) {
head = head->child(j);
if (head == nullptr)
goto hell;
}
output = head->findOutput();
if (output != nullptr) {
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;
}
hell:;
}
auto used_workspaces = root->accumulateWorkspaces();
@ -50,6 +38,7 @@ public:
cmd << "workspace number " << i;
auto resp = SwayTreeModel::sway.sendIPC(swaymsg(0, cmd.str().c_str(), cmd.str().length()));
std::cout << resp.msg << "\n";
QApplication::quit();
}
std::string getDescription() override {

View file

@ -21,6 +21,12 @@ using json = nlohmann::json;
int main(int argc, char *argv[])
{
/*
auto sway = Sway();
auto resp = sway.sendIPC(SWAY_GET_OUTPUTS);
std::cout << resp.msg << "\n";
*/
QApplication a(argc, argv);
QCoreApplication::setOrganizationName("Grimmauld");
QCoreApplication::setApplicationName("SwayMux");

View file

@ -4,6 +4,7 @@
#include "Keys/HelpKeyListener.h"
#include "Keys/CloseHelpKeyListener.h"
#include "Keys/CreateWorkspaceKeyListener.h"
#include "Keys/CloseSwaymuxKeyListener.h"
#include <QTimer>
#include <iostream>
@ -29,6 +30,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));
closeHelpKeyHandler = new KeyHandler(ui->tree_page);

View file

@ -0,0 +1,58 @@
//
// Created by grimmauld on 08.03.24.
//
#ifndef SWAYMUX_SWAYOUTPUTS_H
#define SWAYMUX_SWAYOUTPUTS_H
#include <string>
#include <nlohmann/json.hpp>
#include "Sway.h"
#include "../tree/swaytree.h"
using json = nlohmann::json;
struct SwayOutput {
explicit SwayOutput() : id(0), name("none"), active(false), power(false) {};
explicit SwayOutput(const json &rep) :
id(rep["id"]),
name(rep["name"].is_null() ? "none" : rep["name"]),
active(rep["active"]),
power(rep["power"]) {};
const std::string name;
int id;
const bool active;
const bool power;
[[nodiscard]] bool isValid() const {
return active && power;
}
};
class SwayOutputList {
public:
explicit SwayOutputList(const json &rep) {
for (const auto &o: rep) {
outputs.emplace_back(o);
}
}
std::vector<SwayOutput> outputs;
explicit SwayOutputList(const Sway &sway) : SwayOutputList(json::parse(sway.sendIPC(SWAY_GET_OUTPUTS).msg)) {};
[[nodiscard]] bool isOutputValid(const SwayTreeNode *test) {
if (test == nullptr)
return false;
if (test->node.type != NodeType::output)
return false;
return std::ranges::any_of(outputs.begin(), outputs.end(), [test](const auto &o) {
return o.name == test->node.name && o.isValid();
});
}
};
#endif //SWAYMUX_SWAYOUTPUTS_H

View file

@ -18,3 +18,29 @@ QModelIndex SwayTreeModel::findFocusedWindowIndex() const {
return idx;
}
std::set<SwayTreeNode *> SwayTreeModel::getSelectedNodes(const QTreeView *treeView) const {
std::set<SwayTreeNode *> selectedNodes;
auto indexes = treeView->selectionModel()->selectedIndexes();
for (auto i: indexes) {
if (rootItem == nullptr)
return selectedNodes;
std::vector<int> indices;
for (auto j = i; j.isValid(); j = j.parent()) {
indices.insert(indices.cbegin(), j.row());
}
auto *head = rootItem;
for (auto j: indices) {
head = head->child(j);
if (head == nullptr)
goto hell;
}
selectedNodes.insert(head);
hell:;
}
return selectedNodes;
}

View file

@ -14,6 +14,7 @@
#include <nlohmann/json.hpp>
#include <utility>
#include <QTreeView>
using json = nlohmann::json;
@ -44,6 +45,8 @@ public:
[[nodiscard]] QModelIndex findFocusedWindowIndex() const;
[[nodiscard]] std::set<SwayTreeNode*> getSelectedNodes(const QTreeView* treeView) const;
private:
SwayTreeNode * rootItem;

View file

@ -10,5 +10,5 @@ QVariant SwayTreeNode::data(int column) const {
}
QVariant SwayTreeNode::headerData(int column) const {
return "Title";
return "Swaymux";
}

View file

@ -11,6 +11,7 @@
using json = nlohmann::json;
#include "AbstractTreeNode.h"
#include "../sway_bindings/Sway.h"
namespace NodeType {
enum NodeType {