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
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155

ash / quick_insert / views / quick_insert_list_item_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_LIST_ITEM_VIEW_H_
#define ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_LIST_ITEM_VIEW_H_

#include <optional>
#include <string>

#include "ash/ash_export.h"
#include "ash/public/cpp/holding_space/holding_space_image.h"
#include "ash/quick_insert/model/quick_insert_action_type.h"
#include "ash/quick_insert/views/quick_insert_item_view.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/geometry/size.h"

namespace base {
class FilePath;
}

namespace ui {
class ImageModel;
}

namespace views {
class ImageView;
class Label;
class View;
}  // namespace views

namespace ash {

class QuickInsertBadgeView;
class QuickInsertPreviewBubbleController;
class QuickInsertShortcutHintView;

// View for a Quick Insert list item with text or an image as its primary
// contents. Can optionally have other parts such as a leading icon and
// secondary text.
class ASH_EXPORT QuickInsertListItemView : public QuickInsertItemView {
  METADATA_HEADER(QuickInsertListItemView, QuickInsertItemView)

 public:
  using AsyncBitmapResolver = HoldingSpaceImage::AsyncBitmapResolver;
  using FileInfoResolver =
      base::OnceCallback<std::optional<base::File::Info>()>;

  explicit QuickInsertListItemView(SelectItemCallback select_item_callback);
  QuickInsertListItemView(const QuickInsertListItemView&) = delete;
  QuickInsertListItemView& operator=(const QuickInsertListItemView&) = delete;
  ~QuickInsertListItemView() override;

  // QuickInsertItemView:
  void SetItemState(ItemState item_state) override;

  void SetLeadingIcon(const ui::ImageModel& icon,
                      std::optional<gfx::Size> icon_size = std::nullopt);

  // Sets the primary text. This replaces any existing contents in the primary
  // container.
  void SetPrimaryText(const std::u16string& primary_text);

  // Sets the primary image. This replaces any existing contents in the primary
  // container. `available_width` is the available width for this list item
  // (including any leading icons). The image will be resized to fill
  // `available_width`, while maintaining a fixed height and aspect ratio by
  // cropping out any excess.
  void SetPrimaryImage(const ui::ImageModel& primary_image,
                       int available_width);

  void SetSecondaryText(const std::u16string& secondary_text);

  void SetShortcutHintView(
      std::unique_ptr<QuickInsertShortcutHintView> shortcut_hint_view);

  void SetBadgeAction(QuickInsertActionType action);
  void SetBadgeVisible(bool visible);

  // Starts to retrieve a thumbnail preview of `file_path` to be used when the
  // item is hovered on. If `update_icon` is true, then the leading icon of this
  // item will also be updated to match the thumbnail.
  void SetPreview(QuickInsertPreviewBubbleController* preview_bubble_controller,
                  FileInfoResolver get_file_info,
                  const base::FilePath& file_path,
                  AsyncBitmapResolver async_bitmap_resolver,
                  bool update_icon = false);

  // views::Button:
  void OnMouseEntered(const ui::MouseEvent& event) override;
  void OnMouseExited(const ui::MouseEvent& event) override;

  const views::ImageView& leading_icon_view_for_testing() const {
    return *leading_icon_view_;
  }
  const views::View* primary_container_for_testing() const {
    return primary_container_;
  }
  const QuickInsertShortcutHintView* shortcut_hint_view_for_testing() const {
    return shortcut_hint_view_;
  }
  const QuickInsertBadgeView& trailing_badge_for_testing() const {
    return *trailing_badge_;
  }
  std::u16string GetPrimaryTextForTesting() const;
  ui::ImageModel GetPrimaryImageForTesting() const;
  std::u16string_view GetSecondaryTextForTesting() const;

 private:
  void UpdateIconWithPreview();
  std::u16string GetAccessibilityLabel() const;
  void UpdateAccessibleName();
  void OnFileInfoResolved(std::optional<base::File::Info> info);

  void ShowPreview();
  void HidePreview();

  raw_ptr<views::ImageView> leading_icon_view_ = nullptr;

  // Contains the item's primary contents, which can be text or an image.
  raw_ptr<views::View> primary_container_ = nullptr;
  raw_ptr<views::Label> primary_label_ = nullptr;

  // Contains the item's secondary text if it has been set.
  raw_ptr<views::View> secondary_container_ = nullptr;
  raw_ptr<views::Label> secondary_label_ = nullptr;

  // Contains the item's shortcut hint if it has been set.
  raw_ptr<views::View> shortcut_hint_container_ = nullptr;
  raw_ptr<QuickInsertShortcutHintView> shortcut_hint_view_ = nullptr;

  // Contains the item's trailing badge if it has been set.
  raw_ptr<QuickInsertBadgeView> trailing_badge_ = nullptr;
  QuickInsertActionType badge_action_ = QuickInsertActionType::kDo;

  // These are only used for file items.
  // TODO: b/344457947 - Combine the two async images by allowing the
  // placeholder image to be dynamically generated based on the size.
  std::unique_ptr<HoldingSpaceImage> async_preview_image_;
  std::unique_ptr<HoldingSpaceImage> async_preview_icon_;
  base::FilePath file_path_;
  std::optional<base::File::Info> file_info_;
  raw_ptr<QuickInsertPreviewBubbleController> preview_bubble_controller_;
  base::CallbackListSubscription async_icon_subscription_;

  base::WeakPtrFactory<QuickInsertListItemView> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_LIST_ITEM_VIEW_H_