iv_delegate_impl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "iv/iv_delegate_impl.h"
  8. #include "core/application.h"
  9. #include "core/core_settings.h"
  10. #include "mainwindow.h"
  11. #include "window/main_window.h"
  12. #include "window/window_controller.h"
  13. #include "styles/style_window.h"
  14. #include <QtGui/QGuiApplication>
  15. #include <QtGui/QScreen>
  16. #include <QtGui/QWindow>
  17. namespace Iv {
  18. namespace {
  19. [[nodiscard]] Core::WindowPosition DefaultPosition() {
  20. auto center = qApp->primaryScreen()->geometry().center();
  21. const auto moncrc = [&] {
  22. if (const auto active = Core::App().activeWindow()) {
  23. const auto widget = active->widget();
  24. center = widget->geometry().center();
  25. if (const auto screen = widget->screen()) {
  26. return Platform::ScreenNameChecksum(screen->name());
  27. }
  28. }
  29. return Core::App().settings().windowPosition().moncrc;
  30. }();
  31. return {
  32. .moncrc = moncrc,
  33. .scale = cScale(),
  34. .x = (center.x() - st::ivWidthDefault / 2),
  35. .y = (center.y() - st::ivHeightDefault / 2),
  36. .w = st::ivWidthDefault,
  37. .h = st::ivHeightDefault,
  38. };
  39. }
  40. } // namespace
  41. void DelegateImpl::ivSetLastSourceWindow(not_null<QWidget*> window) {
  42. _lastSourceWindow = window;
  43. }
  44. QRect DelegateImpl::ivGeometry() const {
  45. const auto found = _lastSourceWindow
  46. ? Core::App().findWindow(_lastSourceWindow)
  47. : nullptr;
  48. const auto saved = Core::App().settings().ivPosition();
  49. const auto adjusted = Core::AdjustToScale(saved, u"IV"_q);
  50. const auto initial = DefaultPosition();
  51. auto result = initial.rect();
  52. if (const auto window = found ? found : Core::App().activeWindow()) {
  53. result = window->widget()->countInitialGeometry(
  54. adjusted,
  55. initial,
  56. { st::ivWidthMin, st::ivHeightMin });
  57. }
  58. return result;
  59. }
  60. void DelegateImpl::ivSaveGeometry(not_null<Ui::RpWindow*> window) {
  61. if (!window->windowHandle()) {
  62. return;
  63. }
  64. const auto state = window->windowHandle()->windowState();
  65. if (state == Qt::WindowMinimized) {
  66. return;
  67. }
  68. const auto &savedPosition = Core::App().settings().ivPosition();
  69. auto realPosition = savedPosition;
  70. if (state == Qt::WindowMaximized) {
  71. realPosition.maximized = 1;
  72. realPosition.moncrc = 0;
  73. DEBUG_LOG(("IV Pos: Saving maximized position."));
  74. } else {
  75. auto r = window->body()->mapToGlobal(window->body()->rect());
  76. realPosition.x = r.x();
  77. realPosition.y = r.y();
  78. realPosition.w = r.width();
  79. realPosition.h = r.height();
  80. realPosition.scale = cScale();
  81. realPosition.maximized = 0;
  82. realPosition.moncrc = 0;
  83. DEBUG_LOG(("IV Pos: "
  84. "Saving non-maximized position: %1, %2, %3, %4"
  85. ).arg(realPosition.x
  86. ).arg(realPosition.y
  87. ).arg(realPosition.w
  88. ).arg(realPosition.h));
  89. }
  90. realPosition = Window::PositionWithScreen(
  91. realPosition,
  92. window,
  93. { st::ivWidthMin, st::ivHeightMin });
  94. if (realPosition.w >= st::ivWidthMin
  95. && realPosition.h >= st::ivHeightMin
  96. && realPosition != savedPosition) {
  97. DEBUG_LOG(("IV Pos: "
  98. "Writing: %1, %2, %3, %4 (scale %5%, maximized %6)")
  99. .arg(realPosition.x)
  100. .arg(realPosition.y)
  101. .arg(realPosition.w)
  102. .arg(realPosition.h)
  103. .arg(realPosition.scale)
  104. .arg(Logs::b(realPosition.maximized)));
  105. Core::App().settings().setIvPosition(realPosition);
  106. Core::App().saveSettingsDelayed();
  107. }
  108. }
  109. int DelegateImpl::ivZoom() const {
  110. return Core::App().settings().ivZoom();
  111. }
  112. rpl::producer<int> DelegateImpl::ivZoomValue() const {
  113. return Core::App().settings().ivZoomValue();
  114. }
  115. void DelegateImpl::ivSetZoom(int value) {
  116. Core::App().settings().setIvZoom(value);
  117. Core::App().saveSettingsDelayed();
  118. }
  119. } // namespace Iv