Skip to content

Commit

Permalink
Add keybindings for switching focus
Browse files Browse the repository at this point in the history
  • Loading branch information
falbru committed Mar 26, 2024
1 parent 0b35a9e commit 2f83892
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ qt_add_executable(kak-qt
src/rpc/color.cpp
src/rpc/attribute.hpp
src/rpc/rpc.cpp
src/keybindings.cpp
src/drawoptions.cpp
src/kakounesession.cpp
src/kakouneclient.cpp
Expand Down
5 changes: 5 additions & 0 deletions src/kakounewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ void KakouneWidget::clientRefreshed()
repaint();
emit refresh();
}

void KakouneWidget::installEventFilter(QObject *filter)
{
m_textedit->installEventFilter(filter);
}
2 changes: 2 additions & 0 deletions src/kakounewidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class KakouneWidget : public QWidget
QWidget *parent = nullptr);
~KakouneWidget();

void installEventFilter(QObject *filter);

QUuid getID();
KakouneClient *getClient();

Expand Down
29 changes: 29 additions & 0 deletions src/keybindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "keybindings.hpp"

QList<KeyBinding> keybindings = {
{QKeyCombination(Qt::ControlModifier | Qt::ShiftModifier, Qt::Key_H),
[](MainWindow *window) { window->focusLeft(); }},
{QKeyCombination(Qt::ControlModifier | Qt::ShiftModifier, Qt::Key_L),
[](MainWindow *window) { window->focusRight(); }},
};

KeyBindingsFilter::KeyBindingsFilter(MainWindow *main_window) : m_main_window(main_window)
{
}

bool KeyBindingsFilter::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
for (const KeyBinding &binding : keybindings)
{
if (binding.keyCombination == keyEvent->keyCombination())
{
binding.action(m_main_window);
return true;
}
}
}
return false;
}
26 changes: 26 additions & 0 deletions src/keybindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef KEYBINDINGS_HPP
#define KEYBINDINGS_HPP

#include "mainwindow.hpp"
#include <qobject.h>

struct KeyBinding
{
QKeyCombination keyCombination;
std::function<void(MainWindow *)> action;
};

class KeyBindingsFilter : public QObject
{
Q_OBJECT
public:
KeyBindingsFilter(MainWindow *main_window);

protected:
bool eventFilter(QObject *object, QEvent *event);

private:
MainWindow *m_main_window;
};

#endif
28 changes: 27 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mainwindow.hpp"
#include "keybindings.hpp"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
Expand All @@ -9,7 +10,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)

m_session = new KakouneSession();

m_root = new QSplitter(parent);
m_root = new QSplitter(this);
this->newClient();

setCentralWidget(m_root);
Expand All @@ -32,6 +33,7 @@ void MainWindow::newClient()
void MainWindow::newClient(const QString &arguments)
{
KakouneWidget *kakwidget = new KakouneWidget(m_session->getSessionId(), m_draw_options, arguments, m_root);
kakwidget->installEventFilter(new KeyBindingsFilter(this));
connect(kakwidget, &KakouneWidget::finished, m_root, [=]() {
kakwidget->setParent(nullptr);
m_windows.removeOne(kakwidget);
Expand All @@ -57,3 +59,27 @@ void MainWindow::focusWindow(const QString &uuid)
}
}
}

void MainWindow::focusLeft()
{
QWidget *focused_widget = qApp->focusWidget();
int index = m_root->indexOf((QWidget *)focused_widget->parent()); // TODO
if (index <= 0)
{
return;
}

m_windows[index - 1]->setFocus();
}

void MainWindow::focusRight()
{
QWidget *focused_widget = qApp->focusWidget();
int index = m_root->indexOf((QWidget *)focused_widget->parent()); // TODO
if (index >= m_windows.size() - 1)
{
return;
}

m_windows[index + 1]->setFocus();
}
3 changes: 3 additions & 0 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void focusLeft();
void focusRight();

public slots:
void newClient();
void newClient(const QString &arguments);
Expand Down

0 comments on commit 2f83892

Please sign in to comment.