2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
2024-03-18 03:44:31 -07:00
|
|
|
using System.Collections;
|
2024-03-21 06:08:30 -07:00
|
|
|
using System.Globalization;
|
2024-03-17 18:37:06 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
using Avalonia.Data.Core.Plugins;
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
using Avalonia.Media;
|
2024-03-04 23:53:38 -08:00
|
|
|
using Avalonia.Media.Fonts;
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia.Styling;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit
|
|
|
|
{
|
|
|
|
public partial class App : Application
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
[STAThread]
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var builder = new StringBuilder();
|
|
|
|
builder.Append("Crash: ");
|
|
|
|
builder.Append(ex.Message);
|
|
|
|
builder.Append("\n\n");
|
|
|
|
builder.Append("----------------------------\n");
|
|
|
|
builder.Append($"Version: {Assembly.GetExecutingAssembly().GetName().Version}\n");
|
|
|
|
builder.Append($"OS: {Environment.OSVersion.ToString()}\n");
|
|
|
|
builder.Append($"Framework: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}\n");
|
|
|
|
builder.Append($"Source: {ex.Source}\n");
|
|
|
|
builder.Append($"---------------------------\n\n");
|
|
|
|
builder.Append(ex.StackTrace);
|
|
|
|
|
|
|
|
var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
|
2024-02-18 23:41:13 -08:00
|
|
|
var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
|
|
"SourceGit",
|
|
|
|
$"crash_{time}.log");
|
2024-02-05 23:08:37 -08:00
|
|
|
File.WriteAllText(file, builder.ToString());
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static AppBuilder BuildAvaloniaApp()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var builder = AppBuilder.Configure<App>();
|
|
|
|
builder.UsePlatformDetect();
|
2024-03-07 20:22:22 -08:00
|
|
|
builder.LogToTrace();
|
2024-03-17 18:37:06 -07:00
|
|
|
builder.ConfigureFonts(manager =>
|
|
|
|
{
|
2024-03-04 23:53:38 -08:00
|
|
|
var monospace = new EmbeddedFontCollection(
|
|
|
|
new Uri("fonts:SourceGit", UriKind.Absolute),
|
|
|
|
new Uri("avares://SourceGit/Resources/Fonts", UriKind.Absolute));
|
|
|
|
manager.AddFontCollection(monospace);
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-14 03:23:36 -07:00
|
|
|
Native.OS.SetupApp(builder);
|
2024-02-05 23:08:37 -08:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void RaiseException(string context, string message)
|
|
|
|
{
|
|
|
|
if (Current is App app && app._notificationReceiver != null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var notice = new Models.Notification() { IsError = true, Message = message };
|
2024-02-29 21:40:12 -08:00
|
|
|
app._notificationReceiver.OnReceiveNotification(context, notice);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void SendNotification(string context, string message)
|
|
|
|
{
|
|
|
|
if (Current is App app && app._notificationReceiver != null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var notice = new Models.Notification() { IsError = false, Message = message };
|
2024-02-29 21:40:12 -08:00
|
|
|
app._notificationReceiver.OnReceiveNotification(context, notice);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void SetLocale(string localeKey)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var app = Current as App;
|
2024-03-18 03:44:31 -07:00
|
|
|
var rd = new ResourceDictionary();
|
2024-03-17 19:11:24 -07:00
|
|
|
|
2024-03-18 03:44:31 -07:00
|
|
|
var culture = CultureInfo.GetCultureInfo(localeKey.Replace("_", "-"));
|
|
|
|
SourceGit.Resources.Locales.Culture = culture;
|
2024-03-16 01:25:20 -07:00
|
|
|
|
2024-03-18 03:44:31 -07:00
|
|
|
var sets = SourceGit.Resources.Locales.ResourceManager.GetResourceSet(culture, true, true);
|
|
|
|
foreach (var obj in sets)
|
|
|
|
{
|
|
|
|
if (obj is DictionaryEntry entry)
|
|
|
|
{
|
|
|
|
rd.Add(entry.Key, entry.Value);
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (app._activeLocale != null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
app.Resources.MergedDictionaries.Remove(app._activeLocale);
|
|
|
|
}
|
|
|
|
|
2024-03-18 03:44:31 -07:00
|
|
|
app.Resources.MergedDictionaries.Add(rd);
|
|
|
|
app._activeLocale = rd;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void SetTheme(string theme)
|
|
|
|
{
|
|
|
|
if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2024-03-07 20:22:22 -08:00
|
|
|
Current.RequestedThemeVariant = ThemeVariant.Light;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2024-03-07 20:22:22 -08:00
|
|
|
Current.RequestedThemeVariant = ThemeVariant.Dark;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-07 20:22:22 -08:00
|
|
|
Current.RequestedThemeVariant = ThemeVariant.Default;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static async void CopyText(string data)
|
|
|
|
{
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
|
|
|
if (desktop.MainWindow.Clipboard is { } clipbord)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
await clipbord.SetTextAsync(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static string Text(string key, params object[] args)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var fmt = Current.FindResource($"Text.{key}") as string;
|
|
|
|
if (string.IsNullOrWhiteSpace(fmt)) return $"Text.{key}";
|
|
|
|
return string.Format(fmt, args);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static Avalonia.Controls.Shapes.Path CreateMenuIcon(string key)
|
|
|
|
{
|
2024-02-27 02:26:05 -08:00
|
|
|
var icon = new Avalonia.Controls.Shapes.Path();
|
|
|
|
icon.Width = 12;
|
|
|
|
icon.Height = 12;
|
|
|
|
icon.Stretch = Stretch.Uniform;
|
|
|
|
icon.Data = Current.FindResource(key) as StreamGeometry;
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static TopLevel GetTopLevel()
|
|
|
|
{
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return desktop.MainWindow;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void Quit()
|
|
|
|
{
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
desktop.MainWindow.Close();
|
|
|
|
desktop.Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Initialize()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
|
2024-03-21 05:55:08 -07:00
|
|
|
var pref = ViewModels.Preference.Instance;
|
|
|
|
|
|
|
|
SetLocale(pref.Locale);
|
|
|
|
SetTheme(pref.Theme);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void OnFrameworkInitializationCompleted()
|
|
|
|
{
|
|
|
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
BindingPlugins.DataValidators.RemoveAt(0);
|
|
|
|
|
|
|
|
var launcher = new Views.Launcher();
|
|
|
|
_notificationReceiver = launcher;
|
|
|
|
desktop.MainWindow = launcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
base.OnFrameworkInitializationCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ResourceDictionary _activeLocale = null;
|
|
|
|
private Models.INotificationReceiver _notificationReceiver = null;
|
|
|
|
}
|
|
|
|
}
|