Esempio n. 1
0
        bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

            //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);

            chromiumWebBrowser.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)
                {
                    menu.Items.Add(new MenuItem
                    {
                        Header  = item.Item1,
                        Command = new RelayCommand(() => { callback.Continue(item.Item2, CefEventFlags.None); })
                    });
                }
                chromiumWebBrowser.ContextMenu = menu;
            });

            return(true);
        }
        public override bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            var menuItems = GetMenuItems(model).ToList();

            var linkUrl       = parameters.LinkUrl;
            var selectionText = parameters.SelectionText;

            wfChromiumWebBrowser.Dispatcher.Invoke(() =>
            {
                var menu = new ContextMenu {
                    IsOpen = true
                };

                RoutedEventHandler handler = null;
                handler = (s, e) =>
                {
                    menu.Closed -= handler;
                    if (!callback.IsDisposed)
                    {
                        callback.Cancel();
                    }
                };
                menu.Closed += handler;

                foreach (var item in menuItems)
                {
                    string header = string.Empty;

                    if (item.commandId == CefMenuCommand.NotFound)
                    {
                        menu.Items.Add(new Separator());
                        continue;
                    }

                    switch (item.commandId)
                    {
                    case CefMenuCommand.Back:
                        {
                            header = LanguageManager.GetString("menu_back");
                            break;
                        }

                    case CefMenuCommand.Forward:
                        {
                            header = LanguageManager.GetString("menu_forward");
                            break;
                        }

                    case CefMenuCommand.Cut:
                        {
                            header = LanguageManager.GetString("menu_cut");
                            break;
                        }

                    case CefMenuCommand.Copy:
                        {
                            header = LanguageManager.GetString("menu_copy");
                            break;
                        }

                    case CefMenuCommand.Paste:
                        {
                            header = LanguageManager.GetString("menu_paste");
                            break;
                        }

                    case CefMenuCommand.Print:
                        {
                            header = LanguageManager.GetString("menu_print");
                            break;
                        }

                    case CefMenuCommand.ViewSource:
                        {
                            header = LanguageManager.GetString("menu_viewSource");
                            break;
                        }

                    case CefMenuCommand.Undo:
                        {
                            header = LanguageManager.GetString("menu_undo");
                            break;
                        }

                    case CefMenuCommand.StopLoad:
                        {
                            header = LanguageManager.GetString("menu_stop");
                            break;
                        }

                    case CefMenuCommand.SelectAll:
                        {
                            header = LanguageManager.GetString("menu_selectAll");
                            break;
                        }

                    case CefMenuCommand.Redo:
                        {
                            header = LanguageManager.GetString("menu_redo");
                            break;
                        }

                    case CefMenuCommand.Find:
                        {
                            header = LanguageManager.GetString("menu_find");
                            break;
                        }

                    case CefMenuCommand.AddToDictionary:
                        {
                            header = LanguageManager.GetString("menu_addToDictionary");
                            break;
                        }

                    case CefMenuCommand.Reload:
                        {
                            header = LanguageManager.GetString("menu_reload");
                            break;
                        }

                    case CefMenuCommand.ReloadNoCache:
                        {
                            header = LanguageManager.GetString("menu_reloadNoCache");
                            break;
                        }

                    case OpenInNewWindow:
                        {
                            header = string.Format(LanguageManager.GetString(item.header), linkUrl);
                            break;
                        }

                    case Search:
                        {
                            var tmp = selectionText.Length > 32 ? selectionText.Substring(0, 32) + "..." : selectionText;
                            header  = string.Format(LanguageManager.GetString(item.header), tmp.Replace('\n', ' '));
                            break;
                        }

                    default:
                        {
                            header = item.header;
                            break;
                        }
                    }

                    menu.Items.Add(new MenuItem
                    {
                        Header  = header,
                        Command = new DelegateCommand
                        {
                            CanExecute = item.isEnable,
                            Execute    = obj => callback.Continue(item.commandId, CefEventFlags.None)
                        }
                    });
                }

                wfChromiumWebBrowser.ContextMenu = menu;
            });

            return(true);
        }
Esempio n. 3
0
        bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

            //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);

            chromiumWebBrowser.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)
                {
                    menu.Items.Add(new MenuItem
                    {
                        Header = item.Item1,
                        Command = new RelayCommand(() => { callback.Continue(item.Item2, CefEventFlags.None); })
                    });
                }
                chromiumWebBrowser.ContextMenu = menu;
            });

            return true;
        }