From c872cd76e1d8af504c27b93cc42b817593b3032b Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 16 Nov 2017 20:43:52 +0400 Subject: [PATCH] Improve window extension by third column. --- Telegram/SourceFiles/auth_session.cpp | 6 +++ Telegram/SourceFiles/auth_session.h | 7 +++ Telegram/SourceFiles/window/main_window.cpp | 29 +++++++--- Telegram/SourceFiles/window/main_window.h | 9 ++-- .../SourceFiles/window/window_controller.cpp | 53 +++++++++++++------ 5 files changed, 79 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/auth_session.cpp b/Telegram/SourceFiles/auth_session.cpp index 6b9e207913..c0a8f23dd7 100644 --- a/Telegram/SourceFiles/auth_session.cpp +++ b/Telegram/SourceFiles/auth_session.cpp @@ -78,6 +78,7 @@ QByteArray AuthSessionData::serialize() const { 0, 1000000)); stream << qint32(_variables.thirdColumnWidth.current()); + stream << qint32(_variables.thirdSectionExtendedBy); } return result; } @@ -101,6 +102,7 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) { qint32 smallDialogsList = 0; float64 dialogsWidthRatio = _variables.dialogsWidthRatio.current(); int thirdColumnWidth = _variables.thirdColumnWidth.current(); + int thirdSectionExtendedBy = _variables.thirdSectionExtendedBy; stream >> selectorTab; stream >> lastSeenWarningSeen; if (!stream.atEnd()) { @@ -145,6 +147,9 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) { stream >> value; thirdColumnWidth = value; + + stream >> value; + thirdSectionExtendedBy = value; } if (stream.status() != QDataStream::Ok) { LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()")); @@ -179,6 +184,7 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) { _variables.smallDialogsList = smallDialogsList; _variables.dialogsWidthRatio = dialogsWidthRatio; _variables.thirdColumnWidth = thirdColumnWidth; + _variables.thirdSectionExtendedBy = thirdSectionExtendedBy; if (_variables.thirdSectionInfoEnabled) { _variables.tabbedSelectorSectionEnabled = false; } diff --git a/Telegram/SourceFiles/auth_session.h b/Telegram/SourceFiles/auth_session.h index 0074ce2e52..e528671495 100644 --- a/Telegram/SourceFiles/auth_session.h +++ b/Telegram/SourceFiles/auth_session.h @@ -170,6 +170,12 @@ public: return _thirdSectionInfoEnabledValue.events_starting_with( thirdSectionInfoEnabled()); } + int thirdSectionExtendedBy() const { + return _variables.thirdSectionExtendedBy; + } + void setThirdSectionExtendedBy(int savedValue) { + _variables.thirdSectionExtendedBy = savedValue; + } bool tabbedReplacedWithInfo() const { return _tabbedReplacedWithInfo; } @@ -342,6 +348,7 @@ private: base::flat_set groupStickersSectionHidden; bool thirdSectionInfoEnabled = true; // per-window bool smallDialogsList = false; // per-window + int thirdSectionExtendedBy = -1; // per-window rpl::variable dialogsWidthRatio = kDefaultDialogsWidthRatio; // per-window rpl::variable thirdColumnWidth diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index ec5cbdaa97..d070175b6c 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -369,20 +369,35 @@ void MainWindow::showRightColumn(object_ptr widget) { } } -bool MainWindow::canExtendWidthBy(int addToWidth) { +int MainWindow::maximalExtendBy() const { auto desktop = QDesktopWidget().availableGeometry(this); - return (width() + addToWidth) <= desktop.width(); + return std::max(desktop.width() - geometry().width(), 0); } -void MainWindow::tryToExtendWidthBy(int addToWidth) { +bool MainWindow::canExtendNoMove(int extendBy) const { auto desktop = QDesktopWidget().availableGeometry(this); - auto newWidth = qMin(width() + addToWidth, desktop.width()); - auto newLeft = qMin(x(), desktop.x() + desktop.width() - newWidth); - if (x() != newLeft || width() != newWidth) { - setGeometry(newLeft, y(), newWidth, height()); + auto inner = geometry(); + auto innerRight = (inner.x() + inner.width() + extendBy); + auto desktopRight = (desktop.x() + desktop.width()); + return innerRight <= desktopRight; +} + +int MainWindow::tryToExtendWidthBy(int addToWidth) { + auto desktop = QDesktopWidget().availableGeometry(this); + auto inner = geometry(); + accumulate_min( + addToWidth, + std::max(desktop.width() - inner.width(), 0)); + auto newWidth = inner.width() + addToWidth; + auto newLeft = std::min( + inner.x(), + desktop.x() + desktop.width() - newWidth); + if (inner.x() != newLeft || inner.width() != newWidth) { + setGeometry(newLeft, inner.y(), newWidth, inner.height()); } else { updateControlsGeometry(); } + return addToWidth; } void MainWindow::launchDrag(std::unique_ptr data) { diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index ace1cb2abc..72b5120860 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -76,8 +76,11 @@ public: } void showRightColumn(object_ptr widget); - bool canExtendWidthBy(int addToWidth); - void tryToExtendWidthBy(int addToWidth); + int maximalExtendBy() const; + bool canExtendNoMove(int extendBy) const; + + // Returns how much could the window get extended. + int tryToExtendWidthBy(int addToWidth); virtual void updateTrayMenu(bool force = false) { } @@ -184,7 +187,7 @@ private: base::Timer _inactivePressTimer; base::Observable _dragFinished; - + }; } // namespace Window diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index 3af5a3610a..0a25a91075 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -167,12 +167,9 @@ Controller::ShrinkResult Controller::shrinkDialogsAndThirdColumns( bool Controller::canShowThirdSection() const { auto currentLayout = computeColumnLayout(); - auto extendBy = minimalThreeColumnWidth() + auto minimalExtendBy = minimalThreeColumnWidth() - currentLayout.bodyWidth; - if (extendBy <= 0) { - return true; - } - return window()->canExtendWidthBy(extendBy); + return (minimalExtendBy <= window()->maximalExtendBy()); } bool Controller::canShowThirdSectionWithoutResize() const { @@ -197,14 +194,35 @@ void Controller::resizeForThirdSection() { Auth().data().setTabbedSelectorSectionEnabled(false); Auth().data().setThirdSectionInfoEnabled(false); - auto extendBy = qMax( - minimalThreeColumnWidth() - layout.bodyWidth, - countThirdColumnWidthFromRatio(layout.bodyWidth)); - auto newBodyWidth = layout.bodyWidth + extendBy; - auto currentRatio = Auth().data().dialogsWidthRatio(); - Auth().data().setDialogsWidthRatio( - (currentRatio * layout.bodyWidth) / newBodyWidth); - window()->tryToExtendWidthBy(extendBy); + auto wanted = countThirdColumnWidthFromRatio(layout.bodyWidth); + auto minimal = st::columnMinimalWidthThird; + auto extendBy = wanted; + auto extendedBy = [&] { + // Best - extend by third column without moving the window. + // Next - extend by minimal third column without moving. + // Next - show third column inside the window without moving. + // Last - extend with moving. + if (window()->canExtendNoMove(wanted)) { + return window()->tryToExtendWidthBy(wanted); + } else if (window()->canExtendNoMove(minimal)) { + extendBy = minimal; + return window()->tryToExtendWidthBy(minimal); + } else if (layout.bodyWidth >= minimalThreeColumnWidth()) { + return 0; + } + return window()->tryToExtendWidthBy(minimal); + }(); + if (extendedBy) { + if (extendBy != Auth().data().thirdColumnWidth()) { + Auth().data().setThirdColumnWidth(extendBy); + } + auto newBodyWidth = layout.bodyWidth + extendedBy; + auto currentRatio = Auth().data().dialogsWidthRatio(); + Auth().data().setDialogsWidthRatio( + (currentRatio * layout.bodyWidth) / newBodyWidth); + } + auto savedValue = (extendedBy == extendBy) ? -1 : extendedBy; + Auth().data().setThirdSectionExtendedBy(savedValue); Auth().data().setTabbedSelectorSectionEnabled( tabbedSelectorSectionEnabled); @@ -218,11 +236,16 @@ void Controller::closeThirdSection() { if (layout.windowLayout == Adaptive::WindowLayout::ThreeColumn) { auto noResize = window()->isFullScreen() || window()->isMaximized(); + auto savedValue = Auth().data().thirdSectionExtendedBy(); + auto extendedBy = (savedValue == -1) + ? layout.thirdWidth + : savedValue; auto newBodyWidth = noResize ? layout.bodyWidth - : (layout.bodyWidth - layout.thirdWidth); + : (layout.bodyWidth - extendedBy); auto currentRatio = Auth().data().dialogsWidthRatio(); - Auth().data().setDialogsWidthRatio((currentRatio * layout.bodyWidth) / newBodyWidth); + Auth().data().setDialogsWidthRatio( + (currentRatio * layout.bodyWidth) / newBodyWidth); newWindowSize = QSize( window()->width() + (newBodyWidth - layout.bodyWidth), window()->height());