1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/app_list/app_list_menu.h"
6
7#include "ui/app_list/app_list_view_delegate.h"
8#include "ui/base/l10n/l10n_util.h"
9#include "ui/base/models/menu_separator_types.h"
10#include "ui/base/resource/resource_bundle.h"
11#include "ui/resources/grit/ui_resources.h"
12#include "ui/strings/grit/ui_strings.h"
13
14namespace app_list {
15
16AppListMenu::AppListMenu(AppListViewDelegate* delegate)
17    : menu_model_(this),
18      delegate_(delegate),
19      users_(delegate->GetUsers()) {
20  InitMenu();
21}
22
23AppListMenu::~AppListMenu() {}
24
25void AppListMenu::InitMenu() {
26  // User selector menu section. We don't show the user selector if there is
27  // only 1 user.
28  if (users_.size() > 1) {
29    for (size_t i = 0; i < users_.size(); ++i) {
30#if defined(OS_MACOSX)
31      menu_model_.AddRadioItem(SELECT_PROFILE + i,
32                               users_[i].email.empty() ? users_[i].name
33                                                       : users_[i].email,
34                               0 /* group_id */);
35#elif defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
36      menu_model_.AddItem(SELECT_PROFILE + i, users_[i].name);
37      int menu_index = menu_model_.GetIndexOfCommandId(SELECT_PROFILE + i);
38      menu_model_.SetSublabel(menu_index, users_[i].email);
39      // Use custom check mark.
40      if (users_[i].active) {
41        ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
42        menu_model_.SetIcon(menu_index, gfx::Image(*rb.GetImageSkiaNamed(
43            IDR_APP_LIST_USER_INDICATOR)));
44      }
45#endif
46    }
47    menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
48  }
49
50  menu_model_.AddItem(SHOW_SETTINGS, l10n_util::GetStringUTF16(
51      IDS_APP_LIST_OPEN_SETTINGS));
52
53  menu_model_.AddItem(SHOW_HELP, l10n_util::GetStringUTF16(
54      IDS_APP_LIST_HELP));
55
56  menu_model_.AddItem(SHOW_FEEDBACK, l10n_util::GetStringUTF16(
57      IDS_APP_LIST_OPEN_FEEDBACK));
58}
59
60bool AppListMenu::IsCommandIdChecked(int command_id) const {
61#if defined(OS_MACOSX)
62  DCHECK_LT(static_cast<unsigned>(command_id) - SELECT_PROFILE, users_.size());
63  return users_[command_id - SELECT_PROFILE].active;
64#else
65  return false;
66#endif
67}
68
69bool AppListMenu::IsCommandIdEnabled(int command_id) const {
70  return true;
71}
72
73bool AppListMenu::GetAcceleratorForCommandId(int command_id,
74                                             ui::Accelerator* accelerator) {
75  return false;
76}
77
78void AppListMenu::ExecuteCommand(int command_id, int event_flags) {
79  if (command_id >= SELECT_PROFILE) {
80    delegate_->ShowForProfileByPath(
81        users_[command_id - SELECT_PROFILE].profile_path);
82    return;
83  }
84  switch (command_id) {
85    case SHOW_SETTINGS:
86      delegate_->OpenSettings();
87      break;
88    case SHOW_HELP:
89      delegate_->OpenHelp();
90      break;
91    case SHOW_FEEDBACK:
92      delegate_->OpenFeedback();
93      break;
94    default:
95      NOTREACHED();
96  }
97}
98
99}  // namespace app_list
100