Implement the "inline" account switch button

Make the account switch listview into a widget
This commit is contained in:
Rasmus Karlsson 2017-12-19 02:16:01 +01:00
parent 4010c49c84
commit 1a870685b0
9 changed files with 223 additions and 47 deletions

View file

@ -99,7 +99,9 @@ SOURCES += \
src/widgets/splitcontainer.cpp \
src/widgets/helper/droppreview.cpp \
src/widgets/window.cpp \
src/widgets/helper/splitcolumn.cpp
src/widgets/helper/splitcolumn.cpp \
src/widgets/accountswitchwidget.cpp \
src/widgets/accountswitchpopupwidget.cpp
HEADERS += \
src/asyncexec.hpp \
@ -171,7 +173,9 @@ HEADERS += \
src/widgets/helper/droppreview.hpp \
src/widgets/helper/splitcolumn.hpp \
src/util/irchelpers.hpp \
src/util/helpers.hpp
src/util/helpers.hpp \
src/widgets/accountswitchwidget.hpp \
src/widgets/accountswitchpopupwidget.hpp
PRECOMPILED_HEADER =

View file

@ -0,0 +1,51 @@
#include "widgets/accountswitchpopupwidget.hpp"
#include "debug/log.hpp"
#include "widgets/settingsdialog.hpp"
#include <QHBoxLayout>
#include <QLayout>
#include <QPushButton>
#include <QVBoxLayout>
namespace chatterino {
namespace widgets {
AccountSwitchPopupWidget::AccountSwitchPopupWidget(QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint);
this->setContentsMargins(0, 0, 0, 0);
this->ui.accountSwitchWidget = new AccountSwitchWidget(this);
QVBoxLayout *vbox = new QVBoxLayout(this);
this->ui.accountSwitchWidget->setFocusPolicy(Qt::NoFocus);
vbox->addWidget(this->ui.accountSwitchWidget);
// vbox->setSizeConstraint(QLayout::SetMinimumSize);
auto hbox = new QHBoxLayout();
auto manageAccountsButton = new QPushButton(this);
manageAccountsButton->setText("Manage Accounts");
hbox->addWidget(manageAccountsButton);
vbox->addLayout(hbox);
connect(manageAccountsButton, &QPushButton::clicked, []() {
SettingsDialog::showDialog(SettingsDialog::PreferredTab::Accounts); //
});
this->setLayout(vbox);
}
void AccountSwitchPopupWidget::refresh()
{
this->ui.accountSwitchWidget->refresh();
}
void AccountSwitchPopupWidget::focusOutEvent(QFocusEvent *)
{
this->hide();
}
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,29 @@
#pragma once
#include "widgets/accountswitchwidget.hpp"
#include <QWidget>
namespace chatterino {
namespace widgets {
class AccountSwitchPopupWidget : public QWidget
{
Q_OBJECT
public:
AccountSwitchPopupWidget(QWidget *parent = nullptr);
void refresh();
protected:
virtual void focusOutEvent(QFocusEvent *event) override final;
private:
struct {
AccountSwitchWidget *accountSwitchWidget = nullptr;
} ui;
};
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,64 @@
#include "accountswitchwidget.hpp"
#include "accountmanager.hpp"
namespace chatterino {
namespace widgets {
AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
: QListWidget(parent)
{
static QString anonUsername(" - anonymous - ");
this->addItem(anonUsername);
for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) {
this->addItem(userName);
}
this->refreshSelection();
QObject::connect(this, &QListWidget::clicked, [this] {
if (!this->selectedItems().isEmpty()) {
QString newUsername = this->currentItem()->text();
if (newUsername.compare(anonUsername, Qt::CaseInsensitive) == 0) {
AccountManager::getInstance().Twitch.currentUsername = "";
} else {
AccountManager::getInstance().Twitch.currentUsername = newUsername.toStdString();
}
}
});
}
void AccountSwitchWidget::refresh()
{
this->refreshSelection();
}
void AccountSwitchWidget::refreshSelection()
{
this->blockSignals(true);
// Select the currently logged in user
if (this->count() > 0) {
auto currentUser = AccountManager::getInstance().Twitch.getCurrent();
if (currentUser->isAnon()) {
this->setCurrentRow(0);
} else {
const QString &currentUsername = currentUser->getUserName();
for (int i = 0; i < this->count(); ++i) {
QString itemText = this->item(i)->text();
if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0) {
this->setCurrentRow(i);
break;
}
}
}
}
this->blockSignals(false);
}
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,22 @@
#pragma once
#include <QListWidget>
namespace chatterino {
namespace widgets {
class AccountSwitchWidget : public QListWidget
{
Q_OBJECT
public:
explicit AccountSwitchWidget(QWidget *parent = nullptr);
void refresh();
private:
void refreshSelection();
};
} // namespace widgets
} // namespace chatterino

View file

@ -113,8 +113,9 @@ void SplitHeader::updateChannelText()
"<br>"
"Live for " +
twitchChannel->streamUptime + " with " +
twitchChannel->streamViewerCount + " viewers"
"</p>");
twitchChannel->streamViewerCount +
" viewers"
"</p>");
} else {
this->channelNameLabel.setText(QString::fromStdString(channelName));
this->setToolTip("");

View file

@ -1,5 +1,7 @@
#include "widgets/notebook.hpp"
#include "colorscheme.hpp"
#include "debug/log.hpp"
#include "widgets/accountswitchpopupwidget.hpp"
#include "widgets/helper/notebookbutton.hpp"
#include "widgets/helper/notebooktab.hpp"
#include "widgets/settingsdialog.hpp"
@ -227,6 +229,26 @@ void Notebook::settingsButtonClicked()
void Notebook::usersButtonClicked()
{
static QWidget *lastFocusedWidget = nullptr;
static AccountSwitchPopupWidget *w = new AccountSwitchPopupWidget(this);
if (w->hasFocus()) {
w->hide();
if (lastFocusedWidget) {
lastFocusedWidget->setFocus();
}
return;
}
lastFocusedWidget = this->focusWidget();
w->refresh();
QPoint buttonPos = this->userButton.rect().bottomRight();
w->move(buttonPos.x(), buttonPos.y());
w->show();
w->setFocus();
}
void Notebook::addPageButtonClicked()

View file

@ -126,48 +126,9 @@ QVBoxLayout *SettingsDialog::createAccountsTab()
layout->addWidget(buttonBox);
// listview
auto listWidget = new QListWidget(this);
this->ui.accountSwitchWidget = new AccountSwitchWidget(this);
static QString anonUsername(" - anonymous - ");
listWidget->addItem(anonUsername);
for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) {
listWidget->addItem(userName);
}
// Select the currently logged in user
if (listWidget->count() > 0) {
auto currentUser = AccountManager::getInstance().Twitch.getCurrent();
if (currentUser->isAnon()) {
listWidget->setCurrentRow(0);
} else {
const QString &currentUsername = currentUser->getUserName();
for (int i = 0; i < listWidget->count(); ++i) {
QString itemText = listWidget->item(i)->text();
if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0) {
listWidget->setCurrentRow(i);
break;
}
}
}
}
QObject::connect(listWidget, &QListWidget::clicked, this, [&, listWidget] {
if (!listWidget->selectedItems().isEmpty()) {
QString newUsername = listWidget->currentItem()->text();
if (newUsername.compare(anonUsername, Qt::CaseInsensitive) == 0) {
AccountManager::getInstance().Twitch.currentUsername = "";
} else {
AccountManager::getInstance().Twitch.currentUsername = newUsername.toStdString();
}
}
});
layout->addWidget(listWidget);
layout->addWidget(this->ui.accountSwitchWidget);
return layout;
}
@ -618,9 +579,16 @@ void SettingsDialog::select(SettingsDialogTab *tab)
this->selectedTab = tab;
}
void SettingsDialog::showDialog()
void SettingsDialog::showDialog(PreferredTab preferredTab)
{
static SettingsDialog *instance = new SettingsDialog();
instance->refresh();
switch (preferredTab) {
case SettingsDialog::PreferredTab::Accounts: {
instance->select(instance->tabs.at(0));
} break;
}
instance->show();
instance->activateWindow();
@ -628,6 +596,11 @@ void SettingsDialog::showDialog()
instance->setFocus();
}
void SettingsDialog::refresh()
{
this->ui.accountSwitchWidget->refresh();
}
void SettingsDialog::dpiMultiplierChanged(float oldDpi, float newDpi)
{
QFile file(":/qss/settings.qss");

View file

@ -2,6 +2,7 @@
#include "settingsmanager.hpp"
#include "settingssnapshot.hpp"
#include "widgets/accountswitchwidget.hpp"
#include "widgets/helper/settingsdialogtab.hpp"
#include <QButtonGroup>
@ -31,12 +32,19 @@ public:
void select(SettingsDialogTab *tab);
static void showDialog();
enum class PreferredTab {
NoPreference,
Accounts,
};
static void showDialog(PreferredTab preferredTab = PreferredTab::NoPreference);
protected:
virtual void dpiMultiplierChanged(float oldDpi, float newDpi) override;
private:
void refresh();
SettingsSnapshot snapshot;
std::vector<SettingsDialogTab *> tabs;
@ -51,6 +59,8 @@ private:
QDialogButtonBox buttonBox;
QPushButton okButton;
QPushButton cancelButton;
AccountSwitchWidget *accountSwitchWidget = nullptr;
} ui;
void addTab(QBoxLayout *layout, QString title, QString imageRes);