@@ -146,14 +146,8 @@ public ConfigUi(Task<ChromaReader?> rzSdkManager, Task<PluginManager> pluginMana
146
146
_keyboardTimerCallback = KeyboardTimerCallback ;
147
147
_virtualKeyboardTimer . Elapsed += virtual_keyboard_timer_Tick ;
148
148
149
- //Show hidden profiles button
150
- _profileHidden = new Image
151
- {
152
- Source = _notVisible ,
153
- ToolTip = "Toggle Hidden profiles' visibility" ,
154
- Margin = new Thickness ( 0 , 5 , 0 , 0 )
155
- } ;
156
- _profileHidden . MouseDown += HiddenProfile_MouseDown ;
149
+ ProfileAdd . MouseDown += AddProfile_MouseDown ;
150
+ ProfileHidden . MouseDown += HiddenProfile_MouseDown ;
157
151
}
158
152
159
153
private async Task KeyboardTimerCallback ( )
@@ -195,7 +189,7 @@ private async Task KeyboardTimerCallback()
195
189
await Dispatcher . BeginInvoke ( _updateKeyboardLayouts , IsDragging ? DispatcherPriority . Background : DispatcherPriority . Render ) ;
196
190
}
197
191
198
- public async Task Initialize ( )
192
+ private async void LightingStateManagerOnEventAdded ( object ? sender , EventArgs e )
199
193
{
200
194
await GenerateProfileStack ( ) ;
201
195
}
@@ -250,20 +244,29 @@ private async void KbLayout_KeyboardLayoutUpdated(object? sender)
250
244
251
245
private async void Window_Loaded ( object ? sender , RoutedEventArgs e )
252
246
{
247
+ if ( ! IsVisible )
248
+ {
249
+ return ;
250
+ }
251
+
253
252
( await _layoutManager ) . KeyboardLayoutUpdated += KbLayout_KeyboardLayoutUpdated ;
253
+ ( await _lightingStateManager ) . EventAdded += LightingStateManagerOnEventAdded ;
254
254
255
255
var handle = new WindowInteropHelper ( this ) . Handle ;
256
256
// Subclass the window to intercept messages
257
257
var source = HwndSource . FromHwnd ( handle ) ;
258
258
source ? . AddHook ( WndProcDrag ) ;
259
259
260
+ _virtualKeyboardTimer . Start ( ) ;
261
+
260
262
KeyboardRecordMessage . Visibility = Visibility . Hidden ;
261
263
262
- _currentColor = SimpleColor . Transparent ;
264
+ _currentColor = SimpleColor . Black ;
263
265
264
266
var keyboardLayoutManager = await _layoutManager ;
265
267
var virtualKb = await keyboardLayoutManager . VirtualKeyboard ;
266
268
269
+ KeyboardGrid . Children . Clear ( ) ;
267
270
KeyboardGrid . Children . Add ( virtualKb ) ;
268
271
KeyboardGrid . Children . Add ( new LayerEditor ( ) ) ;
269
272
@@ -275,16 +278,15 @@ private async void Window_Loaded(object? sender, RoutedEventArgs e)
275
278
KeyboardViewbox . MaxHeight = virtualKb . Height + 30 ;
276
279
KeyboardViewbox . UpdateLayout ( ) ;
277
280
278
- UpdateManagerStackFocus ( ctrlLayerManager ) ;
279
-
281
+ await GenerateProfileStack ( ) ;
282
+ UpdateManagerStackFocus ( ctrlLayerManager , true ) ;
280
283
foreach ( Image child in profiles_stack . Children )
281
284
{
282
285
if ( child . Visibility != Visibility . Visible ) continue ;
283
286
ProfileImage_MouseDown ( child , null ) ;
284
287
break ;
285
288
}
286
289
287
- _virtualKeyboardTimer . Start ( ) ;
288
290
return ;
289
291
290
292
IntPtr WndProcDrag ( IntPtr winHandle , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
@@ -310,6 +312,7 @@ private async void Window_Unloaded(object sender, RoutedEventArgs e)
310
312
_virtualKeyboardTimer . Stop ( ) ;
311
313
312
314
( await _layoutManager ) . KeyboardLayoutUpdated -= KbLayout_KeyboardLayoutUpdated ;
315
+ ( await _lightingStateManager ) . EventAdded -= LightingStateManagerOnEventAdded ;
313
316
314
317
KeyboardGrid . Children . Clear ( ) ;
315
318
}
@@ -326,7 +329,10 @@ private void virtual_keyboard_timer_Tick(object? sender, EventArgs e)
326
329
327
330
private async void Window_Closing ( object ? sender , CancelEventArgs e )
328
331
{
329
- FocusedApplication ? . SaveAll ( ) ;
332
+ if ( FocusedApplication != null )
333
+ {
334
+ await FocusedApplication . SaveAll ( ) ;
335
+ }
330
336
331
337
switch ( Global . Configuration . CloseMode )
332
338
{
@@ -375,77 +381,67 @@ private void Window_Activated(object? sender, EventArgs e)
375
381
_lastActivated = DateTime . UtcNow ;
376
382
}
377
383
378
- private readonly Image _profileAdd = new ( )
379
- {
380
- Source = new BitmapImage ( new Uri ( @"Resources/addprofile_icon.png" , UriKind . Relative ) ) ,
381
- ToolTip = "Add a new Lighting Profile" ,
382
- Margin = new Thickness ( 0 , 5 , 0 , 0 )
383
- } ;
384
-
385
- private readonly Image _profileHidden ;
386
-
387
384
private readonly BitmapImage _visible = new ( new Uri ( @"Resources/Visible.png" , UriKind . Relative ) ) ;
388
385
private readonly BitmapImage _notVisible = new ( new Uri ( @"Resources/Not Visible.png" , UriKind . Relative ) ) ;
389
386
390
387
private async Task GenerateProfileStack ( string ? focusedKey = null )
391
388
{
392
389
profiles_stack . Children . Clear ( ) ;
393
390
391
+ var focusedSetTaskCompletion = new TaskCompletionSource ( ) ;
392
+
394
393
var lightingStateManager = await _lightingStateManager ;
395
- foreach ( var application in Global . Configuration . ProfileOrder
396
- . Where ( profileName => lightingStateManager . Events . ContainsKey ( profileName ) )
397
- . Select ( profileName => ( Application ) lightingStateManager . Events [ profileName ] )
398
- . OrderBy ( item => item . Settings . Hidden ? 1 : 0 ) )
399
- {
400
- if ( application is GenericApplication )
394
+ var profileLoadTasks = Global . Configuration . ProfileOrder
395
+ . Where ( profileName => lightingStateManager . Events . ContainsKey ( profileName ) )
396
+ . Select ( profileName => ( Application ) lightingStateManager . Events [ profileName ] )
397
+ . OrderBy ( item => item . Settings is { Hidden : false } ? 0 : 1 )
398
+ . Select ( application =>
401
399
{
402
- await Dispatcher . BeginInvoke ( ( ) => { CreateInsertGenericApplication ( focusedKey , application ) ; } , DispatcherPriority . Loaded ) ;
403
- }
404
- else
405
- {
406
- await Dispatcher . BeginInvoke ( ( ) =>
400
+ if ( application is GenericApplication )
401
+ {
402
+ return Dispatcher . BeginInvoke ( ( ) => { CreateInsertGenericApplication ( focusedKey , application ) ; } , DispatcherPriority . Loaded ) ;
403
+ }
404
+
405
+ return Dispatcher . BeginInvoke ( ( ) =>
407
406
{
408
407
var profileImage = new Image
409
408
{
410
409
Tag = application ,
411
410
Source = application . Icon ,
412
411
ToolTip = application . Config . Name + " Settings" ,
413
412
Margin = new Thickness ( 0 , 5 , 0 , 0 ) ,
414
- Visibility = application . Settings . Hidden ? Visibility . Collapsed : Visibility . Visible
413
+ Visibility = application . Settings is { Hidden : false } ? Visibility . Visible : Visibility . Collapsed ,
415
414
} ;
416
415
profileImage . MouseDown += ProfileImage_MouseDown ;
417
416
profiles_stack . Children . Add ( profileImage ) ;
418
417
419
418
if ( ! application . Config . ID . Equals ( focusedKey ) ) return ;
420
419
FocusedApplication = application ;
421
420
TransitionToProfile ( profileImage ) ;
421
+ focusedSetTaskCompletion . TrySetResult ( ) ;
422
422
} , DispatcherPriority . Loaded ) ;
423
- }
424
- }
423
+ } ) . Select ( x => x . Task ) ;
425
424
426
- //Add new profiles button
427
- _profileAdd . MouseDown -= AddProfile_MouseDown ;
428
- _profileAdd . MouseDown += AddProfile_MouseDown ;
429
- profiles_stack . Children . Add ( _profileAdd ) ;
430
- profiles_stack . Children . Add ( _profileHidden ) ;
425
+ var allCompleted = Task . WhenAll ( profileLoadTasks ) ;
426
+ await Task . WhenAny ( allCompleted , focusedSetTaskCompletion . Task ) ;
431
427
}
432
428
433
429
private void CreateInsertGenericApplication ( string ? focusedKey , Application application )
434
430
{
435
- var settings = ( GenericApplicationSettings ) application . Settings ;
431
+ var settings = application . Settings as GenericApplicationSettings ;
436
432
var profileImage = new Image
437
433
{
438
434
Tag = application ,
439
435
Source = application . Icon ,
440
- ToolTip = settings . ApplicationName + " Settings" ,
436
+ ToolTip = ( settings ? . ApplicationName ?? "" ) + " Settings" ,
441
437
Margin = new Thickness ( 0 , 0 , 0 , 5 )
442
438
} ;
443
439
profileImage . MouseDown += ProfileImage_MouseDown ;
444
440
445
441
var profileRemove = new Image
446
442
{
447
443
Source = new BitmapImage ( new Uri ( @"Resources/removeprofile_icon.png" , UriKind . Relative ) ) ,
448
- ToolTip = $ "Remove { settings . ApplicationName } Profile",
444
+ ToolTip = $ "Remove { ( settings ? . ApplicationName ?? "" ) } Profile",
449
445
HorizontalAlignment = HorizontalAlignment . Right ,
450
446
VerticalAlignment = VerticalAlignment . Bottom ,
451
447
Height = 16 ,
@@ -482,7 +478,7 @@ private void HiddenProfile_MouseDown(object? sender, EventArgs e)
482
478
483
479
private void ShowHiddenChanged ( bool value )
484
480
{
485
- _profileHidden . Source = value ? _visible : _notVisible ;
481
+ ProfileHidden . Source = value ? _visible : _notVisible ;
486
482
487
483
foreach ( FrameworkElement ctrl in profiles_stack . Children )
488
484
{
@@ -621,7 +617,7 @@ private async void AddProfile_MouseDown(object? sender, MouseButtonEventArgs e)
621
617
}
622
618
623
619
var genAppPm = new GenericApplication ( filename ) ;
624
- genAppPm . Initialize ( ) ;
620
+ await genAppPm . Initialize ( CancellationToken . None ) ;
625
621
( ( GenericApplicationSettings ) genAppPm . Settings ) . ApplicationName = Path . GetFileNameWithoutExtension ( filename ) ;
626
622
627
623
var ico = System . Drawing . Icon . ExtractAssociatedIcon ( dialog . ChosenExecutablePath . ToLowerInvariant ( ) ) ;
@@ -637,7 +633,7 @@ private async void AddProfile_MouseDown(object? sender, MouseButtonEventArgs e)
637
633
638
634
lightingStateManager . RegisterEvent ( genAppPm ) ;
639
635
ConfigManager . Save ( Global . Configuration ) ;
640
- GenerateProfileStack ( filename ) ;
636
+ await GenerateProfileStack ( filename ) ;
641
637
}
642
638
643
639
private void DesktopControl_MouseLeftButtonDown ( object ? sender , MouseButtonEventArgs e )
0 commit comments