Skip to content

Commit 39a8228

Browse files
committed
async profiles loading, more async overload usages
1 parent c050463 commit 39a8228

16 files changed

+347
-259
lines changed

Project-Aurora/Project-Aurora/AuroraApp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private async Task<ConfigUi> CreateWindow()
173173
Global.logger.Debug("new ConfigUI() took {Elapsed} milliseconds", stopwatch.ElapsedMilliseconds);
174174

175175
stopwatch.Restart();
176-
await configUi.Initialize();
176+
177177
Global.logger.Debug("configUi.Initialize() took {Elapsed} milliseconds", stopwatch.ElapsedMilliseconds);
178178
stopwatch.Stop();
179179

Project-Aurora/Project-Aurora/ConfigUi.xaml

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@
3232
<DockPanel x:Name="bg_grid" Background="#AA660000" IsHitTestVisible="True">
3333
<Grid Width="Auto" Height="25" Background="#10000000" DockPanel.Dock="Top"></Grid>
3434
<Grid HorizontalAlignment="Left" Width="56" Background="#3F000000" DockPanel.Dock="Left">
35-
<ScrollViewer HorizontalAlignment="Left" Width="56" UseLayoutRounding="False" CanContentScroll="True"
35+
<Grid.RowDefinitions>
36+
<RowDefinition Height="*" />
37+
<RowDefinition Height="Auto" />
38+
</Grid.RowDefinitions>
39+
<ScrollViewer HorizontalAlignment="Left" Width="56" Height="Auto" UseLayoutRounding="False" CanContentScroll="True"
3640
PanningMode="VerticalOnly"
37-
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="0,0,0,40" Padding="0,0,6,0">
41+
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0,0,6,0">
3842
<StackPanel x:Name="profiles_stack" RenderTransformOrigin="0.5,0" MinHeight="542" MaxHeight="542" MinWidth="40" MaxWidth="40"
3943
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="6,0,0,0">
4044
<StackPanel.ContextMenu>
@@ -47,20 +51,25 @@
4751
</StackPanel.ContextMenu>
4852
</StackPanel>
4953
</ScrollViewer>
50-
<Image Source="Resources/settings_icon.png" ToolTip="General Settings" Margin="6,0,10,0"
51-
MouseLeftButtonDown="DesktopControl_MouseLeftButtonDown" RenderTransformOrigin="0.5,0.5" Height="40" VerticalAlignment="Bottom" HorizontalAlignment="Center">
52-
<Image.ContextMenu>
53-
<ContextMenu Background="#FF181818" HorizontalContentAlignment="Center">
54-
<ContextMenu.ItemsPanel>
55-
<ItemsPanelTemplate>
56-
<StackPanel Background="#FF181818" />
57-
</ItemsPanelTemplate>
58-
</ContextMenu.ItemsPanel>
59-
<MenuItem Header="Open Bitmap Window" Click="cmbtnOpenBitmapWindow_Clicked" Foreground="White"/>
60-
<MenuItem Header="Open GSI/HTTP Window" Click="cmbtnOpenHttpDebugWindow_Clicked" Foreground="White"/>
61-
</ContextMenu>
62-
</Image.ContextMenu>
63-
</Image>
54+
<StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="40">
55+
<Separator />
56+
<Image Name="ProfileHidden" Source="Resources/Not Visible.png"></Image>
57+
<Image Name="ProfileAdd" Source="Resources/addprofile_icon.png" ToolTip="Add a new Lighting Profile" VerticalAlignment="Bottom" HorizontalAlignment="Center" />
58+
<Image Source="Resources/settings_icon.png" ToolTip="General Settings"
59+
MouseLeftButtonDown="DesktopControl_MouseLeftButtonDown" VerticalAlignment="Bottom" HorizontalAlignment="Center">
60+
<Image.ContextMenu>
61+
<ContextMenu Background="#FF181818" HorizontalContentAlignment="Center">
62+
<ContextMenu.ItemsPanel>
63+
<ItemsPanelTemplate>
64+
<StackPanel Background="#FF181818" />
65+
</ItemsPanelTemplate>
66+
</ContextMenu.ItemsPanel>
67+
<MenuItem Header="Open Bitmap Window" Click="cmbtnOpenBitmapWindow_Clicked" Foreground="White"/>
68+
<MenuItem Header="Open GSI/HTTP Window" Click="cmbtnOpenHttpDebugWindow_Clicked" Foreground="White"/>
69+
</ContextMenu>
70+
</Image.ContextMenu>
71+
</Image>
72+
</StackPanel>
6473
</Grid>
6574

