OLD | NEW |
(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 "content/shell/browser/shell_web_contents_view_delegate.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/public/browser/web_contents.h" |
| 9 #include "content/shell/browser/shell_devtools_frontend.h" |
| 10 #include "content/shell/common/shell_switches.h" |
| 11 #include "ui/aura/client/screen_position_client.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/base/models/simple_menu_model.h" |
| 14 #include "ui/views/controls/menu/menu_runner.h" |
| 15 #include "ui/views/widget/widget.h" |
| 16 |
| 17 namespace content { |
| 18 namespace { |
| 19 |
| 20 // Model for the "Debug" menu |
| 21 class ContextMenuModel : public ui::SimpleMenuModel, |
| 22 public ui::SimpleMenuModel::Delegate { |
| 23 public: |
| 24 ContextMenuModel(WebContents* web_contents, const ContextMenuParams& params) |
| 25 : ui::SimpleMenuModel(this), |
| 26 web_contents_(web_contents), |
| 27 params_(params) { |
| 28 AddItem(COMMAND_OPEN_DEVTOOLS, base::ASCIIToUTF16("Inspect Element")); |
| 29 } |
| 30 ~ContextMenuModel() override {} |
| 31 |
| 32 // ui::SimpleMenuModel::Delegate: |
| 33 bool IsCommandIdChecked(int command_id) const override { return false; } |
| 34 bool IsCommandIdEnabled(int command_id) const override { return true; } |
| 35 bool GetAcceleratorForCommandId(int command_id, |
| 36 ui::Accelerator* accelerator) const override { |
| 37 return false; |
| 38 } |
| 39 void ExecuteCommand(int command_id, int event_flags) override { |
| 40 switch (command_id) { |
| 41 case COMMAND_OPEN_DEVTOOLS: |
| 42 ShellDevToolsFrontend* devtools_frontend = |
| 43 ShellDevToolsFrontend::Show(web_contents_); |
| 44 devtools_frontend->Activate(); |
| 45 devtools_frontend->Focus(); |
| 46 devtools_frontend->InspectElementAt(params_.x, params_.y); |
| 47 break; |
| 48 }; |
| 49 } |
| 50 |
| 51 private: |
| 52 enum CommandID { COMMAND_OPEN_DEVTOOLS }; |
| 53 |
| 54 WebContents* web_contents_; |
| 55 ContextMenuParams params_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ContextMenuModel); |
| 58 }; |
| 59 |
| 60 } // namespace |
| 61 |
| 62 WebContentsViewDelegate* CreateShellWebContentsViewDelegate( |
| 63 WebContents* web_contents) { |
| 64 return new ShellWebContentsViewDelegate(web_contents); |
| 65 } |
| 66 |
| 67 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate( |
| 68 WebContents* web_contents) |
| 69 : web_contents_(web_contents) {} |
| 70 |
| 71 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {} |
| 72 |
| 73 void ShellWebContentsViewDelegate::ShowContextMenu( |
| 74 RenderFrameHost* render_frame_host, |
| 75 const ContextMenuParams& params) { |
| 76 if (switches::IsRunLayoutTestSwitchPresent()) |
| 77 return; |
| 78 |
| 79 gfx::Point screen_point(params.x, params.y); |
| 80 |
| 81 // Convert from content coordinates to window coordinates. |
| 82 // This code copied from chrome_web_contents_view_delegate_views.cc |
| 83 aura::Window* web_contents_window = web_contents_->GetNativeView(); |
| 84 aura::Window* root_window = web_contents_window->GetRootWindow(); |
| 85 aura::client::ScreenPositionClient* screen_position_client = |
| 86 aura::client::GetScreenPositionClient(root_window); |
| 87 if (screen_position_client) { |
| 88 screen_position_client->ConvertPointToScreen(web_contents_window, |
| 89 &screen_point); |
| 90 } |
| 91 |
| 92 context_menu_model_.reset(new ContextMenuModel(web_contents_, params)); |
| 93 context_menu_runner_.reset(new views::MenuRunner( |
| 94 context_menu_model_.get(), views::MenuRunner::CONTEXT_MENU)); |
| 95 |
| 96 views::Widget* widget = views::Widget::GetWidgetForNativeView( |
| 97 web_contents_->GetTopLevelNativeWindow()); |
| 98 if (context_menu_runner_->RunMenuAt( |
| 99 widget, nullptr, gfx::Rect(screen_point, gfx::Size()), |
| 100 views::MENU_ANCHOR_TOPRIGHT, |
| 101 ui::MENU_SOURCE_NONE) == views::MenuRunner::MENU_DELETED) { |
| 102 return; |
| 103 } |
| 104 } |
| 105 |
| 106 } // namespace content |
OLD | NEW |