Skip to content

Commit

Permalink
save changes hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Dec 7, 2023
1 parent 5113737 commit 2a95d6c
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 19 deletions.
14 changes: 0 additions & 14 deletions src/OneWare.ApplicationCommands/Serialization/HotkeySerializer.cs

This file was deleted.

12 changes: 12 additions & 0 deletions src/OneWare.ApplicationCommands/Serialization/KeyConfigItem.cs
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;
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.LogicalTree;
using OneWare.ApplicationCommands.Serialization;
using OneWare.SDK.Models;
using OneWare.SDK.Services;

Expand All @@ -10,10 +11,14 @@ namespace OneWare.ApplicationCommands.Services;
public class ApplicationCommandService : IApplicationCommandService
{
public ObservableCollection<IApplicationCommand> ApplicationCommands { get; } = new();

private readonly string _keyConfigFile;

public ApplicationCommandService()
public ApplicationCommandService(IPaths paths)
{
InputElement.KeyDownEvent.AddClassHandler<TopLevel>(HandleKeyDown);

_keyConfigFile = Path.Combine(paths.AppDataDirectory, "keyConfig.json");
}

public void RegisterCommand(IApplicationCommand command)
Expand All @@ -38,4 +43,14 @@ private void HandleKeyDown(object? sender, KeyEventArgs args)
};
}
}

public void LoadKeyConfiguration()
{
KeyConfigSerializer.LoadHotkeys(_keyConfigFile, ApplicationCommands);
}

public void SaveKeyConfiguration()
{
KeyConfigSerializer.SaveHotkeys(_keyConfigFile, ApplicationCommands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ namespace OneWare.ApplicationCommands.ViewModels;
public partial class AssignGestureViewModel(IApplicationCommand command) : FlexibleWindowViewModelBase
{
public IApplicationCommand ApplicationCommand { get; } = command;

[ObservableProperty]

private KeyGesture? _capturedKeyGesture = command.ActiveGesture;
public KeyGesture? CapturedKeyGesture
{
get => _capturedKeyGesture;
set => SetProperty(ref _capturedKeyGesture, value);
}

public void Clear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class CommandManagerViewModel : FlexibleWindowViewModelBase

private readonly IProjectExplorerService _projectExplorerService;
private readonly IWindowService _windowService;
private readonly IApplicationCommandService _applicationCommandService;

public ILogical ActiveFocus { get; }
public ObservableCollection<CommandManagerTabBase> Tabs { get; } = new();
Expand All @@ -31,6 +32,7 @@ public CommandManagerViewModel(ILogical logical, IApplicationCommandService comm
{
ActiveFocus = logical;
_projectExplorerService = projectExplorerService;
_applicationCommandService = commandService;
_windowService = windowService;

Tabs.Add(new CommandManagerAllTab(logical)
Expand Down Expand Up @@ -88,6 +90,8 @@ await _windowService.ShowDialogAsync(new AssignGestureView()
DataContext = new AssignGestureViewModel(SelectedTab.SelectedItem.Command)
}, window.Host);

_applicationCommandService.SaveKeyConfiguration();

window.Host?.Activate();
window.CloseOnDeactivated = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/OneWare.Core/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
public override void OnFrameworkInitializationCompleted()
{
Container.Resolve<ISettingsService>().Load(Container.Resolve<IPaths>().SettingsPath);

Container.Resolve<IApplicationCommandService>().LoadKeyConfiguration();

Container.Resolve<ISettingsService>().GetSettingObservable<string>("General_SelectedTheme").Subscribe(x =>
{
TypeAssistanceIconStore.Instance.Load();
Expand Down
1 change: 0 additions & 1 deletion src/OneWare.SDK/Controls/HotkeyTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public HotkeyTextBox()
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
CapturedKeyGesture = null;
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Expand Down
2 changes: 2 additions & 0 deletions src/OneWare.SDK/Services/IApplicationCommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface IApplicationCommandService
{
public ObservableCollection<IApplicationCommand> ApplicationCommands { get; }
public void RegisterCommand(IApplicationCommand command);
public void LoadKeyConfiguration();
public void SaveKeyConfiguration();
}

0 comments on commit 2a95d6c

Please sign in to comment.