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