cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.87k stars 2.92k forks source link

Cut, Copy, Paste commands are not exposed on IWpfWebBrowser #541

Closed JDCasterlineJr closed 10 years ago

JDCasterlineJr commented 10 years ago

There is no way to cut, copy, and paste from a context menu.

In order to cut, copy, and paste using a context menu on the ChromiumWebBrowser control, the cut, copy, and paste commands must be exposed.

I have solved this new feature with the code below, but I am not sure how to contribute or if this is even something that should be contributed. It would help me out if it made it the nuget package.

Sample CefSharp web browser with cut, copy, paste commands

<cefSharp:ChromiumWebBrowser x:Name="cefBrowser" Address="{Binding Address}" WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}">
    <FrameworkElement.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Cut" Command="{Binding WebBrowser.CutCommand}"></MenuItem>
            <MenuItem Header="Copy" Command="{Binding WebBrowser.CopyCommand}"></MenuItem>
            <MenuItem Header="Paste" Command="{Binding WebBrowser.PasteCommand}"></MenuItem>
        </ContextMenu>
    </FrameworkElement.ContextMenu>
</cefSharp:ChromiumWebBrowser>

First expose the commands on the IWpfWebBrowser class

/// <summary>
/// Cut selected text to the clipboard.
/// </summary>
ICommand CutCommand { get; }

/// <summary>
/// Copy selected text to the clipboard.
/// </summary>
ICommand CopyCommand { get; }

/// <summary>
/// Paste text from the clipboard.
/// </summary>
ICommand PasteCommand { get; }

The commands then have to be implemented in the ChromiumWebBrowser control Add these lines to the list of command declarations

public ICommand CutCommand { get; private set; }
public ICommand CopyCommand { get; private set; }
public ICommand PasteCommand { get; private set; }

Add these lines to the ChromiumWebBrowser constructor

CutCommand = new DelegateCommand(Cut);
CopyCommand = new DelegateCommand(Copy);
PasteCommand = new DelegateCommand(Paste);

Thank you, Jamie

amaitland commented 10 years ago

@JCasterline Thank you for the very detailed suggestion :+1: Sounds like a reasonable request. Feel free to create a PR and we'll look at merging your changes.

Some links that maybe useful.