6675
<Grid x:Name="gridManagers" Margin="5,5,0,5" HorizontalAlignment="Left" Width="200" DockPanel.Dock="Left">

Project-Aurora/Project-Aurora/ConfigUi.xaml.cs

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,8 @@ public ConfigUi(Task<ChromaReader?> rzSdkManager, Task<PluginManager> pluginMana
146146
_keyboardTimerCallback = KeyboardTimerCallback;
147147
_virtualKeyboardTimer.Elapsed += virtual_keyboard_timer_Tick;
148148

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;
157151
}
158152

159153
private async Task KeyboardTimerCallback()
@@ -195,7 +189,7 @@ private async Task KeyboardTimerCallback()
195189
await Dispatcher.BeginInvoke(_updateKeyboardLayouts, IsDragging ? DispatcherPriority.Background : DispatcherPriority.Render);
196190
}
197191

198-
public async Task Initialize()
192+
private async void LightingStateManagerOnEventAdded(object? sender, EventArgs e)
199193
{
200194
await GenerateProfileStack();
201195
}
@@ -250,20 +244,29 @@ private async void KbLayout_KeyboardLayoutUpdated(object? sender)
250244

251245
private async void Window_Loaded(object? sender, RoutedEventArgs e)
252246
{
247+
if (!IsVisible)
248+
{
249+
return;
250+
}
251+
253252
(await _layoutManager).KeyboardLayoutUpdated += KbLayout_KeyboardLayoutUpdated;
253+
(await _lightingStateManager).EventAdded += LightingStateManagerOnEventAdded;
254254

255255
var handle = new WindowInteropHelper(this).Handle;
256256
// Subclass the window to intercept messages
257257
var source = HwndSource.FromHwnd(handle);
258258
source?.AddHook(WndProcDrag);
259259

260+
_virtualKeyboardTimer.Start();
261+
260262
KeyboardRecordMessage.Visibility = Visibility.Hidden;
261263

262-
_currentColor = SimpleColor.Transparent;
264+
_currentColor = SimpleColor.Black;
263265

264266
var keyboardLayoutManager = await _layoutManager;
265267
var virtualKb = await keyboardLayoutManager.VirtualKeyboard;
266268

269+
KeyboardGrid.Children.Clear();
267270
KeyboardGrid.Children.Add(virtualKb);
268271
KeyboardGrid.Children.Add(new LayerEditor());
269272

@@ -275,16 +278,15 @@ private async void Window_Loaded(object? sender, RoutedEventArgs e)
275278
KeyboardViewbox.MaxHeight = virtualKb.Height + 30;
276279
KeyboardViewbox.UpdateLayout();
277280

278-
UpdateManagerStackFocus(ctrlLayerManager);
279-
281+
await GenerateProfileStack();
282+
UpdateManagerStackFocus(ctrlLayerManager, true);
280283
foreach (Image child in profiles_stack.Children)
281284
{
282285
if (child.Visibility != Visibility.Visible) continue;
283286
ProfileImage_MouseDown(child, null);
284287
break;
285288
}
286289

287-
_virtualKeyboardTimer.Start();
288290
return;
289291

290292
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)
310312
_virtualKeyboardTimer.Stop();
311313

312314
(await _layoutManager).KeyboardLayoutUpdated -= KbLayout_KeyboardLayoutUpdated;
315+
(await _lightingStateManager).EventAdded -= LightingStateManagerOnEventAdded;
313316

314317
KeyboardGrid.Children.Clear();
315318
}
@@ -326,7 +329,10 @@ private void virtual_keyboard_timer_Tick(object? sender, EventArgs e)
326329

327330
private async void Window_Closing(object? sender, CancelEventArgs e)
328331
{
329-
FocusedApplication?.SaveAll();
332+
if (FocusedApplication != null)
333+
{
334+
await FocusedApplication.SaveAll();
335+
}
330336

331337
switch (Global.Configuration.CloseMode)
332338
{
@@ -375,77 +381,67 @@ private void Window_Activated(object? sender, EventArgs e)
375381
_lastActivated = DateTime.UtcNow;
376382
}
377383

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-
387384
private readonly BitmapImage _visible = new(new Uri(@"Resources/Visible.png", UriKind.Relative));
388385
private readonly BitmapImage _notVisible = new(new Uri(@"Resources/Not Visible.png", UriKind.Relative));
389386

390387
private async Task GenerateProfileStack(string? focusedKey = null)
391388
{
392389
profiles_stack.Children.Clear();
393390

391+
var focusedSetTaskCompletion = new TaskCompletionSource();
392+
394393
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 =>
401399
{
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(() =>
407406
{
408407
var profileImage = new Image
409408
{
410409
Tag = application,
411410
Source = application.Icon,
412411
ToolTip = application.Config.Name + " Settings",
413412
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,
415414
};
416415
profileImage.MouseDown += ProfileImage_MouseDown;
417416
profiles_stack.Children.Add(profileImage);
418417

419418
if (!application.Config.ID.Equals(focusedKey)) return;
420419
FocusedApplication = application;
421420
TransitionToProfile(profileImage);
421+
focusedSetTaskCompletion.TrySetResult();
422422
}, DispatcherPriority.Loaded);
423-
}
424-
}
423+
}).Select(x => x.Task);
425424

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);
431427
}
432428

