Skip to content

Commit ae12eb4

Browse files
committed
ElectronNET.WebApp: Adjust startup to use new onAppReady callback
1 parent 2a26b46 commit ae12eb4

File tree

2 files changed

+72
-77
lines changed

2 files changed

+72
-77
lines changed

src/ElectronNET.WebApp/Program.cs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace ElectronNET.WebApp
77
{
8+
using System.Threading.Tasks;
9+
using ElectronNET.API.Entities;
10+
811
public class Program
912
{
1013
public static void Main(string[] args)
@@ -16,8 +19,75 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
1619
{
1720
return WebHost.CreateDefaultBuilder(args)
1821
.ConfigureLogging((hostingContext, logging) => { logging.AddConsole(); })
19-
.UseElectron(args)
22+
.UseElectron(args, ElectronBootstrap)
2023
.UseStartup<Startup>();
2124
}
25+
26+
public static async Task ElectronBootstrap()
27+
{
28+
//AddDevelopmentTests();
29+
30+
var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
31+
{
32+
Width = 1152,
33+
Height = 940,
34+
Show = false
35+
});
36+
37+
await browserWindow.WebContents.Session.ClearCacheAsync();
38+
39+
browserWindow.OnReadyToShow += () => browserWindow.Show();
40+
}
41+
42+
private static void AddDevelopmentTests()
43+
{
44+
// NOTE: on mac you will need to allow the app to post notifications when asked.
45+
46+
_ = Electron.App.On("activate", (obj) =>
47+
{
48+
// obj should be a boolean that represents where there are active windows or not.
49+
var hasWindows = (bool)obj;
50+
51+
Electron.Notification.Show(
52+
new NotificationOptions("Activate", $"activate event has been captured. Active windows = {hasWindows}")
53+
{
54+
Silent = false,
55+
});
56+
});
57+
58+
Electron.Dock.SetMenu(new[]
59+
{
60+
new MenuItem
61+
{
62+
Type = MenuType.normal,
63+
Label = "MenuItem",
64+
Click = () =>
65+
{
66+
Electron.Notification.Show(new NotificationOptions(
67+
"Dock MenuItem Click",
68+
"A menu item added to the Dock was selected;"));
69+
},
70+
},
71+
new MenuItem
72+
{
73+
Type = MenuType.submenu,
74+
Label = "SubMenu",
75+
Submenu = new[]
76+
{
77+
new MenuItem
78+
{
79+
Type = MenuType.normal,
80+
Label = "Sub MenuItem",
81+
Click = () =>
82+
{
83+
Electron.Notification.Show(new NotificationOptions(
84+
"Dock Sub MenuItem Click",
85+
"A menu item added to the Dock was selected;"));
86+
},
87+
},
88+
}
89+
}
90+
});
91+
}
2292
}
2393
}

src/ElectronNET.WebApp/Startup.cs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using ElectronNET.API;
2-
using ElectronNET.API.Entities;
3-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
42
using Microsoft.AspNetCore.Hosting;
53
using Microsoft.Extensions.Configuration;
64
using Microsoft.Extensions.DependencyInjection;
@@ -40,79 +38,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4038
{
4139
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
4240
});
43-
44-
if (HybridSupport.IsElectronActive)
45-
{
46-
ElectronBootstrap();
47-
}
48-
}
49-
50-
public async void ElectronBootstrap()
51-
{
52-
//AddDevelopmentTests();
53-
54-
var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
55-
{
56-
Width = 1152,
57-
Height = 940,
58-
Show = false
59-
});
60-
61-
await browserWindow.WebContents.Session.ClearCacheAsync();
62-
63-
browserWindow.OnReadyToShow += () => browserWindow.Show();
64-
browserWindow.SetTitle(Configuration["DemoTitleInSettings"]);
65-
}
66-
67-
private static void AddDevelopmentTests()
68-
{
69-
// NOTE: on mac you will need to allow the app to post notifications when asked.
70-
71-
Electron.App.On("activate", (obj) =>
72-
{
73-
// obj should be a boolean that represents where there are active windows or not.
74-
var hasWindows = (bool) obj;
75-
76-
Electron.Notification.Show(
77-
new NotificationOptions("Activate", $"activate event has been captured. Active windows = {hasWindows}")
78-
{
79-
Silent = false,
80-
});
81-
});
82-
83-
Electron.Dock.SetMenu(new[]
84-
{
85-
new MenuItem
86-
{
87-
Type = MenuType.normal,
88-
Label = "MenuItem",
89-
Click = () =>
90-
{
91-
Electron.Notification.Show(new NotificationOptions(
92-
"Dock MenuItem Click",
93-
"A menu item added to the Dock was selected;"));
94-
},
95-
},
96-
new MenuItem
97-
{
98-
Type = MenuType.submenu,
99-
Label = "SubMenu",
100-
Submenu = new[]
101-
{
102-
new MenuItem
103-
{
104-
Type = MenuType.normal,
105-
Label = "Sub MenuItem",
106-
Click = () =>
107-
{
108-
Electron.Notification.Show(new NotificationOptions(
109-
"Dock Sub MenuItem Click",
110-
"A menu item added to the Dock was selected;"));
111-
},
112-
},
113-
}
114-
}
115-
});
11641
}
11742
}
11843
}

0 commit comments

Comments
 (0)