refactor: SourceGit.App

This commit is contained in:
leo 2025-01-09 18:12:11 +08:00
parent 495b3a9296
commit b06d14fec7
No known key found for this signature in database
5 changed files with 131 additions and 107 deletions

View file

@ -41,8 +41,18 @@ namespace SourceGit
public static readonly Command OpenHotkeysCommand = new Command(_ => OpenDialog(new Views.Hotkeys())); public static readonly Command OpenHotkeysCommand = new Command(_ => OpenDialog(new Views.Hotkeys()));
public static readonly Command OpenAppDataDirCommand = new Command(_ => Native.OS.OpenInFileManager(Native.OS.DataDir)); public static readonly Command OpenAppDataDirCommand = new Command(_ => Native.OS.OpenInFileManager(Native.OS.DataDir));
public static readonly Command OpenAboutCommand = new Command(_ => OpenDialog(new Views.About())); public static readonly Command OpenAboutCommand = new Command(_ => OpenDialog(new Views.About()));
public static readonly Command CheckForUpdateCommand = new Command(_ => Check4Update(true)); public static readonly Command CheckForUpdateCommand = new Command(_ => (Current as App)?.Check4Update(true));
public static readonly Command QuitCommand = new Command(_ => Quit(0)); public static readonly Command QuitCommand = new Command(_ => Quit(0));
public static readonly Command CopyTextBlockCommand = new Command(p => CopyTextBlock(p as TextBlock)); public static readonly Command CopyTextBlockCommand = new Command(p =>
{
var textBlock = p as TextBlock;
if (textBlock == null)
return;
if (textBlock.Inlines is { Count: > 0 } inlines)
CopyText(inlines.Text);
else if (!string.IsNullOrEmpty(textBlock.Text))
CopyText(textBlock.Text);
});
} }
} }

View file

