From f316e3bd171ac5b99008486c422849c374cc45b8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 3 Jul 2017 15:23:41 +0300 Subject: [PATCH] Add debug logs for window position and autoupdate. --- Telegram/SourceFiles/autoupdater.cpp | 5 +- .../platform/linux/main_window_linux.cpp | 1 + .../platform/mac/main_window_mac.mm | 1 + .../platform/win/main_window_win.cpp | 2 + Telegram/SourceFiles/settings.h | 12 ++- Telegram/SourceFiles/storage/localstorage.cpp | 15 ++-- Telegram/SourceFiles/window/main_window.cpp | 74 ++++++++++++------- 7 files changed, 71 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/autoupdater.cpp b/Telegram/SourceFiles/autoupdater.cpp index 358bb6c1f..5dcda0fb4 100644 --- a/Telegram/SourceFiles/autoupdater.cpp +++ b/Telegram/SourceFiles/autoupdater.cpp @@ -427,9 +427,10 @@ void UpdateChecker::unpackUpdate() { LOG(("Update Error: cant open file '%1' for writing").arg(tempDirPath + '/' + relativeName)); return fatalFail(); } - if (f.write(fileInnerData) != fileSize) { + auto writtenBytes = f.write(fileInnerData); + if (writtenBytes != fileSize) { f.close(); - LOG(("Update Error: cant write file '%1'").arg(tempDirPath + '/' + relativeName)); + LOG(("Update Error: cant write file '%1', desiredSize: %2, write result: %3").arg(tempDirPath + '/' + relativeName).arg(fileSize).arg(writtenBytes)); return fatalFail(); } f.close(); diff --git a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp index 099b504d8..d3e3816f1 100644 --- a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp @@ -546,6 +546,7 @@ void MainWindow::psFirstShow() { show(); //_private.enableShadow(winId()); if (cWindowPos().maximized) { + DEBUG_LOG(("Window Pos: First show, setting maximized.")); setWindowState(Qt::WindowMaximized); } diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index 30c379366..fcf843b31 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -383,6 +383,7 @@ void MainWindow::psFirstShow() { show(); _private->enableShadow(winId()); if (cWindowPos().maximized) { + DEBUG_LOG(("Window Pos: First show, setting maximized.")); setWindowState(Qt::WindowMaximized); } diff --git a/Telegram/SourceFiles/platform/win/main_window_win.cpp b/Telegram/SourceFiles/platform/win/main_window_win.cpp index 04a7b2a58..0fe024087 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.cpp +++ b/Telegram/SourceFiles/platform/win/main_window_win.cpp @@ -808,10 +808,12 @@ void MainWindow::psFirstShow() { show(); if (cWindowPos().maximized) { + DEBUG_LOG(("Window Pos: First show, setting maximized.")); setWindowState(Qt::WindowMaximized); } if ((cLaunchMode() == LaunchModeAutoStart && cStartMinimized() && !App::passcoded()) || cStartInTray()) { + DEBUG_LOG(("Window Pos: First show, setting minimized after.")); setWindowState(Qt::WindowMinimized); if (Global::WorkMode().value() == dbiwmTrayOnly || Global::WorkMode().value() == dbiwmWindowAndTray) { hide(); diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 4245c368a..e8c7ee2f9 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -91,10 +91,14 @@ DeclareSetting(bool, CtrlEnter); DeclareSetting(bool, AutoUpdate); struct TWindowPos { - TWindowPos() : moncrc(0), maximized(0), x(0), y(0), w(0), h(0) { - } - int32 moncrc, maximized; - int32 x, y, w, h; + TWindowPos() = default; + + int32 moncrc = 0; + int maximized = 0; + int x = 0; + int y = 0; + int w = 0; + int h = 0; }; DeclareSetting(TWindowPos, WindowPos); DeclareSetting(bool, SupportTray); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 9b22a33e1..07c16f89e 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -1270,11 +1270,13 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting } break; case dbiWindowPosition: { - TWindowPos pos; - stream >> pos.x >> pos.y >> pos.w >> pos.h >> pos.moncrc >> pos.maximized; + auto position = TWindowPos(); + stream >> position.x >> position.y >> position.w >> position.h; + stream >> position.moncrc >> position.maximized; if (!_checkStreamStatus(stream)) return false; - cSetWindowPos(pos); + DEBUG_LOG(("Window Pos: Read from storage %1, %2, %3, %4 (maximized %5)").arg(position.x).arg(position.y).arg(position.w).arg(position.h).arg(Logs::b(position.maximized))); + cSetWindowPos(position); } break; case dbiLoggedPhoneNumber: { @@ -2380,8 +2382,11 @@ void writeSettings() { data.stream << quint32(dbiLangPackKey) << quint64(_langPackKey); } - TWindowPos pos(cWindowPos()); - data.stream << quint32(dbiWindowPosition) << qint32(pos.x) << qint32(pos.y) << qint32(pos.w) << qint32(pos.h) << qint32(pos.moncrc) << qint32(pos.maximized); + auto position = cWindowPos(); + data.stream << quint32(dbiWindowPosition) << qint32(position.x) << qint32(position.y) << qint32(position.w) << qint32(position.h); + data.stream << qint32(position.moncrc) << qint32(position.maximized); + + DEBUG_LOG(("Window Pos: Writing to storage %1, %2, %3, %4 (maximized %5)").arg(position.x).arg(position.y).arg(position.w).arg(position.h).arg(Logs::b(position.maximized))); settings.writeEncrypted(data, SettingsKey); } diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 0756ef6cd..493aeff80 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -250,32 +250,38 @@ void MainWindow::initSize() { setMinimumWidth(st::windowMinWidth); setMinimumHeight((_title ? _title->height() : 0) + st::windowMinHeight); - auto pos = cWindowPos(); + auto position = cWindowPos(); + DEBUG_LOG(("Window Pos: Initializing first %1, %2, %3, %4 (maximized %5)").arg(position.x).arg(position.y).arg(position.w).arg(position.h).arg(Logs::b(position.maximized))); + auto avail = QDesktopWidget().availableGeometry(); bool maximized = false; auto geom = QRect(avail.x() + (avail.width() - st::windowDefaultWidth) / 2, avail.y() + (avail.height() - st::windowDefaultHeight) / 2, st::windowDefaultWidth, st::windowDefaultHeight); - if (pos.w && pos.h) { + if (position.w && position.h) { for (auto screen : QGuiApplication::screens()) { - if (pos.moncrc == screenNameChecksum(screen->name())) { + if (position.moncrc == screenNameChecksum(screen->name())) { auto screenGeometry = screen->geometry(); + DEBUG_LOG(("Window Pos: Screen found, screen geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height())); + auto w = screenGeometry.width(), h = screenGeometry.height(); if (w >= st::windowMinWidth && h >= st::windowMinHeight) { - if (pos.w > w) pos.w = w; - if (pos.h > h) pos.h = h; - pos.x += screenGeometry.x(); - pos.y += screenGeometry.y(); - if (pos.x + st::windowMinWidth <= screenGeometry.x() + screenGeometry.width() && - pos.y + st::windowMinHeight <= screenGeometry.y() + screenGeometry.height()) { - geom = QRect(pos.x, pos.y, pos.w, pos.h); + if (position.w > w) position.w = w; + if (position.h > h) position.h = h; + position.x += screenGeometry.x(); + position.y += screenGeometry.y(); + if (position.x + st::windowMinWidth <= screenGeometry.x() + screenGeometry.width() && + position.y + st::windowMinHeight <= screenGeometry.y() + screenGeometry.height()) { + DEBUG_LOG(("Window Pos: Resulting geometry is %1, %2, %3, %4").arg(position.x).arg(position.y).arg(position.w).arg(position.h)); + geom = QRect(position.x, position.y, position.w, position.h); } } break; } } - if (pos.y < 0) pos.y = 0; - maximized = pos.maximized; + if (position.y < 0) position.y = 0; + maximized = position.maximized; } + DEBUG_LOG(("Window Pos: Setting first %1, %2, %3, %4").arg(geom.x()).arg(geom.y()).arg(geom.width()).arg(geom.height())); setGeometry(geom); } @@ -335,39 +341,51 @@ void MainWindow::savePosition(Qt::WindowState state) { if (state == Qt::WindowActive) state = windowHandle()->windowState(); if (state == Qt::WindowMinimized || !positionInited()) return; - auto pos = cWindowPos(), curPos = pos; + auto savedPosition = cWindowPos(); + auto realPosition = savedPosition; if (state == Qt::WindowMaximized) { - curPos.maximized = 1; + realPosition.maximized = 1; } else { auto r = geometry(); - curPos.x = r.x(); - curPos.y = r.y(); - curPos.w = r.width() - (_rightColumn ? _rightColumn->width() : 0); - curPos.h = r.height(); - curPos.maximized = 0; + realPosition.x = r.x(); + realPosition.y = r.y(); + realPosition.w = r.width() - (_rightColumn ? _rightColumn->width() : 0); + realPosition.h = r.height(); + realPosition.maximized = 0; + realPosition.moncrc = 0; } + DEBUG_LOG(("Window Pos: Saving position: %1, %2, %3, %4 (maximized %5)").arg(realPosition.x).arg(realPosition.y).arg(realPosition.w).arg(realPosition.h).arg(Logs::b(realPosition.maximized))); - int px = curPos.x + curPos.w / 2, py = curPos.y + curPos.h / 2; + auto centerX = realPosition.x + realPosition.w / 2; + auto centerY = realPosition.y + realPosition.h / 2; int minDelta = 0; - QScreen *chosen = 0; + QScreen *chosen = nullptr; auto screens = QGuiApplication::screens(); for (auto screen : QGuiApplication::screens()) { - auto delta = (screen->geometry().center() - QPoint(px, py)).manhattanLength(); + auto delta = (screen->geometry().center() - QPoint(centerX, centerY)).manhattanLength(); if (!chosen || delta < minDelta) { minDelta = delta; chosen = screen; } } if (chosen) { - curPos.x -= chosen->geometry().x(); - curPos.y -= chosen->geometry().y(); - curPos.moncrc = screenNameChecksum(chosen->name()); + auto screenGeometry = chosen->geometry(); + DEBUG_LOG(("Window Pos: Screen found, geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height())); + realPosition.x -= screenGeometry.x(); + realPosition.y -= screenGeometry.y(); + realPosition.moncrc = screenNameChecksum(chosen->name()); } - if (curPos.w >= st::windowMinWidth && curPos.h >= st::windowMinHeight) { - if (curPos.x != pos.x || curPos.y != pos.y || curPos.w != pos.w || curPos.h != pos.h || curPos.moncrc != pos.moncrc || curPos.maximized != pos.maximized) { - cSetWindowPos(curPos); + if (realPosition.w >= st::windowMinWidth && realPosition.h >= st::windowMinHeight) { + if (realPosition.x != savedPosition.x + || realPosition.y != savedPosition.y + || realPosition.w != savedPosition.w + || realPosition.h != savedPosition.h + || realPosition.moncrc != savedPosition.moncrc + || realPosition.maximized != savedPosition.maximized) { + DEBUG_LOG(("Window Pos: Writing: %1, %2, %3, %4 (maximized %5)").arg(realPosition.x).arg(realPosition.y).arg(realPosition.w).arg(realPosition.h).arg(Logs::b(realPosition.maximized))); + cSetWindowPos(realPosition); Local::writeSettings(); } }