summaryrefslogtreecommitdiffstats
path: root/chromium/ash/shelf/shelf_navigator.cc
blob: 0dc5dfd5379adbca1009bf34b7a913cd303ffea2 (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
// 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_navigator.h"

#include "ash/shelf/shelf_model.h"

namespace ash {

namespace {

// Returns true if accelerator processing should skip the launcher item with
// the specified type.
bool ShouldSkip(ash::LauncherItemType type) {
  return type == ash::TYPE_APP_LIST ||
         type == ash::TYPE_BROWSER_SHORTCUT ||
         type == ash::TYPE_APP_SHORTCUT ||
         type == ash::TYPE_WINDOWED_APP;
}

}  // namespace

int GetNextActivatedItemIndex(const ShelfModel& model,
                              CycleDirection direction) {
  const ash::LauncherItems& items = model.items();
  int item_count = model.item_count();
  int current_index = -1;
  int first_running = -1;

  for (int i = 0; i < item_count; ++i) {
    const ash::LauncherItem& item = items[i];
    if (ShouldSkip(item.type))
      continue;

    if (item.status == ash::STATUS_RUNNING && first_running < 0)
      first_running = i;

    if (item.status == ash::STATUS_ACTIVE) {
      current_index = i;
      break;
    }
  }

  // If nothing is active, try to active the first running item.
  if (current_index < 0) {
    if (first_running >= 0)
      return first_running;
    else
      return -1;
  }

  int step = (direction == CYCLE_FORWARD) ? 1 : -1;

  // Find the next item and activate it.
  for (int i = (current_index + step + item_count) % item_count;
       i != current_index; i = (i + step + item_count) % item_count) {
    const ash::LauncherItem& item = items[i];
    if (ShouldSkip(item.type))
      continue;

    // Skip already active item.
    if (item.status == ash::STATUS_ACTIVE)
      continue;

    return i;
  }

  return -1;
}

}  // namespace ash