-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5113737
commit 2a95d6c
Showing
9 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
src/OneWare.ApplicationCommands/Serialization/HotkeySerializer.cs
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/OneWare.ApplicationCommands/Serialization/KeyConfigItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Avalonia.Input; | ||
|
||
namespace OneWare.ApplicationCommands.Serialization; | ||
|
||
public class KeyConfigItem(string command, Key key, KeyModifiers keyModifiers) | ||
{ | ||
public string Command { get; init; } = command; | ||
|
||
public Key Key { get; } = key; | ||
|
||
public KeyModifiers KeyModifiers { get; } = keyModifiers; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/OneWare.ApplicationCommands/Serialization/KeyConfigSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Text.Json; | ||
using Avalonia.Input; | ||
using OneWare.SDK.Models; | ||
using OneWare.SDK.Services; | ||
using Prism.Ioc; | ||
|
||
namespace OneWare.ApplicationCommands.Serialization; | ||
|
||
public static class KeyConfigSerializer | ||
{ | ||
private static readonly JsonSerializerOptions Options = new() | ||
{ | ||
WriteIndented = true, | ||
PropertyNameCaseInsensitive = true, | ||
}; | ||
|
||
public static void SaveHotkeys(string path, IList<IApplicationCommand> commands) | ||
{ | ||
var ser = commands.Where(x => x.ActiveGesture != null && x.ActiveGesture != x.DefaultGesture) | ||
.Select(x => new KeyConfigItem(x.Name, x.ActiveGesture!.Key, x.ActiveGesture.KeyModifiers)) | ||
.ToArray(); | ||
|
||
try | ||
{ | ||
using var stream = File.OpenWrite(path); | ||
JsonSerializer.Serialize(stream, ser, Options); | ||
} | ||
catch (Exception e) | ||
{ | ||
ContainerLocator.Container.Resolve<ILogger>().Error(e.Message, e); | ||
} | ||
} | ||
|
||
public static void LoadHotkeys(string path, IList<IApplicationCommand> commands) | ||
{ | ||
if (!File.Exists(path)) return; | ||
|
||
try | ||
{ | ||
using var stream = File.OpenRead(path); | ||
var hotkeys = JsonSerializer.Deserialize<KeyConfigItem[]>(stream, Options); | ||
|
||
if (hotkeys == null) throw new Exception("Could not load Hotkey json"); | ||
|
||
foreach (var hotkey in hotkeys) | ||
{ | ||
var selected = commands.FirstOrDefault(x => x.Name == hotkey.Command); | ||
if (selected != null) | ||
{ | ||
selected.ActiveGesture = new KeyGesture(hotkey.Key, hotkey.KeyModifiers); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
ContainerLocator.Container.Resolve<ILogger>().Error(e.Message, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters