summaryrefslogtreecommitdiffstats
path: root/chromium/ash/shelf/shelf_model.cc
blob: d5d83f12e6a1001ad4bc7ca3fc351ea965cbdf3b (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 "ash/shelf/shelf_model.h"

#include <algorithm>

#include "ash/ash_switches.h"
#include "ash/shelf/shelf_model_observer.h"

namespace ash {

namespace {

int LauncherItemTypeToWeight(LauncherItemType type) {
  if (ash::switches::UseAlternateShelfLayout()) {
    switch (type) {
      case TYPE_APP_LIST:
        // TODO(skuhne): If the app list item becomes movable again, this need
        // to be a fallthrough.
        return 0;
      case TYPE_BROWSER_SHORTCUT:
      case TYPE_APP_SHORTCUT:
      case TYPE_WINDOWED_APP:
        return 1;
      case TYPE_PLATFORM_APP:
        return 2;
      case TYPE_APP_PANEL:
        return 3;
      case TYPE_UNDEFINED:
        NOTREACHED() << "LauncherItemType must be set";
        return -1;
    }
  } else {
    switch (type) {
      case TYPE_BROWSER_SHORTCUT:
      case TYPE_APP_SHORTCUT:
      case TYPE_WINDOWED_APP:
        return 0;
      case TYPE_PLATFORM_APP:
        return 1;
      case TYPE_APP_LIST:
        return 2;
      case TYPE_APP_PANEL:
        return 3;
      case TYPE_UNDEFINED:
        NOTREACHED() << "LauncherItemType must be set";
        return -1;
    }
  }

  NOTREACHED() << "Invalid type " << type;
  return 1;
}

bool CompareByWeight(const LauncherItem& a, const LauncherItem& b) {
  return LauncherItemTypeToWeight(a.type) < LauncherItemTypeToWeight(b.type);
}

}  // namespace

ShelfModel::ShelfModel() : next_id_(1), status_(STATUS_NORMAL) {
}

ShelfModel::~ShelfModel() {
}

int ShelfModel::Add(const LauncherItem& item) {
  return AddAt(items_.size(), item);
}

int ShelfModel::AddAt(int index, const LauncherItem& item) {
  index = ValidateInsertionIndex(item.type, index);
  items_.insert(items_.begin() + index, item);
  items_[index].id = next_id_++;
  FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index));
  return index;
}

void ShelfModel::RemoveItemAt(int index) {
  DCHECK(index >= 0 && index < item_count());
  // The app list and browser shortcut can't be removed.
  DCHECK(items_[index].type != TYPE_APP_LIST &&
         items_[index].type != TYPE_BROWSER_SHORTCUT);
  LauncherID id = items_[index].id;
  items_.erase(items_.begin() + index);
  FOR_EACH_OBSERVER(ShelfModelObserver, observers_,
                    ShelfItemRemoved(index, id));
}

void ShelfModel::Move(int index, int target_index) {
  if (index == target_index)
    return;
  // TODO: this needs to enforce valid ranges.
  LauncherItem item(items_[index]);
  items_.erase(items_.begin() + index);
  items_.insert(items_.begin() + target_index, item);
  FOR_EACH_OBSERVER(ShelfModelObserver, observers_,
                    ShelfItemMoved(index, target_index));
}

void ShelfModel::Set(int index, const LauncherItem& item) {
  DCHECK(index >= 0 && index < item_count());
  int new_index = item.type == items_[index].type ?
      index : ValidateInsertionIndex(item.type, index);

  LauncherItem old_item(items_[index]);
  items_[index] = item;
  items_[index].id = old_item.id;
  FOR_EACH_OBSERVER(ShelfModelObserver, observers_,
                    ShelfItemChanged(index, old_item));

  // If the type changes confirm that the item is still in the right order.
  if (new_index != index) {
    // The move function works by removing one item and then inserting it at the
    // new location. However - by removing the item first the order will change
    // so that our target index needs to be corrected.
    // TODO(skuhne): Moving this into the Move function breaks lots of unit
    // tests. So several functions were already using this incorrectly.
    // That needs to be cleaned up.
    if (index < new_index)
      new_index--;

    Move(index, new_index);
  }
}

int ShelfModel::ItemIndexByID(LauncherID id) const {
  LauncherItems::const_iterator i = ItemByID(id);
  return i == items_.end() ? -1 : static_cast<int>(i - items_.begin());
}

int ShelfModel::GetItemIndexForType(LauncherItemType type) {
  for (size_t i = 0; i < items_.size(); ++i) {
    if (items_[i].type == type)
      return i;
  }
  return -1;
}

LauncherItems::const_iterator ShelfModel::ItemByID(int id) const {
  for (LauncherItems::const_iterator i = items_.begin();
       i != items_.end(); ++i) {
    if (i->id == id)
      return i;
  }
  return items_.end();
}

int ShelfModel::FirstPanelIndex() const {
  LauncherItem weight_dummy;
  weight_dummy.type = TYPE_APP_PANEL;
  return std::lower_bound(items_.begin(), items_.end(), weight_dummy,
                          CompareByWeight) - items_.begin();
}

void ShelfModel::SetStatus(Status status) {
  if (status_ == status)
    return;

  status_ = status;
  FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfStatusChanged());
}

void ShelfModel::AddObserver(ShelfModelObserver* observer) {
  observers_.AddObserver(observer);
}

void ShelfModel::RemoveObserver(ShelfModelObserver* observer) {
  observers_.RemoveObserver(observer);
}

int ShelfModel::ValidateInsertionIndex(LauncherItemType type, int index) const {
  DCHECK(index >= 0 && index <= item_count() +
      (ash::switches::UseAlternateShelfLayout() ? 1 : 0));

  // Clamp |index| to the allowed range for the type as determined by |weight|.
  LauncherItem weight_dummy;
  weight_dummy.type = type;
  index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy,
                                    CompareByWeight) - items_.begin(),
                   static_cast<LauncherItems::difference_type>(index));
  index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy,
                                    CompareByWeight) - items_.begin(),
                   static_cast<LauncherItems::difference_type>(index));

  return index;
}

}  // namespace ash