media_streaming_round_preview.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "media/streaming/media_streaming_round_preview.h"
  8. namespace Media::Streaming {
  9. RoundPreview::RoundPreview(const QByteArray &bytes, int size)
  10. : _bytes(bytes)
  11. , _reader(
  12. Clip::MakeReader(_bytes, [=](Clip::Notification update) {
  13. clipCallback(update);
  14. }))
  15. , _size(size) {
  16. }
  17. std::shared_ptr<Ui::DynamicImage> RoundPreview::clone() {
  18. Unexpected("RoundPreview::clone.");
  19. }
  20. QImage RoundPreview::image(int size) {
  21. if (!_reader || !_reader->started()) {
  22. return QImage();
  23. }
  24. return _reader->current({
  25. .frame = QSize(_size, _size),
  26. .factor = style::DevicePixelRatio(),
  27. .radius = ImageRoundRadius::Ellipse,
  28. }, crl::now());
  29. }
  30. void RoundPreview::subscribeToUpdates(Fn<void()> callback) {
  31. _repaint = std::move(callback);
  32. }
  33. void RoundPreview::clipCallback(Clip::Notification notification) {
  34. switch (notification) {
  35. case Clip::Notification::Reinit: {
  36. if (_reader->state() == ::Media::Clip::State::Error) {
  37. _reader.setBad();
  38. } else if (_reader->ready() && !_reader->started()) {
  39. _reader->start({
  40. .frame = QSize(_size, _size),
  41. .factor = style::DevicePixelRatio(),
  42. .radius = ImageRoundRadius::Ellipse,
  43. });
  44. }
  45. } break;
  46. case Clip::Notification::Repaint: break;
  47. }
  48. if (const auto onstack = _repaint) {
  49. onstack();
  50. }
  51. }
  52. } // namespace Media::Streaming