private void Build(CefMenuModel model, MenuItemCollection menu) { CefColor color = default; int count = model.Count; for (int i = 0; i < count; i++) { MenuItem menuItem; switch (model.GetTypeAt(i)) { case CefMenuItemType.Separator: menu.Add(new MenuSeparatorItem()); continue; case CefMenuItemType.Check: menuItem = new MenuItem(); //menuItem.CheckOnClick = true; //menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Radio: menuItem = new MenuItem(); //menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Command: menuItem = new MenuItem(); break; case CefMenuItemType.Submenu: menuItem = new MenuItem(); if (model.IsEnabledAt(i)) { //menuItem.DropDownItemClicked += Menu_ItemClicked; Build(model.GetSubMenuAt(i), menuItem.Items); } break; default: continue; } menuItem.Text = model.GetLabelAt(i).Replace("&", ""); menuItem.Click += Menu_ItemClicked; menuItem.Enabled = model.IsEnabledAt(i); //menuItem.Tag = model.GetCommandIdAt(i); MenuItemsTags.Add(menuItem, model.GetCommandIdAt(i)); //menuItem.ForeColor = model.GetColorAt(i, CefMenuColorType.Text, ref color) ? Color.FromArgb(color.ToArgb()) : SystemColors.ControlText; menu.Add(menuItem); } }
protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams menuParams, CefMenuModel model) { model.InsertItemAt(model.Count > 0 ? 1 : 0, (int)CefMenuId.ReloadNocache, "Refresh"); model.AddSeparator(); model.AddItem(SHOW_DEV_TOOLS, "&Show DevTools"); model.AddItem(INSPECT_ELEMENT, "Inspect element"); CefMenuModel submenu = model.AddSubMenu(0, "Submenu Test"); submenu.AddItem((int)CefMenuId.Copy, "Copy"); submenu.AddItem((int)CefMenuId.Paste, "Paste"); submenu.SetColorAt(submenu.Count - 1, CefMenuColorType.Text, CefColor.FromArgb(Colors.Blue.ToArgb())); submenu.AddCheckItem(0, "Checked Test"); submenu.SetCheckedAt(submenu.Count - 1, true); submenu.AddRadioItem(0, "Radio Off", 0); submenu.AddRadioItem(0, "Radio On", 1); submenu.SetCheckedAt(submenu.Count - 1, true); }
private static void CopyItems(CefMenuModel model, MenuModelItem[] dest) { for (int i = 0; i < dest.Length; i++) { var item = new MenuModelItem { Command = model.GetCommandIdAt(i), Label = model.GetLabelAt(i), Type = model.GetTypeAt(i), Group = model.GetGroupIdAt(i), Visible = model.IsVisibleAt(i), Enabled = model.IsEnabledAt(i), Checked = model.IsCheckedAt(i), }; CefColor color = default; item.Colors = new CefColor[(int)CefMenuColorType.Count]; for (int j = 0; j < item.Colors.Length; j++) { item.Colors[j] = model.GetColorAt(i, (CefMenuColorType)j, ref color) ? color : default; } int keycode = 0, alt = 0, ctrl = 0, shift = 0; if (model.GetAcceleratorAt(i, ref keycode, ref shift, ref ctrl, ref alt)) { item.Accelerator = new Accelerator { KeyCode = keycode, Alt = alt != 0, Ctrl = ctrl != 0, Shift = shift != 0 }; } if (item.Type == CefMenuItemType.Submenu) { CefMenuModel submenu = model.GetSubMenuAt(i); var items = new MenuModelItem[submenu.Count]; CopyItems(submenu, items); item.SubMenu = new MenuModel(items) { IsSubMenu = true }; } dest[i] = item; } }
public static Color ToColor(this CefColor color) { return(Color.FromArgb(color.A, color.R, color.G, color.B)); }
/// <summary> /// Starts CEF /// </summary> /// <exception cref="Exception"></exception> public void Init() { // ReSharper disable once RedundantAssignment string[] argv = args; #if LINUX //On Linux we need to do this, otherwise it will just crash, no idea why tho argv = new string[args.Length + 1]; Array.Copy(args, 0, argv, 1, args.Length); argv[0] = "-"; #endif //Set up CEF args and the CEF app CefMainArgs cefMainArgs = new CefMainArgs(argv); BrowserProcessCEFApp cefApp = new BrowserProcessCEFApp(launchArguments); //Run our sub-processes int exitCode = CefRuntime.ExecuteProcess(cefMainArgs, cefApp, IntPtr.Zero); if (exitCode != -1) { Environment.Exit(exitCode); return; } //Backup if (argv.Any(arg => arg.StartsWith("--type="))) { Environment.Exit(-2); return; } //Do we have a cache or not, if not CEF will run in "incognito" mode. string cachePathArgument = null; if (launchArguments.CachePath != null) { cachePathArgument = launchArguments.CachePath.FullName; } //Convert UnityWebBrowser log severity to CefLogSeverity CefLogSeverity logSeverity = launchArguments.LogSeverity switch { LogSeverity.Debug => CefLogSeverity.Debug, LogSeverity.Info => CefLogSeverity.Info, LogSeverity.Warn => CefLogSeverity.Warning, LogSeverity.Error => CefLogSeverity.Error, LogSeverity.Fatal => CefLogSeverity.Fatal, _ => CefLogSeverity.Default }; //Setup the CEF settings CefSettings cefSettings = new CefSettings { WindowlessRenderingEnabled = true, NoSandbox = true, LogFile = launchArguments.LogPath.FullName, CachePath = cachePathArgument, //TODO: On MacOS multi-threaded message loop isn't supported MultiThreadedMessageLoop = true, LogSeverity = logSeverity, Locale = "en-US", ExternalMessagePump = false, RemoteDebuggingPort = launchArguments.RemoteDebugging, #if LINUX //On Linux we need to tell CEF where everything is, this will assume that the working directory is where everything is! ResourcesDirPath = System.IO.Path.Combine(Environment.CurrentDirectory), LocalesDirPath = System.IO.Path.Combine(Environment.CurrentDirectory, "locales"), BrowserSubprocessPath = Environment.GetCommandLineArgs()[0] #endif }; //Init CEF CefRuntime.Initialize(cefMainArgs, cefSettings, cefApp, IntPtr.Zero); //Create a CEF window and set it to windowless CefWindowInfo cefWindowInfo = CefWindowInfo.Create(); cefWindowInfo.SetAsWindowless(IntPtr.Zero, false); //Create our CEF browser settings CefColor backgroundColor = new CefColor(launchArguments.Bca, launchArguments.Bcr, launchArguments.Bcg, launchArguments.Bcb); CefBrowserSettings cefBrowserSettings = new CefBrowserSettings { BackgroundColor = backgroundColor, JavaScript = launchArguments.JavaScript ? CefState.Enabled : CefState.Disabled, LocalStorage = CefState.Disabled }; Logger.Debug($"CEF starting with these options:" + $"\nJS: {launchArguments.JavaScript}" + $"\nBackgroundColor: {backgroundColor}" + $"\nCache Path: {cachePathArgument}" + $"\nLog Path: {launchArguments.LogPath.FullName}" + $"\nLog Severity: {launchArguments.LogSeverity}"); Logger.Info("Starting CEF client..."); //Create cef browser cefClient = new BrowserProcessCEFClient(new CefSize(launchArguments.Width, launchArguments.Height), new ProxySettings(launchArguments.ProxyUsername, launchArguments.ProxyPassword, launchArguments.ProxyEnabled)); CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, launchArguments.InitialUrl); cefClient.OnUrlChange += OnUrlChange; }
/// <summary> /// Returns in |color| the color that was explicitly set for |command_id| and /// |color_type|. Specify an |index| value of -1 to return the default color in /// |color|. If a color was not set then 0 will be returned in |color|. Returns /// true on success. /// </summary> public bool GetColorAt(int index, CefMenuColorType colorType, out CefColor color) { color = _items[index].Colors[(int)colorType]; return(color != 0); }