Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard integration for Streamlink quality confirmation #3169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/util/StreamLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void openStreamlinkForChannel(const QString &channel)
if (preferredQuality == "choose")
{
getStreamQualities(channelURL, [=](QStringList qualityOptions) {
QualityPopup::showDialog(channel, qualityOptions);
QualityPopup::showDialog(channelURL, qualityOptions);
});

return;
Expand Down
64 changes: 64 additions & 0 deletions src/widgets/BasePopup.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "widgets/BasePopup.hpp"

#include <QAbstractButton>
#include <QDialogButtonBox>
#include <QKeyEvent>

namespace chatterino {
Expand All @@ -20,4 +22,66 @@ void BasePopup::keyPressEvent(QKeyEvent *e)
BaseWindow::keyPressEvent(e);
}

bool BasePopup::handleEscape(QKeyEvent *e, QDialogButtonBox *buttonBox)
{
assert(buttonBox != nullptr);

if (e->key() == Qt::Key_Escape)
{
auto buttons = buttonBox->buttons();
for (auto *button : buttons)
{
if (auto role = buttonBox->buttonRole(button);
role == QDialogButtonBox::ButtonRole::RejectRole)
{
button->click();
return true;
}
}
}

return false;
}

bool BasePopup::handleEnter(QKeyEvent *e, QDialogButtonBox *buttonBox)
{
assert(buttonBox != nullptr);

if (!e->modifiers() ||
(e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter))
{
switch (e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return: {
auto buttons = buttonBox->buttons();
QAbstractButton *acceptButton = nullptr;
for (auto *button : buttons)
{
if (button->hasFocus())
{
button->click();
return true;
}

if (auto role = buttonBox->buttonRole(button);
role == QDialogButtonBox::ButtonRole::AcceptRole)
{
acceptButton = button;
}
}

if (acceptButton != nullptr)
{
acceptButton->click();
return true;
}
}
break;
}
}

return false;
}

} // namespace chatterino
8 changes: 8 additions & 0 deletions src/widgets/BasePopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "common/FlagsEnum.hpp"
#include "widgets/BaseWindow.hpp"

class QDialogButtonBox;

namespace chatterino {

class BasePopup : public BaseWindow
Expand All @@ -13,6 +15,12 @@ class BasePopup : public BaseWindow

protected:
void keyPressEvent(QKeyEvent *e) override;

// handleEscape is a helper function for clicking the "Reject" role button of a button box when the Escape button is pressed
bool handleEscape(QKeyEvent *e, QDialogButtonBox *buttonBox);

// handleEnter is a helper function for clicking the "Accept" role button of a button box when Return or Enter is pressed
bool handleEnter(QKeyEvent *e, QDialogButtonBox *buttonBox);
};

} // namespace chatterino
50 changes: 29 additions & 21 deletions src/widgets/dialogs/QualityPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,60 @@

namespace chatterino {

QualityPopup::QualityPopup(const QString &_channelName, QStringList options)
QualityPopup::QualityPopup(const QString &channelURL, QStringList options)
: BasePopup({},
static_cast<QWidget *>(&(getApp()->windows->getMainWindow())))
, channelName_(_channelName)
, channelURL_(channelURL)
{
this->ui_.okButton.setText("OK");
this->ui_.cancelButton.setText("Cancel");
this->ui_.selector = new QComboBox(this);
this->ui_.vbox = new QVBoxLayout(this);
this->ui_.buttonBox = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);

QObject::connect(&this->ui_.okButton, &QPushButton::clicked, this,
QObject::connect(this->ui_.buttonBox, &QDialogButtonBox::accepted, this,
&QualityPopup::okButtonClicked);
QObject::connect(&this->ui_.cancelButton, &QPushButton::clicked, this,
QObject::connect(this->ui_.buttonBox, &QDialogButtonBox::rejected, this,
&QualityPopup::cancelButtonClicked);

this->ui_.buttonBox.addButton(&this->ui_.okButton,
QDialogButtonBox::ButtonRole::AcceptRole);
this->ui_.buttonBox.addButton(&this->ui_.cancelButton,
QDialogButtonBox::ButtonRole::RejectRole);
this->ui_.selector->addItems(options);

this->ui_.selector.addItems(options);
this->ui_.vbox->addWidget(this->ui_.selector);
this->ui_.vbox->addWidget(this->ui_.buttonBox);

this->ui_.vbox.addWidget(&this->ui_.selector);
this->ui_.vbox.addWidget(&this->ui_.buttonBox);

this->setLayout(&this->ui_.vbox);
this->setLayout(this->ui_.vbox);
}

void QualityPopup::showDialog(const QString &channelName, QStringList options)
void QualityPopup::showDialog(const QString &channelURL, QStringList options)
{
QualityPopup *instance = new QualityPopup(channelName, options);
QualityPopup *instance = new QualityPopup(channelURL, options);

instance->window()->setWindowTitle("Chatterino - select stream quality");
instance->setAttribute(Qt::WA_DeleteOnClose, true);

instance->show();
instance->activateWindow();
instance->raise();
instance->setFocus();
}

void QualityPopup::okButtonClicked()
void QualityPopup::keyPressEvent(QKeyEvent *e)
{
QString channelURL = "twitch.tv/" + this->channelName_;
if (this->handleEscape(e, this->ui_.buttonBox))
{
return;
}
if (this->handleEnter(e, this->ui_.buttonBox))
{
return;
}

BasePopup::keyPressEvent(e);
}

void QualityPopup::okButtonClicked()
{
try
{
openStreamlink(channelURL, this->ui_.selector.currentText());
openStreamlink(this->channelURL_, this->ui_.selector->currentText());
}
catch (const Exception &ex)
{
Expand Down
18 changes: 9 additions & 9 deletions src/widgets/dialogs/QualityPopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

#include <QComboBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>

namespace chatterino {

class QualityPopup : public BasePopup
{
public:
QualityPopup(const QString &_channelName, QStringList options);
static void showDialog(const QString &_channelName, QStringList options);
QualityPopup(const QString &channelURL, QStringList options);
static void showDialog(const QString &channelURL, QStringList options);

protected:
void keyPressEvent(QKeyEvent *e) override;

private:
void okButtonClicked();
void cancelButtonClicked();

struct {
QVBoxLayout vbox;
QComboBox selector;
QDialogButtonBox buttonBox;
QPushButton okButton;
QPushButton cancelButton;
QVBoxLayout *vbox;
QComboBox *selector;
QDialogButtonBox *buttonBox;
} ui_;

QString channelName_;
QString channelURL_;
};

} // namespace chatterino