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
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284

ash / quick_insert / views / quick_insert_image_item_grid_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_grid_view.h"

#include <iterator>
#include <memory>
#include <utility>

#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_traversable_item_container.h"
#include "base/notimplemented.h"
#include "base/ranges/algorithm.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/layout/box_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace ash {
namespace {

// Padding between image grid items.
constexpr int kImageGridPadding = 8;

// Margin for the image grid.
constexpr auto kImageGridMargin = gfx::Insets::TLBR(16, 16, 0, 16);

std::unique_ptr<views::View> CreateImageGridColumn() {
  auto column =
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kVertical)
          .SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kStretch)
          .Build();
  column->SetBetweenChildSpacing(kImageGridPadding);
  return column;
}

views::View* ItemInColumnWithIndexClosestTo(views::View* column,
                                            const size_t index) {
  if (column->children().empty()) {
    return nullptr;
  } else if (index < column->children().size()) {
    return column->children()[index]->children().front().get();
  } else {
    return column->children().back()->children().front().get();
  }
}

std::unique_ptr<views::View> CreateListItemView(size_t pos_in_set) {
  auto view = std::make_unique<views::View>();
  view->SetUseDefaultFillLayout(true);
  view->GetViewAccessibility().SetRole(ax::mojom::Role::kListItem);
  view->GetViewAccessibility().SetPosInSet(pos_in_set);
  // Setting the hierarchical level explicitly allows the SetSize to be
  // overridden later.
  view->GetViewAccessibility().SetHierarchicalLevel(1);
  return view;
}

}  // namespace

QuickInsertImageItemGridView::QuickInsertImageItemGridView(int grid_width,
                                                           bool has_top_margin)
    : focus_search_(std::make_unique<FocusSearch>(
          this,
          base::BindRepeating(&QuickInsertImageItemGridView::GetFocusableItems,
                              base::Unretained(this)))) {
  SetLayoutManager(std::make_unique<views::TableLayout>())
      ->AddColumn(/*h_align=*/views::LayoutAlignment::kCenter,
                  /*v_align=*/views::LayoutAlignment::kStretch,
                  /*horizontal_resize=*/1.0f,
                  /*size_type=*/views::TableLayout::ColumnSize::kFixed,
                  /*fixed_width=*/0, /*min_width=*/0)
      .AddPaddingColumn(
          /*horizontal_resize=*/views::TableLayout::kFixedSize,
          /*width=*/kImageGridPadding)
      .AddColumn(/*h_align=*/views::LayoutAlignment::kCenter,
                 /*v_align=*/views::LayoutAlignment::kStretch,
                 /*horizontal_resize=*/1.0f,
                 /*size_type=*/views::TableLayout::ColumnSize::kFixed,
                 /*fixed_width=*/0, /*min_width=*/0)
      .AddRows(1, /*vertical_resize=*/views::TableLayout::kFixedSize,
               /*height=*/0);

  gfx::Insets margins = kImageGridMargin;
  if (!has_top_margin) {
    margins.set_top(0);
  }
  SetProperty(views::kMarginsKey, margins);

  AddChildView(CreateImageGridColumn());
  AddChildView(CreateImageGridColumn());
}

QuickInsertImageItemGridView::~QuickInsertImageItemGridView() = default;

views::FocusTraversable*
QuickInsertImageItemGridView::GetPaneFocusTraversable() {
  return focus_search_.get();
}

views::View* QuickInsertImageItemGridView::GetTopItem() {
  views::View* column = children().front();
  return column->children().empty()
             ? nullptr
             : column->children().front()->children().front().get();
}

views::View* QuickInsertImageItemGridView::GetBottomItem() {
  views::View* tallest_column =
      base::ranges::max(children(),
                        /*comp=*/base::ranges::less(),
                        /*proj=*/[](const views::View* v) {
                          return v->GetPreferredSize().height();
                        });
  return tallest_column->children().empty()
             ? nullptr
             : tallest_column->children().back()->children().front().get();
}

views::View* QuickInsertImageItemGridView::GetItemAbove(views::View* item) {
  views::View* column = GetColumnContaining(item);
  if (!column || item->parent() == column->children().front()) {
    return nullptr;
  }
  return std::prev(base::ranges::find(column->children(), item->parent()))
      ->get()
      ->children()
      .front()
      .get();
}

views::View* QuickInsertImageItemGridView::GetItemBelow(views::View* item) {
  views::View* column = GetColumnContaining(item);
  if (!column || item->parent() == column->children().back()) {
    return nullptr;
  }
  return std::next(base::ranges::find(column->children(), item->parent()))
      ->get()
      ->children()
      .front()
      .get();
}

