@@ -89,20 +89,32 @@ public static AppBuilder BuildAvaloniaApp()
89
89
90
90
public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand ( ( ) =>
91
91
{
92
+ var toplevel = GetTopLevel ( ) as Window ;
93
+ if ( toplevel == null )
94
+ return ;
95
+
92
96
var dialog = new Views . Preference ( ) ;
93
- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
97
+ dialog . ShowDialog ( toplevel ) ;
94
98
} ) ;
95
99
96
100
public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand ( ( ) =>
97
101
{
102
+ var toplevel = GetTopLevel ( ) as Window ;
103
+ if ( toplevel == null )
104
+ return ;
105
+
98
106
var dialog = new Views . Hotkeys ( ) ;
99
- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
107
+ dialog . ShowDialog ( toplevel ) ;
100
108
} ) ;
101
109
102
110
public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand ( ( ) =>
103
111
{
112
+ var toplevel = GetTopLevel ( ) as Window ;
113
+ if ( toplevel == null )
114
+ return ;
115
+
104
116
var dialog = new Views . About ( ) ;
105
- dialog . ShowDialog ( GetTopLevel ( ) as Window ) ;
117
+ dialog . ShowDialog ( toplevel ) ;
106
118
} ) ;
107
119
108
120
public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand ( ( ) =>
@@ -127,7 +139,7 @@ public static void SendNotification(string context, string message)
127
139
public static void SetLocale ( string localeKey )
128
140
{
129
141
var app = Current as App ;
130
- var targetLocale = app . Resources [ localeKey ] as ResourceDictionary ;
142
+ var targetLocale = app ? . Resources [ localeKey ] as ResourceDictionary ;
131
143
if ( targetLocale == null || targetLocale == app . _activeLocale )
132
144
return ;
133
145
@@ -141,6 +153,8 @@ public static void SetLocale(string localeKey)
141
153
public static void SetTheme ( string theme , string themeOverridesFile )
142
154
{
143
155
var app = Current as App ;
156
+ if ( app == null )
157
+ return ;
144
158
145
159
if ( theme . Equals ( "Light" , StringComparison . OrdinalIgnoreCase ) )
146
160
app . RequestedThemeVariant = ThemeVariant . Light ;
@@ -179,6 +193,7 @@ public static void SetTheme(string theme, string themeOverridesFile)
179
193
}
180
194
catch
181
195
{
196
+ // ignore
182
197
}
183
198
}
184
199
else
@@ -189,18 +204,18 @@ public static void SetTheme(string theme, string themeOverridesFile)
189
204
190
205
public static async void CopyText ( string data )
191
206
{
192
- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
207
+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
193
208
{
194
- if ( desktop . MainWindow . Clipboard is { } clipbord )
209
+ if ( desktop . MainWindow ? . Clipboard is { } clipbord )
195
210
await clipbord . SetTextAsync ( data ) ;
196
211
}
197
212
}
198
213
199
214
public static async Task < string > GetClipboardTextAsync ( )
200
215
{
201
- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
216
+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
202
217
{
203
- if ( desktop . MainWindow . Clipboard is { } clipboard )
218
+ if ( desktop . MainWindow ? . Clipboard is { } clipboard )
204
219
{
205
220
return await clipboard . GetTextAsync ( ) ;
206
221
}
@@ -210,7 +225,7 @@ public static async Task<string> GetClipboardTextAsync()
210
225
211
226
public static string Text ( string key , params object [ ] args )
212
227
{
213
- var fmt = Current . FindResource ( $ "Text.{ key } ") as string ;
228
+ var fmt = Current ? . FindResource ( $ "Text.{ key } ") as string ;
214
229
if ( string . IsNullOrWhiteSpace ( fmt ) )
215
230
return $ "Text.{ key } ";
216
231
@@ -226,16 +241,21 @@ public static Avalonia.Controls.Shapes.Path CreateMenuIcon(string key)
226
241
icon . Width = 12 ;
227
242
icon . Height = 12 ;
228
243
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
+
230
249
return icon ;
231
250
}
232
251
233
252
public static TopLevel GetTopLevel ( )
234
253
{
235
- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
254
+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
236
255
{
237
256
return desktop . MainWindow ;
238
257
}
258
+
239
259
return null ;
240
260
}
241
261
@@ -297,9 +317,9 @@ public static ViewModels.Repository FindOpenedRepository(string repoPath)
297
317
298
318
public static void Quit ( )
299
319
{
300
- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
320
+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
301
321
{
302
- desktop . MainWindow . Close ( ) ;
322
+ desktop . MainWindow ? . Close ( ) ;
303
323
desktop . Shutdown ( ) ;
304
324
}
305
325
}
@@ -360,7 +380,7 @@ private static void ShowSelfUpdateResult(object data)
360
380
{
361
381
Dispatcher . UIThread . Post ( ( ) =>
362
382
{
363
- if ( Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
383
+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow : not null } desktop )
364
384
{
365
385
var dialog = new Views . SelfUpdate ( )
366
386
{
@@ -384,11 +404,11 @@ private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCod
384
404
if ( ! filename . Equals ( "git-rebase-todo" , StringComparison . OrdinalIgnoreCase ) )
385
405
return true ;
386
406
387
- var dirInfo = new DirectoryInfo ( Path . GetDirectoryName ( file ) ) ;
407
+ var dirInfo = new DirectoryInfo ( Path . GetDirectoryName ( file ) ! ) ;
388
408
if ( ! dirInfo . Exists || ! dirInfo . Name . Equals ( "rebase-merge" , StringComparison . Ordinal ) )
389
409
return true ;
390
410
391
- var jobsFile = Path . Combine ( dirInfo . Parent . FullName , "sourcegit_rebase_jobs.json" ) ;
411
+ var jobsFile = Path . Combine ( dirInfo . Parent ! . FullName , "sourcegit_rebase_jobs.json" ) ;
392
412
if ( ! File . Exists ( jobsFile ) )
393
413
return true ;
394
414
@@ -437,16 +457,16 @@ private static bool TryLaunchedAsRebaseMessageEditor(string[] args, out int exit
437
457
if ( ! filename . Equals ( "COMMIT_EDITMSG" , StringComparison . OrdinalIgnoreCase ) )
438
458
return true ;
439
459
440
- var jobsFile = Path . Combine ( Path . GetDirectoryName ( file ) , "sourcegit_rebase_jobs.json" ) ;
460
+ var jobsFile = Path . Combine ( Path . GetDirectoryName ( file ) ! , "sourcegit_rebase_jobs.json" ) ;
441
461
if ( ! File . Exists ( jobsFile ) )
442
462
return true ;
443
463
444
464
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" ) ;
446
466
if ( ! File . Exists ( doneFile ) )
447
467
return true ;
448
468
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 ) ;
450
470
if ( done . Length > collection . Jobs . Count )
451
471
return true ;
452
472
@@ -460,7 +480,7 @@ private static bool TryLaunchedAsRebaseMessageEditor(string[] args, out int exit
460
480
private bool TryLaunchedAsCoreEditor ( IClassicDesktopStyleApplicationLifetime desktop )
461
481
{
462
482
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 ) )
464
484
return false ;
465
485
466
486
var file = args [ 1 ] ;
@@ -474,7 +494,7 @@ private bool TryLaunchedAsCoreEditor(IClassicDesktopStyleApplicationLifetime des
474
494
private bool TryLaunchedAsAskpass ( IClassicDesktopStyleApplicationLifetime desktop )
475
495
{
476
496
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 ) )
478
498
return false ;
479
499
480
500
desktop . MainWindow = new Views . Askpass ( args [ 0 ] ) ;
@@ -485,7 +505,10 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
485
505
{
486
506
Native . OS . SetupEnternalTools ( ) ;
487
507
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
+
489
512
_launcher = new ViewModels . Launcher ( startupRepo ) ;
490
513
desktop . MainWindow = new Views . Launcher ( ) { DataContext = _launcher } ;
491
514
0 commit comments