Skip to content

Commit b06d14f

Browse files
committed
refactor: SourceGit.App
1 parent 495b3a9 commit b06d14f

File tree

5 files changed

+131
-107
lines changed

5 files changed

+131
-107
lines changed

src/App.Commands.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ public static bool IsCheckForUpdateCommandVisible
4141
public static readonly Command OpenHotkeysCommand = new Command(_ => OpenDialog(new Views.Hotkeys()));
4242
public static readonly Command OpenAppDataDirCommand = new Command(_ => Native.OS.OpenInFileManager(Native.OS.DataDir));
4343
public static readonly Command OpenAboutCommand = new Command(_ => OpenDialog(new Views.About()));
44-
public static readonly Command CheckForUpdateCommand = new Command(_ => Check4Update(true));
44+
public static readonly Command CheckForUpdateCommand = new Command(_ => (Current as App)?.Check4Update(true));
4545
public static readonly Command QuitCommand = new Command(_ => Quit(0));
46-
public static readonly Command CopyTextBlockCommand = new Command(p => CopyTextBlock(p as TextBlock));
46+
public static readonly Command CopyTextBlockCommand = new Command(p =>
47+
{
48+
var textBlock = p as TextBlock;
49+
if (textBlock == null)
50+
return;
51+
52+
if (textBlock.Inlines is { Count: > 0 } inlines)
53+
CopyText(inlines.Text);
54+
else if (!string.IsNullOrEmpty(textBlock.Text))
55+
CopyText(textBlock.Text);
56+
});
4757
}
4858
}

src/App.axaml.cs

Lines changed: 83 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace SourceGit
2222
{
2323
public partial class App : Application
2424
{
25+
#region App Entry Point
2526
[STAThread]
2627
public static void Main(string[] args)
2728
{
@@ -74,35 +75,9 @@ public static AppBuilder BuildAvaloniaApp()
7475
Native.OS.SetupApp(builder);
7576
return builder;
7677
}
78+
#endregion
7779

78-
public override void Initialize()
79-
{
80-
AvaloniaXamlLoader.Load(this);
81-
82-
var pref = ViewModels.Preference.Instance;
83-
pref.PropertyChanged += (_, _) => pref.Save();
84-
85-
SetLocale(pref.Locale);
86-
SetTheme(pref.Theme, pref.ThemeOverrides);
87-
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
88-
}
89-
90-
public override void OnFrameworkInitializationCompleted()
91-
{
92-
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
93-
{
94-
BindingPlugins.DataValidators.RemoveAt(0);
95-
96-
if (TryLaunchedAsCoreEditor(desktop))
97-
return;
98-
99-
if (TryLaunchedAsAskpass(desktop))
100-
return;
101-
102-
TryLaunchedAsNormal(desktop);
103-
}
104-
}
105-
80+
#region Utility Functions
10681
public static void OpenDialog(Window window)
10782
{
10883
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
@@ -304,21 +279,6 @@ public static ViewModels.Launcher GetLauncer()
304279
return Current is App app ? app._launcher : null;
305280
}
306281

307-
public static ViewModels.Repository FindOpenedRepository(string repoPath)
308-
{
309-
if (Current is App app && app._launcher != null)
310-
{
311-
foreach (var page in app._launcher.Pages)
312-
{
313-
var id = page.Node.Id.Replace("\\", "/");
314-
if (id == repoPath && page.Data is ViewModels.Repository repo)
315-
return repo;
316-
}
317-
}
318-
319-
return null;
320-
}
321-
322282
public static void Quit(int exitCode)
323283
{
324284
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
@@ -331,18 +291,38 @@ public static void Quit(int exitCode)
331291
Environment.Exit(exitCode);
332292
}
333293
}
294+
#endregion
334295

335-
private static void CopyTextBlock(TextBlock textBlock)
296+
#region Overrides
297+
public override void Initialize()
336298
{
337-
if (textBlock == null)
338-
return;
299+
AvaloniaXamlLoader.Load(this);
339300

340-
if (textBlock.Inlines is { Count: > 0 } inlines)
341-
CopyText(inlines.Text);
342-
else if (!string.IsNullOrEmpty(textBlock.Text))
343-
CopyText(textBlock.Text);
301+
var pref = ViewModels.Preference.Instance;
302+
pref.PropertyChanged += (_, _) => pref.Save();
303+
304+
SetLocale(pref.Locale);
305+
SetTheme(pref.Theme, pref.ThemeOverrides);
306+
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
344307
}
345308

309+
public override void OnFrameworkInitializationCompleted()
310+
{
311+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
312+
{
313+
BindingPlugins.DataValidators.RemoveAt(0);
314+
315+
if (TryLaunchedAsCoreEditor(desktop))
316+
return;
317+
318+
if (TryLaunchedAsAskpass(desktop))
319+
return;
320+
321+
TryLaunchedAsNormal(desktop);
322+
}
323+
}
324+
#endregion
325+
346326
private static void LogException(Exception ex)
347327
{
348328
if (ex == null)
@@ -369,55 +349,6 @@ private static void LogException(Exception ex)
369349
File.WriteAllText(file, builder.ToString());
370350
}
371351

372-
private static void Check4Update(bool manually = false)
373-
{
374-
Task.Run(async () =>
375-
{
376-
try
377-
{
378-
// Fetch lastest release information.
379-
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
380-
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");
381-
382-
// Parse json into Models.Version.
383-
var ver = JsonSerializer.Deserialize(data, JsonCodeGen.Default.Version);
384-
if (ver == null)
385-
return;
386-
387-
// Check if already up-to-date.
388-
if (!ver.IsNewVersion)
389-
{
390-
if (manually)
391-
ShowSelfUpdateResult(new Models.AlreadyUpToDate());
392-
return;
393-
}
394-
395-
// Should not check ignored tag if this is called manually.
396-
if (!manually)
397-
{
398-
var pref = ViewModels.Preference.Instance;
399-
if (ver.TagName == pref.IgnoreUpdateTag)
400-
return;
401-
}
402-
403-
ShowSelfUpdateResult(ver);
404-
}
405-
catch (Exception e)
406-
{
407-
if (manually)
408-
ShowSelfUpdateResult(e);
409-
}
410-
});
411-
}
412-
413-
private static void ShowSelfUpdateResult(object data)
414-
{
415-
Dispatcher.UIThread.Post(() =>
416-
{
417-
OpenDialog(new Views.SelfUpdate() { DataContext = new ViewModels.SelfUpdate() { Data = data } });
418-
});
419-
}
420-
421352
private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCode)
422353
{
423354
exitCode = -1;
@@ -555,6 +486,59 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
555486
#endif
556487
}
557488

