Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: ash/common/system/chromeos/palette/palette_tool_manager.cc

Issue 2140343002: Add palette tool/manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stylus-add-icons
Patch Set: Remove base::Optional, use VECTOR_ICON_NONE instead Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/bind.h"
11 #include "ui/gfx/vector_icons_public.h"
12
13 namespace ash {
14
15 PaletteToolManager::PaletteToolManager(Delegate* delegate)
16 : delegate_(delegate) {
17 DCHECK(delegate_);
18 }
19
20 PaletteToolManager::~PaletteToolManager() {}
21
22 void PaletteToolManager::AddTool(std::unique_ptr<PaletteTool> tool) {
23 // The same PaletteToolId cannot be registered twice.
24 DCHECK_EQ(0, std::count_if(tools_.begin(), tools_.end(),
25 [&tool](const std::unique_ptr<PaletteTool>& t) {
26 return t->GetToolId() == tool->GetToolId();
27 }));
28
29 tools_.emplace_back(std::move(tool));
30 }
31
32 void PaletteToolManager::ActivateTool(PaletteToolId tool_id) {
33 PaletteTool* new_tool = FindToolById(tool_id);
34 DCHECK(new_tool);
35
36 PaletteTool* previous_tool = active_tools_[new_tool->GetGroup()];
37
38 if (new_tool == previous_tool)
39 return;
40
41 if (previous_tool)
42 previous_tool->OnDisable();
43
44 active_tools_[new_tool->GetGroup()] = new_tool;
45 new_tool->OnEnable();
46
47 delegate_->OnActiveToolChanged();
48 }
49
50 void PaletteToolManager::DeactivateTool(PaletteToolId tool_id) {
51 PaletteTool* tool = FindToolById(tool_id);
52 DCHECK(tool);
53
54 active_tools_[tool->GetGroup()] = nullptr;
55 tool->OnDisable();
56
57 delegate_->OnActiveToolChanged();
58 }
59
60 bool PaletteToolManager::IsToolActive(PaletteToolId tool_id) {
61 PaletteTool* tool = FindToolById(tool_id);
62 DCHECK(tool);
63
64 return active_tools_[tool->GetGroup()] == tool;
65 }
66
67 PaletteToolId PaletteToolManager::GetActiveTool(PaletteGroup group) {
68 PaletteTool* active_tool = active_tools_[group];
69 return active_tool ? active_tool->GetToolId() : PaletteToolId::NONE;
70 }
71
72 gfx::VectorIconId PaletteToolManager::GetActiveTrayIcon(PaletteToolId tool_id) {
73 PaletteTool* tool = FindToolById(tool_id);
74 if (!tool)
75 return gfx::VectorIconId::VECTOR_ICON_NONE;
76
77 return tool->GetActiveTrayIcon();
78 }
79
80 std::vector<PaletteToolView> PaletteToolManager::CreateViews() {
81 std::vector<PaletteToolView> views(tools_.size());
82
83 for (size_t i = 0; i < tools_.size(); ++i) {
84 PaletteToolView* view = &views[i];
85 view->group = tools_[i]->GetGroup();
86 view->tool_id = tools_[i]->GetToolId();
87 view->view = tools_[i]->CreateView();
88 }
89
90 return views;
91 }
92
93 void PaletteToolManager::NotifyViewsDestroyed() {
94 for (std::unique_ptr<PaletteTool>& tool : tools_)
95 tool->OnViewDestroyed();
96 }
97
98 void PaletteToolManager::EnableTool(PaletteToolId tool_id) {
99 ActivateTool(tool_id);
100 }
101
102 void PaletteToolManager::DisableTool(PaletteToolId tool_id) {
103 DeactivateTool(tool_id);
104 }
105
106 void PaletteToolManager::HidePalette() {
107 delegate_->HidePalette();
108 }
109
110 WmWindow* PaletteToolManager::GetWindow() {
111 return delegate_->GetWindow();
112 }
113
114 PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) {
115 for (std::unique_ptr<PaletteTool>& tool : tools_) {
116 if (tool->GetToolId() == tool_id)
117 return tool.get();
118 }
119
120 return nullptr;
121 }
122
123 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698