diff --git a/src/App.Commands.cs b/src/App.Commands.cs index 41896ee1..fd140f24 100644 --- a/src/App.Commands.cs +++ b/src/App.Commands.cs @@ -41,8 +41,18 @@ namespace SourceGit 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 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 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); + }); } } diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 29a2c81f..f565fe9d 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -22,6 +22,7 @@ namespace SourceGit { public partial class App : Application { + #region App Entry Point [STAThread] public static void Main(string[] args) { @@ -74,35 +75,9 @@ namespace SourceGit Native.OS.SetupApp(builder); return builder; } + #endregion - public override void Initialize() - { - 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); - } - } - + #region Utility Functions public static void OpenDialog(Window window) { if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner }) @@ -304,21 +279,6 @@ namespace SourceGit 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) { if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) @@ -331,18 +291,38 @@ namespace SourceGit Environment.Exit(exitCode); } } + #endregion - private static void CopyTextBlock(TextBlock textBlock) + #region Overrides + public override void Initialize() { - if (textBlock == null) - return; + AvaloniaXamlLoader.Load(this); - if (textBlock.Inlines is { Count: > 0 } inlines) - CopyText(inlines.Text); - else if (!string.IsNullOrEmpty(textBlock.Text)) - CopyText(textBlock.Text); + 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); + } + } + #endregion + private static void LogException(Exception ex) { if (ex == null) @@ -369,55 +349,6 @@ namespace SourceGit 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) { exitCode = -1; @@ -555,6 +486,59 @@ namespace SourceGit #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 ResourceDictionary _activeLocale = null; private ResourceDictionary _themeOverrides = null; diff --git a/src/ViewModels/Blame.cs b/src/ViewModels/Blame.cs index 26d77b23..253626bc 100644 --- a/src/ViewModels/Blame.cs +++ b/src/ViewModels/Blame.cs @@ -43,8 +43,18 @@ namespace SourceGit.ViewModels public void NavigateToCommit(string commitSHA) { - var repo = App.FindOpenedRepository(_repo); - repo?.NavigateToCommit(commitSHA); + var launcher = App.GetLauncer(); + 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; diff --git a/src/ViewModels/BranchCompare.cs b/src/ViewModels/BranchCompare.cs index 1d19a229..7716eb29 100644 --- a/src/ViewModels/BranchCompare.cs +++ b/src/ViewModels/BranchCompare.cs @@ -86,8 +86,18 @@ namespace SourceGit.ViewModels public void NavigateTo(string commitSHA) { - var repo = App.FindOpenedRepository(_repo); - repo?.NavigateToCommit(commitSHA); + var launcher = App.GetLauncer(); + 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() diff --git a/src/ViewModels/RevisionCompare.cs b/src/ViewModels/RevisionCompare.cs index ffc05643..ce10844e 100644 --- a/src/ViewModels/RevisionCompare.cs +++ b/src/ViewModels/RevisionCompare.cs @@ -100,8 +100,18 @@ namespace SourceGit.ViewModels public void NavigateTo(string commitSHA) { - var repo = App.FindOpenedRepository(_repo); - repo?.NavigateToCommit(commitSHA); + var launcher = App.GetLauncer(); + 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()