From aa486a5c0ca9b5e886fedd3b12acf0e5238f2ce7 Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Sun, 7 Oct 2018 14:09:47 +0300
Subject: [PATCH] Adjustments for the minimized sidebar

---
 src/ChatPage.cpp         |  5 +++--
 src/CommunitiesList.cpp  |  6 ++++--
 src/Config.h             |  1 -
 src/MainWindow.cpp       |  7 ++++---
 src/RoomInfoListItem.cpp | 16 +++++++---------
 src/SideBarActions.cpp   |  6 ++++--
 src/Splitter.cpp         | 39 +++++++++++++++++++--------------------
 src/Splitter.h           |  3 +++
 src/UserInfoWidget.cpp   |  7 +++++--
 src/Utils.cpp            | 14 ++++++++++++++
 src/Utils.h              | 11 +++++++++++
 src/ui/Theme.h           |  5 -----
 12 files changed, 74 insertions(+), 46 deletions(-)

diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index a1125d2ad..a60c09cbf 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -76,7 +76,7 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
         // SideBar
         sideBar_ = new QFrame(this);
         sideBar_->setObjectName("sideBar");
-        sideBar_->setMinimumWidth(ui::sidebar::NormalSize);
+        sideBar_->setMinimumWidth(utils::calculateSidebarSizes(QFont{}).normal);
         sideBarLayout_ = new QVBoxLayout(sideBar_);
         sideBarLayout_->setSpacing(0);
         sideBarLayout_->setMargin(0);
@@ -1344,7 +1344,8 @@ ChatPage::timelineWidth()
 bool
 ChatPage::isSideBarExpanded()
 {
-        return sideBar_->size().width() > ui::sidebar::NormalSize;
+        const auto sz = utils::calculateSidebarSizes(QFont{});
+        return sideBar_->size().width() > sz.normal;
 }
 
 void
diff --git a/src/CommunitiesList.cpp b/src/CommunitiesList.cpp
index fc762376a..60bb0eb59 100644
--- a/src/CommunitiesList.cpp
+++ b/src/CommunitiesList.cpp
@@ -1,7 +1,8 @@
-#include "CommunitiesList.h"
 #include "Cache.h"
+#include "CommunitiesList.h"
 #include "Logging.h"
 #include "MatrixClient.h"
+#include "Utils.h"
 
 #include <QLabel>
 
@@ -19,7 +20,8 @@ CommunitiesList::CommunitiesList(QWidget *parent)
         topLayout_->setSpacing(0);
         topLayout_->setMargin(0);
 
-        setFixedWidth(ui::sidebar::CommunitiesSidebarSize);
+        const auto sideBarSizes = utils::calculateSidebarSizes(QFont{});
+        setFixedWidth(sideBarSizes.groups);
 
         scrollArea_ = new QScrollArea(this);
         scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
