Skip to content

Commit c4ea3c0

Browse files
committed
Load and show keys
1 parent 416a89f commit c4ea3c0

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

src/UI/Editor.cs

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.ObjectModel;
22
using System.Reflection;
33
using System.Text;
4+
using System.Text.Json;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.Logging;
67
using Serilog;
@@ -21,7 +22,7 @@ namespace TerminalGuiDesigner.UI;
2122
/// </summary>
2223
public class Editor : Toplevel
2324
{
24-
private readonly KeyMap keyMap;
25+
private KeyMap keyMap;
2526
private readonly KeyboardManager keyboardManager;
2627
private readonly MouseManager mouseManager;
2728

@@ -46,6 +47,7 @@ public class Editor : Toplevel
4647
/// </summary>
4748
internal Guid? LastSavedOperation;
4849

50+
private static string _keymapPath = string.Empty;
4951
private static string _logDirectory = string.Empty;
5052

5153
/// <summary>
@@ -66,9 +68,32 @@ public Editor()
6668
Logging.Logger = CreateLogger();
6769
}
6870

71+
LoadKeyMap();
72+
73+
this.keyboardManager = new KeyboardManager(this.keyMap);
74+
this.mouseManager = new MouseManager();
75+
this.Closing += this.Editor_Closing;
76+
77+
this.BuildRootMenu();
78+
}
79+
80+
private void LoadKeyMap()
81+
{
82+
_keymapPath = Path.Combine(
83+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
84+
"TerminalGuiDesigner", "keymap.json");
85+
6986
try
7087
{
71-
this.keyMap = new ConfigurationBuilder( ).AddYamlFile( "Keys.yaml", true ).Build( ).Get<KeyMap>( ) ?? new( );
88+
if (File.Exists(_keymapPath))
89+
{
90+
var json = File.ReadAllText(_keymapPath);
91+
this.keyMap = JsonSerializer.Deserialize<KeyMap>(json) ?? new KeyMap();
92+
}
93+
else
94+
{
95+
this.keyMap = new KeyMap();
96+
}
7297

7398
SelectionManager.Instance.SelectedScheme = this.keyMap.SelectionColor.Scheme;
7499
}
@@ -78,12 +103,6 @@ public Editor()
78103
ExceptionViewer.ShowException("Failed to read keybindings from configuration file", ex);
79104
this.keyMap = new KeyMap();
80105
}
81-
82-
this.keyboardManager = new KeyboardManager(this.keyMap);
83-
this.mouseManager = new MouseManager();
84-
this.Closing += this.Editor_Closing;
85-
86-
this.BuildRootMenu();
87106
}
88107

89108
static ILogger CreateLogger()
@@ -842,8 +861,13 @@ private void BuildRootMenu()
842861

843862
private void ChangeKeybindings()
844863
{
845-
var kb = new KeyBindingsUI();
864+
var kb = new KeyBindingsUI(keyMap);
846865
Application.Run(kb);
866+
867+
if (kb.Save)
868+
{
869+
// TODO: Save
870+
}
847871
}
848872

849873
private void Editor_Closing(object? sender, ToplevelClosingEventArgs obj)

src/UI/Windows/KeyBindingsUI.Designer.cs

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/UI/Windows/KeyBindingsUI.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,35 @@
77
// You can make changes to this file and they will not be overwritten when saving.
88
// </auto-generated>
99
// -----------------------------------------------------------------------------
10+
11+
using System.Reflection;
12+
1013
namespace TerminalGuiDesigner.UI.Windows {
1114
using Terminal.Gui;
1215

1316

1417
public partial class KeyBindingsUI {
15-
16-
public KeyBindingsUI() {
18+
private readonly KeyMap keyMap;
19+
private readonly PropertyInfo[] _props;
20+
21+
public bool Save { get; set; } = false;
22+
23+
public KeyBindingsUI(KeyMap keyMap) {
24+
this.keyMap = keyMap;
1725
InitializeComponent();
1826

27+
_props = typeof(KeyMap).GetProperties().ToArray();
28+
29+
tableView.Table = new EnumerableTableSource<PropertyInfo>(_props,
30+
new Dictionary<string, Func<PropertyInfo, object>>()
31+
{
32+
{ "Function", p => p.Name },
33+
{ "Key", p => p.GetValue(this.keyMap) }
34+
});
35+
1936
btnSave.Accepting += (s, e) =>
2037
{
21-
// Todo Save and reload keybindings
38+
Save = true;
2239
e.Cancel = true;
2340
Application.RequestStop();
2441
};

0 commit comments

Comments
 (0)