continue_task_view.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2021 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_APP_LIST_VIEWS_CONTINUE_TASK_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_CONTINUE_TASK_VIEW_H_
  6. #include <memory>
  7. #include "ash/app_list/model/search/search_result.h"
  8. #include "ash/app_list/model/search/search_result_observer.h"
  9. #include "ash/ash_export.h"
  10. #include "ui/base/models/simple_menu_model.h"
  11. #include "ui/views/context_menu_controller.h"
  12. #include "ui/views/controls/button/button.h"
  13. namespace views {
  14. class ImageView;
  15. class Label;
  16. class MenuRunner;
  17. } // namespace views
  18. namespace ash {
  19. class AppListViewDelegate;
  20. enum ContinueTaskCommandId {
  21. // Context Menu option to open the selected suggestion.
  22. kOpenResult = 0,
  23. // Context Menu option to prevent the suggestion from showing.
  24. kRemoveResult = 1,
  25. // Context Menu option to hide the continue section.
  26. kHideContinueSection = 2,
  27. };
  28. // A view with a suggested task for the "Continue" section.
  29. class ASH_EXPORT ContinueTaskView : public views::Button,
  30. public views::ContextMenuController,
  31. public ui::SimpleMenuModel::Delegate,
  32. public SearchResultObserver {
  33. public:
  34. // The type of result for the task.
  35. // These values are used for metrics and should not be changed.
  36. enum class TaskResultType {
  37. kLocalFile = 0,
  38. kDriveFile = 1,
  39. kUnknown = 2,
  40. kMaxValue = kUnknown,
  41. };
  42. METADATA_HEADER(ContinueTaskView);
  43. ContinueTaskView(AppListViewDelegate* view_delegate, bool tablet_mode);
  44. ContinueTaskView(const ContinueTaskView&) = delete;
  45. ContinueTaskView& operator=(const ContinueTaskView&) = delete;
  46. ~ContinueTaskView() override;
  47. // views::View:
  48. gfx::Size CalculatePreferredSize() const override;
  49. gfx::Size GetMinimumSize() const override;
  50. gfx::Size GetMaximumSize() const override;
  51. void OnThemeChanged() override;
  52. // SearchResultObserver:
  53. void OnResultDestroying() override;
  54. void OnMetadataChanged() override;
  55. void SetResult(SearchResult* result);
  56. // Returns true if the context menu for this task is showing.
  57. bool IsMenuShowing() const;
  58. void set_index_in_container(size_t index) { index_in_container_ = index; }
  59. SearchResult* result() const { return result_; }
  60. int index_in_container() const { return index_in_container_.value_or(-1); }
  61. // SimpleMenuModel::Delegate:
  62. void ExecuteCommand(int command_id, int event_flags) override;
  63. void MenuClosed(ui::SimpleMenuModel* source) override;
  64. // Returns the type of result for the task. Used for metrics.
  65. TaskResultType GetTaskResultType();
  66. private:
  67. void UpdateIcon();
  68. gfx::Size GetIconSize() const;
  69. void UpdateResult();
  70. void OnButtonPressed(const ui::Event& event);
  71. // views::ContextMenuController:
  72. void ShowContextMenuForViewImpl(views::View* source,
  73. const gfx::Point& point,
  74. ui::MenuSourceType source_type) override;
  75. // Opens the search result related to the view.
  76. void OpenResult(int event_flags);
  77. // Removes the search result related to the view.
  78. void RemoveResult();
  79. // Builds and returns a raw pointer to `context_menu_model_`.
  80. ui::SimpleMenuModel* BuildMenuModel();
  81. // Closes the context menu for this view if it is running.
  82. void CloseContextMenu();
  83. // Updates the background and the border if the ContinueTaskView is in tablet
  84. // mode.
  85. void UpdateStyleForTabletMode();
  86. // Record metrics at the moment when the ContinueTaskView result is removed.
  87. void LogMetricsOnResultRemoved();
  88. // The index of this view within a |SearchResultContainerView| that holds it.
  89. absl::optional<int> index_in_container_;
  90. AppListViewDelegate* const view_delegate_;
  91. views::Label* title_ = nullptr;
  92. views::Label* subtitle_ = nullptr;
  93. views::ImageView* icon_ = nullptr;
  94. SearchResult* result_ = nullptr; // Owned by SearchModel::SearchResults.
  95. const bool is_tablet_mode_;
  96. std::unique_ptr<ui::SimpleMenuModel> context_menu_model_;
  97. std::unique_ptr<views::MenuRunner> context_menu_runner_;
  98. base::ScopedObservation<SearchResult, SearchResultObserver>
  99. search_result_observation_{this};
  100. base::WeakPtrFactory<ContinueTaskView> weak_ptr_factory_{this};
  101. };
  102. } // namespace ash
  103. #endif // ASH_APP_LIST_VIEWS_CONTINUE_TASK_VIEW_H_