Chromium Code Reviews| Index: ash/mus/context_menu_mus.cc |
| diff --git a/ash/mus/context_menu_mus.cc b/ash/mus/context_menu_mus.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..318a4e8db24c30b592611b1c4bde3fad0aeddca3 |
| --- /dev/null |
| +++ b/ash/mus/context_menu_mus.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2016 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/mus/context_menu_mus.h" |
| + |
| +#include "ash/desktop_background/user_wallpaper_delegate.h" |
| +#include "ash/root_window_controller.h" |
| +#include "ash/session/session_state_delegate.h" |
| +#include "ash/shelf/shelf.h" |
| +#include "ash/shelf/shelf_types.h" |
| +#include "ash/shelf/shelf_widget.h" |
| +#include "ash/shell.h" |
| +#include "grit/ash_strings.h" |
| +#include "mojo/common/common_type_converters.h" |
| +#include "mojo/shell/public/cpp/connector.h" |
| + |
| +namespace ash { |
| +namespace sysui { |
| + |
| +ContextMenuMus::ContextMenuMus(ContextMenuMus* root_menu, |
| + Shelf* shelf, |
| + const ShelfItem* item) |
| + : ui::SimpleMenuModel(nullptr), |
| + root_menu_(root_menu ? root_menu : this), |
| + shelf_(shelf), |
| + item_(item), |
| + alignment_menu_(shelf) { |
| + set_delegate(this); |
| +} |
| + |
| +void ContextMenuMus::InitializeRootMenu(mojo::Connector* connector) { |
| + // Only the root menu should add ash items or make a connection to Chrome. |
| + DCHECK_EQ(root_menu_, this); |
| + |
| + connector->ConnectToInterface("exe:chrome", &context_menu_); |
| + context_menu_->GetItemsAndInfo( |
| + item_ ? item_->type : ShelfItemType::TYPE_UNDEFINED, |
| + item_ ? item_->id : 0, |
| + base::Bind(&ContextMenuMus::GetItemsAndInfoCallback, |
| + base::Unretained(this))); |
| + 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
|
| +} |
| + |
| +ContextMenuMus::~ContextMenuMus() {} |
| + |
| +void ContextMenuMus::GetItemsAndInfoCallback( |
| + mojo::Array<mash::shelf::mojom::ContextMenuItemPtr> items, |
| + bool can_user_modify_shelf_auto_hide_behavior, |
| + bool is_guest_session) { |
| + AddItems(std::move(items)); |
| + |
| + // TODO(msw): |controller| is null; ask the window manager about fullscreen? |
| + RootWindowController* controller = RootWindowController::ForWindow( |
| + shelf_->shelf_widget()->GetNativeWindow()->GetRootWindow()); |
| + if (can_user_modify_shelf_auto_hide_behavior && |
| + (!controller || !controller->GetWindowForFullscreenMode())) { |
| + AddCheckItemWithStringId(MENU_AUTO_HIDE, |
| + IDS_ASH_SHELF_CONTEXT_MENU_AUTO_HIDE); |
| + } |
| + if (ShelfWidget::ShelfAlignmentAllowed() && |
| + !Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) { |
| + AddSubMenuWithStringId(MENU_ALIGNMENT_MENU, |
| + IDS_ASH_SHELF_CONTEXT_MENU_POSITION, |
| + &alignment_menu_); |
| + } |
| + if (!is_guest_session) |
| + AddItemWithStringId(MENU_CHANGE_WALLPAPER, IDS_AURA_SET_DESKTOP_WALLPAPER); |
| +} |
| + |
| +void ContextMenuMus::AddItems( |
| + mojo::Array<mash::shelf::mojom::ContextMenuItemPtr> items) { |
| + for (const auto& item : items) { |
| + switch (item->type) { |
| + case mash::shelf::mojom::ContextMenuItem::Type::ITEM: |
| + DCHECK_EQ(item_enabled_.count(item->command_id), 0U); |
| + item_enabled_[item->command_id] = item->enabled; |
| + AddItem(item->command_id, item->label.To<base::string16>()); |
| + break; |
| + case mash::shelf::mojom::ContextMenuItem::Type::CHECK: |
| + DCHECK_EQ(item_enabled_.count(item->command_id), 0U); |
| + DCHECK_EQ(item_checked_.count(item->command_id), 0U); |
| + item_enabled_[item->command_id] = item->enabled; |
| + item_checked_[item->command_id] = item->checked; |
| + AddCheckItem(item->command_id, item->label.To<base::string16>()); |
| + break; |
| + case mash::shelf::mojom::ContextMenuItem::Type::RADIO: |
| + DCHECK_EQ(item_enabled_.count(item->command_id), 0U); |
| + DCHECK_EQ(item_checked_.count(item->command_id), 0U); |
| + item_enabled_[item->command_id] = item->enabled; |
| + item_checked_[item->command_id] = item->checked; |
| + AddRadioItem(item->command_id, item->label.To<base::string16>(), |
| + item->radio_group_id); |
| + break; |
| + case mash::shelf::mojom::ContextMenuItem::Type::SEPARATOR: |
| + AddSeparator(ui::NORMAL_SEPARATOR); |
| + break; |
| + case mash::shelf::mojom::ContextMenuItem::Type::SUBMENU: |
| + DCHECK_EQ(item_enabled_.count(item->command_id), 0U); |
| + DCHECK_EQ(item_submenu_.count(item->command_id), 0U); |
| + item_enabled_[item->command_id] = item->enabled; |
| + item_submenu_[item->command_id].reset( |
| + new ContextMenuMus(root_menu_, shelf_, item_)); |
| + item_submenu_[item->command_id]->AddItems(std::move(item->submenu)); |
| + AddSubMenu(item->command_id, item->label.To<base::string16>(), |
| + item_submenu_[item->command_id].get()); |
| + break; |
| + default: |
| + NOTREACHED() << "Unknown mus context menu item type."; |
| + } |
| + } |
| +} |
| + |
| +bool ContextMenuMus::IsCommandIdChecked(int command_id) const { |
| + switch (command_id) { |
| + case MENU_AUTO_HIDE: |
| + return shelf_->GetAutoHideBehavior() == SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; |
| + case MENU_ALIGNMENT_MENU: |
| + case MENU_CHANGE_WALLPAPER: |
| + return false; |
| + default: |
| + if (item_checked_.find(command_id) != item_checked_.end()) |
| + return item_checked_.at(command_id); |
| + } |
| + NOTREACHED() << "Unknown mus context menu command " << command_id; |
| + return false; |
| +} |
| + |
| +bool ContextMenuMus::IsCommandIdEnabled(int command_id) const { |
| + switch (command_id) { |
| + case MENU_AUTO_HIDE: |
| + case MENU_ALIGNMENT_MENU: |
| + return true; |
| + case MENU_CHANGE_WALLPAPER: |
| + return Shell::GetInstance() |
| + ->user_wallpaper_delegate() |
| + ->CanOpenSetWallpaperPage(); |
| + default: |
| + if (item_enabled_.find(command_id) != item_enabled_.end()) |
| + return item_enabled_.at(command_id); |
| + } |
| + NOTREACHED() << "Unknown mus context menu command " << command_id; |
| + return false; |
| +} |
| + |
| +bool ContextMenuMus::GetAcceleratorForCommandId(int command_id, |
| + ui::Accelerator* accelerator) { |
| + return false; |
| +} |
| + |
| +void ContextMenuMus::ExecuteCommand(int command_id, int event_flags) { |
| + Shell* shell = Shell::GetInstance(); |
| + switch (static_cast<MenuItem>(command_id)) { |
| + case MENU_AUTO_HIDE: |
| + shelf_->SetAutoHideBehavior(shelf_->GetAutoHideBehavior() == |
| + SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS |
| + ? SHELF_AUTO_HIDE_BEHAVIOR_NEVER |
| + : SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
| + break; |
| + case MENU_ALIGNMENT_MENU: |
| + break; |
| + case MENU_CHANGE_WALLPAPER: |
| + shell->user_wallpaper_delegate()->OpenSetWallpaperPage(); |
| + default: |
| + DCHECK_EQ(item_enabled_.count(command_id), 1U); |
| + root_menu_->context_menu_->ExecuteCommand(command_id, event_flags); |
| + } |
| +} |
| + |
| +} // namespace sysui |
| +} // namespace ash |