From 4f356f80cf203c398ef00a333837a965161d9f57 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 8 Sep 2019 21:45:46 +0200 Subject: [PATCH 1/3] BaseWindow smol refactor --- lib/appbase/widgets/BaseWindow.cpp | 51 ++++++++++----------- lib/appbase/widgets/BaseWindow.hpp | 9 ++-- lib/appbase/widgets/TooltipWidget.cpp | 2 +- src/singletons/WindowManager.cpp | 1 - src/widgets/AccountSwitchPopup.cpp | 5 +- src/widgets/Window.cpp | 2 +- src/widgets/dialogs/EmotePopup.cpp | 2 +- src/widgets/dialogs/NotificationPopup.cpp | 2 +- src/widgets/dialogs/SelectChannelDialog.cpp | 2 +- src/widgets/dialogs/SettingsDialog.cpp | 4 +- src/widgets/dialogs/UpdateDialog.cpp | 5 +- src/widgets/dialogs/UserInfoPopup.cpp | 3 +- src/widgets/dialogs/WelcomeDialog.cpp | 2 +- src/widgets/splits/Split.cpp | 2 +- 14 files changed, 43 insertions(+), 49 deletions(-) diff --git a/lib/appbase/widgets/BaseWindow.cpp b/lib/appbase/widgets/BaseWindow.cpp index dc9f9cafb..bc3cc16a1 100644 --- a/lib/appbase/widgets/BaseWindow.cpp +++ b/lib/appbase/widgets/BaseWindow.cpp @@ -44,12 +44,12 @@ namespace AB_NAMESPACE { -BaseWindow::BaseWindow(QWidget *parent, Flags _flags) +BaseWindow::BaseWindow(FlagsEnum _flags, QWidget *parent) : BaseWidget(parent, - Qt::Window | ((_flags & TopMost) ? Qt::WindowStaysOnTopHint - : Qt::WindowFlags())) - , enableCustomFrame_(_flags & EnableCustomFrame) - , frameless_(_flags & Frameless) + Qt::Window | (_flags.has(TopMost) ? Qt::WindowStaysOnTopHint + : Qt::WindowFlags())) + , enableCustomFrame_(_flags.has(EnableCustomFrame)) + , frameless_(_flags.has(Frameless)) , flags_(_flags) { if (this->frameless_) @@ -74,8 +74,6 @@ BaseWindow::BaseWindow(QWidget *parent, Flags _flags) createWindowShortcut(this, "CTRL+0", [] { getSettings()->uiScale.setValue(1); }); - // QTimer::this->scaleChangedEvent(this->getScale()); - this->resize(300, 150); #ifdef USEWINSDK @@ -113,11 +111,6 @@ float BaseWindow::qtFontScale() const return this->scale() / this->nativeScale_; } -BaseWindow::Flags BaseWindow::getFlags() -{ - return this->flags_; -} - void BaseWindow::init() { this->setWindowIcon(QIcon(":/images/icon.png")); @@ -206,7 +199,7 @@ void BaseWindow::init() #ifdef USEWINSDK // fourtf: don't ask me why we need to delay this - if (!(this->flags_ & Flags::TopMost)) + if (!this->flags_.has(TopMost)) { QTimer::singleShot(1, this, [this] { getSettings()->windowTopMost.connect( @@ -541,6 +534,18 @@ void BaseWindow::closeEvent(QCloseEvent *) this->closing.invoke(); } +void BaseWindow::showEvent(QShowEvent *) +{ + if (this->frameless_) + { + this->moveIntoDesktopRect(this); + qDebug() << "show"; + + QTimer::singleShot(30, this, + [this] { this->moveIntoDesktopRect(this); }); + } +} + void BaseWindow::moveIntoDesktopRect(QWidget *parent) { if (!this->stayInScreenRect_) @@ -649,7 +654,7 @@ void BaseWindow::paintEvent(QPaintEvent *) void BaseWindow::updateScale() { auto scale = - this->nativeScale_ * (this->flags_ & DisableCustomScaling + this->nativeScale_ * (this->flags_.has(DisableCustomScaling) ? 1 : getABSettings()->getClampedUiScale()); @@ -708,17 +713,11 @@ bool BaseWindow::handleDPICHANGED(MSG *msg) float _scale = dpi / 96.f; - static bool firstResize = true; - - if (!firstResize) - { - auto *prcNewWindow = reinterpret_cast(msg->lParam); - SetWindowPos(msg->hwnd, nullptr, prcNewWindow->left, prcNewWindow->top, - prcNewWindow->right - prcNewWindow->left, - prcNewWindow->bottom - prcNewWindow->top, - SWP_NOZORDER | SWP_NOACTIVATE); - } - firstResize = false; + auto *prcNewWindow = reinterpret_cast(msg->lParam); + SetWindowPos(msg->hwnd, nullptr, prcNewWindow->left, prcNewWindow->top, + prcNewWindow->right - prcNewWindow->left, + prcNewWindow->bottom - prcNewWindow->top, + SWP_NOZORDER | SWP_NOACTIVATE); this->nativeScale_ = _scale; this->updateScale(); @@ -953,7 +952,7 @@ bool BaseWindow::handleNCHITTEST(MSG *msg, long *result) return true; } - else if (this->flags_ & FramelessDraggable) + else if (this->flags_.has(FramelessDraggable)) { *result = 0; bool client = false; diff --git a/lib/appbase/widgets/BaseWindow.hpp b/lib/appbase/widgets/BaseWindow.hpp index a54a3ba80..53833e32d 100644 --- a/lib/appbase/widgets/BaseWindow.hpp +++ b/lib/appbase/widgets/BaseWindow.hpp @@ -4,6 +4,7 @@ #include #include +#include "common/FlagsEnum.hpp" class QHBoxLayout; struct tagMSG; @@ -32,7 +33,8 @@ public: enum ActionOnFocusLoss { Nothing, Delete, Close, Hide }; - explicit BaseWindow(QWidget *parent = nullptr, Flags flags_ = None); + explicit BaseWindow(FlagsEnum flags_ = None, + QWidget *parent = nullptr); void setInitialBounds(const QRect &bounds); QRect getBounds(); @@ -54,8 +56,6 @@ public: virtual float scale() const override; float qtFontScale() const; - Flags getFlags(); - pajlada::Signals::NoArgSignal closing; static bool supportsCustomWindowFrame(); @@ -72,6 +72,7 @@ protected: virtual void resizeEvent(QResizeEvent *) override; virtual void moveEvent(QMoveEvent *) override; virtual void closeEvent(QCloseEvent *) override; + virtual void showEvent(QShowEvent *) override; virtual void themeChangedEvent() override; virtual bool event(QEvent *event) override; @@ -106,7 +107,7 @@ private: bool frameless_; bool stayInScreenRect_ = false; bool shown_ = false; - Flags flags_; + FlagsEnum flags_; float nativeScale_ = 1; struct { diff --git a/lib/appbase/widgets/TooltipWidget.cpp b/lib/appbase/widgets/TooltipWidget.cpp index 30c7b9ee1..b3cad0d15 100644 --- a/lib/appbase/widgets/TooltipWidget.cpp +++ b/lib/appbase/widgets/TooltipWidget.cpp @@ -21,7 +21,7 @@ TooltipWidget *TooltipWidget::getInstance() } TooltipWidget::TooltipWidget(BaseWidget *parent) - : BaseWindow(parent, BaseWindow::TopMost) + : BaseWindow(BaseWindow::TopMost, parent) , displayImage_(new QLabel()) , displayText_(new QLabel()) { diff --git a/src/singletons/WindowManager.cpp b/src/singletons/WindowManager.cpp index f02e74f57..2afd7c62d 100644 --- a/src/singletons/WindowManager.cpp +++ b/src/singletons/WindowManager.cpp @@ -80,7 +80,6 @@ void WindowManager::showAccountSelectPopup(QPoint point) QPoint buttonPos = point; w->move(buttonPos.x() - 30, buttonPos.y()); - w->show(); w->setFocus(); } diff --git a/src/widgets/AccountSwitchPopup.cpp b/src/widgets/AccountSwitchPopup.cpp index b7b87e693..9857e9269 100644 --- a/src/widgets/AccountSwitchPopup.cpp +++ b/src/widgets/AccountSwitchPopup.cpp @@ -11,8 +11,7 @@ namespace chatterino { AccountSwitchPopup::AccountSwitchPopup(QWidget *parent) - : BaseWindow(parent, - BaseWindow::Flags(BaseWindow::TopMost | BaseWindow::Frameless)) + : BaseWindow({BaseWindow::TopMost, BaseWindow::Frameless}, parent) { #ifdef Q_OS_LINUX this->setWindowFlag(Qt::Popup); @@ -25,8 +24,6 @@ AccountSwitchPopup::AccountSwitchPopup(QWidget *parent) 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"); diff --git a/src/widgets/Window.cpp b/src/widgets/Window.cpp index 7893783a4..5b02b8682 100644 --- a/src/widgets/Window.cpp +++ b/src/widgets/Window.cpp @@ -39,7 +39,7 @@ namespace chatterino { Window::Window(WindowType type) - : BaseWindow(nullptr, BaseWindow::EnableCustomFrame) + : BaseWindow(BaseWindow::EnableCustomFrame) , type_(type) , notebook_(new SplitNotebook(this)) { diff --git a/src/widgets/dialogs/EmotePopup.cpp b/src/widgets/dialogs/EmotePopup.cpp index f1ac2693c..11c3ed886 100644 --- a/src/widgets/dialogs/EmotePopup.cpp +++ b/src/widgets/dialogs/EmotePopup.cpp @@ -101,7 +101,7 @@ namespace { } // namespace EmotePopup::EmotePopup(QWidget *parent) - : BaseWindow(parent, BaseWindow::EnableCustomFrame) + : BaseWindow(BaseWindow::EnableCustomFrame, parent) { auto layout = new QVBoxLayout(this); this->getLayoutContainer()->setLayout(layout); diff --git a/src/widgets/dialogs/NotificationPopup.cpp b/src/widgets/dialogs/NotificationPopup.cpp index 3eb10320d..e799cae13 100644 --- a/src/widgets/dialogs/NotificationPopup.cpp +++ b/src/widgets/dialogs/NotificationPopup.cpp @@ -11,7 +11,7 @@ namespace chatterino { NotificationPopup::NotificationPopup() - : BaseWindow((QWidget *)nullptr, BaseWindow::Frameless) + : BaseWindow(BaseWindow::Frameless) , channel_(std::make_shared("notifications", Channel::Type::None)) { diff --git a/src/widgets/dialogs/SelectChannelDialog.cpp b/src/widgets/dialogs/SelectChannelDialog.cpp index f965ccb5c..5a191621a 100644 --- a/src/widgets/dialogs/SelectChannelDialog.cpp +++ b/src/widgets/dialogs/SelectChannelDialog.cpp @@ -19,7 +19,7 @@ namespace chatterino { SelectChannelDialog::SelectChannelDialog(QWidget *parent) - : BaseWindow(parent, BaseWindow::EnableCustomFrame) + : BaseWindow(BaseWindow::EnableCustomFrame, parent) , selectedChannel_(Channel::getEmpty()) { this->setWindowTitle("Select a channel to join"); diff --git a/src/widgets/dialogs/SettingsDialog.cpp b/src/widgets/dialogs/SettingsDialog.cpp index 71dd56e4f..d3edcb773 100644 --- a/src/widgets/dialogs/SettingsDialog.cpp +++ b/src/widgets/dialogs/SettingsDialog.cpp @@ -24,7 +24,7 @@ namespace chatterino { SettingsDialog *SettingsDialog::handle = nullptr; SettingsDialog::SettingsDialog() - : BaseWindow(nullptr, BaseWindow::DisableCustomScaling) + : BaseWindow(BaseWindow::DisableCustomScaling) { this->setWindowTitle("Chatterino Settings"); @@ -41,7 +41,7 @@ SettingsDialog::SettingsDialog() void SettingsDialog::initUi() { - auto outerBox = LayoutCreator(this) + auto outerBox = LayoutCreator(this->getLayoutContainer()) .setLayoutType() .withoutSpacing(); diff --git a/src/widgets/dialogs/UpdateDialog.cpp b/src/widgets/dialogs/UpdateDialog.cpp index bd10c5f2e..ee0787bcf 100644 --- a/src/widgets/dialogs/UpdateDialog.cpp +++ b/src/widgets/dialogs/UpdateDialog.cpp @@ -11,9 +11,8 @@ namespace chatterino { UpdateDialog::UpdateDialog() - : BaseWindow(nullptr, - BaseWindow::Flags(BaseWindow::Frameless | BaseWindow::TopMost | - BaseWindow::EnableCustomFrame)) + : BaseWindow({BaseWindow::Frameless, BaseWindow::TopMost, + BaseWindow::EnableCustomFrame}) { auto layout = LayoutCreator(this).setLayoutType(); diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 86f0eefa1..9cae49145 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -43,8 +43,7 @@ namespace { } // namespace UserInfoPopup::UserInfoPopup() - : BaseWindow(nullptr, BaseWindow::Flags(BaseWindow::Frameless | - BaseWindow::FramelessDraggable)) + : BaseWindow({BaseWindow::Frameless, BaseWindow::FramelessDraggable}) , hack_(new bool) { this->setStayInScreenRect(true); diff --git a/src/widgets/dialogs/WelcomeDialog.cpp b/src/widgets/dialogs/WelcomeDialog.cpp index b3d2b898a..4892a2b69 100644 --- a/src/widgets/dialogs/WelcomeDialog.cpp +++ b/src/widgets/dialogs/WelcomeDialog.cpp @@ -3,7 +3,7 @@ namespace chatterino { WelcomeDialog::WelcomeDialog() - : BaseWindow(nullptr, BaseWindow::EnableCustomFrame) + : BaseWindow(BaseWindow::EnableCustomFrame) { this->setWindowTitle("Chatterino quick setup"); } diff --git a/src/widgets/splits/Split.cpp b/src/widgets/splits/Split.cpp index e03cab0e4..4e8a41719 100644 --- a/src/widgets/splits/Split.cpp +++ b/src/widgets/splits/Split.cpp @@ -52,7 +52,7 @@ namespace { const QString &title, const QString &description) { auto window = - new BaseWindow(parent, BaseWindow::Flags::EnableCustomFrame); + new BaseWindow(BaseWindow::Flags::EnableCustomFrame, parent); window->setWindowTitle("Chatterino - " + title); window->setAttribute(Qt::WA_DeleteOnClose); auto layout = new QVBoxLayout(); From e1a9a437f181d5093554b902c573cd1a83ef74f8 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 8 Sep 2019 21:57:14 +0200 Subject: [PATCH 2/3] fixed pingsound not playing sometimes --- src/providers/twitch/TwitchMessageBuilder.cpp | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 6159f7563..102d570e0 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -143,9 +143,21 @@ bool TwitchMessageBuilder::isIgnored() const return false; } +inline QMediaPlayer *getPlayer() +{ + if (isGuiThread()) + { + static auto player = new QMediaPlayer; + return player; + } + else + { + return nullptr; + } +} + void TwitchMessageBuilder::triggerHighlights() { - static auto player = new QMediaPlayer; static QUrl currentPlayerUrl; if (this->historicalMessage_) @@ -165,21 +177,24 @@ void TwitchMessageBuilder::triggerHighlights() if (this->highlightSound_ && resolveFocus) { - // update the media player url if necessary - QUrl highlightSoundUrl = - getSettings()->customHighlightSound - ? QUrl::fromLocalFile( - getSettings()->pathHighlightSound.getValue()) - : QUrl("qrc:/sounds/ping2.wav"); - - if (currentPlayerUrl != highlightSoundUrl) + if (auto player = getPlayer()) { - player->setMedia(highlightSoundUrl); + // update the media player url if necessary + QUrl highlightSoundUrl = + getSettings()->customHighlightSound + ? QUrl::fromLocalFile( + getSettings()->pathHighlightSound.getValue()) + : QUrl("qrc:/sounds/ping2.wav"); - currentPlayerUrl = highlightSoundUrl; + if (currentPlayerUrl != highlightSoundUrl) + { + player->setMedia(highlightSoundUrl); + + currentPlayerUrl = highlightSoundUrl; + } + + player->play(); } - - player->play(); } if (this->highlightAlert_) From 3404cc503c861109ddb5676aceb83e48c45afe87 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 8 Sep 2019 22:01:14 +0200 Subject: [PATCH 3/3] fixed compile on unix --- lib/appbase/widgets/BaseWindow.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/appbase/widgets/BaseWindow.cpp b/lib/appbase/widgets/BaseWindow.cpp index bc3cc16a1..0239bf269 100644 --- a/lib/appbase/widgets/BaseWindow.cpp +++ b/lib/appbase/widgets/BaseWindow.cpp @@ -363,7 +363,7 @@ void BaseWindow::onFocusLost() void BaseWindow::mousePressEvent(QMouseEvent *event) { #ifndef Q_OS_WIN - if (this->flags_ & FramelessDraggable) + if (this->flags_.has(FramelessDraggable)) { this->movingRelativePos = event->localPos(); if (auto widget = @@ -399,7 +399,7 @@ void BaseWindow::mousePressEvent(QMouseEvent *event) void BaseWindow::mouseReleaseEvent(QMouseEvent *event) { #ifndef Q_OS_WIN - if (this->flags_ & FramelessDraggable) + if (this->flags_.has(FramelessDraggable)) { if (this->moving) { @@ -415,7 +415,7 @@ void BaseWindow::mouseReleaseEvent(QMouseEvent *event) void BaseWindow::mouseMoveEvent(QMouseEvent *event) { #ifndef Q_OS_WIN - if (this->flags_ & FramelessDraggable) + if (this->flags_.has(FramelessDraggable)) { if (this->moving) {