refactor<*>: use DynamicResource instead of StaticResource for brushes and locales

This commit is contained in:
leo 2021-07-20 16:26:10 +08:00
parent 4a56b47265
commit afc4eafb6f
73 changed files with 630 additions and 601 deletions

View file

@ -1,11 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
namespace SourceGit {
@ -14,7 +8,6 @@ namespace SourceGit {
/// 程序入口.
/// </summary>
public partial class App : Application {
private static bool restart = false;
/// <summary>
/// 读取本地化字串
@ -28,15 +21,6 @@ namespace SourceGit {
return string.Format(data, args);
}
/// <summary>
/// 重启程序
/// </summary>
public static void Restart() {
restart = true;
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Current.Shutdown();
}
/// <summary>
/// 启动.
/// </summary>
@ -48,31 +32,11 @@ namespace SourceGit {
Directory.CreateDirectory(Views.Controls.Avatar.CACHE_PATH);
}
// 控制主题
if (Models.Preference.Instance.General.UseDarkTheme) {
foreach (var rs in Current.Resources.MergedDictionaries) {
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Themes/", StringComparison.Ordinal)) {
rs.Source = new Uri("pack://application:,,,/Resources/Themes/Dark.xaml", UriKind.Absolute);
break;
}
}
}
// 控制显示语言
var lang = Models.Preference.Instance.General.Locale;
if (lang != "en_US") {
foreach (var rs in Current.Resources.MergedDictionaries) {
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Locales/", StringComparison.Ordinal)) {
rs.Source = new Uri($"pack://application:,,,/Resources/Locales/{lang}.xaml", UriKind.Absolute);
break;
}
}
}
// 主界面显示
MainWindow = new Views.Launcher();
Models.Theme.Change();
Models.Locale.Change();
// 如果启动命令中指定了路径,打开指定目录的仓库
var launcher = new Views.Launcher();
if (e.Args.Length > 0) {
var repo = Models.Preference.Instance.FindRepository(e.Args[0]);
if (repo == null) {
@ -99,33 +63,15 @@ namespace SourceGit {
}
}
// 主界面显示
MainWindow = launcher;
MainWindow.Show();
// 检测更新
if (Models.Preference.Instance.General.CheckForUpdate) {
var curDayOfYear = DateTime.Now.DayOfYear;
var lastDayOfYear = Models.Preference.Instance.General.LastCheckDay;
if (lastDayOfYear != curDayOfYear) {
Models.Preference.Instance.General.LastCheckDay = curDayOfYear;
Task.Run(() => {
try {
var web = new WebClient() { Encoding = Encoding.UTF8 };
var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/sourcegit/releases/latest");
var ver = Models.Version.Load(raw);
var cur = Assembly.GetExecutingAssembly().GetName().Version;
var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*");
if (!matches.Success) return;
var major = int.Parse(matches.Groups[1].Value);
var minor = int.Parse(matches.Groups[2].Value);
if (major > cur.Major || (major == cur.Major && minor > cur.Minor)) {
Dispatcher.Invoke(() => Views.Upgrade.Open(MainWindow, ver));
}
} catch {}
});
}
}
// 检测版本更新
Models.Version.Check(ver => Dispatcher.Invoke(() => {
var dialog = new Views.Upgrade(ver) { Owner = MainWindow };
dialog.ShowDialog();
}));
}
/// <summary>
@ -135,7 +81,7 @@ namespace SourceGit {
/// <param name="e"></param>
private void OnAppDeactivated(object sender, EventArgs e) {
GC.Collect();
if (!restart) Models.Preference.Save();
Models.Preference.Save();
}
}
}