Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(727)

Side by Side Diff: ash/mus/context_menu_mus.cc

Issue 1760743002: Add simple mash context menu support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase; use test items; cleanup. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "ash/mus/context_menu_mus.h"
6
7 #include "ash/desktop_background/user_wallpaper_delegate.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/session/session_state_delegate.h"
10 #include "ash/shelf/shelf.h"
11 #include "ash/shelf/shelf_types.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "grit/ash_strings.h"
15 #include "mojo/common/common_type_converters.h"
16 #include "mojo/shell/public/cpp/connector.h"
17
18 namespace ash {
19 namespace sysui {
20
21 ContextMenuMus::ContextMenuMus(ContextMenuMus* root_menu,
22 Shelf* shelf,
23 const ShelfItem* item)
24 : ui::SimpleMenuModel(nullptr),
25 root_menu_(root_menu ? root_menu : this),
26 shelf_(shelf),
27 item_(item),
28 alignment_menu_(shelf) {
29 set_delegate(this);
30 }
31
32 void ContextMenuMus::InitializeRootMenu(mojo::Connector* connector) {
33 // Only the root menu should add ash items or make a connection to Chrome.
34 DCHECK_EQ(root_menu_, this);
35
36 connector->ConnectToInterface("exe:chrome", &context_menu_);
37 context_menu_->GetItemsAndInfo(
38 item_ ? item_->type : ShelfItemType::TYPE_UNDEFINED,
39 item_ ? item_->id : 0,
40 base::Bind(&ContextMenuMus::GetItemsAndInfoCallback,
41 base::Unretained(this)));
42 context_menu_.WaitForIncomingResponse();
sky 2016/03/18 20:30:57 We shouldn't block here. There are two options for
msw 2016/03/18 22:10:30 Adding a menu without Chrome hooks for now, as dis
43 }
44
45 ContextMenuMus::~ContextMenuMus() {}
46
47 void ContextMenuMus::GetItemsAndInfoCallback(
48 mojo::Array<mash::shelf::mojom::ContextMenuItemPtr> items,
49 bool can_user_modify_shelf_auto_hide_behavior,
50 bool is_guest_session) {
51 AddItems(std::move(items));
52
53 // TODO(msw): |controller| is null; ask the window manager about fullscreen?
54 RootWindowController* controller = RootWindowController::ForWindow(
55 shelf_->shelf_widget()->GetNativeWindow()->GetRootWindow());
56 if (can_user_modify_shelf_auto_hide_behavior &&
57 (!controller || !controller->GetWindowForFullscreenMode())) {
58 AddCheckItemWithStringId(MENU_AUTO_HIDE,
59 IDS_ASH_SHELF_CONTEXT_MENU_AUTO_HIDE);
60 }
61 if (ShelfWidget::ShelfAlignmentAllowed() &&
62 !Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) {
63 AddSubMenuWithStringId(MENU_ALIGNMENT_MENU,
64 IDS_ASH_SHELF_CONTEXT_MENU_POSITION,
65 &alignment_menu_);
66 }
67 if (!is_guest_session)
68 AddItemWithStringId(MENU_CHANGE_WALLPAPER, IDS_AURA_SET_DESKTOP_WALLPAPER);
69 }
70
71 void ContextMenuMus::AddItems(
72 mojo::Array<mash::shelf::mojom::ContextMenuItemPtr> items) {
73 for (const auto& item : items) {
74 switch (item->type) {
75 case mash::shelf::mojom::ContextMenuItem::Type::ITEM:
76 DCHECK_EQ(item_enabled_.count(item->command_id), 0U);
77 item_enabled_[item->command_id] = item->enabled;
78 AddItem(item->command_id, item->label.To<base::string16>());
79 break;
80 case mash::shelf::mojom::ContextMenuItem::Type::CHECK:
81 DCHECK_EQ(item_enabled_.count(item->command_id), 0U);
82 DCHECK_EQ(item_checked_.count(item->command_id), 0U);
83 item_enabled_[item->command_id] = item->enabled;
84 item_checked_[item->command_id] = item->checked;
85 AddCheckItem(item->command_id, item->label.To<base::string16>());
86 break;
87 case mash::shelf::mojom::ContextMenuItem::Type::RADIO:
88 DCHECK_EQ(item_enabled_.count(item->command_id), 0U);
89 DCHECK_EQ(item_checked_.count(item->command_id), 0U);
90 item_enabled_[item->command_id] = item->enabled;
91 item_checked_[item->command_id] = item->checked;
92 AddRadioItem(item->command_id, item->label.To<base::string16>(),
93 item->radio_group_id);
94 break;
95 case mash::shelf::mojom::ContextMenuItem::Type::SEPARATOR:
96 AddSeparator(ui::NORMAL_SEPARATOR);
97 break;
98 case mash::shelf::mojom::ContextMenuItem::Type::SUBMENU:
99 DCHECK_EQ(item_enabled_.count(item->command_id), 0U);
100 DCHECK_EQ(item_submenu_.count(item->command_id), 0U);
101 item_enabled_[item->command_id] = item->enabled;
102 item_submenu_[item->command_id].reset(
103 new ContextMenuMus(root_menu_, shelf_, item_));
104 item_submenu_[item->command_id]->AddItems(std::move(item->submenu));
105 AddSubMenu(item->command_id, item->label.To<base::string16>(),
106 item_submenu_[item->command_id].get());
107 break;
108 default:
109 NOTREACHED() << "Unknown mus context menu item type.";
110 }
111 }
112 }
113
114 bool ContextMenuMus::IsCommandIdChecked(int command_id) const {
115 switch (command_id) {
116 case MENU_AUTO_HIDE:
117 return shelf_->GetAutoHideBehavior() == SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
118 case MENU_ALIGNMENT_MENU:
119 case MENU_CHANGE_WALLPAPER:
120 return false;
121 default:
122 if (item_checked_.find(command_id) != item_checked_.end())
123 return item_checked_.at(command_id);
124 }
125 NOTREACHED() << "Unknown mus context menu command " << command_id;
126 return false;
127 }
128
129 bool ContextMenuMus::IsCommandIdEnabled(int command_id) const {
130 switch (command_id) {
131 case MENU_AUTO_HIDE:
132 case MENU_ALIGNMENT_MENU:
133 return true;
134 case MENU_CHANGE_WALLPAPER:
135 return Shell::GetInstance()
136 ->user_wallpaper_delegate()
137 ->CanOpenSetWallpaperPage();
138 default:
139 if (item_enabled_.find(command_id) != item_enabled_.end())
140 return item_enabled_.at(command_id);
141 }
142 NOTREACHED() << "Unknown mus context menu command " << command_id;
143 return false;
144 }
145
146 bool ContextMenuMus::GetAcceleratorForCommandId(int command_id,
147 ui::Accelerator* accelerator) {
148 return false;
149 }
150
151 void ContextMenuMus::ExecuteCommand(int command_id, int event_flags) {
152 Shell* shell = Shell::GetInstance();
153 switch (static_cast<MenuItem>(command_id)) {
154 case MENU_AUTO_HIDE:
155 shelf_->SetAutoHideBehavior(shelf_->GetAutoHideBehavior() ==
156 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
157 ? SHELF_AUTO_HIDE_BEHAVIOR_NEVER
158 : SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
159 break;
160 case MENU_ALIGNMENT_MENU:
161 break;
162 case MENU_CHANGE_WALLPAPER:
163 shell->user_wallpaper_delegate()->OpenSetWallpaperPage();
164 default:
165 DCHECK_EQ(item_enabled_.count(command_id), 1U);
166 root_menu_->context_menu_->ExecuteCommand(command_id, event_flags);
167 }
168 }
169
170 } // namespace sysui
171 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698