@@ -89,20 +89,32 @@ public static AppBuilder BuildAvaloniaApp()
8989
9090 public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand ( ( ) =>
9191 {
92+ var toplevel = GetTopLevel ( ) as Window ;
93+ if ( toplevel == null )
94+ return ;
95+
9296 var dialog = new Views . Preference ( ) ;
93- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
97+ dialog . ShowDialog ( toplevel ) ;
9498 } ) ;
9599
96100 public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand ( ( ) =>
97101 {
102+ var toplevel = GetTopLevel ( ) as Window ;
103+ if ( toplevel == null )
104+ return ;
105+
98106 var dialog = new Views . Hotkeys ( ) ;
99- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
107+ dialog . ShowDialog ( toplevel ) ;
100108 } ) ;
101109
102110 public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand ( ( ) =>
103111 {
112+ var toplevel = GetTopLevel ( ) as Window ;
113+ if ( toplevel == null )
114+ return ;
115+
104116 var dialog = new Views . About ( ) ;
105- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
117+ dialog . ShowDialog ( toplevel ) ;
106118 } ) ;
107119
108120 public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand ( ( ) =>
@@ -127,7 +139,7 @@ public static void SendNotification(string context, string message)
127139 public static void SetLocale ( string localeKey )
128140 {
129141 var app = Current as App ;
130- var targetLocale = app . Resources [ localeKey ] as ResourceDictionary ;
142+ var targetLocale = app ? . Resources [ localeKey ] as ResourceDictionary ;
131143 if ( targetLocale == null || targetLocale == app . _activeLocale )
132144 return ;
133145
@@ -141,6 +153,8 @@ public static void SetLocale(string localeKey)
141153 public static void SetTheme ( string theme , string themeOverridesFile )
142154 {
143155 var app = Current as App ;
156+ if ( app == null )
157+ return ;
144158
145159 if ( theme . Equals ( "Light" , StringComparison . OrdinalIgnoreCase ) )
146160 app . RequestedThemeVariant = ThemeVariant . Light ;
@@ -179,6 +193,7 @@ public static void SetTheme(string theme, string themeOverridesFile)
179193 }
180194 catch
181195 {
196+ // ignore
182197 }
183198 }
184199 else
@@ -189,18 +204,18 @@ public static void SetTheme(string theme, string themeOverridesFile)
189204
190205 public static async void CopyText ( string data )
191206 {
192- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
207+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
193208 {
194- if ( desktop . MainWindow . Clipboard is { } clipbord )
209+ if ( desktop . MainWindow ? . Clipboard is { } clipbord )
195210 await clipbord . SetTextAsync ( data ) ;
196211 }
197212 }
198213
199214 public static async Task < string > GetClipboardTextAsync ( )
200215 {
201- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
216+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
202217 {
203- if ( desktop . MainWindow . Clipboard is { } clipboard )
218+ if ( desktop . MainWindow ? . Clipboard is { } clipboard )
204219 {
205220 return await clipboard . GetTextAsync ( ) ;
206221 }
@@ -210,7 +225,7 @@ public static async Task<string> GetClipboardTextAsync()
210225
211226 public static string Text ( string key , params object [ ] args )
212227 {
213- var fmt = Current . FindResource ( $ "Text.{ key } ") as string ;
228+ var fmt = Current ? . FindResource ( $ "Text.{ key } ") as string ;
214229 if ( string . IsNullOrWhiteSpace ( fmt ) )
215230 return $ "Text.{ key } ";
216231
@@ -226,16 +241,21 @@ public static Avalonia.Controls.Shapes.Path CreateMenuIcon(string key)
226241 icon . Width = 12 ;
227242 icon . Height = 12 ;
228243 icon . Stretch = Stretch . Uniform ;
229- icon . Data = Current . FindResource ( key ) as StreamGeometry ;
244+
245+ var geo = Current ? . FindResource ( key ) as StreamGeometry ;
246+ if ( geo != null )
247+ icon . Data = geo ;
248+
230249 return icon ;
231250 }
232251
233252 public static TopLevel GetTopLevel ( )
234253 {
235- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
254+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
236255 {
237256 return desktop . MainWindow ;
238257 }
258+
239259 return null ;
240260 }
241261
@@ -297,9 +317,9 @@ public static ViewModels.Repository FindOpenedRepository(string repoPath)
297317
298318 public static void Quit ( )
299319 {
300- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
320+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
301321 {
302- desktop . MainWindow . Close ( ) ;
322+ desktop . MainWindow ? . Close ( ) ;
303323 desktop . Shutdown ( ) ;
304324 }
305325 }
@@ -360,7 +380,7 @@ private static void ShowSelfUpdateResult(object data)
360380 {
361381 Dispatcher . UIThread . Post ( ( ) =>
362382 {
363- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
383+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow : not null } desktop )
364384 {
365385 var dialog = new Views . SelfUpdate ( )
366386 {
@@ -384,11 +404,11 @@ private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCod
384404 if ( ! filename . Equals ( "git-rebase-todo" , StringComparison . OrdinalIgnoreCase ) )
385405 return true ;
386406
387- var dirInfo = new DirectoryInfo ( Path . GetDirectoryName ( file ) ) ;
407+ var dirInfo = new DirectoryInfo ( Path . GetDirectoryName ( file ) ! ) ;
388408 if ( ! dirInfo . Exists || ! dirInfo . Name . Equals ( "rebase-merge" , StringComparison . Ordinal ) )
389409 return true ;
390410
391- var jobsFile = Path . Combine ( dirInfo . Parent . FullName , "sourcegit_rebase_jobs.json" ) ;
411+ var jobsFile = Path . Combine ( dirInfo . Parent ! . FullName , "sourcegit_rebase_jobs.json" ) ;
392412 if ( ! File . Exists ( jobsFile ) )
393413 return true ;
394414
@@ -437,16 +457,16 @@ private static bool TryLaunchedAsRebaseMessageEditor(string[] args, out int exit
437457 if ( ! filename . Equals ( "COMMIT_EDITMSG" , StringComparison . OrdinalIgnoreCase ) )
438458 return true ;
439459
440- var jobsFile = Path . Combine ( Path . GetDirectoryName ( file ) , "sourcegit_rebase_jobs.json" ) ;
460+ var jobsFile = Path . Combine ( Path . GetDirectoryName ( file ) ! , "sourcegit_rebase_jobs.json" ) ;
441461 if ( ! File . Exists ( jobsFile ) )
442462 return true ;
443463
444464 var collection = JsonSerializer . Deserialize ( File . ReadAllText ( jobsFile ) , JsonCodeGen . Default . InteractiveRebaseJobCollection ) ;
445- var doneFile = Path . Combine ( Path . GetDirectoryName ( file ) , "rebase-merge" , "done" ) ;
465+ var doneFile = Path . Combine ( Path . GetDirectoryName ( file ) ! , "rebase-merge" , "done" ) ;
446466 if ( ! File . Exists ( doneFile ) )
447467 return true ;
448468
449- var done = File . ReadAllText ( doneFile ) . Split ( new char [ ] { '\n ' , '\r ' } , StringSplitOptions . RemoveEmptyEntries ) ;
469+ var done = File . ReadAllText ( doneFile ) . Split ( new [ ] { '\n ' , '\r ' } , StringSplitOptions . RemoveEmptyEntries ) ;
450470 if ( done . Length > collection . Jobs . Count )
451471 return true ;
452472
@@ -460,7 +480,7 @@ private static bool TryLaunchedAsRebaseMessageEditor(string[] args, out int exit
460480 private bool TryLaunchedAsCoreEditor ( IClassicDesktopStyleApplicationLifetime desktop )
461481 {
462482 var args = desktop . Args ;
463- if ( args . Length <= 1 || ! args [ 0 ] . Equals ( "--core-editor" , StringComparison . Ordinal ) )
483+ if ( args == null || args . Length <= 1 || ! args [ 0 ] . Equals ( "--core-editor" , StringComparison . Ordinal ) )
464484 return false ;
465485
466486 var file = args [ 1 ] ;
@@ -474,7 +494,7 @@ private bool TryLaunchedAsCoreEditor(IClassicDesktopStyleApplicationLifetime des
474494 private bool TryLaunchedAsAskpass ( IClassicDesktopStyleApplicationLifetime desktop )
475495 {
476496 var args = desktop . Args ;
477- if ( args . Length != 1 || ! args [ 0 ] . StartsWith ( "Enter passphrase" , StringComparison . Ordinal ) )
497+ if ( args == null || args . Length != 1 || ! args [ 0 ] . StartsWith ( "Enter passphrase" , StringComparison . Ordinal ) )
478498 return false ;
479499
480500 desktop . MainWindow = new Views . Askpass ( args [ 0 ] ) ;
@@ -485,7 +505,10 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
485505 {
486506 Native . OS . SetupEnternalTools ( ) ;
487507
488- var startupRepo = desktop . Args . Length == 1 && Directory . Exists ( desktop . Args [ 0 ] ) ? desktop . Args [ 0 ] : null ;
508+ string startupRepo = null ;
509+ if ( desktop . Args != null && desktop . Args . Length == 1 && Directory . Exists ( desktop . Args [ 0 ] ) )
510+ startupRepo = desktop . Args [ 0 ] ;
511+
489512 _launcher = new ViewModels . Launcher ( startupRepo ) ;
490513 desktop . MainWindow = new Views . Launcher ( ) { DataContext = _launcher } ;
491514
0 commit comments