Skip to content

Commit

Permalink
Wpf - Context Menu improvements/fixes
Browse files Browse the repository at this point in the history
- Fix separator not showing
- Remove unused code
- Add consts for devtools menu command id's
  • Loading branch information
amaitland committed Nov 30, 2021
1 parent 165a050 commit 6e211bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
27 changes: 6 additions & 21 deletions CefSharp.Wpf.Example/Handlers/MenuHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.Generic;
using CefSharp.Wpf.Handler;

namespace CefSharp.Wpf.Example.Handlers
Expand All @@ -16,6 +15,8 @@ public MenuHandler(bool addDevtoolsMenuItems = false) : base(addDevtoolsMenuItem

protected override void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
base.OnBeforeContextMenu(chromiumWebBrowser, browser, frame, parameters, model);

Console.WriteLine("Context menu opened");
Console.WriteLine(parameters.MisspelledWord);

Expand All @@ -24,33 +25,17 @@ protected override void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBro
model.AddSeparator();
}

//For this menu handler 26501 and 26502 are used by the Show/Close DevTools commands
model.AddItem((CefMenuCommand)26503, "Do Something");
//For this menu handler 28440 and 28441 are used by the Show/Close DevTools commands
model.AddItem((CefMenuCommand)26501, "Do Something");

//To disable context mode then clear
//To disable context menu then clear
// model.Clear();
}

protected override bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == (CefMenuCommand)26501)
{
browser.GetHost().ShowDevTools();
return true;
}
if (commandId == (CefMenuCommand)26502)
{
browser.GetHost().CloseDevTools();
return true;
}

return false;
}

protected override void ExecuteCommand(IBrowser browser, ContextMenuExecuteModel model)
{
//Custom item
if (model.MenuCommand == (CefMenuCommand)26503)
if (model.MenuCommand == (CefMenuCommand)26501)
{
Console.WriteLine("Custom menu used");
}
Expand Down
44 changes: 18 additions & 26 deletions CefSharp.Wpf/Handler/ContextMenuHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ namespace CefSharp.Wpf.Handler
/// </summary>
public class ContextMenuHandler : CefSharp.Handler.ContextMenuHandler
{
/// <summary>
/// Open DevTools <see cref="CefMenuCommand"/> Id
/// </summary>
public const int CefMenuCommandShowDevToolsId = 28440;
/// <summary>
/// Close DevTools <see cref="CefMenuCommand"/> Id
/// </summary>
public const int CefMenuCommandCloseDevToolsId = 28441;

private readonly bool addDevtoolsMenuItems;

public ContextMenuHandler(bool addDevtoolsMenuItems = false)
Expand All @@ -32,28 +41,11 @@ protected override void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBro
model.AddSeparator();
}

model.AddItem((CefMenuCommand)26501, "Show DevTools");
model.AddItem((CefMenuCommand)26502, "Close DevTools");
model.AddItem((CefMenuCommand)CefMenuCommandShowDevToolsId, "Show DevTools (Inspect)");
model.AddItem((CefMenuCommand)CefMenuCommandCloseDevToolsId, "Close DevTools");
}
}

/// <inheritdoc/>
protected override bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == (CefMenuCommand)26501)
{
browser.GetHost().ShowDevTools();
return true;
}
if (commandId == (CefMenuCommand)26502)
{
browser.GetHost().CloseDevTools();
return true;
}

return false;
}

/// <inheritdoc/>
protected override void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
Expand Down Expand Up @@ -105,17 +97,17 @@ protected override bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser
foreach (var item in menuItems)
{
if (item.CommandId == CefMenuCommand.NotFound)
if (item.IsSeperator)
{
menu.Items.Add(new Separator());
continue;
}
if(item.IsSeperator)
if (item.CommandId == CefMenuCommand.NotFound)
{
menu.Items.Add(new Separator());
continue;
}
}
var menuItem = new MenuItem
{
Expand Down Expand Up @@ -284,12 +276,12 @@ protected virtual void ExecuteCommand(IBrowser browser, ContextMenuExecuteModel
break;
}

case (CefMenuCommand)26501:
case (CefMenuCommand)CefMenuCommandShowDevToolsId:
{
browser.GetHost().ShowDevTools(inspectElementAtX: model.XCoord, inspectElementAtY: model.YCoord);
break;
}
case (CefMenuCommand)26502:
case (CefMenuCommand)CefMenuCommandCloseDevToolsId:
{
browser.GetHost().CloseDevTools();
break;
Expand Down

0 comments on commit 6e211bc

Please sign in to comment.