Chromium Code Reviews| 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, | |
| 25 const content::ContextMenuParams& params) | |
|
Peter Beverloo
2016/07/20 16:40:14
micro nit: drop content::
mohsen
2016/07/20 22:16:31
Done.
| |
| 26 : ui::SimpleMenuModel(this), | |
| 27 web_contents_(web_contents), | |
| 28 params_(params) { | |
| 29 AddItem(COMMAND_OPEN_DEVTOOLS, base::ASCIIToUTF16("Inspect Element")); | |
| 30 } | |
| 31 ~ContextMenuModel() override {} | |
| 32 | |
| 33 // ui::SimpleMenuModel::Delegate: | |
| 34 bool IsCommandIdChecked(int command_id) const override { return false; } | |
| 35 bool IsCommandIdEnabled(int command_id) const override { return true; } | |
| 36 bool GetAcceleratorForCommandId(int command_id, | |
| 37 ui::Accelerator* accelerator) const override { | |
| 38 return false; | |
| 39 } | |
| 40 void ExecuteCommand(int command_id, int event_flags) override { | |
| 41 switch (command_id) { | |
| 42 case COMMAND_OPEN_DEVTOOLS: | |
| 43 ShellDevToolsFrontend* devtools_frontend = | |
| 44 ShellDevToolsFrontend::Show(web_contents_); | |
| 45 devtools_frontend->Activate(); | |
| 46 devtools_frontend->Focus(); | |
| 47 devtools_frontend->InspectElementAt(params_.x, params_.y); | |
| 48 break; | |
| 49 }; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 enum CommandID { COMMAND_OPEN_DEVTOOLS }; | |
| 54 | |
| 55 WebContents* web_contents_; | |
| 56 ContextMenuParams params_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(ContextMenuModel); | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 WebContentsViewDelegate* CreateShellWebContentsViewDelegate( | |
| 64 WebContents* web_contents) { | |
| 65 return new ShellWebContentsViewDelegate(web_contents); | |
| 66 } | |
| 67 | |
| 68 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate( | |
| 69 WebContents* web_contents) | |
| 70 : web_contents_(web_contents) {} | |
| 71 | |
| 72 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {} | |
| 73 | |
| 74 void ShellWebContentsViewDelegate::ShowContextMenu( | |
| 75 RenderFrameHost* render_frame_host, | |
| 76 const ContextMenuParams& params) { | |
| 77 if (switches::IsRunLayoutTestSwitchPresent()) | |
| 78 return; | |
| 79 | |
| 80 gfx::Point screen_point(params.x, params.y); | |
| 81 | |
| 82 // Convert from content coordinates to window coordinates. | |
| 83 // This code copied from chrome_web_contents_view_delegate_views.cc | |
| 84 aura::Window* web_contents_window = web_contents_->GetNativeView(); | |
| 85 aura::Window* root_window = web_contents_window->GetRootWindow(); | |
| 86 aura::client::ScreenPositionClient* screen_position_client = | |
| 87 aura::client::GetScreenPositionClient(root_window); | |
| 88 if (screen_position_client) { | |
| 89 screen_position_client->ConvertPointToScreen(web_contents_window, | |
| 90 &screen_point); | |
| 91 } | |
| 92 | |
| 93 ContextMenuModel context_menu_model(web_contents_, params); | |
| 94 views::MenuRunner context_menu_runner(&context_menu_model, | |
| 95 views::MenuRunner::CONTEXT_MENU); | |
| 96 | |
| 97 views::Widget* widget = views::Widget::GetWidgetForNativeView( | |
| 98 web_contents_->GetTopLevelNativeWindow()); | |
| 99 if (context_menu_runner.RunMenuAt( | |
| 100 widget, nullptr, gfx::Rect(screen_point, gfx::Size()), | |
| 101 views::MENU_ANCHOR_TOPRIGHT, | |
| 102 ui::MENU_SOURCE_NONE) == views::MenuRunner::MENU_DELETED) { | |
| 103 return; | |
|
Peter Beverloo
2016/07/20 16:40:14
I'll defer to Sadrul for the details here, but a f
mohsen
2016/07/20 22:16:31
(1) I think that might be possible. I allocated th
| |
| 104 } | |
| 105 } | |
| 106 | |
| 107 } // namespace content | |
| OLD | NEW |