From 1a870685b0f2f1277e598602040b7778b48c4f64 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Tue, 19 Dec 2017 02:16:01 +0100 Subject: [PATCH] Implement the "inline" account switch button Make the account switch listview into a widget --- chatterino.pro | 8 ++- src/widgets/accountswitchpopupwidget.cpp | 51 +++++++++++++++++++ src/widgets/accountswitchpopupwidget.hpp | 29 +++++++++++ src/widgets/accountswitchwidget.cpp | 64 ++++++++++++++++++++++++ src/widgets/accountswitchwidget.hpp | 22 ++++++++ src/widgets/helper/splitheader.cpp | 5 +- src/widgets/notebook.cpp | 22 ++++++++ src/widgets/settingsdialog.cpp | 57 ++++++--------------- src/widgets/settingsdialog.hpp | 12 ++++- 9 files changed, 223 insertions(+), 47 deletions(-) create mode 100644 src/widgets/accountswitchpopupwidget.cpp create mode 100644 src/widgets/accountswitchpopupwidget.hpp create mode 100644 src/widgets/accountswitchwidget.cpp create mode 100644 src/widgets/accountswitchwidget.hpp diff --git a/chatterino.pro b/chatterino.pro index 068fdf9e1..88f4f474c 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -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 = diff --git a/src/widgets/accountswitchpopupwidget.cpp b/src/widgets/accountswitchpopupwidget.cpp new file mode 100644 index 000000000..7e0ac6a51 --- /dev/null +++ b/src/widgets/accountswitchpopupwidget.cpp @@ -0,0 +1,51 @@ +#include "widgets/accountswitchpopupwidget.hpp" +#include "debug/log.hpp" +#include "widgets/settingsdialog.hpp" + +#include +#include +#include +#include + +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 diff --git a/src/widgets/accountswitchpopupwidget.hpp b/src/widgets/accountswitchpopupwidget.hpp new file mode 100644 index 000000000..cb252f823 --- /dev/null +++ b/src/widgets/accountswitchpopupwidget.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "widgets/accountswitchwidget.hpp" + +#include + +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 diff --git a/src/widgets/accountswitchwidget.cpp b/src/widgets/accountswitchwidget.cpp new file mode 100644 index 000000000..6404d09a9 --- /dev/null +++ b/src/widgets/accountswitchwidget.cpp @@ -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 ¤tUsername = 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 diff --git a/src/widgets/accountswitchwidget.hpp b/src/widgets/accountswitchwidget.hpp new file mode 100644 index 000000000..14a29a020 --- /dev/null +++ b/src/widgets/accountswitchwidget.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +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 diff --git a/src/widgets/helper/splitheader.cpp b/src/widgets/helper/splitheader.cpp index 97f1aad0a..2566e5f63 100644 --- a/src/widgets/helper/splitheader.cpp +++ b/src/widgets/helper/splitheader.cpp @@ -113,8 +113,9 @@ void SplitHeader::updateChannelText() "
" "Live for " + twitchChannel->streamUptime + " with " + - twitchChannel->streamViewerCount + " viewers" - "

"); + twitchChannel->streamViewerCount + + " viewers" + "

"); } else { this->channelNameLabel.setText(QString::fromStdString(channelName)); this->setToolTip(""); diff --git a/src/widgets/notebook.cpp b/src/widgets/notebook.cpp index b29eb8c4a..16b3fda0a 100644 --- a/src/widgets/notebook.cpp +++ b/src/widgets/notebook.cpp @@ -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() diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 44d38fd31..88e90f45d 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -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 ¤tUsername = 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"); diff --git a/src/widgets/settingsdialog.hpp b/src/widgets/settingsdialog.hpp index b06b85d66..d242a5998 100644 --- a/src/widgets/settingsdialog.hpp +++ b/src/widgets/settingsdialog.hpp @@ -2,6 +2,7 @@ #include "settingsmanager.hpp" #include "settingssnapshot.hpp" +#include "widgets/accountswitchwidget.hpp" #include "widgets/helper/settingsdialogtab.hpp" #include @@ -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 tabs; @@ -51,6 +59,8 @@ private: QDialogButtonBox buttonBox; QPushButton okButton; QPushButton cancelButton; + + AccountSwitchWidget *accountSwitchWidget = nullptr; } ui; void addTab(QBoxLayout *layout, QString title, QString imageRes);