1
1
using System . Collections . ObjectModel ;
2
2
using System . Reflection ;
3
3
using System . Text ;
4
+ using System . Text . Json ;
4
5
using Microsoft . Extensions . Configuration ;
5
6
using Microsoft . Extensions . Logging ;
6
7
using Serilog ;
@@ -21,7 +22,7 @@ namespace TerminalGuiDesigner.UI;
21
22
/// </summary>
22
23
public class Editor : Toplevel
23
24
{
24
- private readonly KeyMap keyMap ;
25
+ private KeyMap keyMap ;
25
26
private readonly KeyboardManager keyboardManager ;
26
27
private readonly MouseManager mouseManager ;
27
28
@@ -46,6 +47,7 @@ public class Editor : Toplevel
46
47
/// </summary>
47
48
internal Guid ? LastSavedOperation ;
48
49
50
+ private static string _keymapPath = string . Empty ;
49
51
private static string _logDirectory = string . Empty ;
50
52
51
53
/// <summary>
@@ -66,9 +68,32 @@ public Editor()
66
68
Logging . Logger = CreateLogger ( ) ;
67
69
}
68
70
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
+
69
86
try
70
87
{
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
+ }
72
97
73
98
SelectionManager . Instance . SelectedScheme = this . keyMap . SelectionColor . Scheme ;
74
99
}
@@ -78,12 +103,6 @@ public Editor()
78
103
ExceptionViewer . ShowException ( "Failed to read keybindings from configuration file" , ex ) ;
79
104
this . keyMap = new KeyMap ( ) ;
80
105
}
81
-
82
- this . keyboardManager = new KeyboardManager ( this . keyMap ) ;
83
- this . mouseManager = new MouseManager ( ) ;
84
- this . Closing += this . Editor_Closing ;
85
-
86
- this . BuildRootMenu ( ) ;
87
106
}
88
107
89
108
static ILogger CreateLogger ( )
@@ -842,8 +861,13 @@ private void BuildRootMenu()
842
861
843
862
private void ChangeKeybindings ( )
844
863
{
845
- var kb = new KeyBindingsUI ( ) ;
864
+ var kb = new KeyBindingsUI ( keyMap ) ;
846
865
Application . Run ( kb ) ;
866
+
867
+ if ( kb . Save )
868
+ {
869
+ // TODO: Save
870
+ }
847
871
}
848
872
849
873
private void Editor_Closing ( object ? sender , ToplevelClosingEventArgs obj )
0 commit comments