Skip to content

Commit 7acde57

Browse files
committed
Encapsulate the config's Hotkeys dict
Exposing a mutable collection as a property never ends well. Instead, keep the collection private and write methods for Add, Remove and Enumerate. Fixes cases where hotkey changes were not getting written to disk.
1 parent 3d25834 commit 7acde57

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

ConfigService/Configurations/EDDIConfiguration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Windows;
45

56
namespace EddiConfigService.Configurations
@@ -45,8 +46,11 @@ public class EDDIConfiguration : Config
4546

4647
// Hotkeys
4748

48-
[ JsonProperty( "Hotkeys" ) ]
49-
public Dictionary<string, string> Hotkeys { get; set; } = new Dictionary<string, string>();
49+
[JsonProperty( "Hotkeys" )]
50+
private ImmutableDictionary<string, string> Hotkeys = ImmutableDictionary<string, string>.Empty;
51+
public ImmutableDictionary<string, string> GetHotkeysCopy () => Hotkeys;
52+
public void AddHotkey ( string name, string gesture ) => Hotkeys = Hotkeys.Add( name, gesture );
53+
public void RemoveHotkey ( string name ) => Hotkeys = Hotkeys.Remove( name );
5054

5155
// Default
5256
public EDDIConfiguration()

EddiCore/Hotkeys/HotkeyManager.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using EddiConfigService;
2-
using EddiSpeechService;
1+
using EddiSpeechService;
32
using System;
43
using System.Collections.Generic;
54
using System.Runtime.CompilerServices;
@@ -32,8 +31,6 @@ public class HotkeyManager
3231

3332
public void SetHandle ( IntPtr newHandle )
3433
{
35-
ConfigService.Instance.eddiConfiguration.Hotkeys = ConfigService.Instance.eddiConfiguration.Hotkeys ??
36-
new Dictionary<string, string>();
3734
Hotkeys?.UnregisterAll();
3835
Hotkeys = new HotkeyRegistration( newHandle, new HotkeyActionCollection( HotkeyActions ) );
3936
Hotkeys.RegisterAll();

EddiCore/Hotkeys/HotkeyRegistration.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public HotkeyRegistration ( IntPtr handle, HotkeyActionCollection collection )
2424

2525
internal void RegisterAll ()
2626
{
27-
foreach ( var configuredHotkey in ConfigService.Instance.eddiConfiguration.Hotkeys )
27+
foreach ( var configuredHotkey in ConfigService.Instance.eddiConfiguration.GetHotkeysCopy() )
2828
{
2929
if ( Collection.TryGetValue( configuredHotkey.Key, out var hotkeyAction ) )
3030
{
@@ -40,8 +40,7 @@ public void RegisterHotkey ( string name, KeyGesture keyGesture )
4040
if ( TryRegisterHotkey( name, keyGesture, out var id ) )
4141
{
4242
Collection.AddGesture( name, keyGesture, id );
43-
ConfigService.Instance.eddiConfiguration.Hotkeys.Add( name,
44-
HotkeyConverter.ToString( keyGesture ) );
43+
ConfigService.Instance.eddiConfiguration.AddHotkey( name, HotkeyConverter.ToString( keyGesture ) );
4544
}
4645
else
4746
{
@@ -95,7 +94,7 @@ public void UnregisterHotkey ( string name )
9594
if ( TryUnregisterHotkey( name ) )
9695
{
9796
Collection.RemoveKeyGestures( name );
98-
ConfigService.Instance.eddiConfiguration.Hotkeys.Remove( name );
97+
ConfigService.Instance.eddiConfiguration.RemoveHotkey( name );
9998
}
10099
}
101100

0 commit comments

Comments
 (0)