Skip to content

Commit 0709d61

Browse files
add Auto Update demo sample
1 parent e90ef9e commit 0709d61

File tree

6 files changed

+107
-3
lines changed

6 files changed

+107
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Linq;
2+
using ElectronNET.API;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace ElectronNET.WebApp.Controllers
6+
{
7+
public class UpdateController : Controller
8+
{
9+
public IActionResult Index()
10+
{
11+
if (HybridSupport.IsElectronActive)
12+
{
13+
Electron.IpcMain.On("auto-update", async (args) =>
14+
{
15+
// Electron.NET CLI Command for deploy:
16+
// electronize build /target win /electron-params --publish=always
17+
18+
var currentVersion = await Electron.App.GetVersionAsync();
19+
var updateCheckResult = await Electron.AutoUpdater.CheckForUpdatesAndNotifyAsync();
20+
var availableVersion = updateCheckResult.UpdateInfo.Version;
21+
string information = $"Current version: {currentVersion} - available version: {availableVersion}";
22+
23+
var mainWindow = Electron.WindowManager.BrowserWindows.First();
24+
Electron.IpcMain.Send(mainWindow, "auto-update-reply", information);
25+
});
26+
}
27+
28+
return View();
29+
}
30+
}
31+
}

ElectronNET.WebApp/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async void ElectronBootstrap()
5656
var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
5757
{
5858
Width = 1152,
59-
Height = 864,
59+
Height = 940,
6060
Show = false
6161
});
6262

ElectronNET.WebApp/Views/Home/Index.cshtml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<link rel="import" href="clipboard">
2828
<link rel="import" href="pdf">
2929
<link rel="import" href="desktopcapturer">
30+
<link rel="import" href="update">
3031
</head>
3132
<body>
3233

@@ -83,6 +84,15 @@
8384
<!-- <button type="button" id="button-protocol" data-section="protocol" class="nav-button">Launch app from <em>protocol handler</em></button> -->
8485
</div>
8586

87+
<div class="nav-item u-category-update">
88+
<h5 class="nav-category">
89+
<svg class="nav-icon"><use xlink:href="assets/img/icons.svg#icon-update"></use></svg>
90+
Update
91+
</h5>
92+
<button type="button" id="button-update" data-section="update" class="nav-button">Enable apps to <em>update</em> themselves
93+
</button>
94+
</div>
95+
8696
<div class="nav-item u-category-media">
8797
<h5 class="nav-category">
8898
<svg class="nav-icon"><use xlink:href="assets/img/icons.svg#icon-media"></use></svg>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<template class="task-template">
2+
<section id="update-section" class="section js-section u-category-update">
3+
<header class="section-header">
4+
<div class="section-wrapper">
5+
<h1>
6+
<svg class="section-icon"><use xlink:href="assets/img/icons.svg#icon-update"></use></svg>
7+
Update
8+
</h1>
9+
<h3>The <code>Electron.AutoUpdater</code> allows you to automatically update your application.</h3>
10+
<p>To publish your updates you just need simple file hosting, it does not require a dedicated server.</p>
11+
<p>You find the sample source code in <code>Controllers\UpdateController.cs</code>.</p>
12+
</div>
13+
</header>
14+
15+
<div class="demo">
16+
<div class="demo-wrapper">
17+
<button id="tray-demo-toggle" class="js-container-target demo-toggle-button">
18+
Auto Update this App
19+
<div class="demo-meta u-avoid-clicks">Supports: Win, macOS, Linux | Process: Main</div>
20+
</button>
21+
<div class="demo-box">
22+
<div class="demo-controls">
23+
<button class="demo-button" id="btn-update">View Demo</button>
24+
<span class="demo-response" id="demo-reply"></span>
25+
</div>
26+
<p>The demo button call the <code>Electron.AutoUpdater.CheckForUpdatesAndNotifyAsync()</code> in the main process.</p>
27+
28+
<p>This will immediately download an update, then install in the background when the app quits.</p>
29+
<h5>Main Process (C#)</h5>
30+
<pre><code class="csharp">Electron.IpcMain.On("auto-update", async (args) =>
31+
{
32+
var currentVersion = await Electron.App.GetVersionAsync();
33+
var updateCheckResult = await Electron.AutoUpdater.CheckForUpdatesAndNotifyAsync();
34+
var availableVersion = updateCheckResult.UpdateInfo.Version;
35+
string information = $"Current version: {currentVersion} - available version: {availableVersion}";
36+
37+
var mainWindow = Electron.WindowManager.BrowserWindows.First();
38+
Electron.IpcMain.Send(mainWindow, "auto-update-reply", information);
39+
});
40+
</code></pre>
41+
</div>
42+
</div>
43+
</div>
44+
45+
<script>
46+
(function(){
47+
const { ipcRenderer } = require("electron");
48+
49+
document.getElementById("btn-update").addEventListener("click", () => {
50+
ipcRenderer.send("auto-update");
51+
});
52+
53+
ipcRenderer.on('auto-update-reply', (event, message) => {
54+
document.getElementById('demo-reply').innerHTML = message;
55+
});
56+
}());
57+
</script>
58+
59+
</section>
60+
</template>

ElectronNET.WebApp/wwwroot/assets/css/variables.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
.u-category-native-ui { --color-accent: hsl(222, 53%, 50%); }
2222
.u-category-communication { --color-accent: hsl(285, 47%, 46%); }
2323
.u-category-system { --color-accent: hsl(330, 65%, 48%); }
24+
.u-category-update { --color-accent: hsl(0, 92%, 43%); }
2425
.u-category-media { --color-accent: hsl( 36, 77%, 34%); }

ElectronNET.WebApp/wwwroot/assets/img/icons.svg

Lines changed: 4 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)