code_style: remove all IDE warnings

This commit is contained in:
leo 2024-07-15 00:30:31 +08:00
parent 24ca3eaf8c
commit f4eca45754
No known key found for this signature in database
79 changed files with 1462 additions and 1378 deletions

View file

@ -89,20 +89,32 @@ namespace SourceGit
public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand(() =>
{
var toplevel = GetTopLevel() as Window;
if (toplevel == null)
return;
var dialog = new Views.Preference();
dialog.ShowDialog(GetTopLevel() as Window);
dialog.ShowDialog(toplevel);
});
public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand(() =>
{
var toplevel = GetTopLevel() as Window;
if (toplevel == null)
return;
var dialog = new Views.Hotkeys();
dialog.ShowDialog(GetTopLevel() as Window);
dialog.ShowDialog(toplevel);
});
public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand(() =>
{
var toplevel = GetTopLevel() as Window;
if (toplevel == null)
return;
var dialog = new Views.About();
dialog.ShowDialog(GetTopLevel() as Window);
dialog.ShowDialog(toplevel);
});
public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand(() =>
@ -127,7 +139,7 @@ namespace SourceGit
public static void SetLocale(string localeKey)
{
var app = Current as App;
var targetLocale = app.Resources[localeKey] as ResourceDictionary;
var targetLocale = app?.Resources[localeKey] as ResourceDictionary;
if (targetLocale == null || targetLocale == app._activeLocale)
return;
@ -141,6 +153,8 @@ namespace SourceGit
public static void SetTheme(string theme, string themeOverridesFile)
{
var app = Current as App;
if (app == null)
return;
if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase))
app.RequestedThemeVariant = ThemeVariant.Light;
@ -179,6 +193,7 @@ namespace SourceGit
}
catch
{
// ignore
}
}
else
@ -189,18 +204,18 @@ namespace SourceGit
public static async void CopyText(string data)
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (desktop.MainWindow.Clipboard is { } clipbord)
if (desktop.MainWindow?.Clipboard is { } clipbord)
await clipbord.SetTextAsync(data);
}
}
public static async Task<string> GetClipboardTextAsync()
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (desktop.MainWindow.Clipboard is { } clipboard)
if (desktop.MainWindow?.Clipboard is { } clipboard)
{
return await clipboard.GetTextAsync();
}
@ -210,7 +225,7 @@ namespace SourceGit
public static string Text(string key, params object[] args)
{
var fmt = Current.FindResource($"Text.{key}") as string;
var fmt = Current?.FindResource($"Text.{key}") as string;
if (string.IsNullOrWhiteSpace(fmt))
return $"Text.{key}";
@ -226,16 +241,21 @@ namespace SourceGit
icon.Width = 12;
icon.Height = 12;
icon.Stretch = Stretch.Uniform;
icon.Data = Current.FindResource(key) as StreamGeometry;
var geo = Current?.FindResource(key) as StreamGeometry;
if (geo != null)
icon.Data = geo;
return icon;
}
public static TopLevel GetTopLevel()
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
return desktop.MainWindow;
}
return null;
}
@ -297,9 +317,9 @@ namespace SourceGit
public static void Quit()
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow.Close();
desktop.MainWindow?.Close();
desktop.Shutdown();
}
}
@ -360,7 +380,7 @@ namespace SourceGit
{
Dispatcher.UIThread.Post(() =>
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: not null } desktop)
{
var dialog = new Views.SelfUpdate()
{
@ -384,11 +404,11 @@ namespace SourceGit
if (!filename.Equals("git-rebase-todo", StringComparison.OrdinalIgnoreCase))
return true;
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(file));
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(file)!);
if (!dirInfo.Exists || !dirInfo.Name.Equals("rebase-merge", StringComparison.Ordinal))
return true;
var jobsFile = Path.Combine(dirInfo.Parent.FullName, "sourcegit_rebase_jobs.json");
var jobsFile = Path.Combine(dirInfo.Parent!.FullName, "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return true;
@ -437,16 +457,16 @@ namespace SourceGit
if (!filename.Equals("COMMIT_EDITMSG", StringComparison.OrdinalIgnoreCase))
return true;
var jobsFile = Path.Combine(Path.GetDirectoryName(file), "sourcegit_rebase_jobs.json");
var jobsFile = Path.Combine(Path.GetDirectoryName(file)!, "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return true;
var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
var doneFile = Path.Combine(Path.GetDirectoryName(file), "rebase-merge", "done");
var doneFile = Path.Combine(Path.GetDirectoryName(file)!, "rebase-merge", "done");
if (!File.Exists(doneFile))
return true;
var done = File.ReadAllText(doneFile).Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
var done = File.ReadAllText(doneFile).Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (done.Length > collection.Jobs.Count)
return true;
@ -460,7 +480,7 @@ namespace SourceGit
private bool TryLaunchedAsCoreEditor(IClassicDesktopStyleApplicationLifetime desktop)
{
var args = desktop.Args;
if (args.Length <= 1 || !args[0].Equals("--core-editor", StringComparison.Ordinal))
if (args == null || args.Length <= 1 || !args[0].Equals("--core-editor", StringComparison.Ordinal))
return false;
var file = args[1];
@ -474,7 +494,7 @@ namespace SourceGit
private bool TryLaunchedAsAskpass(IClassicDesktopStyleApplicationLifetime desktop)
{
var args = desktop.Args;
if (args.Length != 1 || !args[0].StartsWith("Enter passphrase", StringComparison.Ordinal))
if (args == null || args.Length != 1 || !args[0].StartsWith("Enter passphrase", StringComparison.Ordinal))
return false;
desktop.MainWindow = new Views.Askpass(args[0]);
@ -485,7 +505,10 @@ namespace SourceGit
{
Native.OS.SetupEnternalTools();
var startupRepo = desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]) ? desktop.Args[0] : null;
string startupRepo = null;
if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))
startupRepo = desktop.Args[0];
_launcher = new ViewModels.Launcher(startupRepo);
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };