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

ash / quick_insert / views / quick_insert_image_item_grid_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_IMAGE_ITEM_GRID_VIEW_H_
#define ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_IMAGE_ITEM_GRID_VIEW_H_

#include <memory>

#include "ash/ash_export.h"
#include "ash/quick_insert/views/quick_insert_traversable_item_container.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/focus/focus_search.h"
#include "ui/views/view.h"

namespace ash {

class QuickInsertImageItemView;

// Container view for the image items in a section. The image items are
// displayed in a grid with two columns.
class ASH_EXPORT QuickInsertImageItemGridView
    : public views::View,
      public QuickInsertTraversableItemContainer {
  METADATA_HEADER(QuickInsertImageItemGridView, views::View)

 public:
  explicit QuickInsertImageItemGridView(int grid_width,
                                        bool has_top_margin = true);
  QuickInsertImageItemGridView(const QuickInsertImageItemGridView&) = delete;
  QuickInsertImageItemGridView& operator=(const QuickInsertImageItemGridView&) =
      delete;
  ~QuickInsertImageItemGridView() override;

  // views::View:
  views::FocusTraversable* GetPaneFocusTraversable() override;

  // QuickInsertTraversableItemContainer:
  views::View* GetTopItem() override;
  views::View* GetBottomItem() override;
  views::View* GetItemAbove(views::View* item) override;
  views::View* GetItemBelow(views::View* item) override;
  views::View* GetItemLeftOf(views::View* item) override;
  views::View* GetItemRightOf(views::View* item) override;
  bool ContainsItem(views::View* item) override;

  QuickInsertImageItemView* AddImageItem(
      std::unique_ptr<QuickInsertImageItemView> image_item);

 private:
  class FocusSearch : public views::FocusSearch,
                      public views::FocusTraversable {
   public:
    using GetFocusableViewsCallback =
        base::RepeatingCallback<const views::View::Views&(void)>;

    FocusSearch(views::View* view, const GetFocusableViewsCallback& callback);
    FocusSearch(const FocusSearch&) = delete;
    FocusSearch& operator=(const FocusSearch) = delete;
    ~FocusSearch() override;

    // views::FocusSearch:
    views::View* 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) override;

    // views::FocusTraversable:
    views::FocusSearch* GetFocusSearch() override;
    views::FocusTraversable* GetFocusTraversableParent() override;
    views::View* GetFocusTraversableParentView() override;

   private:
    const raw_ptr<views::View> view_ = nullptr;
    const GetFocusableViewsCallback get_focusable_views_callback_;
  };

  // Returns the column containing `item`, or nullptr if `item` is not part of
  // this grid.
  views::View* GetColumnContaining(views::View* item);

  // Returns items in this grid in focus traversal order.
  const views::View::Views& GetFocusableItems() const;

  views::View::Views focusable_items_;
  std::unique_ptr<FocusSearch> focus_search_;
};

}  // namespace ash

#endif  // ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_IMAGE_ITEM_GRID_VIEW_H_