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
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182

ash / quick_insert / views / quick_insert_image_item_row_view.cc [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.

#include "ash/quick_insert/views/quick_insert_image_item_row_view.h"

#include <memory>
#include <utility>

#include "ash/ash_element_identifiers.h"
#include "ash/quick_insert/views/quick_insert_image_item_view.h"
#include "ash/quick_insert/views/quick_insert_item_view.h"
#include "ash/quick_insert/views/quick_insert_pseudo_focus.h"
#include "ash/quick_insert/views/quick_insert_traversable_item_container.h"
#include "ash/style/icon_button.h"
#include "base/ranges/algorithm.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace ash {
namespace {

constexpr auto kMargin = gfx::Insets::TLBR(4, 16, 4, 12);
// Padding between the leading icon and the image items.
constexpr int kBetweenIconAndImageItemsPadding = 16;
// Padding between image items.
constexpr int kImageItemPadding = 8;
constexpr gfx::Size kLeadingIconSizeDip(20, 20);
constexpr int kHeight = 64;

std::unique_ptr<views::View> CreateEmptyCell() {
  auto cell_view = std::make_unique<views::View>();
  cell_view->SetUseDefaultFillLayout(true);
  cell_view->GetViewAccessibility().SetRole(ax::mojom::Role::kGridCell);
  return cell_view;
}

}  // namespace

QuickInsertImageItemRowView::QuickInsertImageItemRowView(
    base::RepeatingClosure more_items_callback,
    std::u16string more_items_accessible_name) {
  views::View* row_view = nullptr;
  views::Builder<QuickInsertImageItemRowView>(this)
      .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
      .SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kStart)
      .SetInsideBorderInsets(kMargin)
      .SetBetweenChildSpacing(kBetweenIconAndImageItemsPadding)
      .AddChildren(
          views::Builder<views::ImageView>()
              .CopyAddressTo(&leading_icon_view_)
              .SetPreferredSize(kLeadingIconSizeDip)
              .SetCanProcessEventsWithinSubtree(false),
          views::Builder<views::BoxLayoutView>()
              .CopyAddressTo(&row_view)
              .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
              .SetBetweenChildSpacing(kImageItemPadding)
              .SetProperty(views::kBoxLayoutFlexKey,
                           views::BoxLayoutFlexSpecification().WithWeight(1))
              .AddChildren(
                  views::Builder<views::BoxLayoutView>()
                      .CopyAddressTo(&items_container_)
                      .SetOrientation(
                          views::BoxLayout::Orientation::kHorizontal)
                      .SetDefaultFlex(1)
                      .SetProperty(
                          views::kBoxLayoutFlexKey,
                          views::BoxLayoutFlexSpecification().WithWeight(1))
                      .SetBetweenChildSpacing(kImageItemPadding),
                  views::Builder<views::View>(CreateEmptyCell())
                      .AddChildren(
                          views::Builder<views::ImageButton>(
                              std::make_unique<IconButton>(
                                  std::move(more_items_callback),
                                  IconButton::Type::kMediumFloating,
                                  &vector_icons::kSubmenuArrowChromeRefreshIcon,
                                  std::move(more_items_accessible_name),
                                  /*is_togglable=*/false,
                                  /*has_border=*/false))
                              // The kSubmenuArrowChromeRefreshIcon flips
                              // itself, so don't flip it again.
                              .SetFlipCanvasOnPaintForRTLUI(false)
                              .CopyAddressTo(&more_items_button_))))
      .BuildChildren();
  GetViewAccessibility().SetRole(ax::mojom::Role::kGrid);
  row_view->GetViewAccessibility().SetRole(ax::mojom::Role::kRow);
  items_container_->GetViewAccessibility().SetRole(ax::mojom::Role::kNone);
  SetProperty(views::kElementIdentifierKey,
              kQuickInsertSearchResultsImageRowElementId);
}

QuickInsertImageItemRowView::~QuickInsertImageItemRowView() = default;

void QuickInsertImageItemRowView::SetLeadingIcon(const ui::ImageModel& icon) {
  leading_icon_view_->SetImage(icon);
}

QuickInsertImageItemView* QuickInsertImageItemRowView::AddImageItem(
    std::unique_ptr<QuickInsertImageItemView> image_item) {
  auto* view = items_container_->AddChildView(CreateEmptyCell())
                   ->AddChildView(std::move(image_item));
  on_items_changed_.Notify();
  return view;
}

gfx::Size QuickInsertImageItemRowView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  const int preferred_width =
      views::BoxLayoutView::CalculatePreferredSize(available_size).width();
  return gfx::Size(preferred_width, kHeight);
}

views::View* QuickInsertImageItemRowView::GetTopItem() {
  return GetLeftmostItem();
}

views::View* QuickInsertImageItemRowView::GetBottomItem() {
  return GetLeftmostItem();
}

views::View* QuickInsertImageItemRowView::GetItemAbove(views::View* item) {
  return nullptr;
}

views::View* QuickInsertImageItemRowView::GetItemBelow(views::View* item) {
  return nullptr;
}

views::View* QuickInsertImageItemRowView::GetItemLeftOf(views::View* item) {
  views::View* item_left_of = GetNextQuickInsertPseudoFocusableView(
      item, QuickInsertPseudoFocusDirection::kBackward, /*should_loop=*/false);
  return Contains(item_left_of) ? item_left_of : nullptr;
}

views::View* QuickInsertImageItemRowView::GetItemRightOf(views::View* item) {
  views::View* item_left_of = GetNextQuickInsertPseudoFocusableView(
      item, QuickInsertPseudoFocusDirection::kForward, /*should_loop=*/false);
  return Contains(item_left_of) ? item_left_of : nullptr;
}

bool QuickInsertImageItemRowView::ContainsItem(views::View* item) {
  return Contains(item);
}

views::View::Views QuickInsertImageItemRowView::GetItems() const {
  views::View::Views items;
  for (views::View* child : items_container_->children()) {
    items.push_back(child->children().front());
  }
  return items;
}

base::CallbackListSubscription
QuickInsertImageItemRowView::AddItemsChangedCallback(
    views::PropertyChangedCallback callback) {
  return on_items_changed_.Add(std::move(callback));
}

views::View* QuickInsertImageItemRowView::GetLeftmostItem() {
  if (GetFocusManager() == nullptr) {
    return nullptr;
  }
  views::View* leftmost_item = GetFocusManager()->GetNextFocusableView(
      this, GetWidget(), /*reverse=*/false,
      /*dont_loop=*/false);
  return Contains(leftmost_item) ? leftmost_item : nullptr;
}

BEGIN_METADATA(QuickInsertImageItemRowView)
END_METADATA

}  // namespace ash