Skip to content

Commit

Permalink
Adjustments for the minimized sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
mujx committed Oct 7, 2018
1 parent 2295d68 commit aa486a5
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 46 deletions.
5 changes: 3 additions & 2 deletions src/ChatPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/CommunitiesList.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "CommunitiesList.h"
#include "Cache.h"
#include "CommunitiesList.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"

#include <QLabel>

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 7 additions & 9 deletions src/RoomInfoListItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "ui/Menu.h"
#include "ui/Ripple.h"
#include "ui/RippleOverlay.h"
#include "ui/Theme.h"

constexpr int MaxUnreadCountDisplayed = 99;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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_);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/SideBarActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 19 additions & 20 deletions src/Splitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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_) {
Expand All @@ -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_) {
Expand All @@ -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;
}
Expand All @@ -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);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Splitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#pragma once

#include "Utils.h"
#include <QSplitter>

class Splitter : public QSplitter
Expand All @@ -43,4 +44,6 @@ public slots:

int leftMoveCount_ = 0;
int rightMoveCount_ = 0;

utils::SideBarSizes sz_;
};
7 changes: 5 additions & 2 deletions src/UserInfoWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 0 additions & 5 deletions src/ui/Theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit aa486a5

Please sign in to comment.