Skip to content

Commit 5021f20

Browse files
author
Aytackydln
committed
profile priority mechanism for profile processes
1 parent 8fad330 commit 5021f20

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
namespace AuroraRgb.Profiles;
4+
5+
public class ApplicationPriorityComparer : IComparer<Application>
6+
{
7+
public int Compare(Application? x, Application? y)
8+
{
9+
if (x == null && y == null)
10+
return 0;
11+
if (x == null)
12+
return 1;
13+
if (y == null)
14+
return -1;
15+
16+
// Compare by EnableByDefault first
17+
if (x.Config.EnableByDefault && !y.Config.EnableByDefault)
18+
return -1;
19+
if (!x.Config.EnableByDefault && y.Config.EnableByDefault)
20+
return 1;
21+
22+
// Then compare by Priority
23+
return Comparer<int>.Default.Compare(y.Config.Priority, x.Config.Priority);
24+
}
25+
}

Project-Aurora/Project-Aurora/Profiles/GenericGames/GenericGamesApplication.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public sealed class GenericGamesApplication() : Application(new LightEventConfig
1717
GameStateType = typeof(GameState_Wrapper),
1818
IconURI = "Resources/controller-icon.png",
1919
EnableByDefault = false,
20+
Priority = -10,
2021
})
2122
{
2223
public override async Task<bool> Initialize(CancellationToken cancellationToken)

Project-Aurora/Project-Aurora/Profiles/LightEventConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public string[] ProcessNames
5555

5656
public bool EnableByDefault { get; init; } = true;
5757
public bool EnableOverlaysByDefault { get; set; } = true;
58+
public int Priority { get; init; }
5859

5960
public event PropertyChangedEventHandler? PropertyChanged;
6061

Project-Aurora/Project-Aurora/Profiles/LightingStateManager.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public sealed class LightingStateManager : IDisposable
5353
private readonly HashSet<ILightEvent> _startedEvents = [];
5454
private readonly HashSet<ILightEvent> _updatedEvents = [];
5555

56-
// TODO convert to Dictionary of SortedList with profile priority as key
57-
private Dictionary<string, Application> EventProcesses { get; } = new();
56+
private Dictionary<string, SortedSet<Application>> EventProcesses { get; } = new();
5857
private Dictionary<Regex, Application> EventTitles { get; } = new();
5958
private Dictionary<string, Application> EventAppIDs { get; } = new();
6059
public Dictionary<Type, LayerHandlerMeta> LayerHandlers { get; } = new();
@@ -233,17 +232,25 @@ public async Task RegisterEvent(Application application)
233232

234233
foreach (var exe in application.Config.ProcessNames)
235234
{
236-
EventProcesses[exe.ToLower()] = application;
235+
if (!EventProcesses.TryGetValue(exe, out var applicationList))
236+
{
237+
applicationList = new SortedSet<Application>(new ApplicationPriorityComparer());
238+
EventProcesses[exe] = applicationList;
239+
}
240+
applicationList.Add(application);
237241
}
238242

239243
application.Config.ProcessNamesChanged += (_, _) =>
240244
{
241245
var keysToRemove = new List<string>();
242-
foreach (var (s, value) in EventProcesses)
246+
foreach (var (s, applications) in EventProcesses)
243247
{
244-
if (value.Config.ID == profileId)
248+
foreach (var app in applications)
245249
{
246-
keysToRemove.Add(s);
250+
if (app.Config.ID == profileId)
251+
{
252+
keysToRemove.Add(s);
253+
}
247254
}
248255
}
249256

@@ -254,8 +261,14 @@ public async Task RegisterEvent(Application application)
254261

255262
foreach (var exe in application.Config.ProcessNames)
256263
{
257-
if (!exe.Equals(profileId))
258-
EventProcesses.TryAdd(exe.ToLower(), application);
264+
if (exe.Equals(profileId)) continue;
265+
var processKey = exe.ToLower();
266+
if (!EventProcesses.TryGetValue(processKey, out var applicationList))
267+
{
268+
applicationList = new SortedSet<Application>(new ApplicationPriorityComparer());
269+
EventProcesses[processKey] = applicationList;
270+
}
271+
applicationList.Add(application);
259272
}
260273
};
261274

@@ -318,7 +331,7 @@ public void RemoveGenericProfile(string key)
318331

319332
private Application? GetProfileFromProcessName(string process)
320333
{
321-
return EventProcesses.GetValueOrDefault(process);
334+
return EventProcesses.GetValueOrDefault(process)?.First();
322335
}
323336

324337
/// <summary>

0 commit comments

Comments
 (0)