Electron.Net default menu shows before click, then custom menu loads

165 views Asked by At

I'm using Electron.Net to wrap an ASP.Net MVC webpage, and I'm currently trying to replace the default menu. I've got a custom menu working, and the code is replacing the menu. But when the program is run using electronize start, the default menu shows until I click on the leftmost default item (file). Once I click on the item, my menu loads.

I think the issue is related to the MenuController only running when the Menu is interacted with, so I think I need to implement some kind of async capabilities. I do not know how to do this, I've looked online but not found much.

MenuController.cs

public IActionResult Index()
        {
            if (HybridSupport.IsElectronActive)
            {
                MenuItem[] menu = new MenuItem[]
                {
                    new MenuItem
                    {
                        Label = "File",
                        Submenu = new MenuItem[]
                        {
                            new MenuItem
                            {
                                Label = "Open",
                                Accelerator = "CmdOrCtrl+O"
                            },
                            new MenuItem
                            {
                                Label = "Save",
                                Accelerator = "CmdOrCtrl+S"
                            },
                            new MenuItem
                            {
                                Label = "Save As",
                                Accelerator = "CmdorCtrl+Shift+S"
                            }
                        }
                    },

                    new MenuItem
                    {
                        Label = "Edit",
                        Submenu = new MenuItem[]
                        {
                            new MenuItem
                            {
                                Label = "Preferences"
                            }
                        }
                    },

                    new MenuItem
                    {
                        Label = "View",
                        Submenu = new MenuItem[]
                        {
                            new MenuItem
                            {
                                Label = "TODO"
                            },

                            new MenuItem
                            {
                                Label = "Reload",
                                Click = () =>
                                {
                                    Electron.WindowManager.BrowserWindows.ToList().ForEach(BrowserWindow =>
                                    {
                                        if (BrowserWindow.Id != 1)
                                            BrowserWindow.Close();

                                        else
                                            BrowserWindow.Reload();
                                    });
                                }
                            },

                            new MenuItem
                            {
                                Label = "Toggle Full Screen",
                                Accelerator = "CmdOrCtrl+F",
                                Click = async () =>
                                {
                                    bool isFullScreen = await Electron.WindowManager.BrowserWindows.First().IsFullScreenAsync();
                                    Electron.WindowManager.BrowserWindows.First().SetFullScreen(!isFullScreen);
                                }
                            },

                            new MenuItem
                            {
                                Type = MenuType.separator
                            },

                            new MenuItem
                            {
                                Label = "Open Developer Tools",
                                Accelerator = "CmdOrCtrl+Shift+I",
                                Click = () =>
                                {
                                    Electron.WindowManager.BrowserWindows.First().WebContents.OpenDevTools();
                                }
                            }
                        }
                    },

                    new MenuItem
                    {
                        Label = "Navigate",
                        Submenu = new MenuItem[]
                        {
                            new MenuItem
                            {
                                Label = "Home",
                                Submenu = new MenuItem[]
                                {
                                    new MenuItem
                                    {
                                        Label = "Home",
                                        Click = () =>
                                        {
                                            Electron.WindowManager.BrowserWindows.First().LoadURL($"http://localhost:{BridgeSettings.WebPort}/");
                                        }
                                    },

                                    new MenuItem
                                    {
                                        Label = "Privacy",
                                        Click = () =>
                                        {
                                            Electron.WindowManager.BrowserWindows.First().LoadURL($"http://localhost:{BridgeSettings.WebPort}/Privacy");
                                        }
                                    }
                                }
                            },

                            new MenuItem
                            {
                                Type = MenuType.separator
                            },

                            new MenuItem
                            {
                                Label = "Editor",
                                Submenu = new MenuItem[]
                                {
                                    new MenuItem
                                    {
                                        Label = "Editor",
                                        Click = () =>
                                        {
                                            Electron.WindowManager.BrowserWindows.First().LoadURL($"http://localhost:{BridgeSettings.WebPort}/Editor/");
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                Electron.Menu.SetApplicationMenu(menu);
            }

            return Ok();
        }

Views/Editor/Index.cshtml

@model CircuitSharp.Models.EditorModel

@{
    Layout = null;
}
<!--
    EDITOR INDEX PAGE
-->
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Editor</title>

    <!-- 
        IMPORTING THE CUSTOM MENU
    -->
    <link rel="import" href="Menu" />
</head>
<body>



@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
</body>
</html>

1

There are 1 answers

0
Eyrim On

Update: I just re-ran the program the next day and it works now. ¯_(ツ)_/¯