SystemTrayMenu/Config/Config.cs

165 lines
5.2 KiB
C#
Raw Normal View History

// <copyright file="Config.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu
{
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
using SystemTrayMenu.UserInterface.FolderBrowseDialog;
using SystemTrayMenu.Utilities;
public static class Config
{
public const string Language = "en";
private static bool readDarkModeDone = false;
private static bool isDarkModeFromFirstCall = false;
public static string Path => Properties.Settings.Default.PathDirectory;
public static void UpgradeIfNotUpgraded()
{
if (!Properties.Settings.Default.IsUpgraded)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.IsUpgraded = true;
Properties.Settings.Default.Save();
FileVersionInfo versionInfo = FileVersionInfo.
GetVersionInfo(Assembly.GetEntryAssembly().Location);
string upgradedFromPath = $"%localappdata%\\{versionInfo.CompanyName}\\";
try
{
upgradedFromPath = System.IO.Path.GetFullPath(upgradedFromPath);
}
catch (Exception ex)
{
if (ex is ArgumentException ||
ex is System.Security.SecurityException ||
ex is NotSupportedException ||
ex is PathTooLongException)
{
Log.Warn($"Resolve path {upgradedFromPath} failed", ex);
}
else
{
throw;
}
}
Log.Info($"Settings upgraded from {upgradedFromPath}");
}
}
public static bool LoadOrSetByUser()
{
bool pathOK = Directory.Exists(Path);
if (!pathOK)
{
string textFirstStart = Translator.GetText("TextFirstStart");
MessageBox.Show(
textFirstStart,
Translator.GetText("SystemTrayMenu"),
MessageBoxButtons.OK);
ShowHelpFAQ();
pathOK = SetFolderByUser();
}
return pathOK;
}
2020-05-05 05:43:47 +12:00
public static bool SetFolderByUser(bool save = true)
{
bool pathOK = false;
bool userAborted = false;
2020-06-06 02:42:12 +12:00
using (FolderDialog dialog = new FolderDialog())
{
2020-06-06 02:42:12 +12:00
dialog.InitialFolder = Path;
do
{
2020-06-06 02:42:12 +12:00
if (dialog.ShowDialog() == DialogResult.OK)
{
2020-06-06 02:42:12 +12:00
if (Directory.Exists(dialog.Folder))
{
pathOK = true;
Properties.Settings.Default.PathDirectory =
2020-06-06 02:42:12 +12:00
dialog.Folder;
2020-05-05 05:43:47 +12:00
if (save)
{
Properties.Settings.Default.Save();
}
}
}
else
{
userAborted = true;
}
}
while (!pathOK && !userAborted);
}
return pathOK;
}
internal static void ShowHelpFAQ()
{
2020-06-06 02:42:12 +12:00
string browserPath = FileUrl.GetDefaultBrowserPath();
2020-06-06 23:46:12 +12:00
if (!string.IsNullOrEmpty(browserPath))
2020-06-06 02:42:12 +12:00
{
Process.Start(browserPath, "https://github.com/Hofknecht/SystemTrayMenu#FAQ");
}
}
internal static bool IsDarkMode()
{
bool isDarkMode = false;
if (readDarkModeDone)
{
isDarkMode = isDarkModeFromFirstCall;
}
else
{
if (Properties.Settings.Default.IsDarkModeAlwaysOn || IsDarkModeActive())
{
isDarkModeFromFirstCall = true;
isDarkMode = true;
}
readDarkModeDone = true;
}
return isDarkMode;
}
/// <summary>
/// Read the OS setting whether dark mode is enabled.
/// </summary>
/// <returns>true = Dark mode; false = Light mode.</returns>
private static bool IsDarkModeActive()
{
// Check: AppsUseLightTheme (REG_DWORD)
bool isDarkModeActive = false;
object registryValueAppsUseLightTheme = Registry.GetValue(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
"AppsUseLightTheme",
1);
// 0 = Dark mode, 1 = Light mode
if (registryValueAppsUseLightTheme != null &&
registryValueAppsUseLightTheme.ToString() == "0")
{
isDarkModeActive = true;
}
return isDarkModeActive;
}
}
}