diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5b7002a..25f9e9e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -77,7 +77,7 @@ MainWindow::MainWindow(const QCommandLineParser& parser, QWidget* parent) : QMai InnerTube::instance().createContext(InnertubeClient(InnertubeClient::ClientType::WEB, "2.20230718.01.00", "DESKTOP")); tryRestoreData(); - connect(InnerTube::instance().authStore(), &InnertubeAuthStore::authenticateSuccess, m_topbar, &TopBar::postSignInSetup); + connect(InnerTube::instance().authStore(), &InnertubeAuthStore::authenticateSuccess, m_topbar, [this] { m_topbar->postSignInSetup(); }); if (parser.isSet("channel")) ViewController::loadChannel(parser.value("channel")); @@ -264,7 +264,7 @@ void MainWindow::searchByLink(const QString& link) void MainWindow::searchByQuery(const QString& query) { - m_topbar->alwaysShow = true; + m_topbar->setAlwaysShow(true); UIUtils::clearLayout(ui->additionalWidgets); ui->historySearchWidget->clear(); @@ -350,13 +350,12 @@ void MainWindow::showAccountMenu() { if (AccountControllerWidget* accountController = findChild("accountController")) { - if (ui->centralwidget->currentIndex() != 0) - m_topbar->alwaysShow = false; + m_topbar->setAlwaysShow(ui->centralwidget->currentIndex() == 0); accountController->deleteLater(); return; } - m_topbar->alwaysShow = true; + m_topbar->setAlwaysShow(true); AccountControllerWidget* accountController = new AccountControllerWidget(this); accountController->setObjectName("accountController"); @@ -375,14 +374,13 @@ void MainWindow::showNotifications() { if (notificationMenu->isVisible()) { - if (ui->centralwidget->currentIndex() != 0) - m_topbar->alwaysShow = false; + m_topbar->setAlwaysShow(ui->centralwidget->currentIndex() == 0); notificationMenu->clear(); notificationMenu->setVisible(false); return; } - m_topbar->alwaysShow = true; + m_topbar->setAlwaysShow(true); notificationMenu->setVisible(true); BrowseHelper::instance()->browseNotificationMenu(notificationMenu); } @@ -391,12 +389,7 @@ void MainWindow::tryRestoreData() { qtTubeApp->creds().populateAuthStore(qtTubeApp->creds().activeLogin()); if (InnerTube::instance().hasAuthenticated()) - { - m_topbar->avatarButton->setVisible(true); - m_topbar->signInButton->setVisible(false); - m_topbar->setUpAvatarButton(); - m_topbar->setUpNotifications(); - } + m_topbar->postSignInSetup(false); } void MainWindow::trySwitchGridStatus(QListWidget* listWidget) diff --git a/src/qttubeapplication.cpp b/src/qttubeapplication.cpp index f073e43..bb12e0c 100644 --- a/src/qttubeapplication.cpp +++ b/src/qttubeapplication.cpp @@ -1,47 +1,15 @@ #include "qttubeapplication.h" #include "eastereggs.h" #include "mainwindow.h" -#include -#include bool QtTubeApplication::notify(QObject* receiver, QEvent* event) { - if (receiver->objectName() != "MainWindowWindow") - return QApplication::notify(receiver, event); - - if (event->type() == QEvent::MouseMove) - { - QMouseEvent* mouseEvent = static_cast(event); - TopBar* topbar = MainWindow::topbar(); - if (!topbar->alwaysShow) - { - if (mouseEvent->pos().y() < topbar->height()) - { - if (topbar->isHidden() && topbar->animation->state() != QAbstractAnimation::Running) - { - topbar->animation->setStartValue(QRect(0, 0, topbar->width(), 0)); - topbar->animation->setEndValue(QRect(0, 0, topbar->width(), topbar->height())); - disconnect(topbar->animation, &QPropertyAnimation::finished, topbar, nullptr); - topbar->animation->start(); - topbar->show(); - } - } - else if (topbar->animation->state() != QAbstractAnimation::Running && !topbar->isHidden()) - { - int height = topbar->height(); - topbar->animation->setEndValue(QRect(0, 0, topbar->width(), 0)); - topbar->animation->setStartValue(QRect(0, 0, topbar->width(), topbar->height())); - connect(topbar->animation, &QPropertyAnimation::finished, topbar, [height, topbar] { - topbar->hide(); - topbar->resize(topbar->width(), height); - }); - topbar->animation->start(); - } - } - } - else if (event->type() == QEvent::KeyPress) + if (receiver->objectName() == "MainWindowWindow") { - EasterEggs::checkEasterEggs(static_cast(event)); + if (event->type() == QEvent::MouseMove) + MainWindow::topbar()->handleMouseEvent(static_cast(event)); + else if (event->type() == QEvent::KeyPress) + EasterEggs::checkEasterEggs(static_cast(event)); } return QApplication::notify(receiver, event); diff --git a/src/ui/views/channelview.cpp b/src/ui/views/channelview.cpp index b6f55f7..90a7eed 100644 --- a/src/ui/views/channelview.cpp +++ b/src/ui/views/channelview.cpp @@ -126,7 +126,7 @@ void ChannelView::setTabsAndStyles(const InnertubeEndpoints::ChannelResponse& ch bool hasBanner = !channelResp.header.banners.isEmpty(); channelBanner->setFixedHeight(hasBanner ? 129 : 40); - MainWindow::topbar()->alwaysShow = !hasBanner; + MainWindow::topbar()->setAlwaysShow(!hasBanner); MainWindow::topbar()->setVisible(!hasBanner); if (hasBanner) diff --git a/src/ui/views/viewcontroller.cpp b/src/ui/views/viewcontroller.cpp index 34df40b..246540e 100644 --- a/src/ui/views/viewcontroller.cpp +++ b/src/ui/views/viewcontroller.cpp @@ -36,7 +36,7 @@ void ViewController::loadChannel(const QString& channelId) if (channelView) { channelView->deleteLater(); - MainWindow::topbar()->alwaysShow = true; + MainWindow::topbar()->setAlwaysShow(true); } } @@ -45,7 +45,7 @@ void ViewController::loadChannel(const QString& channelId) QObject::connect(MainWindow::topbar()->logo, &TubeLabel::clicked, channelView, [channelView] { channelView->deleteLater(); - MainWindow::topbar()->alwaysShow = true; + MainWindow::topbar()->setAlwaysShow(true); }); } @@ -69,12 +69,12 @@ void ViewController::loadVideo(const QString& videoId, int progress) QObject::connect(MainWindow::topbar()->logo, &TubeLabel::clicked, watchView, [watchView] { watchView->deleteLater(); - MainWindow::topbar()->alwaysShow = true; + MainWindow::topbar()->setAlwaysShow(true); }); QObject::connect(watchView, &WatchView::loadFailed, [watchView](const InnertubeException& ie) { QMessageBox::critical(nullptr, "Failed to load video", ie.message()); watchView->deleteLater(); - MainWindow::topbar()->alwaysShow = true; + MainWindow::topbar()->setAlwaysShow(true); }); } diff --git a/src/ui/views/watchview.cpp b/src/ui/views/watchview.cpp index 2561980..1e46883 100644 --- a/src/ui/views/watchview.cpp +++ b/src/ui/views/watchview.cpp @@ -26,8 +26,8 @@ WatchView::~WatchView() WatchView::WatchView(const QString& videoId, int progress, QWidget* parent) : QWidget(parent), ui(new Ui::WatchView) { + MainWindow::topbar()->setAlwaysShow(false); MainWindow::topbar()->setVisible(false); - MainWindow::topbar()->alwaysShow = false; ui->setupUi(this); diff --git a/src/ui/widgets/accountmenu/accountswitcherwidget.cpp b/src/ui/widgets/accountmenu/accountswitcherwidget.cpp index d2b6443..b1f5c7d 100644 --- a/src/ui/widgets/accountmenu/accountswitcherwidget.cpp +++ b/src/ui/widgets/accountmenu/accountswitcherwidget.cpp @@ -4,6 +4,7 @@ #include "mainwindow.h" #include "qttubeapplication.h" #include +#include AccountSwitcherWidget::AccountSwitcherWidget(QWidget* parent) : QWidget(parent), addAccountButton(new QPushButton(this)), backButton(new QPushButton(this)), layout(new QVBoxLayout(this)) diff --git a/src/ui/widgets/topbar/topbar.cpp b/src/ui/widgets/topbar/topbar.cpp index 2356c80..c701c18 100644 --- a/src/ui/widgets/topbar/topbar.cpp +++ b/src/ui/widgets/topbar/topbar.cpp @@ -9,6 +9,8 @@ #include "ui/forms/settings/settingsform.h" #include "utils/uiutils.h" #include +#include +#include #ifdef INNERTUBE_NO_WEBENGINE #include @@ -58,13 +60,43 @@ TopBar::TopBar(QWidget* parent) connect(signInButton, &QPushButton::clicked, this, &TopBar::trySignIn); } -void TopBar::postSignInSetup() +void TopBar::handleMouseEvent(QMouseEvent* event) +{ + if (alwaysShow || animation->state() == QAbstractAnimation::Running) + return; + + if (event->pos().y() < height()) + { + if (isHidden()) + { + animation->setStartValue(QRect(0, 0, width(), 0)); + animation->setEndValue(QRect(0, 0, width(), height())); + disconnect(animation, &QPropertyAnimation::finished, this, nullptr); + animation->start(); + show(); + } + } + else if (isVisible()) + { + animation->setStartValue(QRect(0, 0, width(), height())); + animation->setEndValue(QRect(0, 0, width(), 0)); + connect(animation, &QPropertyAnimation::finished, this, [this] { + hide(); + resize(width(), animation->startValue().toRect().height()); + }); + animation->start(); + } +} + +void TopBar::postSignInSetup(bool emitSignal) { avatarButton->setVisible(true); signInButton->setVisible(false); setUpAvatarButton(); setUpNotifications(); - emit signInStatusChanged(); + + if (emitSignal) + emit signInStatusChanged(); } void TopBar::scaleAppropriately() diff --git a/src/ui/widgets/topbar/topbar.h b/src/ui/widgets/topbar/topbar.h index 89ea277..2067d11 100644 --- a/src/ui/widgets/topbar/topbar.h +++ b/src/ui/widgets/topbar/topbar.h @@ -3,34 +3,38 @@ #include "searchbox.h" #include "topbarbell.h" #include "ui/widgets/labels/tubelabel.h" -#include class QPropertyAnimation; +class QPushButton; class TopBar : public QWidget { Q_OBJECT public: - bool alwaysShow = true; - QPropertyAnimation* animation; TubeLabel* avatarButton; TubeLabel* logo; TopBarBell* notificationBell; SearchBox* searchBox; - TubeLabel* settingsButton; - QPushButton* signInButton; explicit TopBar(QWidget* parent); - void postSignInSetup(); + void handleMouseEvent(QMouseEvent* event); + void postSignInSetup(bool emitSignal = true); void scaleAppropriately(); + void setAlwaysShow(bool alwaysShow) { this->alwaysShow = alwaysShow; } void updatePalette(const QPalette& palette); +private: + bool alwaysShow = true; + QPropertyAnimation* animation; + TubeLabel* settingsButton; + QPushButton* signInButton; public slots: - void setUpAvatarButton(); - void setUpNotifications(); - void showSettings(); void signOut(); void trySignIn(); void updateNotificationCount(); +private slots: + void setUpAvatarButton(); + void setUpNotifications(); + void showSettings(); signals: void signInStatusChanged(); };