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

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: 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..f6dbd7ed53213921b34fad8c07306fa98ce31c29
--- /dev/null
+++ b/ash/common/system/chromeos/palette/palette_tool_manager.cc
@@ -0,0 +1,95 @@
+// 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)
oshima 2016/07/19 00:08:53 nit: ternary operator?
jdufault 2016/07/19 18:06:21 Done.
+ return PaletteToolId::NONE;
+ return active_tool->tool_id();
+}
+
+std::vector<PaletteToolView> PaletteToolManager::CreateViews() {
+ std::vector<PaletteToolView> views(tools_.size());
+
+ for (size_t i = 0; i < tools_.size(); ++i) {
+ PaletteToolView* view = &views[i];
+ view->group = tools_[i]->group();
+ view->tool_id = tools_[i]->tool_id();
+ view->view = tools_[i]->CreateView();
+ }
+
+ return views;
+}
+
+void PaletteToolManager::NotifyViewsDestroyed() {
+ for (std::unique_ptr<PaletteTool>& tool : tools_)
+ tool->OnViewDestroyed();
+}
+
+PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) {
+ for (std::unique_ptr<PaletteTool>& tool : tools_) {
+ if (tool->tool_id() == tool_id)
+ return tool.get();
+ }
+
+ NOTREACHED();
+ return nullptr;
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698