Skip to content

Commit

Permalink
Issues #9 Check local directory for Settings.json file before AppData
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPro committed Sep 29, 2024
1 parent 057f1eb commit e0ecedf
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 51 deletions.
37 changes: 25 additions & 12 deletions WindowTextExtractor/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ private void MenuItemFontClick(object sender, EventArgs e)
lock (_lockObject)
{
txtContent.Font = dialog.Font;
_settings.FontName = dialog.Font.Name;
_settings.FontSize = dialog.Font.Size;
_settings.FontStyle = dialog.Font.Style;
_settings.FontUnit = dialog.Font.Unit;
_settings.Font.Name = dialog.Font.Name;
_settings.Font.Size = dialog.Font.Size;
_settings.Font.Style = dialog.Font.Style;
_settings.Font.Unit = dialog.Font.Unit;
SaveSettings(_settings);
}
}
Expand Down Expand Up @@ -471,10 +471,10 @@ private void MenuItemChangeIcon(object sender, EventArgs e)
var result = form.ShowDialog(this);
if (result == DialogResult.OK)
{
var imageFileName = ApplicationSettingsFile.GetImageFileName();
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
try
{
File.Copy(form.FileName, imageFileName, true);
File.Copy(form.FileName, imageFileInfo.FullName, true);
}
catch (Exception ex)
{
Expand All @@ -484,11 +484,11 @@ private void MenuItemChangeIcon(object sender, EventArgs e)

try
{
SetImage(imageFileName);
SetImage(imageFileInfo.FullName);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load the file {imageFileName}.{Environment.NewLine}{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Failed to load the file {imageFileInfo.FullName}.{Environment.NewLine}{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

Expand Down Expand Up @@ -1368,8 +1368,21 @@ private void LoadTargetIcon()
}
else
{
var imageFileName = ApplicationSettingsFile.GetImageFileName();
SetImage(imageFileName);
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
if (imageFileInfo.Exists)
{
SetImage(imageFileInfo.FullName);
}
else
{
btnTarget.Image?.Dispose();
btnTarget.Image = Properties.Resources.TargetButton;
menuItemDefaultIcon.Checked = true;
menuItemSystemCursor.Checked = false;
menuItemChangeIcon.Checked = false;
_settings.TargetIcon = TargetIconType.Default;
SaveSettings(_settings);
}
}
}

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

private void LoadSettings()
{
_settings = ApplicationSettingsFile.Load();
_settings = ApplicationSettingsFile.Read();
_videoFileName = Path.Combine(AssemblyUtils.AssemblyDirectory, _settings.VideoFileName);
numericFps.Value = _settings.FPS;
numericScale.Value = _settings.Scale;
Expand All @@ -1402,7 +1415,7 @@ private void LoadSettings()
menuItemMagnifierEnabled.Checked = _settings.Magnifier.Enabled;
cmbRefresh.SelectedIndex = _settings.RefreshImage ? 0 : 1;
cmbCaptureCursor.SelectedIndex = _settings.CaptureCursor ? 0 : 1;
txtContent.Font = new Font(_settings.FontName, _settings.FontSize, _settings.FontStyle, _settings.FontUnit);
txtContent.Font = new Font(_settings.Font.Name, _settings.Font.Size, _settings.Font.Style, _settings.Font.Unit);
if (!_settings.ShowTextList)
{
splitTextContainer.Panel2Collapsed = true;
Expand Down
4 changes: 2 additions & 2 deletions WindowTextExtractor/Forms/TargetIconForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private void LoadTargetIcon()
}
else
{
var imageFileName = ApplicationSettingsFile.GetImageFileName();
SetImageSafely(imageFileName);
var imageFileInfo = ApplicationSettingsFile.GetImageFileName();
SetImageSafely(imageFileInfo.FullName);
}
}

Expand Down
26 changes: 5 additions & 21 deletions WindowTextExtractor/Settings/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
using System.Drawing;

namespace WindowTextExtractor.Settings
namespace WindowTextExtractor.Settings
{
public class ApplicationSettings
{
private const string DefaultVideoFileName = "Window.avi";
private const float DefaultFontSize = 10;
private const FontStyle DefaultFontStyle = FontStyle.Regular;
private const GraphicsUnit DefaultFontUnit = GraphicsUnit.Point;
private const int DefaultFps = 12;
private const decimal DefaultScale = 1;
private const int DefaultBorderWidth = 5;
private const int DefaultBorderWidth = 3;
private const string DefaultBorderColor = "Blue";

public const int ImageSize = 48;
public const int IconSize = 32;

public const string DefaultFontName = "Courier New";

public string VideoFileName { get; set; }

public string FontName { get; set; }

public float FontSize { get; set; }

public FontStyle FontStyle { get; set; }

public GraphicsUnit FontUnit { get; set; }

public int FPS { get; set; }

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

public TargetIconType TargetIcon { get; set; }

public FontSettings Font { get; set; }

public MagnifierSettings Magnifier { get; set; }

public static ApplicationSettings CreateDefault() => new ApplicationSettings
{
VideoFileName = DefaultVideoFileName,
FontName = DefaultFontName,
FontSize = DefaultFontSize,
FontStyle = DefaultFontStyle,
FontUnit = DefaultFontUnit,
FPS = DefaultFps,
Scale = DefaultScale,
BorderWidth = DefaultBorderWidth,
Expand All @@ -73,6 +56,7 @@ public class ApplicationSettings
RefreshImage = true,
CaptureCursor = true,
TargetIcon = TargetIconType.Default,
Font = new FontSettings(),
Magnifier = new MagnifierSettings()
};
}
Expand Down
78 changes: 62 additions & 16 deletions WindowTextExtractor/Settings/ApplicationSettingsFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using WindowTextExtractor.Utils;
using Newtonsoft.Json;

namespace WindowTextExtractor.Settings
Expand All @@ -8,36 +9,81 @@ public static class ApplicationSettingsFile
{
private const string ImageFileName = "TargetIcon";
private const string SettingsFileName = "Settings.json";
private const string SettingsDirectory = "WindowTextExtractor";

public static void Save(ApplicationSettings settings, string fileName = SettingsFileName)
public static ApplicationSettings Read()
{
var fileInfo = GetFileInfo(fileName);
if (!Directory.Exists(fileInfo.Directory.FullName))
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
if (fileInfo.Exists)
{
Directory.CreateDirectory(fileInfo.Directory.FullName);
if (fileInfo.Length == 0)
{
return ApplicationSettings.CreateDefault();
}
return Read(fileInfo.FullName);
}

var json = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(fileInfo.FullName, json);
fileInfo = GetProfileFileInfo(SettingsFileName);
if (!fileInfo.Exists)
{
return ApplicationSettings.CreateDefault();
}
return Read(fileInfo.FullName);
}

public static ApplicationSettings Load(string fileName = SettingsFileName)
public static void Save(ApplicationSettings settings)
{
var fileInfo = GetFileInfo(fileName);
if (!File.Exists(fileInfo.FullName))
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
if (fileInfo.Exists)
{
return ApplicationSettings.CreateDefault();
Save(fileInfo, settings);
return;
}

fileInfo = GetProfileFileInfo(SettingsFileName);
Save(fileInfo, settings);
}

public static FileInfo GetImageFileName()
{
var fileInfo = GetCurrentDirectoryFileInfo(SettingsFileName);
if (fileInfo.Exists)
{
return GetCurrentDirectoryFileInfo(ImageFileName);
}

var jsonContent = File.ReadAllText(fileInfo.FullName);
return GetProfileFileInfo(ImageFileName);
}

private static FileInfo GetCurrentDirectoryFileInfo(string fileName)
{
var fullFileName = Path.Combine(AssemblyUtils.AssemblyDirectory, fileName);
return new FileInfo(fullFileName);
}

private static FileInfo GetProfileFileInfo(string fileName)
{
var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AssemblyUtils.AssemblyTitle, AssemblyUtils.AssemblyProductVersion);
var fullFileName = Path.Combine(directoryName, fileName);
return new FileInfo(fullFileName);
}

private static ApplicationSettings Read(string fileName)
{
var jsonContent = File.ReadAllText(fileName);
var settings = JsonConvert.DeserializeObject<ApplicationSettings>(jsonContent);
settings.Font ??= new FontSettings();
settings.Magnifier ??= new MagnifierSettings();
return settings;
}

public static string GetImageFileName(string fileName = ImageFileName) => GetFileInfo(fileName).FullName;

private static FileInfo GetFileInfo(string fileName) => new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SettingsDirectory, fileName));
private static void Save(FileInfo fileInfo, ApplicationSettings settings)
{
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
var json = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(fileInfo.FullName, json);
}
}
}
}
28 changes: 28 additions & 0 deletions WindowTextExtractor/Settings/FontSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Drawing;

namespace WindowTextExtractor.Settings
{
public class FontSettings
{
private const float DefaultFontSize = 10;
private const FontStyle DefaultFontStyle = FontStyle.Regular;
private const GraphicsUnit DefaultFontUnit = GraphicsUnit.Point;
private const string DefaultFontName = "Courier New";

public string Name { get; set; }

public float Size { get; set; }

public FontStyle Style { get; set; }

public GraphicsUnit Unit { get; set; }

public FontSettings()
{
Name = DefaultFontName;
Size = DefaultFontSize;
Style = DefaultFontStyle;
Unit = DefaultFontUnit;
}
}
}
1 change: 1 addition & 0 deletions WindowTextExtractor/WindowTextExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<Compile Include="Native\Winmm.cs" />
<Compile Include="Settings\ApplicationSettings.cs" />
<Compile Include="Settings\ApplicationSettingsFile.cs" />
<Compile Include="Settings\FontSettings.cs" />
<Compile Include="Settings\MagnifierSettings.cs" />
<Compile Include="Settings\TargetIconType.cs" />
<Compile Include="Utils\DPIUtils.cs" />
Expand Down

0 comments on commit e0ecedf

Please sign in to comment.