1 // Copyright 2014 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 "components/renderer_context_menu/views/toolkit_delegate_views.h" 6 7 #include "ui/gfx/geometry/rect.h" 8 #include "ui/views/controls/menu/menu_item_view.h" 9 #include "ui/views/controls/menu/menu_model_adapter.h" 10 #include "ui/views/controls/menu/menu_runner.h" 11 12 ToolkitDelegateViews::ToolkitDelegateViews() : menu_view_(NULL) {} 13 14 ToolkitDelegateViews::~ToolkitDelegateViews() {} 15 16 void ToolkitDelegateViews::RunMenuAt(views::Widget* parent, 17 const gfx::Point& point, 18 ui::MenuSourceType type) { 19 views::MenuAnchorPosition anchor_position = 20 (type == ui::MENU_SOURCE_TOUCH || 21 type == ui::MENU_SOURCE_TOUCH_EDIT_MENU) 22 ? views::MENU_ANCHOR_BOTTOMCENTER 23 : views::MENU_ANCHOR_TOPLEFT; 24 views::MenuRunner::RunResult result ALLOW_UNUSED = menu_runner_->RunMenuAt( 25 parent, NULL, gfx::Rect(point, gfx::Size()), anchor_position, type); 26 } 27 28 void ToolkitDelegateViews::Init(ui::SimpleMenuModel* menu_model) { 29 menu_adapter_.reset(new views::MenuModelAdapter(menu_model)); 30 menu_view_ = menu_adapter_->CreateMenu(); 31 menu_runner_.reset(new views::MenuRunner( 32 menu_view_, 33 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU)); 34 } 35 36 void ToolkitDelegateViews::Cancel() { 37 DCHECK(menu_runner_.get()); 38 menu_runner_->Cancel(); 39 } 40 41 void ToolkitDelegateViews::UpdateMenuItem(int command_id, 42 bool enabled, 43 bool hidden, 44 const base::string16& title) { 45 views::MenuItemView* item = menu_view_->GetMenuItemByID(command_id); 46 if (!item) 47 return; 48 49 item->SetEnabled(enabled); 50 item->SetTitle(title); 51 item->SetVisible(!hidden); 52 53 views::MenuItemView* parent = item->GetParentMenuItem(); 54 if (!parent) 55 return; 56 57 parent->ChildrenChanged(); 58 } 59 60