1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142

ash / system / palette / palette_tool_manager.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/palette/palette_tool_manager.h"

#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/system/palette/palette_tool.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"

namespace ash {

PaletteToolManager::PaletteToolManager(Delegate* delegate)
    : delegate_(delegate) {
  DCHECK(delegate_);
}

PaletteToolManager::~PaletteToolManager() = default;

bool PaletteToolManager::HasTool(PaletteToolId tool_id) {
  return FindToolById(tool_id);
}

void PaletteToolManager::AddTool(std::unique_ptr<PaletteTool> tool) {
  // The same PaletteToolId cannot be registered twice.
  DCHECK(!base::Contains(tools_, tool->GetToolId(), &PaletteTool::GetToolId));

  tools_.emplace_back(std::move(tool));
}

void PaletteToolManager::ActivateTool(PaletteToolId tool_id) {
  PaletteTool* new_tool = FindToolById(tool_id);
  DCHECK(new_tool);

  PaletteTool* previous_tool = active_tools_[new_tool->GetGroup()];

  if (new_tool == previous_tool)
    return;

  if (previous_tool) {
    previous_tool->OnDisable();
  }

  active_tools_[new_tool->GetGroup()] = new_tool;
  new_tool->OnEnable();

  delegate_->OnActiveToolChanged();
}

void PaletteToolManager::DeactivateTool(PaletteToolId tool_id) {
  PaletteTool* tool = FindToolById(tool_id);
  DCHECK(tool);

  active_tools_[tool->GetGroup()] = nullptr;
  tool->OnDisable();

  delegate_->OnActiveToolChanged();
}

bool PaletteToolManager::IsToolActive(PaletteToolId tool_id) {
  PaletteTool* tool = FindToolById(tool_id);
  DCHECK(tool);

  return active_tools_[tool->GetGroup()] == tool;
}

PaletteToolId PaletteToolManager::GetActiveTool(PaletteGroup group) {
  PaletteTool* active_tool = active_tools_[group];
  return active_tool ? active_tool->GetToolId() : PaletteToolId::NONE;
}

const gfx::VectorIcon& PaletteToolManager::GetActiveTrayIcon(
    PaletteToolId tool_id) const {
  PaletteTool* tool = FindToolById(tool_id);
  if (!tool)
    return kPaletteTrayIconDefaultNewuiIcon;

  return tool->GetActiveTrayIcon();
}

std::vector<PaletteToolView> PaletteToolManager::CreateViews() {
  std::vector<PaletteToolView> views;
  views.reserve(tools_.size());

  for (const auto& tool : tools_) {
    views::View* tool_view = tool->CreateView();
    if (!tool_view)
      continue;

    PaletteToolView view;
    view.group = tool->GetGroup();
    view.tool_id = tool->GetToolId();
    view.view = tool_view;
    views.push_back(view);
  }

  return views;
}

void PaletteToolManager::NotifyViewsDestroyed() {
  for (std::unique_ptr<PaletteTool>& tool : tools_)
    tool->OnViewDestroyed();
}

void PaletteToolManager::DisableActiveTool(PaletteGroup group) {
  PaletteToolId tool_id = GetActiveTool(group);
  if (tool_id != PaletteToolId::NONE)
    DeactivateTool(tool_id);
}

void PaletteToolManager::EnableTool(PaletteToolId tool_id) {
  ActivateTool(tool_id);
}

void PaletteToolManager::DisableTool(PaletteToolId tool_id) {
  DeactivateTool(tool_id);
}

void PaletteToolManager::HidePalette() {
  delegate_->HidePalette();
}

void PaletteToolManager::HidePaletteImmediately() {
  delegate_->HidePaletteImmediately();
}

aura::Window* PaletteToolManager::GetWindow() {
  return delegate_->GetWindow();
}

PaletteTool* PaletteToolManager::FindToolById(PaletteToolId tool_id) const {
  for (const std::unique_ptr<PaletteTool>& tool : tools_) {
    if (tool->GetToolId() == tool_id)
      return tool.get();
  }

  return nullptr;
}

}  // namespace ash