holding_space_item_view.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_
  5. #define ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/holding_space/holding_space_model.h"
  9. #include "ash/public/cpp/holding_space/holding_space_model_observer.h"
  10. #include "base/callback_list.h"
  11. #include "base/scoped_observation.h"
  12. #include "ui/base/metadata/metadata_header_macros.h"
  13. #include "ui/views/metadata/view_factory.h"
  14. #include "ui/views/view.h"
  15. namespace views {
  16. class ImageButton;
  17. class ImageView;
  18. class ToggleImageButton;
  19. } // namespace views
  20. namespace ash {
  21. class HoldingSpaceItem;
  22. class HoldingSpaceViewDelegate;
  23. // Base class for `HoldingSpaceItemChipView` and
  24. // `HoldingSpaceItemScreenCaptureView`. Note that `HoldingSpaceItemView` may
  25. // temporarily outlive its associated `HoldingSpaceItem` when it is being
  26. // animated out.
  27. class ASH_EXPORT HoldingSpaceItemView : public views::View,
  28. public HoldingSpaceModelObserver {
  29. public:
  30. METADATA_HEADER(HoldingSpaceItemView);
  31. HoldingSpaceItemView(HoldingSpaceViewDelegate*, const HoldingSpaceItem*);
  32. HoldingSpaceItemView(const HoldingSpaceItemView&) = delete;
  33. HoldingSpaceItemView& operator=(const HoldingSpaceItemView&) = delete;
  34. ~HoldingSpaceItemView() override;
  35. // Returns `view` cast as a `HoldingSpaceItemView`. Note that this performs a
  36. // DCHECK to assert that `view` is in fact a `HoldingSpaceItemView` instance.
  37. static HoldingSpaceItemView* Cast(views::View* view);
  38. static const HoldingSpaceItemView* Cast(const views::View* view);
  39. // Returns if `view` is an instance of `HoldingSpaceItemView`.
  40. static bool IsInstance(const views::View* view);
  41. // Resets the view. Called when the tray bubble starts closing to ensure
  42. // that any references that may be outlived are cleared out.
  43. void Reset();
  44. // views::View:
  45. bool HandleAccessibleAction(const ui::AXActionData& action_data) override;
  46. void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
  47. void OnFocus() override;
  48. void OnBlur() override;
  49. void OnGestureEvent(ui::GestureEvent* event) override;
  50. bool OnKeyPressed(const ui::KeyEvent& event) override;
  51. void OnMouseEvent(ui::MouseEvent* event) override;
  52. bool OnMousePressed(const ui::MouseEvent& event) override;
  53. void OnMouseReleased(const ui::MouseEvent& event) override;
  54. void OnThemeChanged() override;
  55. // HoldingSpaceModelObserver:
  56. void OnHoldingSpaceItemUpdated(const HoldingSpaceItem* item,
  57. uint32_t updated_fields) override;
  58. // Starts a drag from this view at the location specified by the given `event`
  59. // and with the specified `source`. Note that this method copies the logic of
  60. // `views::View::DoDrag()` as a workaround to that API being private.
  61. void StartDrag(const ui::LocatedEvent& event,
  62. ui::mojom::DragEventSource source);
  63. const HoldingSpaceItem* item() const { return item_; }
  64. const std::string& item_id() const { return item_id_; }
  65. void SetSelected(bool selected);
  66. bool selected() const { return selected_; }
  67. protected:
  68. views::Builder<views::ImageView> CreateCheckmarkBuilder();
  69. views::Builder<views::View> CreatePrimaryActionBuilder(
  70. const gfx::Size& min_size);
  71. virtual void OnPrimaryActionVisibilityChanged(bool visible) {}
  72. virtual void OnSelectionUiChanged();
  73. HoldingSpaceViewDelegate* delegate() { return delegate_; }
  74. views::ImageView* checkmark() { return checkmark_; }
  75. views::View* primary_action_container() { return primary_action_container_; }
  76. private:
  77. void OnPaintFocus(gfx::Canvas* canvas, gfx::Size size);
  78. void OnPaintSelect(gfx::Canvas* canvas, gfx::Size size);
  79. void OnPrimaryActionPressed();
  80. void UpdatePrimaryAction();
  81. // NOTE: This view may outlive `delegate_` and/or `item_` during destruction
  82. // since the widget is closed asynchronously and the model is updated prior
  83. // to animation completion.
  84. HoldingSpaceViewDelegate* delegate_ = nullptr;
  85. const HoldingSpaceItem* item_ = nullptr;
  86. // Cache the id of the associated holding space item so that it can be
  87. // accessed even after `item_` has been destroyed. Note that `item_` may be
  88. // destroyed if this view is in the process of animating out.
  89. const std::string item_id_;
  90. // Owned by view hierarchy.
  91. views::ImageView* checkmark_ = nullptr;
  92. views::View* primary_action_container_ = nullptr;
  93. views::ImageButton* primary_action_cancel_ = nullptr;
  94. views::ToggleImageButton* primary_action_pin_ = nullptr;
  95. // Owners for the layers used to paint focused and selected states.
  96. std::unique_ptr<ui::LayerOwner> selected_layer_owner_;
  97. std::unique_ptr<ui::LayerOwner> focused_layer_owner_;
  98. // Whether or not this view is selected.
  99. bool selected_ = false;
  100. // Subscription to be notified of `item_` deletion.
  101. base::RepeatingClosureList::Subscription item_deletion_subscription_;
  102. // Subscription to be notified of changes to `delegate_''s selection UI.
  103. base::RepeatingClosureList::Subscription selection_ui_changed_subscription_;
  104. base::ScopedObservation<HoldingSpaceModel, HoldingSpaceModelObserver>
  105. model_observer_{this};
  106. base::WeakPtrFactory<HoldingSpaceItemView> weak_factory_{this};
  107. };
  108. BEGIN_VIEW_BUILDER(/* no export */, HoldingSpaceItemView, views::View)
  109. END_VIEW_BUILDER
  110. } // namespace ash
  111. DEFINE_VIEW_BUILDER(/* no export */, ash::HoldingSpaceItemView)
  112. #endif // ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_ITEM_VIEW_H_