Add custom hotkeys. (#2340)

Co-authored-by: LosFarmosCTL <80157503+LosFarmosCTL@users.noreply.github.com>
Co-authored-by: Paweł <zneix@zneix.eu>
Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Mm2PL
2021-11-21 17:46:21 +00:00
committed by GitHub
parent b94e21a600
commit 703f3717e2
54 changed files with 3613 additions and 617 deletions
+93 -39
View File
@@ -1,4 +1,5 @@
#include "GenericListView.hpp"
#include "widgets/listview/GenericListView.hpp"
#include "singletons/Theme.hpp"
#include "widgets/listview/GenericListModel.hpp"
@@ -18,7 +19,7 @@ GenericListView::GenericListView()
auto *item = GenericListItem::fromVariant(index.data());
item->action();
emit this->closeRequested();
this->requestClose();
});
}
@@ -42,64 +43,58 @@ void GenericListView::setInvokeActionOnTab(bool value)
bool GenericListView::eventFilter(QObject * /*watched*/, QEvent *event)
{
if (!this->model_)
if (this->model_ == nullptr)
{
return false;
}
if (event->type() == QEvent::KeyPress)
{
auto *keyEvent = static_cast<QKeyEvent *>(event);
int key = keyEvent->key();
const QModelIndex &curIdx = this->currentIndex();
const int curRow = curIdx.row();
const int count = this->model_->rowCount(curIdx);
if (key == Qt::Key_Enter || key == Qt::Key_Return ||
(key == Qt::Key_Tab && this->invokeActionOnTab_))
if (key == Qt::Key_Enter || key == Qt::Key_Return)
{
// keep this before the other tab handler
if (count <= 0)
return true;
const auto index = this->currentIndex();
auto *item = GenericListItem::fromVariant(index.data());
item->action();
emit this->closeRequested();
this->acceptCompletion();
return true;
}
else if (key == Qt::Key_Down || key == Qt::Key_Tab)
if (key == Qt::Key_Tab)
{
if (count <= 0)
return true;
if (this->invokeActionOnTab_)
{
this->acceptCompletion();
}
else
{
this->focusNextCompletion();
}
const int newRow = (curRow + 1) % count;
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
return true;
}
else if (key == Qt::Key_Up ||
(!this->invokeActionOnTab_ && key == Qt::Key_Backtab))
if (key == Qt::Key_Backtab && !this->invokeActionOnTab_)
{
if (count <= 0)
return true;
int newRow = curRow - 1;
if (newRow < 0)
newRow += count;
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
this->focusPreviousCompletion();
return true;
}
else if (key == Qt::Key_Escape)
if (key == Qt::Key_Down)
{
emit this->closeRequested();
this->focusNextCompletion();
return true;
}
else
if (key == Qt::Key_Up)
{
return false;
this->focusPreviousCompletion();
return true;
}
if (key == Qt::Key_Escape)
{
this->requestClose();
return true;
}
}
@@ -126,4 +121,63 @@ void GenericListView::refreshTheme(const Theme &theme)
this->setStyleSheet(listStyle);
}
bool GenericListView::acceptCompletion()
{
const QModelIndex &curIdx = this->currentIndex();
const int curRow = curIdx.row();
const int count = this->model_->rowCount(curIdx);
if (count <= 0)
{
return false;
}
const auto index = this->currentIndex();
auto *item = GenericListItem::fromVariant(index.data());
item->action();
this->requestClose();
return true;
}
void GenericListView::focusNextCompletion()
{
const QModelIndex &curIdx = this->currentIndex();
const int curRow = curIdx.row();
const int count = this->model_->rowCount(curIdx);
if (count <= 0)
{
return;
}
const int newRow = (curRow + 1) % count;
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
}
void GenericListView::focusPreviousCompletion()
{
const QModelIndex &curIdx = this->currentIndex();
const int curRow = curIdx.row();
const int count = this->model_->rowCount(curIdx);
if (count <= 0)
{
return;
}
int newRow = curRow - 1;
if (newRow < 0)
{
newRow += count;
}
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
}
void GenericListView::requestClose()
{
emit this->closeRequested();
}
} // namespace chatterino
+24 -1
View File
@@ -1,9 +1,10 @@
#pragma once
#include <QListView>
#include "widgets/listview/GenericItemDelegate.hpp"
#include "widgets/listview/GenericListItem.hpp"
#include <QListView>
namespace chatterino {
class GenericListModel;
@@ -31,6 +32,28 @@ signals:
private:
bool invokeActionOnTab_{};
/**
* @brief Gets the currently selected item (if any) and calls its action
*
* @return true if an action was called on an item, false if no item was selected and thus no action was called
**/
bool acceptCompletion();
/**
* @brief Select the next item in the list. Wraps around if the bottom of the list has been reached.
**/
void focusNextCompletion();
/**
* @brief Select the previous item in the list. Wraps around if the top of the list has been reached.
**/
void focusPreviousCompletion();
/**
* @brief Request for the GUI powering this list view to be closed. Shorthand for emit this->closeRequested()
**/
void requestClose();
};
} // namespace chatterino