views::View* QuickInsertImageItemGridView::GetItemLeftOf(views::View* item) {
  views::View* column = GetColumnContaining(item);
  if (!column || column == children().front()) {
    return nullptr;
  }
  // Prefer to return the item with the same index in the column to the left,
  // since this will probably be at a similar height to `item` (at least in
  // usual scenarios where the grid items all have similar dimensions).
  const size_t item_index = column->GetIndexOf(item->parent()).value();
  views::View* left_column =
      std::prev(base::ranges::find(children(), column))->get();
  return ItemInColumnWithIndexClosestTo(left_column, item_index);
}

views::View* QuickInsertImageItemGridView::GetItemRightOf(views::View* item) {
  views::View* column = GetColumnContaining(item);
  if (!column || column == children().back()) {
    return nullptr;
  }
  // Prefer to return the item with the same index in the column to the right,
  // since this will probably be at a similar height to `item` (at least in
  // usual scenarios where the grid items all have similar dimensions).
  const size_t item_index = column->GetIndexOf(item->parent()).value();
  views::View* right_column =
      std::next(base::ranges::find(children(), column))->get();
  return ItemInColumnWithIndexClosestTo(right_column, item_index);
}

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

QuickInsertImageItemView* QuickInsertImageItemGridView::AddImageItem(
    std::unique_ptr<QuickInsertImageItemView> image_item) {
  views::View* shortest_column =
      base::ranges::min(children(),
                        /*comp=*/base::ranges::less(),
                        /*proj=*/[](const views::View* v) {
                          return v->GetPreferredSize().height();
                        });
  QuickInsertImageItemView* new_item =
      shortest_column
          ->AddChildView(CreateListItemView(focusable_items_.size() + 1))
          ->AddChildView(std::move(image_item));
  focusable_items_.push_back(new_item);

  // Update the SetSize for all items.
  for (views::View* view : focusable_items_) {
    view->parent()->GetViewAccessibility().SetSetSize(focusable_items_.size());
  }

  return new_item;
}

QuickInsertImageItemGridView::FocusSearch::FocusSearch(
    views::View* view,
    const GetFocusableViewsCallback& callback)
    : views::FocusSearch(/*root=*/view,
                         /*cycle=*/true,
                         /*accessibility_mode=*/true),
      view_(view),
      get_focusable_views_callback_(callback) {}

QuickInsertImageItemGridView::FocusSearch::~FocusSearch() = default;

views::View* QuickInsertImageItemGridView::FocusSearch::FindNextFocusableView(
    views::View* starting_view,
    SearchDirection search_direction,
    TraversalDirection traversal_direction,
    StartingViewPolicy check_starting_view,
    AnchoredDialogPolicy can_go_into_anchored_dialog,
    views::FocusTraversable** focus_traversable,
    views::View** focus_traversable_view) {
  // The callback polls the currently focusable views.
  const views::View::Views& focusable_views =
      get_focusable_views_callback_.Run();
  if (focusable_views.empty()) {
    return nullptr;
  }

  int delta =
      search_direction == FocusSearch::SearchDirection::kForwards ? 1 : -1;
  int focusable_views_size = static_cast<int>(focusable_views.size());
  for (int i = 0; i < focusable_views_size; ++i) {
    // If current view from the set is found to be focused, return the view
    // next (or previous) to it as next focusable view.
    if (focusable_views[i] == starting_view) {
      const int next_index = i + delta;
      if (next_index >= 0 && next_index < focusable_views_size) {
        return focusable_views[next_index];
      } else {
        return nullptr;
      }
    }
  }

  // Case when none of the views are already focused.
  return (search_direction == FocusSearch::SearchDirection::kForwards)
             ? focusable_views.front()
             : focusable_views.back();
}

views::FocusSearch*
QuickInsertImageItemGridView::FocusSearch::GetFocusSearch() {
  return this;
}

views::FocusTraversable*
QuickInsertImageItemGridView::FocusSearch::GetFocusTraversableParent() {
  return nullptr;
}

views::View*
QuickInsertImageItemGridView::FocusSearch::GetFocusTraversableParentView() {
  return nullptr;
}

views::View* QuickInsertImageItemGridView::GetColumnContaining(
    views::View* item) {
  views::View* column =
      item->parent() == nullptr ? nullptr : item->parent()->parent();
  return column && column->parent() == this ? column : nullptr;
}

const views::View::Views& QuickInsertImageItemGridView::GetFocusableItems()
    const {
  return focusable_items_;
}

BEGIN_METADATA(QuickInsertImageItemGridView)
END_METADATA

}  // namespace ash