Chromium Code Reviews| 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 "ash/common/system/chromeos/palette/palette_tool.h" | |
| 8 #include "base/bind.h" | |
| 9 | |
| 10 namespace ash { | |
| 11 | |
| 12 PaletteToolManager::PaletteToolManager(WmWindow* window, | |
| 13 const base::Closure& on_tool_changed) | |
| 14 : on_tool_changed_(on_tool_changed), window_(window) {} | |
| 15 | |
| 16 PaletteToolManager::~PaletteToolManager() {} | |
| 17 | |
| 18 void PaletteToolManager::AddTool(std::unique_ptr<PaletteTool> tool) { | |
| 19 PaletteTool::ToolAction enable_tool = | |
| 20 base::Bind(&PaletteToolManager::ActivateTool, base::Unretained(this)); | |
| 21 PaletteTool::ToolAction disable_tool = | |
| 22 base::Bind(&PaletteToolManager::DeactivateTool, base::Unretained(this)); | |
| 23 tool->Initialize(window_, enable_tool, disable_tool); | |
| 24 | |
| 25 tools_.emplace_back(std::move(tool)); | |
| 26 } | |
| 27 | |
| 28 void PaletteToolManager::ActivateTool(PaletteToolId tool_id) { | |
| 29 PaletteTool* new_tool = FindToolById(tool_id); | |
| 30 PaletteTool* previous_tool = active_tools_[new_tool->group()]; | |
| 31 | |
| 32 if (new_tool == previous_tool) | |
| 33 return; | |
| 34 | |
| 35 if (previous_tool) | |
| 36 previous_tool->OnDisable(); | |
| 37 | |
| 38 active_tools_[new_tool->group()] = new_tool; | |
| 39 new_tool->OnEnable(); | |
| 40 | |
| 41 if (!on_tool_changed_.is_null()) | |
| 42 on_tool_changed_.Run(); | |
| 43 } | |
| 44 | |
| 45 void PaletteToolManager::DeactivateTool(PaletteToolId tool_id) { | |
| 46 PaletteTool* tool = FindToolById(tool_id); | |
| 47 | |
| 48 active_tools_[tool->group()] = nullptr; | |
| 49 tool->OnDisable(); | |
| 50 | |
| 51 if (!on_tool_changed_.is_null()) | |
| 52 on_tool_changed_.Run(); | |
| 53 } | |
| 54 | |
| 55 bool PaletteToolManager::IsToolActive(PaletteToolId tool_id) { | |
| 56 PaletteTool* tool = FindToolById(tool_id); | |
| 57 return active_tools_[tool->group()] == tool; | |
| 58 } | |
| 59 | |
| 60 PaletteToolId PaletteToolManager::GetActiveTool(PaletteGroup group) { | |
| 61 PaletteTool* active_tool = active_tools_[group]; | |
| 62 if (!active_tool) | |
| 63 return PaletteToolId::NONE; | |
| 64 return active_tool->tool_id(); | |
| 65 } | |
| 66 | |
| 67 std::vector<PaletteToolView> PaletteToolManager::CreateViews() { | |
| 68 std::vector<PaletteToolView> views; | |
| 69 views.reserve(tools_.size()); | |
|
oshima
2016/07/14 13:58:32
views(tools_.size())
jdufault
2016/07/18 20:39:58
Done. This is actually equivalent to views.resize(
| |
| 70 | |
| 71 for (std::unique_ptr<PaletteTool>& tool : tools_) { | |
| 72 PaletteToolView view; | |
| 73 view.group = tool->group(); | |
| 74 view.tool_id = tool->tool_id(); | |
| 75 view.view = tool->CreateView(); | |
| 76 views.push_back(view); | |
| 77 } | |
| 78 | |
| 79 return views; | |
| 80 } | |
| 81 | |
| 82 void PaletteToolManager::ViewsDestroyed() { | |
| 83 for (std::unique_ptr<PaletteTool>& tool : tools_) | |
| 84 tool->ViewDestroyed(); | |
| 85 } | |
| 86 | |
| 87 PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) { | |
| 88 for (std::unique_ptr<PaletteTool>& tool : tools_) { | |
| 89 if (tool->tool_id() == tool_id) | |
| 90 return tool.get(); | |
| 91 } | |
| 92 | |
|
oshima
2016/07/14 13:58:32
find_if + lambda? you may leave it if it looks mor
jdufault
2016/07/18 20:39:58
Here's find_if. I prefer the for loop.
auto res
| |
| 93 LOG(ERROR) << "Could not find tool"; | |
| 94 NOTREACHED(); | |
| 95 return nullptr; | |
| 96 } | |
| 97 | |
| 98 } // namespace ash | |
| OLD | NEW |