mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2026-06-05 11:59:15 +00:00
Persist observed physical key labels
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Ryujinx.Ava.Common.Locale;
|
using Ryujinx.Ava.Common.Locale;
|
||||||
using Ryujinx.Ava.Input;
|
using Ryujinx.Ava.Input;
|
||||||
|
using Ryujinx.Common.Configuration;
|
||||||
using Ryujinx.Common.Configuration.Hid;
|
using Ryujinx.Common.Configuration.Hid;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
using AvaPhysicalKey = Avalonia.Input.PhysicalKey;
|
using AvaPhysicalKey = Avalonia.Input.PhysicalKey;
|
||||||
using ConfigPhysicalKey = Ryujinx.Common.Configuration.Hid.PhysicalKey;
|
using ConfigPhysicalKey = Ryujinx.Common.Configuration.Hid.PhysicalKey;
|
||||||
using InputKey = Ryujinx.Input.Key;
|
using InputKey = Ryujinx.Input.Key;
|
||||||
@@ -12,11 +16,22 @@ namespace Ryujinx.Ava.UI.Helpers
|
|||||||
{
|
{
|
||||||
internal static class PhysicalKeyLabelHelper
|
internal static class PhysicalKeyLabelHelper
|
||||||
{
|
{
|
||||||
|
private const string ObservedLabelsFileName = "keyboard_layout_labels.json";
|
||||||
private static readonly ConcurrentDictionary<ConfigPhysicalKey, string> _observedLayoutLabels = new();
|
private static readonly ConcurrentDictionary<ConfigPhysicalKey, string> _observedLayoutLabels = new();
|
||||||
|
private static readonly object _observedLayoutLabelsLock = new();
|
||||||
|
private static readonly JsonSerializerOptions _serializerOptions = new()
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
AllowTrailingCommas = true,
|
||||||
|
ReadCommentHandling = JsonCommentHandling.Skip
|
||||||
|
};
|
||||||
|
private static bool _observedLayoutLabelsLoaded;
|
||||||
public static event Action LabelsChanged;
|
public static event Action LabelsChanged;
|
||||||
|
|
||||||
public static string GetDisplayString(ConfigPhysicalKey key)
|
public static string GetDisplayString(ConfigPhysicalKey key)
|
||||||
{
|
{
|
||||||
|
EnsureObservedLayoutLabelsLoaded();
|
||||||
|
|
||||||
if (KeyboardLayoutLocaleHelper.TryGetPhysicalLabel(key, out string localizedLabel))
|
if (KeyboardLayoutLocaleHelper.TryGetPhysicalLabel(key, out string localizedLabel))
|
||||||
{
|
{
|
||||||
return localizedLabel;
|
return localizedLabel;
|
||||||
@@ -37,6 +52,8 @@ namespace Ryujinx.Ava.UI.Helpers
|
|||||||
|
|
||||||
public static void ObserveKeyPress(object sender, KeyEventArgs args)
|
public static void ObserveKeyPress(object sender, KeyEventArgs args)
|
||||||
{
|
{
|
||||||
|
EnsureObservedLayoutLabelsLoaded();
|
||||||
|
|
||||||
if (args.KeyModifiers != KeyModifiers.None)
|
if (args.KeyModifiers != KeyModifiers.None)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -62,10 +79,80 @@ namespace Ryujinx.Ava.UI.Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
_observedLayoutLabels[physicalKey] = label;
|
_observedLayoutLabels[physicalKey] = label;
|
||||||
|
SaveObservedLayoutLabels();
|
||||||
LabelsChanged?.Invoke();
|
LabelsChanged?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void EnsureObservedLayoutLabelsLoaded()
|
||||||
|
{
|
||||||
|
if (_observedLayoutLabelsLoaded)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock (_observedLayoutLabelsLock)
|
||||||
|
{
|
||||||
|
if (_observedLayoutLabelsLoaded)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string labelsPath = GetObservedLabelsPath();
|
||||||
|
|
||||||
|
if (File.Exists(labelsPath))
|
||||||
|
{
|
||||||
|
Dictionary<string, string> labels = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(labelsPath), _serializerOptions);
|
||||||
|
|
||||||
|
if (labels != null)
|
||||||
|
{
|
||||||
|
foreach ((string key, string value) in labels)
|
||||||
|
{
|
||||||
|
if (Enum.TryParse(key, out ConfigPhysicalKey physicalKey) &&
|
||||||
|
!string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
_observedLayoutLabels[physicalKey] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
_observedLayoutLabelsLoaded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SaveObservedLayoutLabels()
|
||||||
|
{
|
||||||
|
lock (_observedLayoutLabelsLock)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Dictionary<string, string> labels = [];
|
||||||
|
|
||||||
|
foreach ((ConfigPhysicalKey key, string value) in _observedLayoutLabels)
|
||||||
|
{
|
||||||
|
labels[key.ToString()] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(GetObservedLabelsPath(), JsonSerializer.Serialize(labels, _serializerOptions));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetObservedLabelsPath()
|
||||||
|
{
|
||||||
|
return Path.Combine(AppDataManager.BaseDirPath, ObservedLabelsFileName);
|
||||||
|
}
|
||||||
|
|
||||||
private static bool TryGetFallbackPrintableKeyLabel(ConfigPhysicalKey key, out string label)
|
private static bool TryGetFallbackPrintableKeyLabel(ConfigPhysicalKey key, out string label)
|
||||||
{
|
{
|
||||||
// The legacy enum name for the ISO extra key is misleading, so give it a distinct physical label.
|
// The legacy enum name for the ISO extra key is misleading, so give it a distinct physical label.
|
||||||
|
|||||||
Reference in New Issue
Block a user