Skip to content

Commit 0ebbeae

Browse files
Merge pull request #54 from freshprogrammer/dev
Dev - 6.0
2 parents 6f48adc + d629e41 commit 0ebbeae

8 files changed

Lines changed: 290 additions & 149 deletions

File tree

FreshTools/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("0.5.0.0")]
36-
[assembly: AssemblyFileVersion("0.5.0.0")]
35+
[assembly: AssemblyVersion("0.6.0.0")]
36+
[assembly: AssemblyFileVersion("0.6.0.0")]

FreshTools/code/FreshTools.cs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Win32;
2+
using System;
23
using System.Drawing;
34
using System.Reflection;
45
using 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

FreshTools/code/Generics/FreshArchives.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using System.Drawing;
3+
using System.IO;
24
using System.Reflection;
35
using System.Windows.Forms;
46
using Microsoft.Win32;
5-
using System.Drawing;
67

78
namespace FreshTools
89
{
@@ -32,7 +33,7 @@ public static void AddApplicationToStartup()
3233
{
3334
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
3435
{
35-
key.SetValue(Assembly.GetExecutingAssembly().GetName().Name, "\"" + Application.ExecutablePath + "\"");
36+
key.SetValue(Assembly.GetExecutingAssembly().GetName().Name, "\"" + Assembly.GetEntryAssembly().Location + "\"");
3637
}
3738
}
3839

@@ -46,31 +47,53 @@ public static void RemoveApplicationFromStartup()
4647

4748
public static bool IsApplicationInStartup()
4849
{
49-
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
50+
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", false))
5051
{
5152
var val = key.GetValue(Assembly.GetExecutingAssembly().GetName().Name);
5253
return val != null;
5354
}
5455
}
5556

5657
/// <summary>
57-
/// Parses "X,Y,Width,Hight" into RectangleF
58+
/// Parses "X,Y,Width,Hight" into RectangleF. Returns zero rectangle if failure
5859
/// </summary>
5960
/// <param name="input"></param>
6061
/// <returns></returns>
6162
public static RectangleF ParseRectangleF(string input)
6263
{
63-
try
64+
RectangleF result = RectangleF.Empty;
65+
string[] vals = input.Split(',');
66+
float x = 0, y = 0, w = 0, h = 0;
67+
bool valid = true;
68+
valid = valid && float.TryParse(vals[0], out x);
69+
valid = valid && float.TryParse(vals[1], out y);
70+
valid = valid && float.TryParse(vals[2], out w);
71+
valid = valid && float.TryParse(vals[3], out h);
72+
73+
if (valid)
74+
{
75+
result.X = x;
76+
result.Y = y;
77+
result.Width = w;
78+
result.Height = h;
79+
}
80+
return result;
81+
}
82+
83+
/// <summary>
84+
/// Move the file to new destination, deleting any file that might exist there
85+
/// </summary>
86+
/// <param name="src">current full path to file</param>
87+
/// <param name="dest">full path to new file</param>
88+
public static void MoveFileOverwrite(string src, string dest)
89+
{
90+
if (File.Exists(src))
6491
{
65-
RectangleF result = RectangleF.Empty;
66-
string[] vals = input.Split(',');
67-
result.X = float.Parse(vals[0]);
68-
result.Y = float.Parse(vals[1]);
69-
result.Width = float.Parse(vals[2]);
70-
result.Height = float.Parse(vals[3]);
71-
return result;
92+
Directory.CreateDirectory(Path.GetDirectoryName(dest));
93+
if (File.Exists(dest))
94+
File.Delete(dest);
95+
File.Move(src, dest);
7296
}
73-
catch (Exception) { return RectangleF.Empty; }
7497
}
7598
}
7699
}

FreshTools/code/Scripting/FreshScript.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,13 +793,12 @@ private bool HasLeadingKeyword(string line, out string remainingLine, out Keywor
793793
keywordMatch = Keywords.comment;
794794
return true;
795795
}
796-
797-
if (line == "{")
796+
else if (line == "{")
798797
{
799798
keywordMatch = Keywords.openBrace;
800799
return true;
801800
}
802-
if (line == "}")
801+
else if (line == "}")
803802
{
804803
keywordMatch = Keywords.closeBrace;
805804
return true;
@@ -809,7 +808,7 @@ private bool HasLeadingKeyword(string line, out string remainingLine, out Keywor
809808
{
810809
if (HasLeadingKeyword(line, keyword, out remainingLine))
811810
{
812-
keywordMatch = (Keywords)Enum.Parse(typeof(Keywords), keyword);
811+
Enum.TryParse<Keywords>(keyword, caseSensitive, out keywordMatch);
813812
return true;
814813
}
815814
}

FreshTools/code/Scripting/Variable.cs

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -100,57 +100,27 @@ public static bool CanCast(VariableType origin, VariableType dest)
100100

101101
public static VariableType DetermineType(string val)
102102
{
103-
VariableType result = 0;
103+
bool b;
104+
int i;
105+
float f;
106+
double d;
104107

105-
if (String.Compare(val.Trim(), "true", true) == 0 || String.Compare(val.Trim(), "false", true) == 0)
106-
{
107-
result = VariableType.Boolean;
108-
}
109-
else
110-
{
111-
try
112-
{
113-
if (val.Trim().Equals("" + int.Parse(val)))
114-
{
115-
result = VariableType.Int;
116-
}
117-
}
118-
catch (FormatException)
119-
{
120-
try
121-
{
122-
if (val.Trim().Equals("" + float.Parse(val)))
123-
{
124-
result = VariableType.Float;
125-
}
126-
}
127-
catch (FormatException)
128-
{
129-
try
130-
{
131-
if (val.Trim().Equals("" + double.Parse(val)))
132-
{
133-
result = VariableType.Double;
134-
}
135-
}
136-
catch (FormatException)
137-
{
138-
string trimmed = val.Trim();
139-
if (trimmed.Length>0 && trimmed[0] == '\"' && trimmed[trimmed.Length - 1] == '\"')
140-
{
141-
result = VariableType.String;
142-
}
143-
else
144-
{
145-
result = VariableType.Unknown;
146-
}
147-
}
148-
}
108+
val = val.Trim();
149109

150-
}
151-
}
110+
if (val.Length <= 0)
111+
return VariableType.Unknown;
112+
else if (bool.TryParse(val, out b))
113+
return VariableType.Boolean;
114+
else if (int.TryParse(val, out i))
115+
return VariableType.Int;
116+
else if (float.TryParse(val, out f))
117+
return VariableType.Float;
118+
else if (double.TryParse(val, out d))
119+
return VariableType.Double;
120+
else if (val[0] == '\"' && val[val.Length - 1] == '\"')//start and end eith quotes
121+
return VariableType.String;
152122

153-
return result;
123+
return VariableType.Unknown;
154124
}
155125

156126
public Variable(string name, string defaultValue)
@@ -213,7 +183,7 @@ public object GetValueAsObject()
213183
}
214184
}
215185

216-
public bool Boolean
186+
public bool Boolean
217187
{
218188
get
219189
{

0 commit comments

Comments
 (0)