1- using System ;
1+ using Microsoft . Win32 ;
2+ using System ;
23using System . Drawing ;
34using System . Reflection ;
45using System . Threading ;
@@ -22,6 +23,7 @@ public FreshTools()
2223 Thread . CurrentThread . Name = "FreshTools Thread" ;
2324 Log . Init ( ) ;
2425 LoadConfig ( ) ;
26+ UpdateRegistryForStartup ( ) ;
2527
2628 Application . ApplicationExit += new EventHandler ( this . OnApplicationExit ) ;
2729 InitializeNotificationIcon ( ) ;
@@ -56,17 +58,15 @@ public void InitializeNotificationIcon()
5658 windowManagerMiscHotKeysEnabledMenuItem . Checked = WindowManager . MiscHotKeysEnabled ;
5759
5860 MenuItem windowManagerMenu = new MenuItem ( "Window Manager" ) ;
59- MenuItem windowManagerSaveWindowsMenuItem = new MenuItem ( "Save All Window Positions" ) ;
60- MenuItem windowManagerRestoreWindowsMenuItem = new MenuItem ( "Restore All Window Positions" ) ;
61- MenuItem windowManagerUndoRestoreWindowsMenuItem = new MenuItem ( "Restore All Window Positions (undo)" ) ;
61+ MenuItem windowManagerSaveLayoutMenuItem = new MenuItem ( "Save Window Layout" ) ;
62+ MenuItem windowManagerRestoreLayoutMenuItem = new MenuItem ( "Restore Window Layout" ) ;
6263
6364 windowManagerMenu . MenuItems . Add ( windowManagerSnapHotKeysEnabledMenuItem ) ;
6465 windowManagerMenu . MenuItems . Add ( windowManagerSnapAltHotKeysEnabledMenuItem ) ;
6566 windowManagerMenu . MenuItems . Add ( windowManagerMiscHotKeysEnabledMenuItem ) ;
6667 windowManagerMenu . MenuItems . Add ( new MenuItem ( "-" ) ) ;
67- windowManagerMenu . MenuItems . Add ( windowManagerSaveWindowsMenuItem ) ;
68- windowManagerMenu . MenuItems . Add ( windowManagerRestoreWindowsMenuItem ) ;
69- windowManagerMenu . MenuItems . Add ( windowManagerUndoRestoreWindowsMenuItem ) ;
68+ windowManagerMenu . MenuItems . Add ( windowManagerSaveLayoutMenuItem ) ;
69+ windowManagerMenu . MenuItems . Add ( windowManagerRestoreLayoutMenuItem ) ;
7070
7171 MenuItem settingsMenu = new MenuItem ( "Settings" ) ;
7272 MenuItem settingsDirMenuItem = new MenuItem ( "Open AppData" ) ;
@@ -95,9 +95,8 @@ public void InitializeNotificationIcon()
9595 windowManagerSnapHotKeysEnabledMenuItem . Click += windowManagerSnapHotKeysEnabledMenuItem_Click ;
9696 windowManagerSnapAltHotKeysEnabledMenuItem . Click += windowManagerSnapAltHotKeysEnabledMenuItem_Click ;
9797 windowManagerMiscHotKeysEnabledMenuItem . Click += windowManagerMiscHotKeysEnabledMenuItem_Click ;
98- windowManagerSaveWindowsMenuItem . Click += WindowManager . SaveAllWindowPositions ;
99- windowManagerRestoreWindowsMenuItem . Click += WindowManager . RestoreAllWindowPositions ;
100- windowManagerUndoRestoreWindowsMenuItem . Click += WindowManager . UndoRestoreAllWindowPositions ;
98+ windowManagerSaveLayoutMenuItem . Click += WindowManager . SaveLayout0 ;
99+ windowManagerRestoreLayoutMenuItem . Click += WindowManager . RestoreLayout0 ;
101100 startupEnabledMenuItem . Click += startupEnabledMenuItem_Click ;
102101 launchAsAdminMenuItem . Click += launchAsAdminMenuItem_Click ;
103102 settingsDirMenuItem . Click += settingsDirMenuItem_Click ;
@@ -130,34 +129,40 @@ static void Main(string[] args)
130129 }
131130 }
132131
132+ /// <summary>
133+ /// Load variables from config file and update real time settings
134+ /// </summary>
133135 public void LoadConfig ( )
134136 {
135137 settingsFile = new VariablesFile ( configFilePath , null , false ) ;
136138 VariableLibrary vars = settingsFile . variables ;
137139
138140 //load variables
139- bool snapHotKeysEnabled = WindowManager . SnapHotKeysEnabled ;
141+ bool snapHotKeysEnabled = WindowManager . SnapHotKeysEnabled_Default ;
140142 WindowManager . SnapHotKeysEnabled = vars . GetVariable ( "SnapWindowHotKeysEnabled" , ref snapHotKeysEnabled , true ) . Boolean ;
141- bool snapAltHotKeysEnabled = WindowManager . SnapAltHotKeysEnabled ;
143+ bool snapAltHotKeysEnabled = WindowManager . SnapAltHotKeysEnabled_Default ;
142144 WindowManager . SnapAltHotKeysEnabled = vars . GetVariable ( "SnapAltWindowHotKeysEnabled" , ref snapAltHotKeysEnabled , true ) . Boolean ;
143- bool miscHotKeysEnabled = WindowManager . MiscHotKeysEnabled ;
145+ bool miscHotKeysEnabled = WindowManager . MiscHotKeysEnabled_Default ;
144146 WindowManager . MiscHotKeysEnabled = vars . GetVariable ( "MiscWindowHotKeysEnabled" , ref miscHotKeysEnabled , true ) . Boolean ;
145147 WindowManager . LoadSnapSizes ( settingsFile ) ;
146-
147148 Log . I ( "Finisihed loading config" ) ;
149+ //re-write config file in case one didn't exist already
150+ SaveConfig ( ) ;
148151 }
149152
150153 /// <summary>
151154 /// Make sure config variables that can be changed are updated in config file
152155 /// </summary>
153- private void SaveVariables ( )
156+ private void SaveConfig ( )
154157 {
155158 settingsFile . variables . SetValue ( "SnapWindowHotKeysEnabled" , "" + WindowManager . SnapHotKeysEnabled ) ;
156159 settingsFile . variables . SetValue ( "SnapAltWindowHotKeysEnabled" , "" + WindowManager . SnapAltHotKeysEnabled ) ;
157160 settingsFile . variables . SetValue ( "MiscWindowHotKeysEnabled" , "" + WindowManager . MiscHotKeysEnabled ) ;
158161
159162 WindowManager . SaveSnapSizes ( settingsFile ) ;
160163 settingsFile . SaveAs ( configFilePath ) ;
164+
165+ Log . I ( "Finisihed updating config" ) ;
161166 }
162167
163168 #region Context Menu Event Handlers
@@ -174,23 +179,23 @@ private void windowManagerSnapHotKeysEnabledMenuItem_Click(object sender, EventA
174179 MenuItem i = ( MenuItem ) sender ;
175180 i . Checked = ! i . Checked ;
176181 WindowManager . SnapHotKeysEnabled = i . Checked ;
177- SaveVariables ( ) ;
182+ SaveConfig ( ) ;
178183 }
179184
180185 private void windowManagerSnapAltHotKeysEnabledMenuItem_Click ( object sender , EventArgs e )
181186 {
182187 MenuItem i = ( MenuItem ) sender ;
183188 i . Checked = ! i . Checked ;
184189 WindowManager . SnapAltHotKeysEnabled = i . Checked ;
185- SaveVariables ( ) ;
190+ SaveConfig ( ) ;
186191 }
187192
188193 private void windowManagerMiscHotKeysEnabledMenuItem_Click ( object sender , EventArgs e )
189194 {
190195 MenuItem i = ( MenuItem ) sender ;
191196 i . Checked = ! i . Checked ;
192197 WindowManager . MiscHotKeysEnabled = i . Checked ;
193- SaveVariables ( ) ;
198+ SaveConfig ( ) ;
194199 }
195200
196201 private void startupEnabledMenuItem_Click ( object sender , EventArgs e )
@@ -201,12 +206,14 @@ private void startupEnabledMenuItem_Click(object sender, EventArgs e)
201206 else
202207 FreshArchives . AddApplicationToStartup ( ) ;
203208 i . Checked = FreshArchives . IsApplicationInStartup ( ) ;
209+ Log . I ( "Finisihed updating config" ) ;
204210 }
205211
206212 private void launchAsAdminMenuItem_Click ( object sender , EventArgs e )
207213 {
214+ Log . I ( "Relaunching as Admin" ) ;
208215 Process p = new Process ( ) ;
209- p . StartInfo . FileName = Application . ExecutablePath ;
216+ p . StartInfo . FileName = Assembly . GetEntryAssembly ( ) . Location ;
210217 p . StartInfo . Verb = "runas" ;
211218 p . Start ( ) ;
212219 //this process will be killed by the single instance check in the new process
@@ -228,11 +235,32 @@ private void quitMenuItem_Click(object sender, EventArgs e)
228235 {
229236 Log . I ( "quitMenuItem_Click()" ) ;
230237 freshToolsNotifyIcon . Dispose ( ) ;
231- SaveVariables ( ) ;
238+ SaveConfig ( ) ;
232239 Application . Exit ( ) ;
233240 }
234241 #endregion
235242
243+ public static void UpdateRegistryForStartup ( )
244+ { //should check for any existing key and delete if the name is outdated and create a new one - especialy since this app kills other processes with the same name
245+ using ( RegistryKey key = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) )
246+ {
247+ bool partOfStartup = false ;
248+
249+ //look for any relevant key values
250+ if ( key . GetValue ( Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ) != null )
251+ partOfStartup = true ;
252+
253+ //should delete old key values here
254+ //key.DeleteValue(Assembly.GetExecutingAssembly().GetName().Name, false);
255+
256+ //update key value to current path
257+ if ( partOfStartup )
258+ {
259+ key . SetValue ( Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name , "\" " + Assembly . GetEntryAssembly ( ) . Location + "\" " ) ;
260+ }
261+ }
262+ }
263+
236264 private void OnApplicationExit ( object sender , EventArgs args )
237265 {
238266 //cleanup
0 commit comments