diff --git a/src/Config.h b/src/Config.h
index 58be100b6..bb99ca87b 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -8,7 +8,6 @@
 // Font sizes are in pixels.
 
 namespace conf {
-constexpr int sideBarCollapsePoint = 450;
 // Global settings.
 constexpr int fontSize                   = 14;
 constexpr int textInputFontSize          = 14;
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index b441978f5..55dbba34a 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -181,9 +181,10 @@ MainWindow::resizeEvent(QResizeEvent *event)
 void
 MainWindow::adjustSideBars()
 {
-        const uint64_t timelineWidth = chat_page_->timelineWidth();
-        const uint64_t minAvailableWidth =
-          conf::sideBarCollapsePoint + ui::sidebar::CommunitiesSidebarSize;
+        const auto sz = utils::calculateSidebarSizes(QFont{});
+
+        const uint64_t timelineWidth     = chat_page_->timelineWidth();
+        const uint64_t minAvailableWidth = sz.collapsePoint + sz.groups;
 
         if (timelineWidth < minAvailableWidth && !chat_page_->isSideBarExpanded()) {
                 chat_page_->hideSideBars();
diff --git a/src/RoomInfoListItem.cpp b/src/RoomInfoListItem.cpp
index 488972877..17880da15 100644
--- a/src/RoomInfoListItem.cpp
+++ b/src/RoomInfoListItem.cpp
@@ -27,7 +27,6 @@
 #include "ui/Menu.h"
 #include "ui/Ripple.h"
 #include "ui/RippleOverlay.h"
-#include "ui/Theme.h"
 
 constexpr int MaxUnreadCountDisplayed = 99;
 
@@ -65,11 +64,6 @@ getMetrics(const QFont &font)
         m.inviteBtnX = m.iconSize + 2 * m.padding;
         m.inviteBtnX = m.iconSize / 2.0 + m.padding + m.padding / 3.0;
 
-        // std::cout << "unit " << m.unit << '\n';
-        // std::cout << "maxHeight " << m.maxHeight << '\n';
-        // std::cout << "iconSize " << m.iconSize << '\n';
-        // std::cout << "padding " << m.padding << '\n';
-
         return m;
 }
 
@@ -130,7 +124,9 @@ RoomInfoListItem::resizeEvent(QResizeEvent *)
         QPainterPath path;
         path.addRect(0, 0, width(), height());
 
-        if (width() > ui::sidebar::SmallSize)
+        const auto sidebarSizes = utils::calculateSidebarSizes(QFont{});
+
+        if (width() > sidebarSizes.small)
                 setToolTip("");
         else
                 setToolTip(roomName_);
@@ -171,7 +167,9 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
         // Description line with the default font.
         int bottom_y = wm.maxHeight - wm.padding - metrics.ascent() / 2;
 
-        if (width() > ui::sidebar::SmallSize) {
+        const auto sidebarSizes = utils::calculateSidebarSizes(QFont{});
+
+        if (width() > sidebarSizes.small) {
                 QFont headingFont;
                 headingFont.setWeight(QFont::Medium);
                 p.setFont(headingFont);
@@ -314,7 +312,7 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
                          bubbleDiameter_ + x_width,
                          bubbleDiameter_);
 
-                if (width() == ui::sidebar::SmallSize)
+                if (width() == sidebarSizes.small)
                         r = QRectF(width() - bubbleDiameter_ - 5,
                                    height() - bubbleDiameter_ - 5,
                                    bubbleDiameter_ + x_width,
diff --git a/src/SideBarActions.cpp b/src/SideBarActions.cpp
index 0d82348ef..2f447cd8f 100644
--- a/src/SideBarActions.cpp
+++ b/src/SideBarActions.cpp
@@ -6,10 +6,10 @@
 #include "Config.h"
 #include "MainWindow.h"
 #include "SideBarActions.h"
+#include "Utils.h"
 #include "ui/FlatButton.h"
 #include "ui/Menu.h"
 #include "ui/OverlayModal.h"
-#include "ui/Theme.h"
 
 SideBarActions::SideBarActions(QWidget *parent)
   : QWidget{parent}
@@ -93,7 +93,9 @@ SideBarActions::resizeEvent(QResizeEvent *event)
 {
         Q_UNUSED(event);
 
-        if (width() <= ui::sidebar::SmallSize) {
+        const auto sidebarSizes = utils::calculateSidebarSizes(QFont{});
+
+        if (width() <= sidebarSizes.small) {
                 roomDirectory_->hide();
                 createRoomBtn_->hide();
         } else {
diff --git a/src/Splitter.cpp b/src/Splitter.cpp
index f5bbf367d..ddb1dc1c9 100644
--- a/src/Splitter.cpp
+++ b/src/Splitter.cpp
@@ -23,12 +23,12 @@
 
 #include "Config.h"
 #include "Splitter.h"
-#include "ui/Theme.h"
 
 constexpr auto MaxWidth = (1 << 24) - 1;
 
 Splitter::Splitter(QWidget *parent)
   : QSplitter(parent)
+  , sz_{utils::calculateSidebarSizes(QFont{})}
 {
         connect(this, &QSplitter::splitterMoved, this, &Splitter::onSplitterMoved);
         setChildrenCollapsible(false);
@@ -45,17 +45,17 @@ Splitter::restoreSizes(int fallback)
         if (savedWidth == 0) {
                 hideSidebar();
                 return;
-        } else if (savedWidth == ui::sidebar::SmallSize) {
+        } else if (savedWidth == sz_.small) {
                 if (left) {
-                        left->setMinimumWidth(ui::sidebar::SmallSize);
-                        left->setMaximumWidth(ui::sidebar::SmallSize);
+                        left->setMinimumWidth(sz_.small);
+                        left->setMaximumWidth(sz_.small);
                         return;
                 }
         }
 
-        left->setMinimumWidth(ui::sidebar::NormalSize);
-        left->setMaximumWidth(2 * ui::sidebar::NormalSize);
-        setSizes({ui::sidebar::NormalSize, fallback - ui::sidebar::NormalSize});
+        left->setMinimumWidth(sz_.normal);
+        left->setMaximumWidth(2 * sz_.normal);
+        setSizes({sz_.normal, fallback - sz_.normal});
 
         setStretchFactor(0, 0);
         setStretchFactor(1, 1);
@@ -84,7 +84,7 @@ Splitter::onSplitterMoved(int pos, int index)
                 return;
         }
 
-        if (s[0] == ui::sidebar::NormalSize) {
+        if (s[0] == sz_.normal) {
                 rightMoveCount_ += 1;
 
                 if (rightMoveCount_ > moveEventLimit_) {
@@ -94,13 +94,13 @@ Splitter::onSplitterMoved(int pos, int index)
                         // if we are coming from the right, the cursor should
                         // end up on the first widget.
                         if (left->rect().contains(cursorPosition)) {
-                                left->setMinimumWidth(ui::sidebar::SmallSize);
-                                left->setMaximumWidth(ui::sidebar::SmallSize);
+                                left->setMinimumWidth(sz_.small);
+                                left->setMaximumWidth(sz_.small);
 
                                 rightMoveCount_ = 0;
                         }
                 }
-        } else if (s[0] == ui::sidebar::SmallSize) {
+        } else if (s[0] == sz_.small) {
                 leftMoveCount_ += 1;
 
                 if (leftMoveCount_ > moveEventLimit_) {
@@ -115,10 +115,9 @@ Splitter::onSplitterMoved(int pos, int index)
                         // if we are coming from the left, the cursor should
                         // end up on the second widget.
                         if (extended.contains(cursorPosition) &&
-                            right->size().width() >=
-                              conf::sideBarCollapsePoint + ui::sidebar::NormalSize) {
-                                left->setMinimumWidth(ui::sidebar::NormalSize);
-                                left->setMaximumWidth(2 * ui::sidebar::NormalSize);
+                            right->size().width() >= sz_.collapsePoint + sz_.normal) {
+                                left->setMinimumWidth(sz_.normal);
+                                left->setMaximumWidth(2 * sz_.normal);
 
                                 leftMoveCount_ = 0;
                         }
@@ -145,12 +144,12 @@ Splitter::showChatView()
                 right->show();
 
                 // Restore previous size.
-                if (left->minimumWidth() == ui::sidebar::SmallSize) {
-                        left->setMinimumWidth(ui::sidebar::SmallSize);
-                        left->setMaximumWidth(ui::sidebar::SmallSize);
+                if (left->minimumWidth() == sz_.small) {
+                        left->setMinimumWidth(sz_.small);
+                        left->setMaximumWidth(sz_.small);
                 } else {
-                        left->setMinimumWidth(ui::sidebar::NormalSize);
-                        left->setMaximumWidth(2 * ui::sidebar::NormalSize);
+                        left->setMinimumWidth(sz_.normal);
+                        left->setMaximumWidth(2 * sz_.normal);
                 }
         }
 }
diff --git a/src/Splitter.h b/src/Splitter.h
index 99e02eed7..14d6773e0 100644
--- a/src/Splitter.h
+++ b/src/Splitter.h
@@ -17,6 +17,7 @@
 
 #pragma once
 
+#include "Utils.h"
 #include <QSplitter>
 
 class Splitter : public QSplitter
@@ -43,4 +44,6 @@ public slots:
 
         int leftMoveCount_  = 0;
         int rightMoveCount_ = 0;
+
+        utils::SideBarSizes sz_;
 };
diff --git a/src/UserInfoWidget.cpp b/src/UserInfoWidget.cpp
index f24f2d792..5345fb2a3 100644
--- a/src/UserInfoWidget.cpp
+++ b/src/UserInfoWidget.cpp
@@ -22,6 +22,7 @@
 #include "Config.h"
 #include "MainWindow.h"
 #include "UserInfoWidget.h"
+#include "Utils.h"
 #include "ui/Avatar.h"
 #include "ui/FlatButton.h"
 #include "ui/OverlayModal.h"
@@ -107,8 +108,10 @@ UserInfoWidget::resizeEvent(QResizeEvent *event)
 {
         Q_UNUSED(event);
 
-        if (width() <= ui::sidebar::SmallSize) {
-                topLayout_->setContentsMargins(0, 0, logoutButtonSize_ / 2 - 5 / 2, 0);
+        const auto sz = utils::calculateSidebarSizes(QFont{});
+
+        if (width() <= sz.small) {
+                topLayout_->setContentsMargins(0, 0, logoutButtonSize_, 0);
 
                 userAvatar_->hide();
                 displayNameLabel_->hide();
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 567e7651d..aabd19cc5 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -412,3 +412,17 @@ utils::restoreCombobox(QComboBox *combo, const QString &value)
                 }
         }
 }
+
+utils::SideBarSizes
+utils::calculateSidebarSizes(const QFont &f)
+{
+        const auto height = static_cast<double>(QFontMetrics{f}.lineSpacing());
+
+        SideBarSizes sz;
+        sz.small         = std::ceil(3.5 * height + height / 4.0);
+        sz.normal        = std::ceil(16 * height);
+        sz.groups        = std::ceil(3 * height);
+        sz.collapsePoint = 2 * sz.normal;
+
+        return sz;
+}
diff --git a/src/Utils.h b/src/Utils.h
index 47754604d..c0a781771 100644
--- a/src/Utils.h
+++ b/src/Utils.h
@@ -233,4 +233,15 @@ centerWidget(QWidget *widget, QWidget *parent);
 
 void
 restoreCombobox(QComboBox *combo, const QString &value);
+
+struct SideBarSizes
+{
+        int small;
+        int normal;
+        int groups;
+	int collapsePoint;
+};
+
+SideBarSizes
+calculateSidebarSizes(const QFont &f);
 }
diff --git a/src/ui/Theme.h b/src/ui/Theme.h
index 7a0bdcb7a..d1d7e2a6c 100644
--- a/src/ui/Theme.h
+++ b/src/ui/Theme.h
@@ -12,11 +12,6 @@ enum class AvatarType
         Letter
 };
 
-namespace sidebar {
-static const int SmallSize              = 60;
-static const int NormalSize             = 260;
-static const int CommunitiesSidebarSize = 48;
-}
 // Default font size.
 const int FontSize = 16;