diff --git a/Telegram/SourceFiles/calls/calls_panel.cpp b/Telegram/SourceFiles/calls/calls_panel.cpp index 4462eec6ca..8f57effdfb 100644 --- a/Telegram/SourceFiles/calls/calls_panel.cpp +++ b/Telegram/SourceFiles/calls/calls_panel.cpp @@ -177,22 +177,20 @@ QRect Panel::panelGeometry() const { const auto center = Core::App().getPointForCallPanelCenter(); const auto simple = QRect(0, 0, st::callWidth, st::callHeight); const auto initial = simple.translated(center - simple.center()); - auto result = initial; - if (const auto window = Core::App().activeWindow()) { - const auto initialPosition = Core::WindowPosition{ - .moncrc = 0, - .scale = cScale(), - .x = initial.x(), - .y = initial.y(), - .w = initial.width(), - .h = initial.height(), - }; - result = window->widget()->countInitialGeometry( - adjusted, - initialPosition, - { st::callWidthMin, st::callHeightMin }); - } - return result; + const auto initialPosition = Core::WindowPosition{ + .moncrc = 0, + .scale = cScale(), + .x = initial.x(), + .y = initial.y(), + .w = initial.width(), + .h = initial.height(), + }; + return ::Window::CountInitialGeometry( + window(), + adjusted, + initialPosition, + { st::callWidthMin, st::callHeightMin }, + u"Call"_q); } ConferencePanelMigration Panel::migrationInfo() const { diff --git a/Telegram/SourceFiles/iv/iv_controller.cpp b/Telegram/SourceFiles/iv/iv_controller.cpp index 3ee6924cb1..2b26d0e40e 100644 --- a/Telegram/SourceFiles/iv/iv_controller.cpp +++ b/Telegram/SourceFiles/iv/iv_controller.cpp @@ -615,7 +615,7 @@ void Controller::createWindow() { updateTitleGeometry(width); }, _subtitle->lifetime()); - window->setGeometry(_delegate->ivGeometry()); + window->setGeometry(_delegate->ivGeometry(window)); window->setMinimumSize({ st::windowMinWidth, st::windowMinHeight }); window->geometryValue( diff --git a/Telegram/SourceFiles/iv/iv_delegate.h b/Telegram/SourceFiles/iv/iv_delegate.h index d722d19f07..6e3a28cb8f 100644 --- a/Telegram/SourceFiles/iv/iv_delegate.h +++ b/Telegram/SourceFiles/iv/iv_delegate.h @@ -15,8 +15,7 @@ namespace Iv { class Delegate { public: - virtual void ivSetLastSourceWindow(not_null window) = 0; - [[nodiscard]] virtual QRect ivGeometry() const = 0; + [[nodiscard]] virtual QRect ivGeometry(not_null window) const = 0; virtual void ivSaveGeometry(not_null window) = 0; [[nodiscard]] virtual int ivZoom() const = 0; diff --git a/Telegram/SourceFiles/iv/iv_delegate_impl.cpp b/Telegram/SourceFiles/iv/iv_delegate_impl.cpp index 3cbb597033..f09418c836 100644 --- a/Telegram/SourceFiles/iv/iv_delegate_impl.cpp +++ b/Telegram/SourceFiles/iv/iv_delegate_impl.cpp @@ -45,26 +45,16 @@ namespace { } // namespace -void DelegateImpl::ivSetLastSourceWindow(not_null window) { - _lastSourceWindow = window; -} - -QRect DelegateImpl::ivGeometry() const { - const auto found = _lastSourceWindow - ? Core::App().findWindow(_lastSourceWindow) - : nullptr; - +QRect DelegateImpl::ivGeometry(not_null window) const { const auto saved = Core::App().settings().ivPosition(); const auto adjusted = Core::AdjustToScale(saved, u"IV"_q); const auto initial = DefaultPosition(); - auto result = initial.rect(); - if (const auto window = found ? found : Core::App().activeWindow()) { - result = window->widget()->countInitialGeometry( - adjusted, - initial, - { st::ivWidthMin, st::ivHeightMin }); - } - return result; + return Window::CountInitialGeometry( + window, + adjusted, + initial, + { st::ivWidthMin, st::ivHeightMin }, + u"IV"_q); } void DelegateImpl::ivSaveGeometry(not_null window) { diff --git a/Telegram/SourceFiles/iv/iv_delegate_impl.h b/Telegram/SourceFiles/iv/iv_delegate_impl.h index 8217d94383..e9767c5cfd 100644 --- a/Telegram/SourceFiles/iv/iv_delegate_impl.h +++ b/Telegram/SourceFiles/iv/iv_delegate_impl.h @@ -15,17 +15,13 @@ class DelegateImpl final : public Delegate { public: DelegateImpl() = default; - void ivSetLastSourceWindow(not_null window) override; - [[nodiscard]] QRect ivGeometry() const override; + [[nodiscard]] QRect ivGeometry(not_null window) const override; void ivSaveGeometry(not_null window) override; [[nodiscard]] int ivZoom() const override; [[nodiscard]] rpl::producer ivZoomValue() const override; void ivSetZoom(int value) override; -private: - QPointer _lastSourceWindow; - }; } // namespace Iv diff --git a/Telegram/SourceFiles/iv/iv_instance.cpp b/Telegram/SourceFiles/iv/iv_instance.cpp index caab6362be..607ab87037 100644 --- a/Telegram/SourceFiles/iv/iv_instance.cpp +++ b/Telegram/SourceFiles/iv/iv_instance.cpp @@ -829,7 +829,6 @@ void Instance::show( not_null controller, not_null data, QString hash) { - _delegate->ivSetLastSourceWindow(controller->widget()); show(controller->uiShow(), data, hash); } diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 14b2fbc43d..56eb332eb6 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -958,13 +958,12 @@ void OverlayWidget::initNormalGeometry() { const auto saved = Core::App().settings().mediaViewPosition(); const auto adjusted = Core::AdjustToScale(saved, u"Viewer"_q); const auto initial = DefaultPosition(); - _normalGeometry = initial.rect(); - if (const auto active = Core::App().activeWindow()) { - _normalGeometry = active->widget()->countInitialGeometry( - adjusted, - initial, - { st::mediaviewMinWidth, st::mediaviewMinHeight }); - } + _normalGeometry = Window::CountInitialGeometry( + _window, + adjusted, + initial, + { st::mediaviewMinWidth, st::mediaviewMinHeight }, + u"Viewer"_q); } void OverlayWidget::savePosition() { diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 092c0a46c3..10d1de34de 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -700,115 +700,12 @@ QRect MainWindow::countInitialGeometry(WindowPosition position) { .w = initialWidth, .h = initialHeight, }; - return countInitialGeometry( + return CountInitialGeometry( + this, position, initial, - { st::windowMinWidth, st::windowMinHeight }); -} - -QRect MainWindow::countInitialGeometry( - WindowPosition position, - WindowPosition initial, - QSize minSize) const { - if (!position.w || !position.h) { - return initial.rect(); - } - const auto screen = [&]() -> QScreen* { - for (const auto screen : QGuiApplication::screens()) { - const auto sum = Platform::ScreenNameChecksum(screen->name()); - if (position.moncrc == sum) { - return screen; - } - } - return QGuiApplication::screenAt(position.rect().center()); - }(); - if (!screen) { - return initial.rect(); - } - const auto frame = frameMargins(); - const auto screenGeometry = screen->geometry(); - const auto availableGeometry = screen->availableGeometry(); - const auto spaceForInner = availableGeometry.marginsRemoved(frame); - DEBUG_LOG(("Window Pos: " - "Screen found, screen geometry: %1, %2, %3, %4, " - "available: %5, %6, %7, %8" - ).arg(screenGeometry.x() - ).arg(screenGeometry.y() - ).arg(screenGeometry.width() - ).arg(screenGeometry.height() - ).arg(availableGeometry.x() - ).arg(availableGeometry.y() - ).arg(availableGeometry.width() - ).arg(availableGeometry.height())); - DEBUG_LOG(("Window Pos: " - "Window frame margins: %1, %2, %3, %4, " - "available space for inner geometry: %5, %6, %7, %8" - ).arg(frame.left() - ).arg(frame.top() - ).arg(frame.right() - ).arg(frame.bottom() - ).arg(spaceForInner.x() - ).arg(spaceForInner.y() - ).arg(spaceForInner.width() - ).arg(spaceForInner.height())); - - const auto x = spaceForInner.x() - - (position.moncrc ? screenGeometry.x() : 0); - const auto y = spaceForInner.y() - - (position.moncrc ? screenGeometry.y() : 0); - const auto w = spaceForInner.width(); - const auto h = spaceForInner.height(); - if (w < st::windowMinWidth || h < st::windowMinHeight) { - return initial.rect(); - } - if (position.x < x) position.x = x; - if (position.y < y) position.y = y; - if (position.w > w) position.w = w; - if (position.h > h) position.h = h; - const auto rightPoint = position.x + position.w; - const auto screenRightPoint = x + w; - if (rightPoint > screenRightPoint) { - const auto distance = rightPoint - screenRightPoint; - const auto newXPos = position.x - distance; - if (newXPos >= x) { - position.x = newXPos; - } else { - position.x = x; - const auto newRightPoint = position.x + position.w; - const auto newDistance = newRightPoint - screenRightPoint; - position.w -= newDistance; - } - } - const auto bottomPoint = position.y + position.h; - const auto screenBottomPoint = y + h; - if (bottomPoint > screenBottomPoint) { - const auto distance = bottomPoint - screenBottomPoint; - const auto newYPos = position.y - distance; - if (newYPos >= y) { - position.y = newYPos; - } else { - position.y = y; - const auto newBottomPoint = position.y + position.h; - const auto newDistance = newBottomPoint - screenBottomPoint; - position.h -= newDistance; - } - } - if (position.moncrc) { - 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())) { - return initial.rect(); - } - DEBUG_LOG(("Window Pos: Resulting geometry is %1, %2, %3, %4" - ).arg(position.x - ).arg(position.y - ).arg(position.w - ).arg(position.h)); - return position.rect(); + { st::windowMinWidth, st::windowMinHeight }, + u"Window"_q); } void MainWindow::firstShow() { @@ -1136,4 +1033,114 @@ WindowPosition PositionWithScreen( name); } +QRect CountInitialGeometry( + not_null widget, + WindowPosition position, + WindowPosition initial, + QSize minSize, + const QString &name) { + if (!position.w || !position.h) { + return initial.rect(); + } + const auto screen = [&]() -> QScreen* { + for (const auto screen : QGuiApplication::screens()) { + const auto sum = Platform::ScreenNameChecksum(screen->name()); + if (position.moncrc == sum) { + return screen; + } + } + return QGuiApplication::screenAt(position.rect().center()); + }(); + if (!screen) { + return initial.rect(); + } + const auto frame = widget->frameMargins(); + const auto screenGeometry = screen->geometry(); + const auto availableGeometry = screen->availableGeometry(); + const auto spaceForInner = availableGeometry.marginsRemoved(frame); + DEBUG_LOG(("%1 Pos: " + "Screen found, screen geometry: %2, %3, %4, %5, " + "available: %6, %7, %8, %9" + ).arg(name + ).arg(screenGeometry.x() + ).arg(screenGeometry.y() + ).arg(screenGeometry.width() + ).arg(screenGeometry.height() + ).arg(availableGeometry.x() + ).arg(availableGeometry.y() + ).arg(availableGeometry.width() + ).arg(availableGeometry.height())); + DEBUG_LOG(("%1 Pos: " + "Window frame margins: %2, %3, %4, %5, " + "available space for inner geometry: %6, %7, %8, %9" + ).arg(name + ).arg(frame.left() + ).arg(frame.top() + ).arg(frame.right() + ).arg(frame.bottom() + ).arg(spaceForInner.x() + ).arg(spaceForInner.y() + ).arg(spaceForInner.width() + ).arg(spaceForInner.height())); + + const auto x = spaceForInner.x() + - (position.moncrc ? screenGeometry.x() : 0); + const auto y = spaceForInner.y() + - (position.moncrc ? screenGeometry.y() : 0); + const auto w = spaceForInner.width(); + const auto h = spaceForInner.height(); + if (w < st::windowMinWidth || h < st::windowMinHeight) { + return initial.rect(); + } + if (position.x < x) position.x = x; + if (position.y < y) position.y = y; + if (position.w > w) position.w = w; + if (position.h > h) position.h = h; + const auto rightPoint = position.x + position.w; + const auto screenRightPoint = x + w; + if (rightPoint > screenRightPoint) { + const auto distance = rightPoint - screenRightPoint; + const auto newXPos = position.x - distance; + if (newXPos >= x) { + position.x = newXPos; + } else { + position.x = x; + const auto newRightPoint = position.x + position.w; + const auto newDistance = newRightPoint - screenRightPoint; + position.w -= newDistance; + } + } + const auto bottomPoint = position.y + position.h; + const auto screenBottomPoint = y + h; + if (bottomPoint > screenBottomPoint) { + const auto distance = bottomPoint - screenBottomPoint; + const auto newYPos = position.y - distance; + if (newYPos >= y) { + position.y = newYPos; + } else { + position.y = y; + const auto newBottomPoint = position.y + position.h; + const auto newDistance = newBottomPoint - screenBottomPoint; + position.h -= newDistance; + } + } + if (position.moncrc) { + 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())) { + return initial.rect(); + } + DEBUG_LOG(("%1 Pos: Resulting geometry is %2, %3, %4, %5" + ).arg(name + ).arg(position.x + ).arg(position.y + ).arg(position.w + ).arg(position.h)); + return position.rect(); +} + } // namespace Window diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index a37988cf5a..8d8592e831 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -139,11 +139,6 @@ public: updateGlobalMenuHook(); } - [[nodiscard]] QRect countInitialGeometry( - Core::WindowPosition position, - Core::WindowPosition initial, - QSize minSize) const; - [[nodiscard]] virtual rpl::producer globalForceClicks() { return rpl::never(); } @@ -237,10 +232,19 @@ private: [[nodiscard]] Core::WindowPosition PositionWithScreen( Core::WindowPosition position, const QScreen *chosen, - QSize minimal); + QSize minimal, + const QString &name); [[nodiscard]] Core::WindowPosition PositionWithScreen( Core::WindowPosition position, not_null widget, - QSize minimal); + QSize minimal, + const QString &name); + +[[nodiscard]] QRect CountInitialGeometry( + not_null widget, + Core::WindowPosition position, + Core::WindowPosition initial, + QSize minSize, + const QString &name); } // namespace Window