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) | |
|
oshima
2016/07/19 00:08:53
nit: ternary operator?
jdufault
2016/07/19 18:06:21
Done.
| |
| 63 return PaletteToolId::NONE; | |
| 64 return active_tool->tool_id(); | |
| 65 } | |
| 66 | |
| 67 std::vector<PaletteToolView> PaletteToolManager::CreateViews() { | |
| 68 std::vector<PaletteToolView> views(tools_.size()); | |
| 69 | |
| 70 for (size_t i = 0; i < tools_.size(); ++i) { | |
| 71 PaletteToolView* view = &views[i]; | |
| 72 view->group = tools_[i]->group(); | |
| 73 view->tool_id = tools_[i]->tool_id(); | |
| 74 view->view = tools_[i]->CreateView(); | |
| 75 } | |
| 76 | |
| 77 return views; | |
| 78 } | |
| 79 | |
| 80 void PaletteToolManager::NotifyViewsDestroyed() { | |
| 81 for (std::unique_ptr<PaletteTool>& tool : tools_) | |
| 82 tool->OnViewDestroyed(); | |
| 83 } | |
| 84 | |
| 85 PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) { | |
| 86 for (std::unique_ptr<PaletteTool>& tool : tools_) { | |
| 87 if (tool->tool_id() == tool_id) | |
| 88 return tool.get(); | |
| 89 } | |
| 90 | |
| 91 NOTREACHED(); | |
| 92 return nullptr; | |
| 93 } | |
| 94 | |
| 95 } // namespace ash | |
| OLD | NEW |