Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,8 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

# Temp folders
[0-9]/
Temp/
66 changes: 44 additions & 22 deletions ClsDebug.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using System.Globalization;

namespace WinSize4
{
public static class ClsDebug
{
public static bool Debug = false;
public static string _text = "";
private static string _activePath; // Will be set by frmMain at startup

//**********************************************
/// <summary>
/// Sets the active directory for logging. Must be called once at startup.
/// </summary>
//**********************************************
public static void Initialize(string path)
{
_activePath = path;
}

//**********************************************
/// <summary>
/// Checks if the path has been set. Prevents crashes.
/// </summary>
//**********************************************
private static bool IsPathInitialized()
{
if (string.IsNullOrEmpty(_activePath))
{
// Fallback to prevent crashes if Initialize() is not called.
// In a properly functioning app, this should not be hit.
_activePath = Path.GetDirectoryName(Application.ExecutablePath);
}
return true;
}

public static void ClearLog()
{
string _path = Environment.GetEnvironmentVariable("LocalAppData") + "\\WinSize4";
Directory.CreateDirectory(_path);
string _FileName = "Debug.txt";
if (File.Exists(Path.Combine(_path, _FileName)))
if (!IsPathInitialized()) return;
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
if (File.Exists(fullPath))
{
File.Delete(Path.Combine(_path, _FileName));
File.Delete(fullPath);
}
_text = "";
}
Expand All @@ -29,32 +57,26 @@ public static void AddText(string Text)

public static void LogText()
{
string _path = Environment.GetEnvironmentVariable("LocalAppData") + "\\WinSize4";
if (!Debug || !IsPathInitialized()) return;
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
Directory.CreateDirectory(_path);
string _FileName = "Debug.txt";
if (Debug)
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
using (var writer = new StreamWriter(fullPath, true))
{
using (var writer = new StreamWriter(_path + "\\" + _FileName, true))
{
writer.WriteLine(dt + " " + _text);
}
writer.WriteLine(dt + " " + _text);
}
_text = "";
}

public static void LogNow(string Text)
{
if (!Debug || !IsPathInitialized()) return;
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
string _path = Environment.GetEnvironmentVariable("LocalAppData") + "\\WinSize4";
Directory.CreateDirectory(_path);
string _FileName = "Debug.txt";
if (Debug)
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
using (var writer = new StreamWriter(fullPath, true))
{
using (var writer = new StreamWriter(_path + "\\" + _FileName, true))
{
writer.WriteLine(dt + " " + Text);
}
writer.WriteLine(dt + " " + Text);
}
}

Expand Down
84 changes: 39 additions & 45 deletions ClsSavedWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class ClsSavedWindows
{
public List<ClsWindowProps> Props = new List<ClsWindowProps>();
private string _path = Environment.GetEnvironmentVariable("LocalAppData") + "\\WinSize4";
private int _nextTag = 0;

//**********************************************
Expand Down Expand Up @@ -284,86 +283,81 @@ public int GetWindowIndexByTag(int Tag)
}

//**********************************************
/// <summary> Saves data to disk </summary>
/// <summary> Saves data to the specified path </summary>
//**********************************************
public void Save()
public void Save(string activePath)
{
string fileNameWindows;
var options = new JsonSerializerOptions()
{
WriteIndented = true
};
List<List<ClsWindowProps>> saveProps = new List<List<ClsWindowProps>>();
bool found;
int j;
var options = new JsonSerializerOptions() { WriteIndented = true };
var saveProps = new List<List<ClsWindowProps>>();

Directory.CreateDirectory(activePath);

// Remove all saved files
var fileEntries = Directory.GetFiles(_path, "*.json");
// Clean out only old window position files from the target directory
var fileEntries = Directory.GetFiles(activePath, "*.json");
foreach (string fileName in fileEntries)
{
if (Regex.Match(fileName, @"\d{3,5}x\d{3,5}P?\.json").Success)
if (Regex.IsMatch(Path.GetFileName(fileName), @"^\d{3,5}x\d{3,5}P?\.json$"))
{
File.Delete(fileName);
}
}

for (int i = 0; i < this.Props.Count; i++)
// Group window properties by screen configuration
foreach (var prop in this.Props)
{
found = false;
for (j = 0; j < saveProps.Count; j++)
var group = saveProps.FirstOrDefault(g =>
g[0].MonitorBoundsWidth == prop.MonitorBoundsWidth &&
g[0].MonitorBoundsHeight == prop.MonitorBoundsHeight &&
g[0].Primary == prop.Primary);

if (group != null)
{
if (this.Props[i].MonitorBoundsWidth == saveProps[j][0].MonitorBoundsWidth &&
this.Props[i].MonitorBoundsHeight == saveProps[j][0].MonitorBoundsHeight &&
this.Props[i].Primary == saveProps[j][0].Primary)
{
saveProps[j].Add(this.Props[i]);
found = true;
break;
}
group.Add(prop);
}
if (!found)
else
{
saveProps.Add(new List<ClsWindowProps>());
saveProps[saveProps.Count - 1].Add(this.Props[i]);
saveProps.Add(new List<ClsWindowProps> { prop });
}
}
Directory.CreateDirectory(_path);
for (int i = 0; i < saveProps.Count; i++)

// Write the new files
foreach (var group in saveProps)
{
fileNameWindows = saveProps[i][0].MonitorBoundsWidth + "x" + saveProps[i][0].MonitorBoundsHeight;
fileNameWindows += (saveProps[i][0].Primary) ? "P.json" : ".json";
using (var writer = new StreamWriter(_path + "\\" + fileNameWindows))
string fileNameWindows = $"{group[0].MonitorBoundsWidth}x{group[0].MonitorBoundsHeight}";
fileNameWindows += group[0].Primary ? "P.json" : ".json";
string fullPath = Path.Combine(activePath, fileNameWindows);
using (var writer = new StreamWriter(fullPath))
{
String json = JsonSerializer.Serialize(saveProps[i], options);
String json = JsonSerializer.Serialize(group, options);
writer.Write(json);
}
}
}

//**********************************************
/// <summary> Loads data from disk </summary>
/// <summary> Loads data from the specified path </summary>
//**********************************************
public void Load()
public void Load(string activePath)
{
this.Props.Clear();
int Id = 0;
System.IO.Directory.CreateDirectory(_path);
List<ClsWindowProps> loadProps = new List<ClsWindowProps>();
var fileEntries = Directory.GetFiles(_path, "*.json");
_nextTag = 0; // Reset tag counter on load

Directory.CreateDirectory(activePath);
var fileEntries = Directory.GetFiles(activePath, "*.json");

foreach (string fileName in fileEntries)
{
if (Regex.Match(fileName, @"\d{3,5}x\d{3,5}P?\.json").Success)
if (Regex.IsMatch(Path.GetFileName(fileName), @"^\d{3,5}x\d{3,5}P?\.json$"))
{
using (StreamReader r = new StreamReader(fileName))
{
String json = r.ReadToEnd();
if (json.Length > 0)
{
loadProps = JsonSerializer.Deserialize<List<ClsWindowProps>>(json);
foreach (var item in loadProps)
var loadedProps = JsonSerializer.Deserialize<List<ClsWindowProps>>(json);
foreach (var item in loadedProps)
{
item.Tag = Id;
Id++;
// AddWindow will assign the correct, unique tag
AddWindow(item);
}
}
Expand Down
32 changes: 17 additions & 15 deletions ClsScreens.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Text.Json;
using Windows.ApplicationModel.Activation;

Expand All @@ -9,8 +10,7 @@ namespace WinSize4
public class ClsScreens
{
public List<ClsScreenList> ScreenList = new List<ClsScreenList>();
private string _path = Environment.GetEnvironmentVariable("LocalAppData") + "\\WinSize4";
private string _fileNameWindows = "Screens.json";
private readonly string _fileNameScreens = "Screens.json";

//**********************************************
/// <summary> Get all current screens </summary>
Expand Down Expand Up @@ -154,33 +154,35 @@ public bool CleanScreenList()
}

//**********************************************
/// <summary> Saves data to disk </summary>
/// <summary> Saves data to the specified path </summary>
//**********************************************
public void Save()
public void Save(string activePath)
{
var options = new JsonSerializerOptions()
{
WriteIndented = true
};
Directory.CreateDirectory(_path);
using (var writer = new StreamWriter(_path + "\\" + _fileNameWindows))
var options = new JsonSerializerOptions() { WriteIndented = true };
Directory.CreateDirectory(activePath);
string fullPath = Path.Combine(activePath, _fileNameScreens);
using (var writer = new StreamWriter(fullPath))
{
String _json = JsonSerializer.Serialize(this.ScreenList, options);
writer.Write(_json);
}
}

//**********************************************
/// <summary> Loads data from disk </summary>
/// <summary> Loads data from the specified path </summary>
//**********************************************
public void Load()
public void Load(string activePath)
{
if (File.Exists(_path + "\\" + _fileNameWindows))
string fullPath = Path.Combine(activePath, _fileNameScreens);
if (File.Exists(fullPath))
{
using (StreamReader r = new StreamReader(_path + "\\" + _fileNameWindows))
using (StreamReader r = new StreamReader(fullPath))
{
String json = r.ReadToEnd();
this.ScreenList = JsonSerializer.Deserialize<List<ClsScreenList>>(json);
if (!string.IsNullOrWhiteSpace(json))
{
this.ScreenList = JsonSerializer.Deserialize<List<ClsScreenList>>(json);
}
}
}
}
Expand Down
Loading