489+
private void Check4Update(bool manually = false)
490+
{
491+
Task.Run(async () =>
492+
{
493+
try
494+
{
495+
// Fetch lastest release information.
496+
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
497+
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");
498+
499+
// Parse json into Models.Version.
500+
var ver = JsonSerializer.Deserialize(data, JsonCodeGen.Default.Version);
501+
if (ver == null)
502+
return;
503+
504+
// Check if already up-to-date.
505+
if (!ver.IsNewVersion)
506+
{
507+
if (manually)
508+
ShowSelfUpdateResult(new Models.AlreadyUpToDate());
509+
return;
510+
}
511+
512+
// Should not check ignored tag if this is called manually.
513+
if (!manually)
514+
{
515+
var pref = ViewModels.Preference.Instance;
516+
if (ver.TagName == pref.IgnoreUpdateTag)
517+
return;
518+
}
519+
520+
ShowSelfUpdateResult(ver);
521+
}
522+
catch (Exception e)
523+
{
524+
if (manually)
525+
ShowSelfUpdateResult(e);
526+
}
527+
});
528+
}
529+
530+
private void ShowSelfUpdateResult(object data)
531+
{
532+
Dispatcher.UIThread.Post(() =>
533+
{
534+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
535+
{
536+
var dialog = new Views.SelfUpdate() { DataContext = new ViewModels.SelfUpdate() { Data = data } };
537+
dialog.ShowDialog(owner);
538+
}
539+
});
540+
}
541+
558542
private ViewModels.Launcher _launcher = null;
559543
private ResourceDictionary _activeLocale = null;
560544
private ResourceDictionary _themeOverrides = null;

src/ViewModels/Blame.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ public Blame(string repo, string file, string revision)
4343

4444
public void NavigateToCommit(string commitSHA)
4545
{
46-
var repo = App.FindOpenedRepository(_repo);
47-
repo?.NavigateToCommit(commitSHA);
46+
var launcher = App.GetLauncer();
47+
if (launcher == null)
48+
return;
49+
50+
foreach (var page in launcher.Pages)
51+
{
52+
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
53+
{
54+
repo.NavigateToCommit(commitSHA);
55+
break;
56+
}
57+
}
4858
}
4959

5060
private readonly string _repo;

src/ViewModels/BranchCompare.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,18 @@ public BranchCompare(string repo, Models.Branch baseBranch, Models.Branch toBran
8686

8787
public void NavigateTo(string commitSHA)
8888
{
89-
var repo = App.FindOpenedRepository(_repo);
90-
repo?.NavigateToCommit(commitSHA);
89+
var launcher = App.GetLauncer();
90+
if (launcher == null)
91+
return;
92+
93+
foreach (var page in launcher.Pages)
94+
{
95+
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
96+
{
97+
repo.NavigateToCommit(commitSHA);
98+
break;
99+
}
100+
}
91101
}
92102

93103
public void Swap()

src/ViewModels/RevisionCompare.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ public void Cleanup()
100100

101101
public void NavigateTo(string commitSHA)
102102
{
103-
var repo = App.FindOpenedRepository(_repo);
104-
repo?.NavigateToCommit(commitSHA);
103+
var launcher = App.GetLauncer();
104+
if (launcher == null)
105+
return;
106+
107+
foreach (var page in launcher.Pages)
108+
{
109+
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
110+
{
111+
repo.NavigateToCommit(commitSHA);
112+
break;
113+
}
114+
}
105115
}
106116

107117
public void Swap()

0 commit comments

Comments
 (0)