Files
FluentTerminal/FluentTerminal.SystemTray/SystemTrayApplicationContext.cs
T
2019-08-30 15:03:58 +02:00

87 lines
2.8 KiB
C#

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Windows.ApplicationModel;
namespace FluentTerminal.SystemTray
{
public class SystemTrayApplicationContext : ApplicationContext
{
private readonly NotifyIcon _notifyIcon;
public SystemTrayApplicationContext()
{
var openMenuItem = new MenuItem("Show", new EventHandler(OpenApp));
var newWindowItem = new MenuItem("New terminal", new EventHandler(NewWindow));
var settingsMenuItem = new MenuItem("Show settings", new EventHandler(ShowSettings));
var exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
openMenuItem.DefaultItem = true;
_notifyIcon = new NotifyIcon();
_notifyIcon.DoubleClick += OpenApp;
_notifyIcon.Text = "Fluent Terminal";
if (SystemUsesLightTheme())
{
_notifyIcon.Icon = Properties.Resources.Square44x44Logo_scale_100_altform_lightunplated;
}
else
{
_notifyIcon.Icon = Properties.Resources.Square44x44Logo_scale_100;
}
_notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { openMenuItem, newWindowItem, settingsMenuItem, exitMenuItem });
_notifyIcon.Visible = true;
}
private void Exit(object sender, EventArgs e)
{
Application.Exit();
}
private void NewWindow(object sender, EventArgs e)
{
Process.Start("flute.exe", "new");
}
private async void OpenApp(object sender, EventArgs e)
{
var appListEntries = await Package.Current.GetAppListEntriesAsync();
await appListEntries.First().LaunchAsync();
}
private void ShowSettings(object sender, EventArgs e)
{
Process.Start("flute.exe", "settings");
}
/// <summary>
/// Checks whether the new light system theme in Windows 10 1903+ is used
/// </summary>
private bool SystemUsesLightTheme()
{
try
{
using (var regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, string.Empty).OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"))
{
if (regKey.GetValueNames().Contains("SystemUsesLightTheme"))
{
var value = regKey.GetValue("SystemUsesLightTheme");
return value is int intValue && intValue == 1;
}
else
{
return false;
}
}
}
catch (Exception e)
{
return false;
}
}
}
}