11using System . Collections . ObjectModel ;
22using System . Reflection ;
33using System . Text ;
4+ using System . Text . Json ;
45using Microsoft . Extensions . Configuration ;
56using Microsoft . Extensions . Logging ;
67using Serilog ;
@@ -21,7 +22,7 @@ namespace TerminalGuiDesigner.UI;
2122/// </summary>
2223public 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 )
0 commit comments