From 4f356f80cf203c398ef00a333837a965161d9f57 Mon Sep 17 00:00:00 2001
From: fourtf <tf.four@gmail.com>
Date: Sun, 8 Sep 2019 21:45:46 +0200
Subject: [PATCH] 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> _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<RECT *>(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<RECT *>(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 <functional>
 #include <pajlada/signals/signalholder.hpp>
+#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> 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> 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<Channel>("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<SettingsDialog>(this)
+    auto outerBox = LayoutCreator<QWidget>(this->getLayoutContainer())
                         .setLayoutType<QVBoxLayout>()
                         .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<UpdateDialog>(this).setLayoutType<QVBoxLayout>();
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();