Skip to content

Commit e0ecedf

Browse files
committed
Issues #9 Check local directory for Settings.json file before AppData
1 parent 057f1eb commit e0ecedf

6 files changed

+123
-51
lines changed

WindowTextExtractor/Forms/MainForm.cs

+25-12
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,10 @@ private void MenuItemFontClick(object sender, EventArgs e)
428428
lock (_lockObject)
429429
{
430430
txtContent.Font = dialog.Font;
431-
_settings.FontName = dialog.Font.Name;
432-
_settings.FontSize = dialog.Font.Size;
433-
_settings.FontStyle = dialog.Font.Style;
434-
_settings.FontUnit = dialog.Font.Unit;
431+
_settings.Font.Name = dialog.Font.Name;
432+
_settings.Font.Size = dialog.Font.Size;
433+
_settings.Font.Style = dialog.Font.Style;
434+
_settings.Font.Unit = dialog.Font.Unit;
435435
SaveSettings(_settings);
436436
}
437437
}
@@ -471,10 +471,10 @@ private void MenuItemChangeIcon(object sender, EventArgs e)
471471
var result = form.ShowDialog(this);
472472
if (result == DialogResult.OK)
473473
{
474-
var imageFileName = ApplicationSettingsFile.GetImageFileName();
474+
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
475475
try
476476
{
477-
File.Copy(form.FileName, imageFileName, true);
477+
File.Copy(form.FileName, imageFileInfo.FullName, true);
478478
}
479479
catch (Exception ex)
480480
{
@@ -484,11 +484,11 @@ private void MenuItemChangeIcon(object sender, EventArgs e)
484484

485485
try
486486
{
487-
SetImage(imageFileName);
487+
SetImage(imageFileInfo.FullName);
488488
}
489489
catch (Exception ex)
490490
{
491-
MessageBox.Show($"Failed to load the file {imageFileName}.{Environment.NewLine}{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
491+
MessageBox.Show($"Failed to load the file {imageFileInfo.FullName}.{Environment.NewLine}{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
492492
return;
493493
}
494494

@@ -1368,8 +1368,21 @@ private void LoadTargetIcon()
13681368
}
13691369
else
13701370
{
1371-
var imageFileName = ApplicationSettingsFile.GetImageFileName();
1372-
SetImage(imageFileName);
1371+
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
1372+
if (imageFileInfo.Exists)
1373+
{
1374+
SetImage(imageFileInfo.FullName);
1375+
}
1376+
else
1377+
{
1378+
btnTarget.Image?.Dispose();
1379+
btnTarget.Image = Properties.Resources.TargetButton;
1380+
menuItemDefaultIcon.Checked = true;
1381+
menuItemSystemCursor.Checked = false;
1382+
menuItemChangeIcon.Checked = false;
1383+
_settings.TargetIcon = TargetIconType.Default;
1384+
SaveSettings(_settings);
1385+
}
13731386
}
13741387
}
13751388

@@ -1386,7 +1399,7 @@ private void SetImage(string fileName)
13861399

13871400
private void LoadSettings()
13881401
{
1389-
_settings = ApplicationSettingsFile.Load();
1402+
_settings = ApplicationSettingsFile.Read();
13901403
_videoFileName = Path.Combine(AssemblyUtils.AssemblyDirectory, _settings.VideoFileName);
13911404
numericFps.Value = _settings.FPS;
13921405
numericScale.Value = _settings.Scale;
@@ -1402,7 +1415,7 @@ private void LoadSettings()
14021415
menuItemMagnifierEnabled.Checked = _settings.Magnifier.Enabled;
14031416
cmbRefresh.SelectedIndex = _settings.RefreshImage ? 0 : 1;
14041417
cmbCaptureCursor.SelectedIndex = _settings.CaptureCursor ? 0 : 1;
1405-
txtContent.Font = new Font(_settings.FontName, _settings.FontSize, _settings.FontStyle, _settings.FontUnit);
1418+
txtContent.Font = new Font(_settings.Font.Name, _settings.Font.Size, _settings.Font.Style, _settings.Font.Unit);
14061419
if (!_settings.ShowTextList)
14071420
{
14081421
splitTextContainer.Panel2Collapsed = true;

WindowTextExtractor/Forms/TargetIconForm.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ private void LoadTargetIcon()
112112
}
113113
else
114114
{
115-
var imageFileName = ApplicationSettingsFile.GetImageFileName();
116-
SetImageSafely(imageFileName);
115+
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
116+
SetImageSafely(imageFileInfo.FullName);
117117
}
118118
}
119119

WindowTextExtractor/Settings/ApplicationSettings.cs

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
using System.Drawing;
2-
3-
namespace WindowTextExtractor.Settings
1+
namespace WindowTextExtractor.Settings
42
{
53
public class ApplicationSettings
64
{
75
private const string DefaultVideoFileName = "Window.avi";
8-
private const float DefaultFontSize = 10;
9-
private const FontStyle DefaultFontStyle = FontStyle.Regular;
10-
private const GraphicsUnit DefaultFontUnit = GraphicsUnit.Point;
116
private const int DefaultFps = 12;
127
private const decimal DefaultScale = 1;
13-
private const int DefaultBorderWidth = 5;
8+
private const int DefaultBorderWidth = 3;
149
private const string DefaultBorderColor = "Blue";
1510

1611
public const int ImageSize = 48;
1712
public const int IconSize = 32;
1813

19-
public const string DefaultFontName = "Courier New";
20-
2114
public string VideoFileName { get; set; }
2215

23-
public string FontName { get; set; }
24-
25-
public float FontSize { get; set; }
26-
27-
public FontStyle FontStyle { get; set; }
28-
29-
public GraphicsUnit FontUnit { get; set; }
30-
3116
public int FPS { get; set; }
3217

3318
public decimal Scale { get; set; }
@@ -52,15 +37,13 @@ public class ApplicationSettings
5237

5338
public TargetIconType TargetIcon { get; set; }
5439

40+
public FontSettings Font { get; set; }
41+
5542
public MagnifierSettings Magnifier { get; set; }
5643

5744
public static ApplicationSettings CreateDefault() => new ApplicationSettings
5845
{
5946
VideoFileName = DefaultVideoFileName,
60-
FontName = DefaultFontName,
61-
FontSize = DefaultFontSize,
62-
FontStyle = DefaultFontStyle,
63-
FontUnit = DefaultFontUnit,
6447
FPS = DefaultFps,
6548
Scale = DefaultScale,
6649
BorderWidth = DefaultBorderWidth,
@@ -73,6 +56,7 @@ public class ApplicationSettings
7356
RefreshImage = true,
7457
CaptureCursor = true,
7558
TargetIcon = TargetIconType.Default,
59+
Font = new FontSettings(),
7660
Magnifier = new MagnifierSettings()
7761
};
7862
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using WindowTextExtractor.Utils;
34
using Newtonsoft.Json;
45

56
namespace WindowTextExtractor.Settings
@@ -8,36 +9,81 @@ public static class ApplicationSettingsFile
89
{
910
private const string ImageFileName = "TargetIcon";
1011
private const string SettingsFileName = "Settings.json";
11-
private const string SettingsDirectory = "WindowTextExtractor";
1212

13-
public static void Save(ApplicationSettings settings, string fileName = SettingsFileName)
13+
public static ApplicationSettings Read()
1414
{
15-
var fileInfo = GetFileInfo(fileName);
16-
if (!Directory.Exists(fileInfo.Directory.FullName))
15+
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
16+
if (fileInfo.Exists)
1717
{
18-
Directory.CreateDirectory(fileInfo.Directory.FullName);
18+
if (fileInfo.Length == 0)
19+
{
20+
return ApplicationSettings.CreateDefault();
21+
}
22+
return Read(fileInfo.FullName);
1923
}
2024

21-
var json = JsonConvert.SerializeObject(settings, Formatting.Indented);
22-
File.WriteAllText(fileInfo.FullName, json);
25+
fileInfo = GetProfileFileInfo(SettingsFileName);
26+
if (!fileInfo.Exists)
27+
{
28+
return ApplicationSettings.CreateDefault();
29+
}
30+
return Read(fileInfo.FullName);
2331
}
2432

25-
public static ApplicationSettings Load(string fileName = SettingsFileName)
33+
public static void Save(ApplicationSettings settings)
2634
{
27-
var fileInfo = GetFileInfo(fileName);
28-
if (!File.Exists(fileInfo.FullName))
35+
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
36+
if (fileInfo.Exists)
2937
{
30-
return ApplicationSettings.CreateDefault();
38+
Save(fileInfo, settings);
39+
return;
40+
}
41+
42+
fileInfo = GetProfileFileInfo(SettingsFileName);
43+
Save(fileInfo, settings);
44+
}
45+
46+
public static FileInfo GetImageFileName()
47+
{
48+
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
49+
if (fileInfo.Exists)
50+
{
51+
return GetCurrentDirectoryFileInfo(ImageFileName);
3152
}
3253

33-
var jsonContent = File.ReadAllText(fileInfo.FullName);
54+
return GetProfileFileInfo(ImageFileName);
55+
}
56+
57+
private static FileInfo GetCurrentDirectoryFileInfo(string fileName)
58+
{
59+
var fullFileName = Path.Combine(AssemblyUtils.AssemblyDirectory, fileName);
60+
return new FileInfo(fullFileName);
61+
}
62+
63+
private static FileInfo GetProfileFileInfo(string fileName)
64+
{
65+
var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AssemblyUtils.AssemblyTitle, AssemblyUtils.AssemblyProductVersion);
66+
var fullFileName = Path.Combine(directoryName, fileName);
67+
return new FileInfo(fullFileName);
68+
}
69+
70+
private static ApplicationSettings Read(string fileName)
71+
{
72+
var jsonContent = File.ReadAllText(fileName);
3473
var settings = JsonConvert.DeserializeObject<ApplicationSettings>(jsonContent);
74+
settings.Font ??= new FontSettings();
3575
settings.Magnifier ??= new MagnifierSettings();
3676
return settings;
3777
}
3878

39-
public static string GetImageFileName(string fileName = ImageFileName) => GetFileInfo(fileName).FullName;
40-
41-
private static FileInfo GetFileInfo(string fileName) => new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SettingsDirectory, fileName));
79+
private static void Save(FileInfo fileInfo, ApplicationSettings settings)
80+
{
81+
if (!fileInfo.Directory.Exists)
82+
{
83+
fileInfo.Directory.Create();
84+
}
85+
var json = JsonConvert.SerializeObject(settings, Formatting.Indented);
86+
File.WriteAllText(fileInfo.FullName, json);
87+
}
4288
}
43-
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Drawing;
2+
3+
namespace WindowTextExtractor.Settings
4+
{
5+
public class FontSettings
6+
{
7+
private const float DefaultFontSize = 10;
8+
private const FontStyle DefaultFontStyle = FontStyle.Regular;
9+
private const GraphicsUnit DefaultFontUnit = GraphicsUnit.Point;
10+
private const string DefaultFontName = "Courier New";
11+
12+
public string Name { get; set; }
13+
14+
public float Size { get; set; }
15+
16+
public FontStyle Style { get; set; }
17+
18+
public GraphicsUnit Unit { get; set; }
19+
20+
public FontSettings()
21+
{
22+
Name = DefaultFontName;
23+
Size = DefaultFontSize;
24+
Style = DefaultFontStyle;
25+
Unit = DefaultFontUnit;
26+
}
27+
}
28+
}

WindowTextExtractor/WindowTextExtractor.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
<Compile Include="Native\Winmm.cs" />
190190
<Compile Include="Settings\ApplicationSettings.cs" />
191191
<Compile Include="Settings\ApplicationSettingsFile.cs" />
192+
<Compile Include="Settings\FontSettings.cs" />
192193
<Compile Include="Settings\MagnifierSettings.cs" />
193194
<Compile Include="Settings\TargetIconType.cs" />
194195
<Compile Include="Utils\DPIUtils.cs" />

0 commit comments

Comments
 (0)