@ -22,6 +22,7 @@ namespace SourceGit
{ {
public partial class App : Application public partial class App : Application
{ {
#region App Entry Point
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
@ -74,35 +75,9 @@ namespace SourceGit
Native.OS.SetupApp(builder); Native.OS.SetupApp(builder);
return builder; return builder;
} }
#endregion
public override void Initialize() #region Utility Functions
{
AvaloniaXamlLoader.Load(this);
var pref = ViewModels.Preference.Instance;
pref.PropertyChanged += (_, _) => pref.Save();
SetLocale(pref.Locale);
SetTheme(pref.Theme, pref.ThemeOverrides);
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
BindingPlugins.DataValidators.RemoveAt(0);
if (TryLaunchedAsCoreEditor(desktop))
return;
if (TryLaunchedAsAskpass(desktop))
return;
TryLaunchedAsNormal(desktop);
}
}
public static void OpenDialog(Window window) public static void OpenDialog(Window window)
{ {
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner }) if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
@ -304,21 +279,6 @@ namespace SourceGit
return Current is App app ? app._launcher : null; return Current is App app ? app._launcher : null;
} }
public static ViewModels.Repository FindOpenedRepository(string repoPath)
{
if (Current is App app && app._launcher != null)
{
foreach (var page in app._launcher.Pages)
{
var id = page.Node.Id.Replace("\\", "/");
if (id == repoPath && page.Data is ViewModels.Repository repo)
return repo;
}
}
return null;
}
public static void Quit(int exitCode) public static void Quit(int exitCode)
{ {
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
@ -331,17 +291,37 @@ namespace SourceGit
Environment.Exit(exitCode); Environment.Exit(exitCode);
} }
} }
#endregion
private static void CopyTextBlock(TextBlock textBlock) #region Overrides
public override void Initialize()
{ {
if (textBlock == null) AvaloniaXamlLoader.Load(this);
var pref = ViewModels.Preference.Instance;
pref.PropertyChanged += (_, _) => pref.Save();
SetLocale(pref.Locale);
SetTheme(pref.Theme, pref.ThemeOverrides);
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
BindingPlugins.DataValidators.RemoveAt(0);
if (TryLaunchedAsCoreEditor(desktop))
return; return;
if (textBlock.Inlines is { Count: > 0 } inlines) if (TryLaunchedAsAskpass(desktop))
CopyText(inlines.Text); return;
else if (!string.IsNullOrEmpty(textBlock.Text))
CopyText(textBlock.Text); TryLaunchedAsNormal(desktop);
} }
}
#endregion
private static void LogException(Exception ex) private static void LogException(Exception ex)
{ {
@ -369,55 +349,6 @@ namespace SourceGit
File.WriteAllText(file, builder.ToString()); File.WriteAllText(file, builder.ToString());
} }
private static void Check4Update(bool manually = false)
{
Task.Run(async () =>
{
try
{
// Fetch lastest release information.
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");
// Parse json into Models.Version.
var ver = JsonSerializer.Deserialize(data, JsonCodeGen.Default.Version);
if (ver == null)
return;
// Check if already up-to-date.
if (!ver.IsNewVersion)
{
if (manually)
ShowSelfUpdateResult(new Models.AlreadyUpToDate());
return;
}
// Should not check ignored tag if this is called manually.
if (!manually)
{
var pref = ViewModels.Preference.Instance;
if (ver.TagName == pref.IgnoreUpdateTag)
return;
}
ShowSelfUpdateResult(ver);
}
catch (Exception e)
{
if (manually)
ShowSelfUpdateResult(e);
}
});
}
private static void ShowSelfUpdateResult(object data)
{
Dispatcher.UIThread.Post(() =>
{
OpenDialog(new Views.SelfUpdate() { DataContext = new ViewModels.SelfUpdate() { Data = data } });
});
}
private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCode) private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCode)
{ {
exitCode = -1; exitCode = -1;
@ -555,6 +486,59 @@ namespace SourceGit
#endif #endif
} }
private void Check4Update(bool manually = false)
{
Task.Run(async () =>
{
try
{
// Fetch lastest release information.
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");
// Parse json into Models.Version.
var ver = JsonSerializer.Deserialize(data, JsonCodeGen.Default.Version);
if (ver == null)
return;
// Check if already up-to-date.
if (!ver.IsNewVersion)
{
if (manually)
ShowSelfUpdateResult(new Models.AlreadyUpToDate());
return;
}
// Should not check ignored tag if this is called manually.
if (!manually)
{
var pref = ViewModels.Preference.Instance;
if (ver.TagName == pref.IgnoreUpdateTag)
return;
}
ShowSelfUpdateResult(ver);
}
catch (Exception e)
{
if (manually)
ShowSelfUpdateResult(e);
}
});
}
private void ShowSelfUpdateResult(object data)
{
Dispatcher.UIThread.Post(() =>
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
{
var dialog = new Views.SelfUpdate() { DataContext = new ViewModels.SelfUpdate() { Data = data } };
dialog.ShowDialog(owner);
}
});
}
private ViewModels.Launcher _launcher = null; private ViewModels.Launcher _launcher = null;
private ResourceDictionary _activeLocale = null; private ResourceDictionary _activeLocale = null;
private ResourceDictionary _themeOverrides = null; private ResourceDictionary _themeOverrides = null;

View file

@ -43,8 +43,18 @@ namespace SourceGit.ViewModels
public void NavigateToCommit(string commitSHA) public void NavigateToCommit(string commitSHA)
{ {
var repo = App.FindOpenedRepository(_repo); var launcher = App.GetLauncer();
repo?.NavigateToCommit(commitSHA); if (launcher == null)
return;
foreach (var page in launcher.Pages)
{
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{
repo.NavigateToCommit(commitSHA);
break;
}
}
} }
private readonly string _repo; private readonly string _repo;

View file

@ -86,8 +86,18 @@ namespace SourceGit.ViewModels
public void NavigateTo(string commitSHA) public void NavigateTo(string commitSHA)
{ {
var repo = App.FindOpenedRepository(_repo); var launcher = App.GetLauncer();
repo?.NavigateToCommit(commitSHA); if (launcher == null)
return;
foreach (var page in launcher.Pages)
{
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{
repo.NavigateToCommit(commitSHA);
break;
}
}
} }
public void Swap() public void Swap()

View file

@ -100,8 +100,18 @@ namespace SourceGit.ViewModels
public void NavigateTo(string commitSHA) public void NavigateTo(string commitSHA)
{ {
var repo = App.FindOpenedRepository(_repo); var launcher = App.GetLauncer();
repo?.NavigateToCommit(commitSHA); if (launcher == null)
return;
foreach (var page in launcher.Pages)
{
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{
repo.NavigateToCommit(commitSHA);
break;
}
}
} }
public void Swap() public void Swap()