1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_
6 #define ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_
7 
8 #include <memory>
9 
10 #include "ash/ash_export.h"
11 #include "ui/views/animation/ink_drop_host_view.h"
12 #include "ui/views/metadata/metadata_header_macros.h"
13 
14 namespace views {
15 class ToggleImageButton;
16 }  // namespace views
17 
18 namespace ash {
19 
20 class HoldingSpaceItem;
21 class HoldingSpaceItemViewDelegate;
22 
23 // Base class for HoldingSpaceItemChipView and
24 // HoldingSpaceItemScreenCaptureView.
25 class ASH_EXPORT HoldingSpaceItemView : public views::InkDropHostView {
26  public:
27   METADATA_HEADER(HoldingSpaceItemView);
28 
29   HoldingSpaceItemView(HoldingSpaceItemViewDelegate*, const HoldingSpaceItem*);
30   HoldingSpaceItemView(const HoldingSpaceItemView&) = delete;
31   HoldingSpaceItemView& operator=(const HoldingSpaceItemView&) = delete;
32   ~HoldingSpaceItemView() override;
33 
34   // Returns `view` cast as a `HoldingSpaceItemView`. Note that this performs a
35   // DCHECK to assert that `view` is in fact a `HoldingSpaceItemView` instance.
36   static HoldingSpaceItemView* Cast(views::View* view);
37 
38   // Returns if `view` is an instance of `HoldingSpaceItemView`.
39   static bool IsInstance(views::View* view);
40 
41   // views::InkDropHostView:
42   SkColor GetInkDropBaseColor() const override;
43   bool HandleAccessibleAction(const ui::AXActionData& action_data) override;
44   void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
45   void OnFocus() override;
46   void OnBlur() override;
47   void OnGestureEvent(ui::GestureEvent* event) override;
48   bool OnKeyPressed(const ui::KeyEvent& event) override;
49   void OnMouseEvent(ui::MouseEvent* event) override;
50   bool OnMousePressed(const ui::MouseEvent& event) override;
51   void OnMouseReleased(const ui::MouseEvent& event) override;
52 
53   // Starts a drag from this view at the location specified by the given `event`
54   // and with the specified `source`. Note that this method copies the logic of
55   // `views::View::DoDrag()` as a workaround to that API being private.
56   void StartDrag(const ui::LocatedEvent& event,
57                  ui::mojom::DragEventSource source);
58 
item()59   const HoldingSpaceItem* item() const { return item_; }
60 
61   void SetSelected(bool selected);
selected()62   bool selected() const { return selected_; }
63 
64  protected:
65   views::ToggleImageButton* AddPin(views::View* parent);
66 
67  private:
68   void OnPaintFocus(gfx::Canvas* canvas, gfx::Size size);
69   void OnPaintSelect(gfx::Canvas* canvas, gfx::Size size);
70   void OnPinPressed();
71   void UpdatePin();
72 
73   HoldingSpaceItemViewDelegate* const delegate_;
74   const HoldingSpaceItem* const item_;
75   views::ToggleImageButton* pin_ = nullptr;
76 
77   // Owners for the layers used to paint focused and selected states.
78   std::unique_ptr<ui::LayerOwner> selected_layer_owner_;
79   std::unique_ptr<ui::LayerOwner> focused_layer_owner_;
80 
81   // Whether or not this view is selected.
82   bool selected_ = false;
83 
84   base::WeakPtrFactory<HoldingSpaceItemView> weak_factory_{this};
85 };
86 
87 }  // namespace ash
88 
89 #endif  // ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_
90