433429
private void CreateInsertGenericApplication(string? focusedKey, Application application)
434430
{
435-
var settings = (GenericApplicationSettings)application.Settings;
431+
var settings = application.Settings as GenericApplicationSettings;
436432
var profileImage = new Image
437433
{
438434
Tag = application,
439435
Source = application.Icon,
440-
ToolTip = settings.ApplicationName + " Settings",
436+
ToolTip = (settings?.ApplicationName ?? "") + " Settings",
441437
Margin = new Thickness(0, 0, 0, 5)
442438
};
443439
profileImage.MouseDown += ProfileImage_MouseDown;
444440

445441
var profileRemove = new Image
446442
{
447443
Source = new BitmapImage(new Uri(@"Resources/removeprofile_icon.png", UriKind.Relative)),
448-
ToolTip = $"Remove {settings.ApplicationName} Profile",
444+
ToolTip = $"Remove {(settings?.ApplicationName ?? "")} Profile",
449445
HorizontalAlignment = HorizontalAlignment.Right,
450446
VerticalAlignment = VerticalAlignment.Bottom,
451447
Height = 16,
@@ -482,7 +478,7 @@ private void HiddenProfile_MouseDown(object? sender, EventArgs e)
482478

483479
private void ShowHiddenChanged(bool value)
484480
{
485-
_profileHidden.Source = value ? _visible : _notVisible;
481+
ProfileHidden.Source = value ? _visible : _notVisible;
486482

487483
foreach (FrameworkElement ctrl in profiles_stack.Children)
488484
{
@@ -621,7 +617,7 @@ private async void AddProfile_MouseDown(object? sender, MouseButtonEventArgs e)
621617
}
622618

623619
var genAppPm = new GenericApplication(filename);
624-
genAppPm.Initialize();
620+
await genAppPm.Initialize(CancellationToken.None);
625621
((GenericApplicationSettings)genAppPm.Settings).ApplicationName = Path.GetFileNameWithoutExtension(filename);
626622

627623
var ico = System.Drawing.Icon.ExtractAssociatedIcon(dialog.ChosenExecutablePath.ToLowerInvariant());
@@ -637,7 +633,7 @@ private async void AddProfile_MouseDown(object? sender, MouseButtonEventArgs e)
637633

638634
lightingStateManager.RegisterEvent(genAppPm);
639635
ConfigManager.Save(Global.Configuration);
640-
GenerateProfileStack(filename);
636+
await GenerateProfileStack(filename);
641637
}
642638

643639
private void DesktopControl_MouseLeftButtonDown(object? sender, MouseButtonEventArgs e)

Project-Aurora/Project-Aurora/Modules/LightingStateManagerModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected override async Task Initialize()
4545
{
4646
httpListener1.NewGameState += lightingStateManager.GameStateUpdate;
4747
}
48+
Global.logger.Information("Loaded Applications");
4849
await lightingStateManager.InitUpdate();
4950
}
5051

Project-Aurora/Project-Aurora/Modules/PluginsModule.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System.Threading;
2+
using System.Threading.Tasks;
23
using AuroraRgb.Settings;
34
using Lombok.NET;
45

@@ -16,11 +17,11 @@ protected override async Task Initialize()
1617
{
1718
Global.logger.Information("Loading Plugins");
1819
_pluginManager = new PluginManager();
19-
_pluginManager.Initialize();
20+
await _pluginManager.Initialize(CancellationToken.None);
2021
_pluginManagerSource.SetResult(_pluginManager);
2122
Global.logger.Information("Loaded Plugins");
2223
}
23-
24+
2425
[Async]
2526
public override void Dispose()
2627
{

0 commit comments

Comments
 (0)