summaryrefslogtreecommitdiffstats
path: root/ui/app_list/app_list_folder_item.cc
blob: 95e5530b9895f31a2d491cd908a86d484d341a78 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2013 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 "ui/app_list/app_list_folder_item.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia_operations.h"

namespace app_list {

namespace {

const int kIconDimension = 48;
const size_t kNumTopApps = 4;
const int kItemIconDimension = 16;

// Generates the folder icon with the top 4 child item icons laid in 2x2 tile.
class FolderImageSource : public gfx::CanvasImageSource {
 public:
  typedef std::vector<gfx::ImageSkia> Icons;

  FolderImageSource(const Icons& icons, const gfx::Size& size)
      : gfx::CanvasImageSource(size, false),
        icons_(icons),
        size_(size) {
    DCHECK(icons.size() <= kNumTopApps);
  }

  virtual ~FolderImageSource() {}

 private:
  void DrawIcon(gfx::Canvas* canvas,
                const gfx::ImageSkia& icon,
                const gfx::Size icon_size,
                int x, int y) {
    gfx::ImageSkia resized(
        gfx::ImageSkiaOperations::CreateResizedImage(
            icon, skia::ImageOperations::RESIZE_BEST, icon_size));
    canvas->DrawImageInt(resized, 0, 0, resized.width(), resized.height(),
        x, y, resized.width(), resized.height(), true);
  }

  // gfx::CanvasImageSource overrides:
  virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
    // Draw folder circle.
    gfx::Point center = gfx::Point(size().width() / 2 , size().height() / 2);
    const SkColor kCircleColor = SkColorSetRGB(0xE1, 0xE1, 0xE1);
    SkPaint paint;
    paint.setStyle(SkPaint::kFill_Style);
    paint.setAntiAlias(true);
    paint.setColor(kCircleColor);
    canvas->DrawCircle(center, size().width() / 2, paint);

    if (icons_.size() == 0)
      return;

    // Tiled icon coordinates.
    const int delta_to_center = 1;
    const gfx::Size item_icon_size =
        gfx::Size(kItemIconDimension, kItemIconDimension);
    int left_x = center.x() - item_icon_size.width() - delta_to_center;
    int top_y = center.y() - item_icon_size.height() - delta_to_center;
    int right_x = center.x() + delta_to_center;
    int bottom_y = center.y() + delta_to_center;

    // top left icon
    size_t i = 0;
    DrawIcon(canvas, icons_[i++], item_icon_size, left_x, top_y);

    // top right icon
    if (i < icons_.size())
      DrawIcon(canvas, icons_[i++], item_icon_size, right_x, top_y);

    // left bottm icon
    if (i < icons_.size())
      DrawIcon(canvas, icons_[i++], item_icon_size, left_x, bottom_y);

    // right bottom icon
    if (i < icons_.size())
      DrawIcon(canvas, icons_[i], item_icon_size, right_x, bottom_y);
  }

  Icons icons_;
  gfx::Size size_;

  DISALLOW_COPY_AND_ASSIGN(FolderImageSource);
};

}  // namespace

AppListFolderItem::AppListFolderItem(const std::string& id)
    : AppListItemModel(id),
      apps_(new Apps) {
}

AppListFolderItem::~AppListFolderItem() {
  for (size_t i = 0; i < top_items_.size(); ++i)
    top_items_[i]->RemoveObserver(this);
}

void AppListFolderItem::AddItem(AppListItemModel* item) {
  std::string sort_order = item->GetSortOrder();
  // Note: ui::ListModel is not a sorted list.
  size_t index = 0;
  for (; index < apps_->item_count(); ++index) {
    if (sort_order < apps_->GetItemAt(index)->GetSortOrder())
      break;
  }
  apps_->AddAt(index, item);
  if (index <= kNumTopApps)
    UpdateTopItems();
}

void AppListFolderItem::DeleteItem(const std::string& id) {
  for (size_t i = 0; i < apps_->item_count(); ++i) {
    AppListItemModel* item = apps_->GetItemAt(i);
    if (item->id() == id) {
      scoped_ptr<AppListItemModel> to_be_deleted(apps_->RemoveAt(i));
      DCHECK(item == to_be_deleted.get());
      if (i <= kNumTopApps)
        UpdateTopItems();
      return;
    }
  }
}

void AppListFolderItem::UpdateIcon() {
  FolderImageSource::Icons top_icons;
  for (size_t i = 0; i < top_items_.size(); ++i)
    top_icons.push_back(top_items_[i]->icon());

  const gfx::Size icon_size = gfx::Size(kIconDimension, kIconDimension);
  gfx::ImageSkia icon = gfx::ImageSkia(
      new FolderImageSource(top_icons, icon_size),
      icon_size);
  SetIcon(icon, false);
}

std::string AppListFolderItem::GetSortOrder() const {
  // For now, put folders at the end of the list.
  // TODO(stevenjb): Implement synced app list ordering.
  return "zzzzzzzz";
}

void AppListFolderItem::Activate(int event_flags) {
  // Folder handling is implemented by the View, so do nothing.
}

// static
const char AppListFolderItem::kAppType[] = "FolderItem";

const char* AppListFolderItem::GetAppType() const {
  return AppListFolderItem::kAppType;
}

ui::MenuModel* AppListFolderItem::GetContextMenuModel() {
  // TODO(stevenjb/jennyz): Implement.
  return NULL;
}

void AppListFolderItem::ItemIconChanged() {
  UpdateIcon();
}

void AppListFolderItem::ItemTitleChanged() {
}

void AppListFolderItem::ItemHighlightedChanged() {
}

void AppListFolderItem::ItemIsInstallingChanged() {
}

void AppListFolderItem::ItemPercentDownloadedChanged() {
}

void AppListFolderItem::UpdateTopItems() {
  for (size_t i = 0; i < top_items_.size(); ++i)
    top_items_[i]->RemoveObserver(this);
  top_items_.clear();

  for (size_t i = 0; i < kNumTopApps && i < apps_->item_count(); ++i) {
    AppListItemModel* item = apps_->GetItemAt(i);
    item->AddObserver(this);
    top_items_.push_back(item);
  }
  UpdateIcon();
}

}  // namespace app_list