-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathMenuHandler.cs
More file actions
222 lines (199 loc) · 9.09 KB
/
MenuHandler.cs
File metadata and controls
222 lines (199 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright © 2014 The CefSharp Authors. All rights reserved.
//
// 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 System.Linq;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
namespace CefSharp.Wpf.Example.Handlers
{
public class MenuHandler : IContextMenuHandler
{
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
Console.WriteLine("Context menu opened");
Console.WriteLine(parameters.MisspelledWord);
if (model.Count > 0)
{
model.AddSeparator();
}
model.AddItem((CefMenuCommand)26501, "Show DevTools");
model.AddItem((CefMenuCommand)26502, "Close DevTools");
//To disable context mode then clear
// model.Clear();
}
bool IContextMenuHandler.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;
}
void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
var webBrowser = (ChromiumWebBrowser)chromiumWebBrowser;
webBrowser.Dispatcher.Invoke(() =>
{
webBrowser.ContextMenu = null;
});
}
bool IContextMenuHandler.RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
//NOTE: Return false to use the built in Context menu - in WPF this requires you integrate into your existing message loop, read the General Usage Guide for more details
//https://github.com/cefsharp/CefSharp/wiki/General-Usage#multithreadedmessageloop
//return false;
var webBrowser = (ChromiumWebBrowser)chromiumWebBrowser;
//IMenuModel is only valid in the context of this method, so need to read the values before invoking on the UI thread
var menuItems = GetMenuItems(model).ToList();
webBrowser.Dispatcher.Invoke(() =>
{
var menu = new ContextMenu
{
IsOpen = true
};
RoutedEventHandler handler = null;
handler = (s, e) =>
{
menu.Closed -= handler;
//If the callback has been disposed then it's already been executed
//so don't call Cancel
if (!callback.IsDisposed)
{
callback.Cancel();
}
};
menu.Closed += handler;
foreach (var item in menuItems)
{
if (item.Item2 == CefMenuCommand.NotFound && string.IsNullOrWhiteSpace(item.Item1))
{
menu.Items.Add(new Separator());
continue;
}
menu.Items.Add(new MenuItem
{
Header = item.Item1.Replace("&", "_"),
IsEnabled = item.Item3,
Command = new RelayCommand(() =>
{
//BUG: CEF currently not executing callbacks correctly so we manually map the commands below
//see https://github.com/cefsharp/CefSharp/issues/1767
//The following line worked in previous versions, it doesn't now, so custom EXAMPLE below
//callback.Continue(item.Item2, CefEventFlags.None);
//NOTE: Note all menu item options below have been tested, you can work out the rest
switch (item.Item2)
{
case CefMenuCommand.Back:
{
browser.GoBack();
break;
}
case CefMenuCommand.Forward:
{
browser.GoForward();
break;
}
case CefMenuCommand.Cut:
{
browser.FocusedFrame.Cut();
break;
}
case CefMenuCommand.Copy:
{
browser.FocusedFrame.Copy();
break;
}
case CefMenuCommand.Paste:
{
browser.FocusedFrame.Paste();
break;
}
case CefMenuCommand.Print:
{
browser.GetHost().Print();
break;
}
case CefMenuCommand.ViewSource:
{
browser.FocusedFrame.ViewSource();
break;
}
case CefMenuCommand.Undo:
{
browser.FocusedFrame.Undo();
break;
}
case CefMenuCommand.StopLoad:
{
browser.StopLoad();
break;
}
case CefMenuCommand.SelectAll:
{
browser.FocusedFrame.SelectAll();
break;
}
case CefMenuCommand.Redo:
{
browser.FocusedFrame.Redo();
break;
}
case CefMenuCommand.Find:
{
browser.GetHost().Find(0, parameters.SelectionText, true, false, false);
break;
}
case CefMenuCommand.AddToDictionary:
{
browser.GetHost().AddWordToDictionary(parameters.MisspelledWord);
break;
}
case CefMenuCommand.Reload:
{
browser.Reload();
break;
}
case CefMenuCommand.ReloadNoCache:
{
browser.Reload(ignoreCache: true);
break;
}
case (CefMenuCommand)26501:
{
browser.GetHost().ShowDevTools();
break;
}
case (CefMenuCommand)26502:
{
browser.GetHost().CloseDevTools();
break;
}
}
}, keepTargetAlive: true)
});
}
webBrowser.ContextMenu = menu;
});
return true;
}
private static IEnumerable<Tuple<string, CefMenuCommand, bool>> GetMenuItems(IMenuModel model)
{
for (var i = 0; i < model.Count; i++)
{
var header = model.GetLabelAt(i);
var commandId = model.GetCommandIdAt(i);
var isEnabled = model.IsEnabledAt(i);
yield return new Tuple<string, CefMenuCommand, bool>(header, commandId, isEnabled);
}
}
}
}