Skip to content

Commit

Permalink
async profiles loading, more async overload usages
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Mar 31, 2024
1 parent c050463 commit 39a8228
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 259 deletions.
2 changes: 1 addition & 1 deletion Project-Aurora/Project-Aurora/AuroraApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private async Task<ConfigUi> CreateWindow()
Global.logger.Debug("new ConfigUI() took {Elapsed} milliseconds", stopwatch.ElapsedMilliseconds);

stopwatch.Restart();
await configUi.Initialize();

Global.logger.Debug("configUi.Initialize() took {Elapsed} milliseconds", stopwatch.ElapsedMilliseconds);
stopwatch.Stop();

Expand Down
41 changes: 25 additions & 16 deletions Project-Aurora/Project-Aurora/ConfigUi.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
<DockPanel x:Name="bg_grid" Background="#AA660000" IsHitTestVisible="True">
<Grid Width="Auto" Height="25" Background="#10000000" DockPanel.Dock="Top"></Grid>
<Grid HorizontalAlignment="Left" Width="56" Background="#3F000000" DockPanel.Dock="Left">
<ScrollViewer HorizontalAlignment="Left" Width="56" UseLayoutRounding="False" CanContentScroll="True"
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer HorizontalAlignment="Left" Width="56" Height="Auto" UseLayoutRounding="False" CanContentScroll="True"
PanningMode="VerticalOnly"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="0,0,0,40" Padding="0,0,6,0">
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0,0,6,0">
<StackPanel x:Name="profiles_stack" RenderTransformOrigin="0.5,0" MinHeight="542" MaxHeight="542" MinWidth="40" MaxWidth="40"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="6,0,0,0">
<StackPanel.ContextMenu>
Expand All @@ -47,20 +51,25 @@
</StackPanel.ContextMenu>
</StackPanel>
</ScrollViewer>
<Image Source="Resources/settings_icon.png" ToolTip="General Settings" Margin="6,0,10,0"
MouseLeftButtonDown="DesktopControl_MouseLeftButtonDown" RenderTransformOrigin="0.5,0.5" Height="40" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<Image.ContextMenu>
<ContextMenu Background="#FF181818" HorizontalContentAlignment="Center">
<ContextMenu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="#FF181818" />
</ItemsPanelTemplate>
</ContextMenu.ItemsPanel>
<MenuItem Header="Open Bitmap Window" Click="cmbtnOpenBitmapWindow_Clicked" Foreground="White"/>
<MenuItem Header="Open GSI/HTTP Window" Click="cmbtnOpenHttpDebugWindow_Clicked" Foreground="White"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
<StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="40">
<Separator />
<Image Name="ProfileHidden" Source="Resources/Not Visible.png"></Image>
<Image Name="ProfileAdd" Source="Resources/addprofile_icon.png" ToolTip="Add a new Lighting Profile" VerticalAlignment="Bottom" HorizontalAlignment="Center" />
<Image Source="Resources/settings_icon.png" ToolTip="General Settings"
MouseLeftButtonDown="DesktopControl_MouseLeftButtonDown" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<Image.ContextMenu>
<ContextMenu Background="#FF181818" HorizontalContentAlignment="Center">
<ContextMenu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="#FF181818" />
</ItemsPanelTemplate>
</ContextMenu.ItemsPanel>
<MenuItem Header="Open Bitmap Window" Click="cmbtnOpenBitmapWindow_Clicked" Foreground="White"/>
<MenuItem Header="Open GSI/HTTP Window" Click="cmbtnOpenHttpDebugWindow_Clicked" Foreground="White"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
</StackPanel>
</Grid>

<Grid x:Name="gridManagers" Margin="5,5,0,5" HorizontalAlignment="Left" Width="200" DockPanel.Dock="Left">
Expand Down
92 changes: 44 additions & 48 deletions Project-Aurora/Project-Aurora/ConfigUi.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,8 @@ public ConfigUi(Task<ChromaReader?> rzSdkManager, Task<PluginManager> pluginMana
_keyboardTimerCallback = KeyboardTimerCallback;
_virtualKeyboardTimer.Elapsed += virtual_keyboard_timer_Tick;

//Show hidden profiles button
_profileHidden = new Image
{
Source = _notVisible,
ToolTip = "Toggle Hidden profiles' visibility",
Margin = new Thickness(0, 5, 0, 0)
};
_profileHidden.MouseDown += HiddenProfile_MouseDown;
ProfileAdd.MouseDown += AddProfile_MouseDown;
ProfileHidden.MouseDown += HiddenProfile_MouseDown;
}

