This commit is contained in:
Louis Lam 2022-10-07 18:38:14 +08:00
parent da778f05ac
commit 4c45654780
2 changed files with 55 additions and 19 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using UptimeKuma.Properties; using UptimeKuma.Properties;
@ -14,7 +15,7 @@ namespace UptimeKuma {
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
[STAThread] [STAThread]
static void Main() { static void Main(string[] args) {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new UptimeKumaApplicationContext()); Application.Run(new UptimeKumaApplicationContext());
@ -28,37 +29,44 @@ namespace UptimeKuma {
public UptimeKumaApplicationContext() public UptimeKumaApplicationContext()
{ {
// Initialize Tray Icon
trayIcon = new NotifyIcon(); trayIcon = new NotifyIcon();
trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
trayIcon.ContextMenu = new ContextMenu(new MenuItem[] { trayIcon.ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Open", Open), new("Open", Open),
new MenuItem("Check for Update", CheckForUpdate), //new("Debug Console", DebugConsole),
new MenuItem("About", About), new("Check for Update...", CheckForUpdate),
new MenuItem("Exit", Exit), new("Visit GitHub...", VisitGitHub),
new("About", About),
new("Exit", Exit),
}); });
trayIcon.MouseDoubleClick += new MouseEventHandler(Open);
trayIcon.Visible = true; trayIcon.Visible = true;
var startInfo = new ProcessStartInfo(); var startInfo = new ProcessStartInfo {
startInfo.FileName = "node/node.exe"; FileName = "node/node.exe",
startInfo.Arguments = "server/server.js"; Arguments = "server/server.js --data-dir=\"../data/\"",
startInfo.RedirectStandardOutput = true; RedirectStandardOutput = false,
startInfo.RedirectStandardError = true; RedirectStandardError = false,
startInfo.UseShellExecute = false; UseShellExecute = false,
startInfo.CreateNoWindow = true; CreateNoWindow = true,
startInfo.WorkingDirectory = "core"; WorkingDirectory = "core"
};
process = new Process(); process = new Process();
process.StartInfo = startInfo; process.StartInfo = startInfo;
process.EnableRaisingEvents = true; process.EnableRaisingEvents = true;
process.Exited += new EventHandler(ProcessExited);
try { try {
process.Start(); process.Start();
Open(null, null); //Open(null, null);
} catch (Exception e) { } catch (Exception e) {
MessageBox.Show("Startup failed: " + e.Message, "Uptime Kuma Error"); MessageBox.Show("Startup failed: " + e.Message, "Uptime Kuma Error");
throw;
} }
} }
@ -66,10 +74,19 @@ namespace UptimeKuma {
Process.Start("http://localhost:3001"); Process.Start("http://localhost:3001");
} }
void CheckForUpdate(object sender, EventArgs e) { void DebugConsole(object sender, EventArgs e) {
} }
void CheckForUpdate(object sender, EventArgs e) {
Process.Start("https://github.com/louislam/uptime-kuma/releases");
}
void VisitGitHub(object sender, EventArgs e)
{
Process.Start("https://github.com/louislam/uptime-kuma");
}
void About(object sender, EventArgs e) void About(object sender, EventArgs e)
{ {
MessageBox.Show("Uptime Kuma v1.0.0" + Environment.NewLine + "© 2022 Louis Lam", "Info"); MessageBox.Show("Uptime Kuma v1.0.0" + Environment.NewLine + "© 2022 Louis Lam", "Info");
@ -79,9 +96,25 @@ namespace UptimeKuma {
{ {
// Hide tray icon, otherwise it will remain shown until user mouses over it // Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false; trayIcon.Visible = false;
process.Close(); process.Kill();
}
void ProcessExited(object sender, EventArgs e) {
if (process.ExitCode != 0) {
var line = "";
while (!process.StandardOutput.EndOfStream)
{
line += process.StandardOutput.ReadLine();
}
MessageBox.Show("Uptime Kuma exited unexpectedly. Exit code: " + process.ExitCode + " " + line);
}
trayIcon.Visible = false;
Application.Exit(); Application.Exit();
} }
} }
} }

View File

@ -13,7 +13,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<ApplicationIcon>..\..\public\favicon.ico</ApplicationIcon> <ApplicationIcon>..\..\public\favicon.ico</ApplicationIcon>
<LangVersion>10</LangVersion> <LangVersion>9</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
@ -34,6 +34,9 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<PostBuildEvent>COPY "$(SolutionDir)bin\Debug\uptime-kuma.exe" "C:\Users\LouisLam\Desktop\uptime-kuma-win64\"</PostBuildEvent>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />