1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84

ash / quick_insert / views / quick_insert_gif_view.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_GIF_VIEW_H_
#define ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_GIF_VIEW_H_

#include <optional>
#include <vector>

#include "ash/ash_export.h"
#include "base/functional/callback_forward.h"
#include "base/timer/timer.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"

namespace ash {

namespace image_util {
struct AnimationFrame;
}  // namespace image_util

class ASH_EXPORT QuickInsertGifView : public views::ImageView {
  METADATA_HEADER(QuickInsertGifView, views::ImageView)

 public:
  using FramesFetchedCallback =
      base::OnceCallback<void(std::vector<image_util::AnimationFrame>)>;
  using FramesFetcher = base::OnceCallback<void(FramesFetchedCallback)>;

  using PreviewImageFetchedCallback =
      base::OnceCallback<void(const gfx::ImageSkia&)>;
  using PreviewImageFetcher =
      base::OnceCallback<void(PreviewImageFetchedCallback)>;

  QuickInsertGifView(FramesFetcher frames_fetcher,
                     PreviewImageFetcher preview_image_fetcher,
                     const gfx::Size& original_dimensions);
  QuickInsertGifView(const QuickInsertGifView&) = delete;
  QuickInsertGifView& operator=(const QuickInsertGifView&) = delete;
  ~QuickInsertGifView() override;

  // views::ImageViewBase:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  void OnBoundsChanged(const gfx::Rect& previous_bounds) override;

 private:
  void UpdateFrame();
  void OnFramesFetched(std::vector<image_util::AnimationFrame> frames);

  void OnPreviewImageFetched(const gfx::ImageSkia& preview_image);

  void RecordFetchFramesTime();

  // Original dimensions of the gif, used to preserve aspect ratio when
  // resizing.
  gfx::Size original_dimensions_;

  // The decoded gif frames.
  std::vector<image_util::AnimationFrame> frames_;

  // Timer to call `UpdateFrame` when the next frame should be shown.
  base::OneShotTimer update_frame_timer_;

  // Index of the frame to show on the next call to `UpdateFrame`.
  size_t next_frame_index_ = 0;

  std::optional<base::TimeTicks> fetch_frames_start_time_;

  base::WeakPtrFactory<QuickInsertGifView> weak_factory_{this};
};

BEGIN_VIEW_BUILDER(ASH_EXPORT, QuickInsertGifView, views::ImageView)
END_VIEW_BUILDER

}  // namespace ash

DEFINE_VIEW_BUILDER(ASH_EXPORT, ash::QuickInsertGifView)

#endif  // ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_GIF_VIEW_H_