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,21 @@ public Editor()
78103 ExceptionViewer . ShowException ( "Failed to read keybindings from configuration file" , ex ) ;
79104 this . keyMap = new KeyMap ( ) ;
80105 }
106+ }
81107
82- this . keyboardManager = new KeyboardManager ( this . keyMap ) ;
83- this . mouseManager = new MouseManager ( ) ;
84- this . Closing += this . Editor_Closing ;
85108
86- this . BuildRootMenu ( ) ;
109+ private void SaveKeyMap ( )
110+ {
111+ try
112+ {
113+ var json = JsonSerializer . Serialize ( this . keyMap ) ;
114+ File . WriteAllText ( _keymapPath , json ) ;
115+ SelectionManager . Instance . SelectedScheme = this . keyMap . SelectionColor . Scheme ;
116+ }
117+ catch ( Exception ex )
118+ {
119+ ExceptionViewer . ShowException ( "Failed to save keybindings from configuration file" , ex ) ;
120+ }
87121 }
88122
89123 static ILogger CreateLogger ( )
@@ -769,6 +803,7 @@ private void BuildRootMenu()
769803 $ "{ this . keyMap . ShowHelp } - Show Help",
770804 $ "{ this . keyMap . New } - New Window/Class",
771805 $ "{ this . keyMap . Open } - Open a .Designer.cs file",
806+ $ "Keybindings",
772807 } ;
773808
774809 // center all the commands
@@ -783,7 +818,7 @@ private void BuildRootMenu()
783818 X = Pos . Center ( ) ,
784819 Y = Pos . Percent ( 75 ) ,
785820 Width = maxWidth ,
786- Height = 3 ,
821+ Height = 4 ,
787822 ColorScheme = new ColorScheme
788823 (
789824 new Attribute ( new Color ( Color . White ) , new Color ( Color . Black ) ) ,
@@ -813,6 +848,9 @@ private void BuildRootMenu()
813848 case 2 :
814849 this . Open ( ) ;
815850 break ;
851+ case 3 :
852+ this . ChangeKeybindings ( ) ;
853+ break ;
816854 }
817855 }
818856
@@ -836,6 +874,18 @@ private void BuildRootMenu()
836874 this . Add ( this . rootCommandsListView ) ;
837875 }
838876
877+ private void ChangeKeybindings ( )
878+ {
879+ var kb = new KeyBindingsUI ( keyMap ) ;
880+ Application . Run ( kb ) ;
881+
882+ if ( kb . Save )
883+ {
884+ SaveKeyMap ( ) ;
885+ }
886+ }
887+
888+
839889 private void Editor_Closing ( object ? sender , ToplevelClosingEventArgs obj )
840890 {
841891 if ( this . viewBeingEdited == null )
0 commit comments