Skip to content

Commit 54683b2

Browse files
ilya-fedinjohn-preston
authored andcommitted
Decompose countInitialGeomety from MainWindow
1 parent d33293b commit 54683b2

File tree

9 files changed

+155
-163
lines changed

9 files changed

+155
-163
lines changed

Telegram/SourceFiles/calls/calls_panel.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,20 @@ QRect Panel::panelGeometry() const {
177177
const auto center = Core::App().getPointForCallPanelCenter();
178178
const auto simple = QRect(0, 0, st::callWidth, st::callHeight);
179179
const auto initial = simple.translated(center - simple.center());
180-
auto result = initial;
181-
if (const auto window = Core::App().activeWindow()) {
182-
const auto initialPosition = Core::WindowPosition{
183-
.moncrc = 0,
184-
.scale = cScale(),
185-
.x = initial.x(),
186-
.y = initial.y(),
187-
.w = initial.width(),
188-
.h = initial.height(),
189-
};
190-
result = window->widget()->countInitialGeometry(
191-
adjusted,
192-
initialPosition,
193-
{ st::callWidthMin, st::callHeightMin });
194-
}
195-
return result;
180+
const auto initialPosition = Core::WindowPosition{
181+
.moncrc = 0,
182+
.scale = cScale(),
183+
.x = initial.x(),
184+
.y = initial.y(),
185+
.w = initial.width(),
186+
.h = initial.height(),
187+
};
188+
return ::Window::CountInitialGeometry(
189+
window(),
190+
adjusted,
191+
initialPosition,
192+
{ st::callWidthMin, st::callHeightMin },
193+
u"Call"_q);
196194
}
197195

198196
ConferencePanelMigration Panel::migrationInfo() const {

Telegram/SourceFiles/iv/iv_controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void Controller::createWindow() {
615615
updateTitleGeometry(width);
616616
}, _subtitle->lifetime());
617617

618-
window->setGeometry(_delegate->ivGeometry());
618+
window->setGeometry(_delegate->ivGeometry(window));
619619
window->setMinimumSize({ st::windowMinWidth, st::windowMinHeight });
620620

