| Index: ash/common/system/chromeos/palette/palette_tool_manager.cc
|
| diff --git a/ash/common/system/chromeos/palette/palette_tool_manager.cc b/ash/common/system/chromeos/palette/palette_tool_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..07a2f3dbfa2d85d285054a5e202826577c34ef33
|
| --- /dev/null
|
| +++ b/ash/common/system/chromeos/palette/palette_tool_manager.cc
|
| @@ -0,0 +1,98 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ash/common/system/chromeos/palette/palette_tool_manager.h"
|
| +
|
| +#include "ash/common/system/chromeos/palette/palette_tool.h"
|
| +#include "base/bind.h"
|
| +
|
| +namespace ash {
|
| +
|
| +PaletteToolManager::PaletteToolManager(WmWindow* window,
|
| + const base::Closure& on_tool_changed)
|
| + : on_tool_changed_(on_tool_changed), window_(window) {}
|
| +
|
| +PaletteToolManager::~PaletteToolManager() {}
|
| +
|
| +void PaletteToolManager::AddTool(std::unique_ptr<PaletteTool> tool) {
|
| + PaletteTool::ToolAction enable_tool =
|
| + base::Bind(&PaletteToolManager::ActivateTool, base::Unretained(this));
|
| + PaletteTool::ToolAction disable_tool =
|
| + base::Bind(&PaletteToolManager::DeactivateTool, base::Unretained(this));
|
| + tool->Initialize(window_, enable_tool, disable_tool);
|
| +
|
| + tools_.emplace_back(std::move(tool));
|
| +}
|
| +
|
| +void PaletteToolManager::ActivateTool(PaletteToolId tool_id) {
|
| + PaletteTool* new_tool = FindToolById(tool_id);
|
| + PaletteTool* previous_tool = active_tools_[new_tool->group()];
|
| +
|
| + if (new_tool == previous_tool)
|
| + return;
|
| +
|
| + if (previous_tool)
|
| + previous_tool->OnDisable();
|
| +
|
| + active_tools_[new_tool->group()] = new_tool;
|
| + new_tool->OnEnable();
|
| +
|
| + if (!on_tool_changed_.is_null())
|
| + on_tool_changed_.Run();
|
| +}
|
| +
|
| +void PaletteToolManager::DeactivateTool(PaletteToolId tool_id) {
|
| + PaletteTool* tool = FindToolById(tool_id);
|
| +
|
| + active_tools_[tool->group()] = nullptr;
|
| + tool->OnDisable();
|
| +
|
| + if (!on_tool_changed_.is_null())
|
| + on_tool_changed_.Run();
|
| +}
|
| +
|
| +bool PaletteToolManager::IsToolActive(PaletteToolId tool_id) {
|
| + PaletteTool* tool = FindToolById(tool_id);
|
| + return active_tools_[tool->group()] == tool;
|
| +}
|
| +
|
| +PaletteToolId PaletteToolManager::GetActiveTool(PaletteGroup group) {
|
| + PaletteTool* active_tool = active_tools_[group];
|
| + if (!active_tool)
|
| + return PaletteToolId::NONE;
|
| + return active_tool->tool_id();
|
| +}
|
| +
|
| +std::vector<PaletteToolView> PaletteToolManager::CreateViews() {
|
| + std::vector<PaletteToolView> views;
|
| + views.reserve(tools_.size());
|
| +
|
| + for (std::unique_ptr<PaletteTool>& tool : tools_) {
|
| + PaletteToolView view;
|
| + view.group = tool->group();
|
| + view.tool_id = tool->tool_id();
|
| + view.view = tool->CreateView();
|
| + views.push_back(view);
|
| + }
|
| +
|
| + return views;
|
| +}
|
| +
|
| +void PaletteToolManager::DestroyViews() {
|
| + for (std::unique_ptr<PaletteTool>& tool : tools_)
|
| + tool->DestroyView();
|
| +}
|
| +
|
| +PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) {
|
| + for (std::unique_ptr<PaletteTool>& tool : tools_) {
|
| + if (tool->tool_id() == tool_id)
|
| + return tool.get();
|
| + }
|
| +
|
| + LOG(ERROR) << "Could not find tool";
|
| + NOTREACHED();
|
| + return nullptr;
|
| +}
|
| +
|
| +} // namespace ash
|
|
|