round_checkbox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/effects/round_checkbox.h"
  8. #include "ui/rp_widget.h"
  9. #include "ui/ui_utility.h"
  10. #include "ui/painter.h"
  11. #include "ui/effects/outline_segments.h"
  12. #include "styles/style_widgets.h"
  13. #include <QtCore/QCoreApplication>
  14. namespace Ui {
  15. namespace {
  16. constexpr auto kAnimationTimerDelta = crl::time(7);
  17. constexpr auto kWideScale = 3;
  18. [[nodiscard]] int CountFramesCount(const style::RoundCheckbox *st) {
  19. return (st->duration / kAnimationTimerDelta) + 1;
  20. }
  21. class CheckCaches : public QObject {
  22. public:
  23. CheckCaches(QObject *parent) : QObject(parent) {
  24. Expects(parent != nullptr);
  25. style::PaletteChanged(
  26. ) | rpl::start_with_next([=] {
  27. _data.clear();
  28. }, _lifetime);
  29. }
  30. QPixmap frame(
  31. const style::RoundCheckbox *st,
  32. bool displayInactive,
  33. float64 progress);
  34. private:
  35. struct Frames {
  36. bool displayInactive = false;
  37. std::vector<QPixmap> list;
  38. QPixmap outerWide;
  39. QPixmap inner;
  40. QPixmap check;
  41. };
  42. Frames &framesForStyle(
  43. const style::RoundCheckbox *st,
  44. bool displayInactive);
  45. void prepareFramesData(
  46. const style::RoundCheckbox *st,
  47. bool displayInactive,
  48. Frames &frames);
  49. QPixmap paintFrame(
  50. const style::RoundCheckbox *st,
  51. const Frames &frames,
  52. float64 progress);
  53. std::map<const style::RoundCheckbox *, Frames> _data;
  54. rpl::lifetime _lifetime;
  55. };
  56. QPixmap PrepareOuterWide(const style::RoundCheckbox *st) {
  57. const auto size = st->size;
  58. const auto wideSize = size * kWideScale;
  59. auto result = QImage(
  60. QSize(wideSize, wideSize) * style::DevicePixelRatio(),
  61. QImage::Format_ARGB32_Premultiplied);
  62. result.setDevicePixelRatio(style::DevicePixelRatio());
  63. result.fill(Qt::transparent);
  64. {
  65. auto p = QPainter(&result);
  66. PainterHighQualityEnabler hq(p);
  67. p.setPen(Qt::NoPen);
  68. p.setBrush(st->border);
  69. const auto half = st->width / 2.;
  70. p.drawEllipse(QRectF(
  71. (wideSize - size) / 2 - half,
  72. (wideSize - size) / 2 - half,
  73. size + 2. * half,
  74. size + 2. * half));
  75. }
  76. return Ui::PixmapFromImage(std::move(result));
  77. }
  78. QPixmap PrepareInner(const style::RoundCheckbox *st, bool displayInactive) {
  79. const auto size = st->size;
  80. auto result = QImage(
  81. QSize(size, size) * style::DevicePixelRatio(),
  82. QImage::Format_ARGB32_Premultiplied);
  83. result.setDevicePixelRatio(style::DevicePixelRatio());
  84. result.fill(Qt::transparent);
  85. {
  86. auto p = QPainter(&result);
  87. PainterHighQualityEnabler hq(p);
  88. p.setPen(Qt::NoPen);
  89. p.setBrush(st->bgActive);
  90. const auto half = st->width / 2.;
  91. p.drawEllipse(QRectF(
  92. displayInactive ? 0. : half,
  93. displayInactive ? 0. : half,
  94. size - (displayInactive ? 0. : 2. * half),
  95. size - (displayInactive ? 0. : 2. * half)));
  96. }
  97. return Ui::PixmapFromImage(std::move(result));
  98. }
  99. QPixmap PrepareCheck(const style::RoundCheckbox *st) {
  100. const auto size = st->size;
  101. auto result = QImage(
  102. QSize(size, size) * style::DevicePixelRatio(),
  103. QImage::Format_ARGB32_Premultiplied);
  104. result.setDevicePixelRatio(style::DevicePixelRatio());
  105. result.fill(Qt::transparent);
  106. {
  107. auto p = QPainter(&result);
  108. st->check.paint(p, 0, 0, size);
  109. }
  110. return Ui::PixmapFromImage(std::move(result));
  111. }
  112. QRect WideDestRect(
  113. const style::RoundCheckbox *st,
  114. int x,
  115. int y,
  116. float64 scale) {
  117. auto iconSizeFull = kWideScale * st->size;
  118. auto iconSize = qRound(iconSizeFull * scale);
  119. if (iconSize % 2 != iconSizeFull % 2) {
  120. ++iconSize;
  121. }
  122. auto iconShift = (iconSizeFull - iconSize) / 2;
  123. auto iconLeft = x - (kWideScale - 1) * st->size / 2 + iconShift;
  124. auto iconTop = y - (kWideScale - 1) * st->size / 2 + iconShift;
  125. return QRect(iconLeft, iconTop, iconSize, iconSize);
  126. }
  127. CheckCaches::Frames &CheckCaches::framesForStyle(
  128. const style::RoundCheckbox *st,
  129. bool displayInactive) {
  130. auto i = _data.find(st);
  131. if (i == _data.end()) {
  132. i = _data.emplace(st, Frames()).first;
  133. prepareFramesData(st, displayInactive, i->second);
  134. } else if (i->second.displayInactive != displayInactive) {
  135. i->second = Frames();
  136. prepareFramesData(st, displayInactive, i->second);
  137. }
  138. return i->second;
  139. }
  140. void CheckCaches::prepareFramesData(
  141. const style::RoundCheckbox *st,
  142. bool displayInactive,
  143. Frames &frames) {
  144. frames.list.resize(CountFramesCount(st));
  145. frames.displayInactive = displayInactive;
  146. if (!frames.displayInactive) {
  147. frames.outerWide = PrepareOuterWide(st);
  148. }
  149. frames.inner = PrepareInner(st, frames.displayInactive);
  150. frames.check = PrepareCheck(st);
  151. }
  152. QPixmap CheckCaches::frame(
  153. const style::RoundCheckbox *st,
  154. bool displayInactive,
  155. float64 progress) {
  156. auto &frames = framesForStyle(st, displayInactive);
  157. const auto frameCount = int(frames.list.size());
  158. const auto frameIndex = int(base::SafeRound(progress * (frameCount - 1)));
  159. Assert(frameIndex >= 0 && frameIndex < frameCount);
  160. if (!frames.list[frameIndex]) {
  161. const auto frameProgress = frameIndex / float64(frameCount - 1);
  162. frames.list[frameIndex] = paintFrame(st, frames, frameProgress);
  163. }
  164. return frames.list[frameIndex];
  165. }
  166. QPixmap CheckCaches::paintFrame(
  167. const style::RoundCheckbox *st,
  168. const Frames &frames,
  169. float64 progress) {
  170. const auto size = st->size;
  171. const auto wideSize = size * kWideScale;
  172. const auto skip = (wideSize - size) / 2;
  173. auto result = QImage(wideSize * style::DevicePixelRatio(), wideSize * style::DevicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
  174. result.setDevicePixelRatio(style::DevicePixelRatio());
  175. result.fill(Qt::transparent);
  176. const auto roundProgress = (progress >= st->bgDuration)
  177. ? 1.
  178. : (progress / st->bgDuration);
  179. const auto checkProgress = (1. - progress >= st->fgDuration)
  180. ? 0.
  181. : (1. - (1. - progress) / st->fgDuration);
  182. {
  183. auto p = QPainter(&result);
  184. PainterHighQualityEnabler hq(p);
  185. if (!frames.displayInactive) {
  186. const auto outerMaxScale = (size - st->width) / float64(size);
  187. const auto outerScale = roundProgress
  188. + (1. - roundProgress) * outerMaxScale;
  189. const auto outerTo = WideDestRect(st, skip, skip, outerScale);
  190. const auto outerFrom = QRect(
  191. QPoint(0, 0),
  192. QSize(wideSize, wideSize) * style::DevicePixelRatio());
  193. p.drawPixmap(outerTo, frames.outerWide, outerFrom);
  194. }
  195. p.drawPixmap(skip, skip, frames.inner);
  196. const auto divider = checkProgress * st->size;
  197. const auto checkTo = QRect(skip, skip, divider, st->size);
  198. const auto checkFrom = QRect(
  199. QPoint(0, 0),
  200. QSize(divider, st->size) * style::DevicePixelRatio());
  201. p.drawPixmap(checkTo, frames.check, checkFrom);
  202. p.setCompositionMode(QPainter::CompositionMode_Source);
  203. p.setPen(Qt::NoPen);
  204. p.setBrush(Qt::transparent);
  205. const auto remove = size * (1. - roundProgress);
  206. p.drawEllipse(QRectF(
  207. (wideSize - remove) / 2.,
  208. (wideSize - remove) / 2.,
  209. remove,
  210. remove));
  211. }
  212. return Ui::PixmapFromImage(std::move(result));
  213. }
  214. CheckCaches *FrameCaches() {
  215. static QPointer<CheckCaches> Instance;
  216. if (const auto instance = Instance.data()) {
  217. return instance;
  218. }
  219. const auto result = new CheckCaches(QCoreApplication::instance());
  220. Instance = result;
  221. return result;
  222. }
  223. } // namespace
  224. RoundCheckbox::RoundCheckbox(const style::RoundCheckbox &st, Fn<void()> updateCallback)
  225. : _st(st)
  226. , _updateCallback(updateCallback) {
  227. }
  228. void RoundCheckbox::paint(QPainter &p, int x, int y, int outerWidth, float64 masterScale) const {
  229. if (!_st.size
  230. || (!_checkedProgress.animating()
  231. && !_checked
  232. && !_displayInactive)) {
  233. return;
  234. }
  235. auto cacheSize = kWideScale * _st.size * style::DevicePixelRatio();
  236. auto cacheFrom = QRect(0, 0, cacheSize, cacheSize);
  237. auto inactiveTo = WideDestRect(&_st, x, y, masterScale);
  238. PainterHighQualityEnabler hq(p);
  239. if (!_inactiveCacheBg.isNull()) {
  240. p.drawPixmap(inactiveTo, _inactiveCacheBg, cacheFrom);
  241. }
  242. const auto progress = _checkedProgress.value(_checked ? 1. : 0.);
  243. if (progress > 0.) {
  244. auto frame = FrameCaches()->frame(&_st, _displayInactive, progress);
  245. p.drawPixmap(inactiveTo, frame, cacheFrom);
  246. }
  247. if (!_inactiveCacheFg.isNull()) {
  248. p.drawPixmap(inactiveTo, _inactiveCacheFg, cacheFrom);
  249. }
  250. }
  251. void RoundCheckbox::setChecked(bool newChecked, anim::type animated) {
  252. if (_checked == newChecked) {
  253. if (animated == anim::type::instant) {
  254. _checkedProgress.stop();
  255. }
  256. return;
  257. }
  258. _checked = newChecked;
  259. if (animated == anim::type::normal) {
  260. _checkedProgress.start(
  261. _updateCallback,
  262. _checked ? 0. : 1.,
  263. _checked ? 1. : 0.,
  264. _st.duration,
  265. anim::linear);
  266. } else {
  267. _checkedProgress.stop();
  268. }
  269. }
  270. void RoundCheckbox::invalidateCache() {
  271. if (!_inactiveCacheBg.isNull() || !_inactiveCacheFg.isNull()) {
  272. prepareInactiveCache();
  273. }
  274. }
  275. void RoundCheckbox::setDisplayInactive(bool displayInactive) {
  276. if (_displayInactive != displayInactive) {
  277. _displayInactive = displayInactive;
  278. if (_displayInactive) {
  279. prepareInactiveCache();
  280. } else {
  281. _inactiveCacheBg = _inactiveCacheFg = QPixmap();
  282. }
  283. }
  284. }
  285. void RoundCheckbox::prepareInactiveCache() {
  286. auto wideSize = _st.size * kWideScale;
  287. auto ellipse = QRect((wideSize - _st.size) / 2, (wideSize - _st.size) / 2, _st.size, _st.size);
  288. auto cacheBg = QImage(wideSize * style::DevicePixelRatio(), wideSize * style::DevicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
  289. cacheBg.setDevicePixelRatio(style::DevicePixelRatio());
  290. cacheBg.fill(Qt::transparent);
  291. auto cacheFg = cacheBg;
  292. if (_st.bgInactive) {
  293. auto p = QPainter(&cacheBg);
  294. PainterHighQualityEnabler hq(p);
  295. p.setPen(Qt::NoPen);
  296. p.setBrush(_st.bgInactive);
  297. p.drawEllipse(ellipse);
  298. }
  299. _inactiveCacheBg = Ui::PixmapFromImage(std::move(cacheBg));
  300. {
  301. auto p = QPainter(&cacheFg);
  302. PainterHighQualityEnabler hq(p);
  303. auto pen = _st.border->p;
  304. pen.setWidth(_st.width);
  305. p.setPen(pen);
  306. p.setBrush(Qt::NoBrush);
  307. p.drawEllipse(ellipse);
  308. }
  309. _inactiveCacheFg = Ui::PixmapFromImage(std::move(cacheFg));
  310. }
  311. RoundImageCheckbox::RoundImageCheckbox(
  312. const style::RoundImageCheckbox &st,
  313. Fn<void()> updateCallback,
  314. PaintRoundImage &&paintRoundImage,
  315. Fn<std::optional<int>(int size)> roundingRadius)
  316. : _st(st)
  317. , _updateCallback(updateCallback)
  318. , _paintRoundImage(std::move(paintRoundImage))
  319. , _roundingRadius(std::move(roundingRadius))
  320. , _check(_st.check, _updateCallback) {
  321. }
  322. RoundImageCheckbox::RoundImageCheckbox(RoundImageCheckbox&&) = default;
  323. RoundImageCheckbox::~RoundImageCheckbox() = default;
  324. void RoundImageCheckbox::paint(Painter &p, int x, int y, int outerWidth) const {
  325. auto selectionLevel = _selection.value(checked() ? 1. : 0.);
  326. if (_selection.animating()) {
  327. auto userpicRadius = qRound(kWideScale * (_st.imageRadius + (_st.imageSmallRadius - _st.imageRadius) * selectionLevel));
  328. auto userpicShift = kWideScale * _st.imageRadius - userpicRadius;
  329. auto userpicLeft = x - (kWideScale - 1) * _st.imageRadius + userpicShift;
  330. auto userpicTop = y - (kWideScale - 1) * _st.imageRadius + userpicShift;
  331. auto to = QRect(userpicLeft, userpicTop, userpicRadius * 2, userpicRadius * 2);
  332. auto from = QRect(QPoint(0, 0), _wideCache.size());
  333. PainterHighQualityEnabler hq(p);
  334. p.drawPixmapLeft(to, outerWidth, _wideCache, from);
  335. } else {
  336. auto userpicRadius = checked() ? _st.imageSmallRadius : _st.imageRadius;
  337. auto userpicShift = _st.imageRadius - userpicRadius;
  338. auto userpicLeft = x + userpicShift;
  339. auto userpicTop = y + userpicShift;
  340. _paintRoundImage(p, userpicLeft, userpicTop, outerWidth, userpicRadius * 2);
  341. }
  342. if (selectionLevel > 0) {
  343. PainterHighQualityEnabler hq(p);
  344. p.setOpacity(std::clamp(selectionLevel, 0., 1.));
  345. p.setBrush(Qt::NoBrush);
  346. const auto segments = int(_segments.size());
  347. const auto rect = style::rtlrect(
  348. x,
  349. y,
  350. _st.imageRadius * 2,
  351. _st.imageRadius * 2,
  352. outerWidth);
  353. const auto add = _st.selectExtendTwice / 2.;
  354. const auto outline = QRectF(rect).marginsAdded({
  355. add, add, add, add });
  356. if (segments < 2) {
  357. const auto radius = _roundingRadius
  358. ? _roundingRadius(_st.imageRadius * 2)
  359. : std::optional<int>();
  360. const auto pen = QPen(
  361. segments ? _segments.front().brush : _st.selectFg->b,
  362. segments ? _segments.front().width : _st.selectWidth);
  363. p.setPen(pen);
  364. if (!radius) {
  365. p.drawEllipse(outline);
  366. } else {
  367. p.drawRoundedRect(outline, *radius, *radius);
  368. }
  369. } else {
  370. PaintOutlineSegments(p, outline, _segments);
  371. }
  372. p.setOpacity(1.);
  373. }
  374. if (_st.check.size > 0) {
  375. auto iconLeft = x + 2 * _st.imageRadius + _st.selectWidth - _st.check.size;
  376. auto iconTop = y + 2 * _st.imageRadius + _st.selectWidth - _st.check.size;
  377. _check.paint(p, iconLeft, iconTop, outerWidth);
  378. }
  379. }
  380. float64 RoundImageCheckbox::checkedAnimationRatio() const {
  381. return std::clamp(_selection.value(checked() ? 1. : 0.), 0., 1.);
  382. }
  383. void RoundImageCheckbox::setChecked(bool newChecked, anim::type animated) {
  384. auto changed = (checked() != newChecked);
  385. _check.setChecked(newChecked, animated);
  386. if (!changed) {
  387. if (animated == anim::type::instant) {
  388. _selection.stop();
  389. _wideCache = QPixmap();
  390. }
  391. return;
  392. }
  393. if (animated == anim::type::normal) {
  394. prepareWideCache();
  395. const auto from = checked() ? 0. : 1.;
  396. const auto to = checked() ? 1. : 0.;
  397. _selection.start(
  398. [=](float64 value) {
  399. if (_updateCallback) {
  400. _updateCallback();
  401. }
  402. if (value == to) {
  403. _wideCache = QPixmap();
  404. }
  405. },
  406. from,
  407. to,
  408. _st.selectDuration,
  409. anim::bumpy(1.25));
  410. } else {
  411. _selection.stop();
  412. _wideCache = QPixmap();
  413. }
  414. }
  415. void RoundImageCheckbox::prepareWideCache() {
  416. if (_wideCache.isNull()) {
  417. auto size = _st.imageRadius * 2;
  418. auto wideSize = size * kWideScale;
  419. QImage cache(wideSize * style::DevicePixelRatio(), wideSize * style::DevicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
  420. cache.setDevicePixelRatio(style::DevicePixelRatio());
  421. {
  422. auto p = Painter(&cache);
  423. p.setCompositionMode(QPainter::CompositionMode_Source);
  424. p.fillRect(0, 0, wideSize, wideSize, Qt::transparent);
  425. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
  426. _paintRoundImage(p, (wideSize - size) / 2, (wideSize - size) / 2, wideSize, size);
  427. }
  428. _wideCache = Ui::PixmapFromImage(std::move(cache));
  429. }
  430. }
  431. void RoundImageCheckbox::setColorOverride(std::optional<QBrush> fg) {
  432. if (fg) {
  433. setCustomizedSegments({
  434. { .brush = *fg, .width = float64(_st.selectWidth) }
  435. });
  436. } else {
  437. setCustomizedSegments({});
  438. }
  439. }
  440. void RoundImageCheckbox::setCustomizedSegments(
  441. std::vector<Ui::OutlineSegment> segments) {
  442. _segments = std::move(segments);
  443. }
  444. } // namespace Ui