621621
window->geometryValue(

Telegram/SourceFiles/iv/iv_delegate.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace Iv {
1515

1616
class Delegate {
1717
public:
18-
virtual void ivSetLastSourceWindow(not_null<QWidget*> window) = 0;
19-
[[nodiscard]] virtual QRect ivGeometry() const = 0;
18+
[[nodiscard]] virtual QRect ivGeometry(not_null<Ui::RpWindow*> window) const = 0;
2019
virtual void ivSaveGeometry(not_null<Ui::RpWindow*> window) = 0;
2120

2221
[[nodiscard]] virtual int ivZoom() const = 0;

Telegram/SourceFiles/iv/iv_delegate_impl.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,16 @@ namespace {
4545

4646
} // namespace
4747

48-
void DelegateImpl::ivSetLastSourceWindow(not_null<QWidget*> window) {
49-
_lastSourceWindow = window;
50-
}
51-
52-
QRect DelegateImpl::ivGeometry() const {
53-
const auto found = _lastSourceWindow
54-
? Core::App().findWindow(_lastSourceWindow)
55-
: nullptr;
56-
48+
QRect DelegateImpl::ivGeometry(not_null<Ui::RpWindow*> window) const {
5749
const auto saved = Core::App().settings().ivPosition();
5850
const auto adjusted = Core::AdjustToScale(saved, u"IV"_q);
5951
const auto initial = DefaultPosition();
60-
auto result = initial.rect();
61-
if (const auto window = found ? found : Core::App().activeWindow()) {
62-
result = window->widget()->countInitialGeometry(
63-
adjusted,
64-
initial,
65-
{ st::ivWidthMin, st::ivHeightMin });
66-
}
67-
return result;
52+
return Window::CountInitialGeometry(
53+
window,
54+
adjusted,
55+
initial,
56+
{ st::ivWidthMin, st::ivHeightMin },
57+
u"IV"_q);
6858
}
6959

7060
void DelegateImpl::ivSaveGeometry(not_null<Ui::RpWindow*> window) {

Telegram/SourceFiles/iv/iv_delegate_impl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ class DelegateImpl final : public Delegate {
1515
public:
1616
DelegateImpl() = default;
1717

18-
void ivSetLastSourceWindow(not_null<QWidget*> window) override;
19-
[[nodiscard]] QRect ivGeometry() const override;
18+
[[nodiscard]] QRect ivGeometry(not_null<Ui::RpWindow*> window) const override;
2019
void ivSaveGeometry(not_null<Ui::RpWindow*> window) override;
2120

2221
[[nodiscard]] int ivZoom() const override;
2322
[[nodiscard]] rpl::producer<int> ivZoomValue() const override;
2423
void ivSetZoom(int value) override;
2524

26-
private:
27-
QPointer<QWidget> _lastSourceWindow;
28-
2925
};
3026

3127
} // namespace Iv

Telegram/SourceFiles/iv/iv_instance.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,6 @@ void Instance::show(
829829
not_null<Window::SessionController*> controller,
830830
not_null<Data*> data,
831831
QString hash) {
832-
_delegate->ivSetLastSourceWindow(controller->widget());
833832
show(controller->uiShow(), data, hash);
834833
}
835834

Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -958,13 +958,12 @@ void OverlayWidget::initNormalGeometry() {
958958
const auto saved = Core::App().settings().mediaViewPosition();
959959
const auto adjusted = Core::AdjustToScale(saved, u"Viewer"_q);
960960
const auto initial = DefaultPosition();
961-
_normalGeometry = initial.rect();
962-
if (const auto active = Core::App().activeWindow()) {
963-
_normalGeometry = active->widget()->countInitialGeometry(
964-
adjusted,
965-
initial,
966-
{ st::mediaviewMinWidth, st::mediaviewMinHeight });
967-
}
961+
_normalGeometry = Window::CountInitialGeometry(
962+
_window,
963+
adjusted,
964+
initial,
965+
{ st::mediaviewMinWidth, st::mediaviewMinHeight },
966+
u"Viewer"_q);
968967
}
969968

970969
void OverlayWidget::savePosition() {

Telegram/SourceFiles/window/main_window.cpp

Lines changed: 114 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -700,115 +700,12 @@ QRect MainWindow::countInitialGeometry(WindowPosition position) {
700700
.w = initialWidth,
701701
.h = initialHeight,
702702
};
703-
return countInitialGeometry(
703+
return CountInitialGeometry(
704+
this,
704705
position,
705706
initial,
706-
{ st::windowMinWidth, st::windowMinHeight });
707-
}
708-
709-
QRect MainWindow::countInitialGeometry(
710-
WindowPosition position,
711-
WindowPosition initial,
712-
QSize minSize) const {
713-
if (!position.w || !position.h) {
714-
return initial.rect();
715-
}
716-
const auto screen = [&]() -> QScreen* {
717-
for (const auto screen : QGuiApplication::screens()) {
718-
const auto sum = Platform::ScreenNameChecksum(screen->name());
719-
if (position.moncrc == sum) {
720-
return screen;
721-
}
722-
}
723-
return QGuiApplication::screenAt(position.rect().center());
724-
}();
725-
if (!screen) {
726-
return initial.rect();
727-
}
728-
const auto frame = frameMargins();
729-
const auto screenGeometry = screen->geometry();
730-
const auto availableGeometry = screen->availableGeometry();
731-
const auto spaceForInner = availableGeometry.marginsRemoved(frame);
732-
DEBUG_LOG(("Window Pos: "
733-
"Screen found, screen geometry: %1, %2, %3, %4, "
734-
"available: %5, %6, %7, %8"
735-
).arg(screenGeometry.x()
736-
).arg(screenGeometry.y()
737-
).arg(screenGeometry.width()
738-
).arg(screenGeometry.height()
739-
).arg(availableGeometry.x()
740-
).arg(availableGeometry.y()
741-
).arg(availableGeometry.width()
742-
).arg(availableGeometry.height()));
743-
DEBUG_LOG(("Window Pos: "
744-
"Window frame margins: %1, %2, %3, %4, "
745-
"available space for inner geometry: %5, %6, %7, %8"
746-
).arg(frame.left()
747-
).arg(frame.top()
748-
).arg(frame.right()
749-
).arg(frame.bottom()
750-
).arg(spaceForInner.x()
751-
).arg(spaceForInner.y()
752-
).arg(spaceForInner.width()
753-
).arg(spaceForInner.height()));
754-
755-
const auto x = spaceForInner.x()
756-
- (position.moncrc ? screenGeometry.x() : 0);
757-
const auto y = spaceForInner.y()
758-
- (position.moncrc ? screenGeometry.y() : 0);
759-
const auto w = spaceForInner.width();
760-
const auto h = spaceForInner.height();
761-
if (w < st::windowMinWidth || h < st::windowMinHeight) {
762-
return initial.rect();
763-
}
764-
if (position.x < x) position.x = x;
765-
if (position.y < y) position.y = y;
766-
if (position.w > w) position.w = w;
767-
if (position.h > h) position.h = h;
768-
const auto rightPoint = position.x + position.w;
769-
const auto screenRightPoint = x + w;
770-
if (rightPoint > screenRightPoint) {
771-
const auto distance = rightPoint - screenRightPoint;
772-
const auto newXPos = position.x - distance;
773-
if (newXPos >= x) {
774-
position.x = newXPos;
775-
} else {
776-
position.x = x;
777-
const auto newRightPoint = position.x + position.w;
778-
const auto newDistance = newRightPoint - screenRightPoint;
779-
position.w -= newDistance;
780-
}
781-
}
782-
const auto bottomPoint = position.y + position.h;
783-
const auto screenBottomPoint = y + h;
784-
if (bottomPoint > screenBottomPoint) {
785-
const auto distance = bottomPoint - screenBottomPoint;
786-
const auto newYPos = position.y - distance;
787-
if (newYPos >= y) {
788-
position.y = newYPos;
789-
} else {
790-
position.y = y;
791-
const auto newBottomPoint = position.y + position.h;
792-
const auto newDistance = newBottomPoint - screenBottomPoint;
793-
position.h -= newDistance;
794-
}
795-
}
796-
if (position.moncrc) {
797-
position.x += screenGeometry.x();
798-
position.y += screenGeometry.y();
799-
}
800-
if ((position.x + st::windowMinWidth
801-
> screenGeometry.x() + screenGeometry.width())
802-
|| (position.y + st::windowMinHeight
803-
> screenGeometry.y() + screenGeometry.height())) {
804-
return initial.rect();
805-
}
806-
DEBUG_LOG(("Window Pos: Resulting geometry is %1, %2, %3, %4"
807-
).arg(position.x
808-
).arg(position.y
809-
).arg(position.w
810-
).arg(position.h));
811-
return position.rect();
707+
{ st::windowMinWidth, st::windowMinHeight },
708+
u"Window"_q);
812709
}
813710

814711
void MainWindow::firstShow() {
@@ -1136,4 +1033,114 @@ WindowPosition PositionWithScreen(
11361033
name);
11371034
}
11381035

1036+
QRect CountInitialGeometry(
1037+
not_null<const Ui::RpWindow*> widget,
1038+
WindowPosition position,
1039+
WindowPosition initial,
1040+
QSize minSize,
1041+
const QString &name) {
1042+
if (!position.w || !position.h) {
1043+
return initial.rect();
1044+
}
1045+
const auto screen = [&]() -> QScreen* {
1046+
for (const auto screen : QGuiApplication::screens()) {
1047+
const auto sum = Platform::ScreenNameChecksum(screen->name());
1048+
if (position.moncrc == sum) {
1049+
return screen;
1050+
}
1051+
}
1052+
return QGuiApplication::screenAt(position.rect().center());
1053+
}();
1054+
if (!screen) {
1055+
return initial.rect();
1056+
}
1057+
const auto frame = widget->frameMargins();
1058+
const auto screenGeometry = screen->geometry();
1059+
const auto availableGeometry = screen->availableGeometry();
1060+
const auto spaceForInner = availableGeometry.marginsRemoved(frame);
1061+
DEBUG_LOG(("%1 Pos: "
1062+
"Screen found, screen geometry: %2, %3, %4, %5, "
1063+
"available: %6, %7, %8, %9"
1064+
).arg(name
1065+
).arg(screenGeometry.x()
1066+
).arg(screenGeometry.y()
1067+
).arg(screenGeometry.width()
1068+
).arg(screenGeometry.height()
1069+
).arg(availableGeometry.x()
1070+
).arg(availableGeometry.y()
1071+
).arg(availableGeometry.width()
1072+
).arg(availableGeometry.height()));
1073+
DEBUG_LOG(("%1 Pos: "
1074+
"Window frame margins: %2, %3, %4, %5, "
1075+
"available space for inner geometry: %6, %7, %8, %9"
1076+
).arg(name
1077+
).arg(frame.left()
1078+
).arg(frame.top()
1079+
).arg(frame.right()
1080+
).arg(frame.bottom()
1081+
).arg(spaceForInner.x()
1082+
).arg(spaceForInner.y()
1083+
).arg(spaceForInner.width()
1084+
).arg(spaceForInner.height()));
1085+
1086+
const auto x = spaceForInner.x()
1087+
- (position.moncrc ? screenGeometry.x() : 0);
1088+
const auto y = spaceForInner.y()
1089+
- (position.moncrc ? screenGeometry.y() : 0);
1090+
const auto w = spaceForInner.width();
1091+
const auto h = spaceForInner.height();
1092+
if (w < st::windowMinWidth || h < st::windowMinHeight) {
1093+
return initial.rect();
1094+
}
1095+
if (position.x < x) position.x = x;
1096+
if (position.y < y) position.y = y;
1097+
if (position.w > w) position.w = w;
1098+
if (position.h > h) position.h = h;
1099+
const auto rightPoint = position.x + position.w;
1100+
const auto screenRightPoint = x + w;
1101+
if (rightPoint > screenRightPoint) {
1102+
const auto distance = rightPoint - screenRightPoint;
1103+
const auto newXPos = position.x - distance;
1104+
if (newXPos >= x) {
1105+
position.x = newXPos;
1106+
} else {
1107+
position.x = x;
1108+
const auto newRightPoint = position.x + position.w;
1109+
const auto newDistance = newRightPoint - screenRightPoint;
1110+
position.w -= newDistance;
1111+
}
1112+
}
1113+
const auto bottomPoint = position.y + position.h;
1114+
const auto screenBottomPoint = y + h;
1115+
if (bottomPoint > screenBottomPoint) {
1116+
const auto distance = bottomPoint - screenBottomPoint;
1117+
const auto newYPos = position.y - distance;
1118+
if (newYPos >= y) {
1119+
position.y = newYPos;
1120+
} else {
1121+
position.y = y;
1122+
const auto newBottomPoint = position.y + position.h;
1123+
const auto newDistance = newBottomPoint - screenBottomPoint;
1124+
position.h -= newDistance;
1125+
}
1126+
}
1127+
if (position.moncrc) {
1128+
position.x += screenGeometry.x();
1129+
position.y += screenGeometry.y();
1130+
}
1131+
if ((position.x + st::windowMinWidth
1132+
> screenGeometry.x() + screenGeometry.width())
1133+
|| (position.y + st::windowMinHeight
1134+
> screenGeometry.y() + screenGeometry.height())) {
1135+
return initial.rect();
1136+
}
1137+
DEBUG_LOG(("%1 Pos: Resulting geometry is %2, %3, %4, %5"
1138+
).arg(name
1139+
).arg(position.x
1140+
).arg(position.y
1141+
).arg(position.w
1142+
).arg(position.h));
1143+
return position.rect();
1144+
}
1145+
11391146
} // namespace Window

0 commit comments

Comments
 (0)