// #docfragment "ContextMenu.Implementation" private Task <ShowContextMenuResponse> ShowContextMenu( ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; WebView.Dispatcher?.BeginInvoke(new Action(() => { System.Windows.Controls.ContextMenu popupMenu = new System.Windows.Controls.ContextMenu(); IEnumerable <string> suggestions = spellCheckMenu.DictionarySuggestions; if (suggestions != null) { // Add menu items with suggestions. foreach (string suggestion in suggestions) { MenuItem menuItem = BuildMenuItem(suggestion, true, delegate { browser.ReplaceMisspelledWord(suggestion); tcs.TrySetResult(ShowContextMenuResponse .Close()); }); popupMenu.Items.Add(menuItem); } } // Add "Add to Dictionary" menu item. string addToDictionary = spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary"; popupMenu.Items.Add(BuildMenuItem(addToDictionary, true, delegate { if (!string.IsNullOrWhiteSpace(spellCheckMenu.MisspelledWord)) { engine.Profiles.Default.SpellChecker?.CustomDictionary ?.Add(spellCheckMenu.MisspelledWord); } tcs.TrySetResult(ShowContextMenuResponse.Close()); })); popupMenu.Closed += (sender, args) => { tcs.TrySetResult(ShowContextMenuResponse.Close()); }; popupMenu.IsOpen = true; })); return(tcs.Task); }
private Task <ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; if (spellCheckMenu != null) { BeginInvoke(new Action(() => { System.Windows.Forms.ContextMenu popupMenu = new System.Windows.Forms.ContextMenu(); IEnumerable <string> suggestions = spellCheckMenu.DictionarySuggestions; if (suggestions != null) { foreach (string suggestion in suggestions) { popupMenu.MenuItems.Add(BuildMenuItem(suggestion, true, delegate { browser.ReplaceMisspelledWord(suggestion); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } } string addToDictionary = spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary"; popupMenu.MenuItems.Add(BuildMenuItem(addToDictionary, true, delegate { if (!string.IsNullOrWhiteSpace(spellCheckMenu.MisspelledWord)) { engine.SpellChecker?.CustomDictionary?.Add(spellCheckMenu.MisspelledWord); } tcs.TrySetResult(ShowContextMenuResponse.Close()); })); popupMenu.Collapse += (sender, args) => { tcs.TrySetResult(ShowContextMenuResponse.Close()); }; Point location = new Point(parameters.Location.X, parameters.Location.Y); popupMenu.Show(this, location); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } else { tcs.TrySetResult(ShowContextMenuResponse.Close()); } return(tcs.Task); }
private Task <ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; if (spellCheckMenu != null) { BeginInvoke(new Action(() => { ContextMenuStrip popupMenu = new ContextMenuStrip(); IEnumerable <string> suggestions = spellCheckMenu.DictionarySuggestions; if (suggestions != null) { // Add menu items with suggestions. foreach (string suggestion in suggestions) { popupMenu.Items.Add(BuildMenuItem(suggestion, true, delegate { browser.ReplaceMisspelledWord(suggestion); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } } // Add "Add to Dictionary" menu item. string addToDictionary = spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary"; popupMenu.Items.Add(BuildMenuItem(addToDictionary, true, delegate { if (!string.IsNullOrWhiteSpace(spellCheckMenu.MisspelledWord)) { engine.Profiles.Default.SpellChecker?.CustomDictionary ?.Add(spellCheckMenu.MisspelledWord); } tcs.TrySetResult(ShowContextMenuResponse.Close()); })); // Close context menu when the browser requests focus back. EventHandler <FocusRequestedEventArgs> onFocusRequested = null; onFocusRequested = (sender, args) => { BeginInvoke((Action)(() => popupMenu.Close())); parameters.Browser.FocusRequested -= onFocusRequested; }; parameters.Browser.FocusRequested += onFocusRequested; // Handle the menu closed event. ToolStripDropDownClosedEventHandler menuOnClosed = null; menuOnClosed = (sender, args) => { bool itemNotClicked = args.CloseReason != ToolStripDropDownCloseReason.ItemClicked; if (itemNotClicked) { tcs.TrySetResult(ShowContextMenuResponse.Close()); } popupMenu.Closed -= menuOnClosed; }; popupMenu.Closed += menuOnClosed; // Show the context menu. Point location = new Point(parameters.Location.X, parameters.Location.Y); popupMenu.Show(this, location); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } else { tcs.TrySetResult(ShowContextMenuResponse.Close()); } return(tcs.Task); }
private Task <ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; if (spellCheckMenu != null) { BeginInvoke(new Action(() => { ContextMenuStrip popupMenu = new ContextMenuStrip(); if (!string.IsNullOrEmpty(parameters.LinkText)) { ToolStripItem buildMenuItem = BuildMenuItem("Show the URL link", true, (sender, args) => { string linkURL = parameters.LinkUrl; Console.WriteLine($"linkURL = {linkURL}"); MessageBox.Show(linkURL, "URL"); tcs.TrySetResult(ShowContextMenuResponse.Close()); }); popupMenu.Items.Add(buildMenuItem); } ToolStripItem reloadMenuItem = BuildMenuItem("Reload", true, (sender, args) => { Console.WriteLine("Reload current web page"); browser.Navigation.Reload(); tcs.TrySetResult(ShowContextMenuResponse.Close()); }); popupMenu.Items.Add(reloadMenuItem); // Close context menu when the browser requests focus back. EventHandler <FocusRequestedEventArgs> onFocusRequested = null; onFocusRequested = (sender, args) => { BeginInvoke((Action)(() => popupMenu.Close())); parameters.Browser.FocusRequested -= onFocusRequested; }; parameters.Browser.FocusRequested += onFocusRequested; // Handle the menu closed event. ToolStripDropDownClosedEventHandler menuOnClosed = null; menuOnClosed = (sender, args) => { bool itemNotClicked = args.CloseReason != ToolStripDropDownCloseReason.ItemClicked; if (itemNotClicked) { tcs.TrySetResult(ShowContextMenuResponse.Close()); } popupMenu.Closed -= menuOnClosed; }; popupMenu.Closed += menuOnClosed; // Show the context menu. Point location = new Point(parameters.Location.X, parameters.Location.Y); popupMenu.Show(this, location); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } else { tcs.TrySetResult(ShowContextMenuResponse.Close()); } return(tcs.Task); }