private async Task KeyboardTimerCallback()
Expand Down Expand Up @@ -195,7 +189,7 @@ private async Task KeyboardTimerCallback()
await Dispatcher.BeginInvoke(_updateKeyboardLayouts, IsDragging ? DispatcherPriority.Background : DispatcherPriority.Render);
}

public async Task Initialize()
private async void LightingStateManagerOnEventAdded(object? sender, EventArgs e)
{
await GenerateProfileStack();
}
Expand Down Expand Up @@ -250,20 +244,29 @@ private async void KbLayout_KeyboardLayoutUpdated(object? sender)

private async void Window_Loaded(object? sender, RoutedEventArgs e)
{
if (!IsVisible)
{
return;
}

(await _layoutManager).KeyboardLayoutUpdated += KbLayout_KeyboardLayoutUpdated;
(await _lightingStateManager).EventAdded += LightingStateManagerOnEventAdded;

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

_virtualKeyboardTimer.Start();

KeyboardRecordMessage.Visibility = Visibility.Hidden;

_currentColor = SimpleColor.Transparent;
_currentColor = SimpleColor.Black;

var keyboardLayoutManager = await _layoutManager;
var virtualKb = await keyboardLayoutManager.VirtualKeyboard;

KeyboardGrid.Children.Clear();
KeyboardGrid.Children.Add(virtualKb);
KeyboardGrid.Children.Add(new LayerEditor());

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

UpdateManagerStackFocus(ctrlLayerManager);

await GenerateProfileStack();
UpdateManagerStackFocus(ctrlLayerManager, true);
foreach (Image child in profiles_stack.Children)
{
if (child.Visibility != Visibility.Visible) continue;
ProfileImage_MouseDown(child, null);
break;
}

_virtualKeyboardTimer.Start();
return;

IntPtr WndProcDrag(IntPtr winHandle, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
Expand All @@ -310,6 +312,7 @@ private async void Window_Unloaded(object sender, RoutedEventArgs e)
_virtualKeyboardTimer.Stop();

(await _layoutManager).KeyboardLayoutUpdated -= KbLayout_KeyboardLayoutUpdated;
(await _lightingStateManager).EventAdded -= LightingStateManagerOnEventAdded;

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

private async void Window_Closing(object? sender, CancelEventArgs e)
{
FocusedApplication?.SaveAll();
if (FocusedApplication != null)
{
await FocusedApplication.SaveAll();
}

switch (Global.Configuration.CloseMode)
{
Expand Down Expand Up @@ -375,77 +381,67 @@ private void Window_Activated(object? sender, EventArgs e)
_lastActivated = DateTime.UtcNow;
}

private readonly Image _profileAdd = new()
{
Source = new BitmapImage(new Uri(@"Resources/addprofile_icon.png", UriKind.Relative)),
ToolTip = "Add a new Lighting Profile",
Margin = new Thickness(0, 5, 0, 0)
};

private readonly Image _profileHidden;

private readonly BitmapImage _visible = new(new Uri(@"Resources/Visible.png", UriKind.Relative));
private readonly BitmapImage _notVisible = new(new Uri(@"Resources/Not Visible.png", UriKind.Relative));

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

var focusedSetTaskCompletion = new TaskCompletionSource();

var lightingStateManager = await _lightingStateManager;
foreach (var application in Global.Configuration.ProfileOrder
.Where(profileName => lightingStateManager.Events.ContainsKey(profileName))
.Select(profileName => (Application)lightingStateManager.Events[profileName])
.OrderBy(item => item.Settings.Hidden ? 1 : 0))
{
if (application is GenericApplication)
var profileLoadTasks = Global.Configuration.ProfileOrder
.Where(profileName => lightingStateManager.Events.ContainsKey(profileName))
.Select(profileName => (Application)lightingStateManager.Events[profileName])
.OrderBy(item => item.Settings is { Hidden: false } ? 0 : 1)
.Select(application =>
{
await Dispatcher.BeginInvoke(() => { CreateInsertGenericApplication(focusedKey, application); }, DispatcherPriority.Loaded);
}
else
{
await Dispatcher.BeginInvoke(() =>
if (application is GenericApplication)
{
return Dispatcher.BeginInvoke(() => { CreateInsertGenericApplication(focusedKey, application); }, DispatcherPriority.Loaded);
}

return Dispatcher.BeginInvoke(() =>
{
var profileImage = new Image
{
Tag = application,
Source = application.Icon,
ToolTip = application.Config.Name + " Settings",
Margin = new Thickness(0, 5, 0, 0),
Visibility = application.Settings.Hidden ? Visibility.Collapsed : Visibility.Visible
Visibility = application.Settings is { Hidden: false } ? Visibility.Visible : Visibility.Collapsed,
};
profileImage.MouseDown += ProfileImage_MouseDown;
profiles_stack.Children.Add(profileImage);

if (!application.Config.ID.Equals(focusedKey)) return;
FocusedApplication = application;
TransitionToProfile(profileImage);
focusedSetTaskCompletion.TrySetResult();
}, DispatcherPriority.Loaded);
}
}
}).Select(x => x.Task);

//Add new profiles button
_profileAdd.MouseDown -= AddProfile_MouseDown;
_profileAdd.MouseDown += AddProfile_MouseDown;
profiles_stack.Children.Add(_profileAdd);
profiles_stack.Children.Add(_profileHidden);
var allCompleted = Task.WhenAll(profileLoadTasks);
await Task.WhenAny(allCompleted, focusedSetTaskCompletion.Task);
}

private void CreateInsertGenericApplication(string? focusedKey, Application application)
{
var settings = (GenericApplicationSettings)application.Settings;
var settings = application.Settings as GenericApplicationSettings;
var profileImage = new Image
{
Tag = application,
Source = application.Icon,
ToolTip = settings.ApplicationName + " Settings",
ToolTip = (settings?.ApplicationName ?? "") + " Settings",
Margin = new Thickness(0, 0, 0, 5)
};
profileImage.MouseDown += ProfileImage_MouseDown;

var profileRemove = new Image
{
Source = new BitmapImage(new Uri(@"Resources/removeprofile_icon.png", UriKind.Relative)),
ToolTip = $"Remove {settings.ApplicationName} Profile",
ToolTip = $"Remove {(settings?.ApplicationName ?? "")} Profile",
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Bottom,
Height = 16,
Expand Down Expand Up @@ -482,7 +478,7 @@ private void HiddenProfile_MouseDown(object? sender, EventArgs e)

private void ShowHiddenChanged(bool value)
{
_profileHidden.Source = value ? _visible : _notVisible;
ProfileHidden.Source = value ? _visible : _notVisible;

foreach (FrameworkElement ctrl in profiles_stack.Children)
{
Expand Down Expand Up @@ -621,7 +617,7 @@ private async void AddProfile_MouseDown(object? sender, MouseButtonEventArgs e)
}

var genAppPm = new GenericApplication(filename);
genAppPm.Initialize();
await genAppPm.Initialize(CancellationToken.None);
((GenericApplicationSettings)genAppPm.Settings).ApplicationName = Path.GetFileNameWithoutExtension(filename);

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

lightingStateManager.RegisterEvent(genAppPm);
ConfigManager.Save(Global.Configuration);
GenerateProfileStack(filename);
await GenerateProfileStack(filename);
}

private void DesktopControl_MouseLeftButtonDown(object? sender, MouseButtonEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected override async Task Initialize()
{
httpListener1.NewGameState += lightingStateManager.GameStateUpdate;
}
Global.logger.Information("Loaded Applications");
await lightingStateManager.InitUpdate();
}

Expand Down
7 changes: 4 additions & 3 deletions Project-Aurora/Project-Aurora/Modules/PluginsModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using AuroraRgb.Settings;
using Lombok.NET;

Expand All @@ -16,11 +17,11 @@ protected override async Task Initialize()
{
Global.logger.Information("Loading Plugins");
_pluginManager = new PluginManager();
_pluginManager.Initialize();
await _pluginManager.Initialize(CancellationToken.None);
_pluginManagerSource.SetResult(_pluginManager);
Global.logger.Information("Loaded Plugins");
}

[Async]
public override void Dispose()
{
Expand Down
Loading

0 comments on commit 39a8228

Please sign in to comment.