cached_round_corners.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "ui/cached_round_corners.h"
  8. #include "ui/chat/chat_style.h"
  9. #include "ui/painter.h"
  10. #include "ui/ui_utility.h"
  11. #include "ui/image/image_prepare.h"
  12. #include "styles/style_chat.h"
  13. #include "styles/style_layers.h"
  14. #include "styles/style_overview.h"
  15. #include "styles/style_media_view.h"
  16. #include "styles/style_chat_helpers.h"
  17. namespace Ui {
  18. namespace {
  19. constexpr auto kCachedCornerRadiusCount = int(CachedCornerRadius::kCount);
  20. std::vector<CornersPixmaps> Corners;
  21. QImage CornersMaskLarge[4], CornersMaskSmall[4];
  22. rpl::lifetime PaletteChangedLifetime;
  23. std::array<std::array<QImage, 4>, kCachedCornerRadiusCount> CachedMasks;
  24. [[nodiscard]] std::array<QImage, 4> PrepareCorners(int32 radius, const QBrush &brush, const style::color *shadow = nullptr) {
  25. int32 r = radius * style::DevicePixelRatio(), s = st::msgShadow * style::DevicePixelRatio();
  26. QImage rect(r * 3, r * 3 + (shadow ? s : 0), QImage::Format_ARGB32_Premultiplied);
  27. rect.fill(Qt::transparent);
  28. {
  29. auto p = QPainter(&rect);
  30. PainterHighQualityEnabler hq(p);
  31. p.setCompositionMode(QPainter::CompositionMode_Source);
  32. p.setPen(Qt::NoPen);
  33. if (shadow) {
  34. p.setBrush((*shadow)->b);
  35. p.drawRoundedRect(0, s, r * 3, r * 3, r, r);
  36. }
  37. p.setBrush(brush);
  38. p.drawRoundedRect(0, 0, r * 3, r * 3, r, r);
  39. }
  40. auto result = std::array<QImage, 4>();
  41. result[0] = rect.copy(0, 0, r, r);
  42. result[1] = rect.copy(r * 2, 0, r, r);
  43. result[2] = rect.copy(0, r * 2, r, r + (shadow ? s : 0));
  44. result[3] = rect.copy(r * 2, r * 2, r, r + (shadow ? s : 0));
  45. return result;
  46. }
  47. void PrepareCorners(CachedRoundCorners index, int32 radius, const QBrush &brush, const style::color *shadow = nullptr) {
  48. Expects(index < Corners.size());
  49. auto images = PrepareCorners(radius, brush, shadow);
  50. for (int i = 0; i < 4; ++i) {
  51. Corners[index].p[i] = PixmapFromImage(std::move(images[i]));
  52. Corners[index].p[i].setDevicePixelRatio(style::DevicePixelRatio());
  53. }
  54. }
  55. void CreateMaskCorners() {
  56. auto mask = PrepareCorners(st::roundRadiusSmall, QColor(255, 255, 255), nullptr);
  57. for (int i = 0; i < 4; ++i) {
  58. CornersMaskSmall[i] = mask[i].convertToFormat(QImage::Format_ARGB32_Premultiplied);
  59. CornersMaskSmall[i].setDevicePixelRatio(style::DevicePixelRatio());
  60. }
  61. mask = PrepareCorners(st::roundRadiusLarge, QColor(255, 255, 255), nullptr);
  62. for (int i = 0; i < 4; ++i) {
  63. CornersMaskLarge[i] = mask[i].convertToFormat(QImage::Format_ARGB32_Premultiplied);
  64. CornersMaskLarge[i].setDevicePixelRatio(style::DevicePixelRatio());
  65. }
  66. }
  67. void CreatePaletteCorners() {
  68. PrepareCorners(MenuCorners, st::roundRadiusSmall, st::menuBg);
  69. PrepareCorners(BoxCorners, st::boxRadius, st::boxBg);
  70. PrepareCorners(DateCorners, st::dateRadius, st::msgDateImgBg);
  71. PrepareCorners(OverviewVideoCorners, st::overviewVideoStatusRadius, st::msgDateImgBg);
  72. PrepareCorners(OverviewVideoSelectedCorners, st::overviewVideoStatusRadius, st::msgDateImgBgSelected);
  73. PrepareCorners(ForwardCorners, st::roundRadiusLarge, st::historyForwardChooseBg);
  74. PrepareCorners(MediaviewSaveCorners, st::mediaviewControllerRadius, st::mediaviewSaveMsgBg);
  75. PrepareCorners(StickerHoverCorners, st::roundRadiusSmall, st::emojiPanHover);
  76. PrepareCorners(BotKeyboardCorners, st::roundRadiusSmall, st::botKbBg);
  77. PrepareCorners(Doc1Corners, st::roundRadiusSmall, st::msgFile1Bg);
  78. PrepareCorners(Doc2Corners, st::roundRadiusSmall, st::msgFile2Bg);
  79. PrepareCorners(Doc3Corners, st::roundRadiusSmall, st::msgFile3Bg);
  80. PrepareCorners(Doc4Corners, st::roundRadiusSmall, st::msgFile4Bg);
  81. }
  82. } // namespace
  83. void StartCachedCorners() {
  84. Corners.resize(RoundCornersCount);
  85. CreateMaskCorners();
  86. CreatePaletteCorners();
  87. style::PaletteChanged(
  88. ) | rpl::start_with_next([=] {
  89. CreatePaletteCorners();
  90. }, PaletteChangedLifetime);
  91. }
  92. void FinishCachedCorners() {
  93. Corners.clear();
  94. PaletteChangedLifetime.destroy();
  95. }
  96. void FillRoundRect(QPainter &p, int32 x, int32 y, int32 w, int32 h, style::color bg, const CornersPixmaps &corners) {
  97. using namespace Images;
  98. const auto fillBg = [&](QRect rect) {
  99. p.fillRect(rect, bg);
  100. };
  101. const auto fillCorner = [&](int x, int y, int index) {
  102. if (const auto &pix = corners.p[index]; !pix.isNull()) {
  103. p.drawPixmap(x, y, pix);
  104. }
  105. };
  106. if (corners.p[kTopLeft].isNull()
  107. && corners.p[kTopRight].isNull()
  108. && corners.p[kBottomLeft].isNull()
  109. && corners.p[kBottomRight].isNull()) {
  110. p.fillRect(x, y, w, h, bg);
  111. return;
  112. }
  113. const auto ratio = style::DevicePixelRatio();
  114. const auto cornerSize = [&](int index) {
  115. return corners.p[index].isNull()
  116. ? 0
  117. : (corners.p[index].width() / ratio);
  118. };
  119. const auto verticalSkip = [&](int left, int right) {
  120. return std::max(cornerSize(left), cornerSize(right));
  121. };
  122. const auto top = verticalSkip(kTopLeft, kTopRight);
  123. const auto bottom = verticalSkip(kBottomLeft, kBottomRight);
  124. if (top) {
  125. const auto left = cornerSize(kTopLeft);
  126. const auto right = cornerSize(kTopRight);
  127. if (left) {
  128. fillCorner(x, y, kTopLeft);
  129. if (const auto add = top - left) {
  130. fillBg({ x, y + left, left, add });
  131. }
  132. }
  133. if (const auto fill = w - left - right; fill > 0) {
  134. fillBg({ x + left, y, fill, top });
  135. }
  136. if (right) {
  137. fillCorner(x + w - right, y, kTopRight);
  138. if (const auto add = top - right) {
  139. fillBg({ x + w - right, y + right, right, add });
  140. }
  141. }
  142. }
  143. if (const auto fill = h - top - bottom; fill > 0) {
  144. fillBg({ x, y + top, w, fill });
  145. }
  146. if (bottom) {
  147. const auto left = cornerSize(kBottomLeft);
  148. const auto right = cornerSize(kBottomRight);
  149. if (left) {
  150. fillCorner(x, y + h - left, kBottomLeft);
  151. if (const auto add = bottom - left) {
  152. fillBg({ x, y + h - bottom, left, add });
  153. }
  154. }
  155. if (const auto fill = w - left - right; fill > 0) {
  156. fillBg({ x + left, y + h - bottom, fill, bottom });
  157. }
  158. if (right) {
  159. fillCorner(x + w - right, y + h - right, kBottomRight);
  160. if (const auto add = bottom - right) {
  161. fillBg({ x + w - right, y + h - bottom, right, add });
  162. }
  163. }
  164. }
  165. }
  166. void FillRoundRect(QPainter &p, int32 x, int32 y, int32 w, int32 h, style::color bg, CachedRoundCorners index) {
  167. FillRoundRect(p, x, y, w, h, bg, CachedCornerPixmaps(index));
  168. }
  169. void FillRoundShadow(QPainter &p, int32 x, int32 y, int32 w, int32 h, style::color shadow, const CornersPixmaps &corners) {
  170. constexpr auto kLeft = 2;
  171. constexpr auto kRight = 3;
  172. const auto ratio = style::DevicePixelRatio();
  173. const auto size = [&](int index) {
  174. const auto &pix = corners.p[index];
  175. return pix.isNull() ? 0 : (pix.width() / ratio);
  176. };
  177. const auto fillCorner = [&](int left, int bottom, int index) {
  178. const auto &pix = corners.p[index];
  179. if (pix.isNull()) {
  180. return;
  181. }
  182. const auto size = pix.width() / ratio;
  183. p.drawPixmap(left, bottom - size, pix);
  184. };
  185. const auto left = size(kLeft);
  186. const auto right = size(kRight);
  187. const auto from = x + left;
  188. fillCorner(x, y + h + st::msgShadow, kLeft);
  189. if (const auto width = w - left - right; width > 0) {
  190. p.fillRect(from, y + h, width, st::msgShadow, shadow);
  191. }
  192. fillCorner(x + w - right, y + h + st::msgShadow, kRight);
  193. }
  194. const CornersPixmaps &CachedCornerPixmaps(CachedRoundCorners index) {
  195. Expects(index >= 0 && index < RoundCornersCount);
  196. return Corners[index];
  197. }
  198. CornersPixmaps PrepareCornerPixmaps(int radius, style::color bg, const style::color *sh) {
  199. auto images = PrepareCorners(radius, bg, sh);
  200. auto result = CornersPixmaps();
  201. for (int j = 0; j < 4; ++j) {
  202. result.p[j] = PixmapFromImage(std::move(images[j]));
  203. result.p[j].setDevicePixelRatio(style::DevicePixelRatio());
  204. }
  205. return result;
  206. }
  207. CornersPixmaps PrepareCornerPixmaps(ImageRoundRadius radius, style::color bg, const style::color *sh) {
  208. switch (radius) {
  209. case ImageRoundRadius::Small:
  210. return PrepareCornerPixmaps(st::roundRadiusSmall, bg, sh);
  211. case ImageRoundRadius::Large:
  212. return PrepareCornerPixmaps(st::roundRadiusLarge, bg, sh);
  213. }
  214. Unexpected("Image round radius in PrepareCornerPixmaps.");
  215. }
  216. CornersPixmaps PrepareInvertedCornerPixmaps(int radius, style::color bg) {
  217. const auto size = radius * style::DevicePixelRatio();
  218. auto circle = style::colorizeImage(
  219. style::createInvertedCircleMask(radius * 2),
  220. bg);
  221. circle.setDevicePixelRatio(style::DevicePixelRatio());
  222. auto result = CornersPixmaps();
  223. const auto fill = [&](int index, int xoffset, int yoffset) {
  224. result.p[index] = PixmapFromImage(
  225. circle.copy(QRect(xoffset, yoffset, size, size)));
  226. };
  227. fill(0, 0, 0);
  228. fill(1, size, 0);
  229. fill(2, size, size);
  230. fill(3, 0, size);
  231. return result;
  232. }
  233. [[nodiscard]] int CachedCornerRadiusValue(CachedCornerRadius tag) {
  234. using Radius = CachedCornerRadius;
  235. switch (tag) {
  236. case Radius::Small: return st::roundRadiusSmall;
  237. case Radius::ThumbSmall: return MsgFileThumbRadiusSmall();
  238. case Radius::ThumbLarge: return MsgFileThumbRadiusLarge();
  239. case Radius::BubbleSmall: return BubbleRadiusSmall();
  240. case Radius::BubbleLarge: return BubbleRadiusLarge();
  241. }
  242. Unexpected("Radius tag in CachedCornerRadiusValue.");
  243. }
  244. [[nodiscard]] const std::array<QImage, 4> &CachedCornersMasks(
  245. CachedCornerRadius radius) {
  246. const auto index = static_cast<int>(radius);
  247. Assert(index >= 0 && index < kCachedCornerRadiusCount);
  248. if (CachedMasks[index][0].isNull()) {
  249. CachedMasks[index] = Images::CornersMask(
  250. CachedCornerRadiusValue(CachedCornerRadius(index)));
  251. }
  252. return CachedMasks[index];
  253. }
  254. } // namespace Ui