palette_tool_manager.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 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. #include "ash/system/palette/palette_tool_manager.h"
  5. #include <algorithm>
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/system/palette/palette_tool.h"
  8. #include "base/bind.h"
  9. #include "base/metrics/histogram_macros.h"
  10. namespace ash {
  11. PaletteToolManager::PaletteToolManager(Delegate* delegate)
  12. : delegate_(delegate) {
  13. DCHECK(delegate_);
  14. }
  15. PaletteToolManager::~PaletteToolManager() = default;
  16. bool PaletteToolManager::HasTool(PaletteToolId tool_id) {
  17. return FindToolById(tool_id);
  18. }
  19. void PaletteToolManager::AddTool(std::unique_ptr<PaletteTool> tool) {
  20. // The same PaletteToolId cannot be registered twice.
  21. DCHECK_EQ(0, std::count_if(tools_.begin(), tools_.end(),
  22. [&tool](const std::unique_ptr<PaletteTool>& t) {
  23. return t->GetToolId() == tool->GetToolId();
  24. }));
  25. tools_.emplace_back(std::move(tool));
  26. }
  27. void PaletteToolManager::ActivateTool(PaletteToolId tool_id) {
  28. PaletteTool* new_tool = FindToolById(tool_id);
  29. DCHECK(new_tool);
  30. PaletteTool* previous_tool = active_tools_[new_tool->GetGroup()];
  31. if (new_tool == previous_tool)
  32. return;
  33. if (previous_tool) {
  34. previous_tool->OnDisable();
  35. RecordPaletteModeCancellation(PaletteToolIdToPaletteModeCancelType(
  36. previous_tool->GetToolId(), true /*is_switched*/));
  37. }
  38. active_tools_[new_tool->GetGroup()] = new_tool;
  39. new_tool->OnEnable();
  40. delegate_->OnActiveToolChanged();
  41. }
  42. void PaletteToolManager::DeactivateTool(PaletteToolId tool_id) {
  43. PaletteTool* tool = FindToolById(tool_id);
  44. DCHECK(tool);
  45. active_tools_[tool->GetGroup()] = nullptr;
  46. tool->OnDisable();
  47. delegate_->OnActiveToolChanged();
  48. }
  49. bool PaletteToolManager::IsToolActive(PaletteToolId tool_id) {
  50. PaletteTool* tool = FindToolById(tool_id);
  51. DCHECK(tool);
  52. return active_tools_[tool->GetGroup()] == tool;
  53. }
  54. PaletteToolId PaletteToolManager::GetActiveTool(PaletteGroup group) {
  55. PaletteTool* active_tool = active_tools_[group];
  56. return active_tool ? active_tool->GetToolId() : PaletteToolId::NONE;
  57. }
  58. const gfx::VectorIcon& PaletteToolManager::GetActiveTrayIcon(
  59. PaletteToolId tool_id) const {
  60. PaletteTool* tool = FindToolById(tool_id);
  61. if (!tool)
  62. return kPaletteTrayIconDefaultNewuiIcon;
  63. return tool->GetActiveTrayIcon();
  64. }
  65. std::vector<PaletteToolView> PaletteToolManager::CreateViews() {
  66. std::vector<PaletteToolView> views;
  67. views.reserve(tools_.size());
  68. for (const auto& tool : tools_) {
  69. views::View* tool_view = tool->CreateView();
  70. if (!tool_view)
  71. continue;
  72. PaletteToolView view;
  73. view.group = tool->GetGroup();
  74. view.tool_id = tool->GetToolId();
  75. view.view = tool_view;
  76. views.push_back(view);
  77. }
  78. return views;
  79. }
  80. void PaletteToolManager::NotifyViewsDestroyed() {
  81. for (std::unique_ptr<PaletteTool>& tool : tools_)
  82. tool->OnViewDestroyed();
  83. }
  84. void PaletteToolManager::DisableActiveTool(PaletteGroup group) {
  85. PaletteToolId tool_id = GetActiveTool(group);
  86. if (tool_id != PaletteToolId::NONE)
  87. DeactivateTool(tool_id);
  88. }
  89. void PaletteToolManager::EnableTool(PaletteToolId tool_id) {
  90. ActivateTool(tool_id);
  91. }
  92. void PaletteToolManager::DisableTool(PaletteToolId tool_id) {
  93. DeactivateTool(tool_id);
  94. }
  95. void PaletteToolManager::HidePalette() {
  96. delegate_->HidePalette();
  97. }
  98. void PaletteToolManager::HidePaletteImmediately() {
  99. delegate_->HidePaletteImmediately();
  100. }
  101. aura::Window* PaletteToolManager::GetWindow() {
  102. return delegate_->GetWindow();
  103. }
  104. void PaletteToolManager::RecordPaletteOptionsUsage(
  105. PaletteTrayOptions option,
  106. PaletteInvocationMethod method) {
  107. return delegate_->RecordPaletteOptionsUsage(option, method);
  108. }
  109. void PaletteToolManager::RecordPaletteModeCancellation(
  110. PaletteModeCancelType type) {
  111. return delegate_->RecordPaletteModeCancellation(type);
  112. }
  113. PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) const {
  114. for (const std::unique_ptr<PaletteTool>& tool : tools_) {
  115. if (tool->GetToolId() == tool_id)
  116. return tool.get();
  117. }
  118. return nullptr;
  119. }
  120. } // namespace ash