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

Unified 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: Address comments 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 side-by-side diff with in-line comments
Download patch
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..d977941d7e2d68d86d646d15000d417283d494bd
--- /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());
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(
+
+ 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::ViewsDestroyed() {
+ for (std::unique_ptr<PaletteTool>& tool : tools_)
+ tool->ViewDestroyed();
+}
+
+PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) {
+ for (std::unique_ptr<PaletteTool>& tool : tools_) {
+ if (tool->tool_id() == tool_id)
+ return tool.get();
+ }
+
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
+ LOG(ERROR) << "Could not find tool";
+ NOTREACHED();
+ return nullptr;
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698