45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using AcDream.UI.Abstractions.Input;
|
|
|
|
namespace AcDream.App.Settings;
|
|
|
|
internal interface IRuntimeKeyBindingTarget
|
|
{
|
|
void Apply(KeyBindings bindings);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applies a settings-panel keymap to the live dispatcher and then persists
|
|
/// that exact map. Persistence failure is reported without rolling back the
|
|
/// already accepted live binding, matching the former startup-root callback.
|
|
/// </summary>
|
|
internal sealed class RuntimeKeyBindingTarget : IRuntimeKeyBindingTarget
|
|
{
|
|
private readonly InputDispatcher _dispatcher;
|
|
private readonly string _path;
|
|
private readonly Action<string> _log;
|
|
|
|
public RuntimeKeyBindingTarget(
|
|
InputDispatcher dispatcher,
|
|
string? path = null,
|
|
Action<string>? log = null)
|
|
{
|
|
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
|
|
_path = path ?? KeyBindings.DefaultPath();
|
|
_log = log ?? Console.WriteLine;
|
|
}
|
|
|
|
public void Apply(KeyBindings bindings)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(bindings);
|
|
_dispatcher.SetBindings(bindings);
|
|
try
|
|
{
|
|
bindings.SaveToFile(_path);
|
|
_log($"keybinds: saved to {_path}");
|
|
}
|
|
catch (Exception failure)
|
|
{
|
|
_log($"keybinds: save failed: {failure.Message}");
|
|
}
|
|
}
|
|
}
|