2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2024-03-26 07:11:06 -07:00
|
|
|
using System.Net.Http;
|
2024-03-17 18:37:06 -07:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
2024-03-26 07:11:06 -07:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading.Tasks;
|
2024-05-10 20:31:14 -07:00
|
|
|
using System.Windows.Input;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
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-26 07:11:06 -07:00
|
|
|
using Avalonia.Threading;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit
|
|
|
|
{
|
2024-05-10 20:31:14 -07:00
|
|
|
public class SimpleCommand : ICommand
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-05-10 20:31:14 -07:00
|
|
|
public event EventHandler CanExecuteChanged
|
|
|
|
{
|
|
|
|
add { }
|
|
|
|
remove { }
|
|
|
|
}
|
|
|
|
|
|
|
|
public SimpleCommand(Action action)
|
|
|
|
{
|
|
|
|
_action = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanExecute(object parameter) => _action != null;
|
|
|
|
public void Execute(object parameter) => _action?.Invoke();
|
|
|
|
|
|
|
|
private Action _action = null;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-05-10 20:31:14 -07:00
|
|
|
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-06-20 02:02:12 -07:00
|
|
|
if (args.Length > 1 && args[0].Equals("--rebase-editor", StringComparison.Ordinal))
|
|
|
|
Environment.Exit(Models.InteractiveRebaseEditor.Process(args[1]));
|
|
|
|
else
|
|
|
|
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-06-02 18:44:12 -07:00
|
|
|
var file = Path.Combine(Native.OS.DataDir, $"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-05-10 20:31:14 -07:00
|
|
|
public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand(() =>
|
|
|
|
{
|
|
|
|
var dialog = new Views.Preference();
|
|
|
|
dialog.ShowDialog(GetTopLevel() as Window);
|
|
|
|
});
|
|
|
|
|
|
|
|
public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand(() =>
|
|
|
|
{
|
|
|
|
var dialog = new Views.Hotkeys();
|
|
|
|
dialog.ShowDialog(GetTopLevel() as Window);
|
|
|
|
});
|
|
|
|
|
|
|
|
public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand(() =>
|
|
|
|
{
|
|
|
|
var dialog = new Views.About();
|
|
|
|
dialog.ShowDialog(GetTopLevel() as Window);
|
|
|
|
});
|
|
|
|
|
|
|
|
public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand(() =>
|
|
|
|
{
|
|
|
|
Check4Update(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
public static readonly SimpleCommand QuitCommand = new SimpleCommand(Quit);
|
|
|
|
|
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-04-06 03:06:32 -07:00
|
|
|
var targetLocale = app.Resources[localeKey] as ResourceDictionary;
|
|
|
|
if (targetLocale == null || targetLocale == app._activeLocale)
|
|
|
|
return;
|
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-04-06 03:06:32 -07:00
|
|
|
app.Resources.MergedDictionaries.Add(targetLocale);
|
|
|
|
app._activeLocale = targetLocale;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-06-05 03:23:28 -07:00
|
|
|
public static void SetTheme(string theme, string colorsFile)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-06-05 03:23:28 -07:00
|
|
|
var app = Current as App;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase))
|
2024-06-05 03:23:28 -07:00
|
|
|
app.RequestedThemeVariant = ThemeVariant.Light;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase))
|
2024-06-05 03:23:28 -07:00
|
|
|
app.RequestedThemeVariant = ThemeVariant.Dark;
|
2024-03-17 18:37:06 -07:00
|
|
|
else
|
2024-06-05 03:23:28 -07:00
|
|
|
app.RequestedThemeVariant = ThemeVariant.Default;
|
|
|
|
|
|
|
|
if (app._colorOverrides != null)
|
|
|
|
{
|
|
|
|
app.Resources.MergedDictionaries.Remove(app._colorOverrides);
|
|
|
|
app._colorOverrides = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(colorsFile) && File.Exists(colorsFile))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var resDic = new ResourceDictionary();
|
2024-06-06 00:31:02 -07:00
|
|
|
|
2024-06-05 03:23:28 -07:00
|
|
|
var schema = JsonSerializer.Deserialize(File.ReadAllText(colorsFile), JsonCodeGen.Default.DictionaryStringString);
|
|
|
|
foreach (var kv in schema)
|
|
|
|
resDic[kv.Key] = Color.Parse(kv.Value);
|
|
|
|
|
|
|
|
app.Resources.MergedDictionaries.Add(resDic);
|
|
|
|
app._colorOverrides = resDic;
|
2024-06-06 00:31:02 -07:00
|
|
|
}
|
2024-06-05 03:23:28 -07:00
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
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;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(fmt))
|
|
|
|
return $"Text.{key}";
|
2024-06-17 19:19:55 -07:00
|
|
|
|
|
|
|
if (args == null || args.Length == 0)
|
|
|
|
return fmt;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
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-26 07:11:06 -07:00
|
|
|
public static void Check4Update(bool manually = false)
|
|
|
|
{
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fetch lastest release information.
|
2024-04-25 18:40:02 -07:00
|
|
|
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
|
2024-04-15 18:23:54 -07:00
|
|
|
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");
|
2024-03-26 07:11:06 -07:00
|
|
|
|
|
|
|
// Parse json into Models.Version.
|
|
|
|
var ver = JsonSerializer.Deserialize(data, JsonCodeGen.Default.Version);
|
2024-03-31 01:54:29 -07:00
|
|
|
if (ver == null)
|
|
|
|
return;
|
2024-03-26 07:11:06 -07:00
|
|
|
|
|
|
|
// Check if already up-to-date.
|
|
|
|
if (!ver.IsNewVersion)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (manually)
|
|
|
|
ShowSelfUpdateResult(new Models.AlreadyUpToDate());
|
2024-03-26 07:11:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not check ignored tag if this is called manually.
|
|
|
|
if (!manually)
|
|
|
|
{
|
|
|
|
var pref = ViewModels.Preference.Instance;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (ver.TagName == pref.IgnoreUpdateTag)
|
|
|
|
return;
|
2024-03-26 07:11:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ShowSelfUpdateResult(ver);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (manually)
|
|
|
|
ShowSelfUpdateResult(e);
|
2024-03-26 07:11:06 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
2024-06-05 03:23:28 -07:00
|
|
|
SetTheme(pref.Theme, pref.ColorOverrides);
|
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);
|
2024-05-16 21:01:29 -07:00
|
|
|
Native.OS.SetupEnternalTools();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var launcher = new Views.Launcher();
|
|
|
|
_notificationReceiver = launcher;
|
|
|
|
desktop.MainWindow = launcher;
|
2024-03-26 07:11:06 -07:00
|
|
|
|
2024-04-06 18:51:55 -07:00
|
|
|
if (ViewModels.Preference.Instance.ShouldCheck4UpdateOnStartup)
|
|
|
|
{
|
|
|
|
ViewModels.Preference.Save();
|
2024-03-31 01:54:29 -07:00
|
|
|
Check4Update();
|
2024-04-06 18:51:55 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
base.OnFrameworkInitializationCompleted();
|
|
|
|
}
|
|
|
|
|
2024-03-26 07:11:06 -07:00
|
|
|
private static void ShowSelfUpdateResult(object data)
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
|
|
|
var dialog = new Views.SelfUpdate()
|
|
|
|
{
|
|
|
|
DataContext = new ViewModels.SelfUpdate
|
|
|
|
{
|
|
|
|
Data = data
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
dialog.Show(desktop.MainWindow);
|
2024-03-28 02:46:03 -07:00
|
|
|
}
|
2024-03-26 07:11:06 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-19 07:24:51 -07:00
|
|
|
public static async Task<string> GetClipboardTextAsync()
|
|
|
|
{
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
|
|
|
if (desktop.MainWindow.Clipboard is { } clipboard)
|
|
|
|
{
|
|
|
|
return await clipboard.GetTextAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
private ResourceDictionary _activeLocale = null;
|
2024-06-05 03:23:28 -07:00
|
|
|
private ResourceDictionary _colorOverrides = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
private Models.INotificationReceiver _notificationReceiver = null;
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|