Enable saving settings to disk
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DD2Switcher {
|
||||
public class Settings {
|
||||
public int FirstIndex { get; set; } = -1;
|
||||
public int LastIndex { get; set; } = -1;
|
||||
public Keys SequenceKeybind { get; set; } = Keys.F1;
|
||||
}
|
||||
|
||||
internal static class Program {
|
||||
private static int NumProc = 19;
|
||||
private static Process[] windows = new Process[NumProc];
|
||||
private static string settingsPath =
|
||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "DD2Switcher.json");
|
||||
|
||||
// Public access to tracked windows for the settings form
|
||||
public static Process[] GetTrackedWindows() {
|
||||
@@ -59,8 +69,9 @@ namespace DD2Switcher {
|
||||
CurrentSequenceIndex++;
|
||||
Console.WriteLine($"Advanced to index {CurrentSequenceIndex}");
|
||||
if (CurrentSequenceIndex > LastIndex) {
|
||||
// End of sequence
|
||||
Console.WriteLine("End of sequence reached, exiting sequence mode");
|
||||
// End of sequence - tab back to first
|
||||
Console.WriteLine("End of sequence reached, tabbing back to first window");
|
||||
TabTo(FirstIndex + 1);
|
||||
ExitSequenceMode();
|
||||
} else {
|
||||
Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}");
|
||||
@@ -80,6 +91,72 @@ namespace DD2Switcher {
|
||||
return CurrentState != SequenceState.INACTIVE;
|
||||
}
|
||||
|
||||
private static void LoadSettings() {
|
||||
try {
|
||||
if (File.Exists(settingsPath)) {
|
||||
string json = File.ReadAllText(settingsPath);
|
||||
var settings = JsonSerializer.Deserialize<Settings>(json);
|
||||
FirstIndex = settings.FirstIndex;
|
||||
LastIndex = settings.LastIndex;
|
||||
SequenceKeybind = settings.SequenceKeybind;
|
||||
Console.WriteLine(
|
||||
$"Loaded settings: First={FirstIndex}, Last={LastIndex}, Keybind={SequenceKeybind}");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine($"Error loading settings: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveSettings() {
|
||||
try {
|
||||
var settings =
|
||||
new Settings { FirstIndex = FirstIndex, LastIndex = LastIndex, SequenceKeybind = SequenceKeybind };
|
||||
string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
|
||||
File.WriteAllText(settingsPath, json);
|
||||
Console.WriteLine($"Saved settings: First={FirstIndex}, Last={LastIndex}, Keybind={SequenceKeybind}");
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine($"Error saving settings: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static int FindFirstNonNullWindow() {
|
||||
for (int i = 0; i < NumProc; i++) {
|
||||
if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static int FindLastNonNullWindow() {
|
||||
for (int i = NumProc - 1; i >= 0; i--) {
|
||||
if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static int FindNextNonNullWindow(int startIndex) {
|
||||
for (int i = startIndex; i < NumProc; i++) {
|
||||
if (windows[i] != null && windows[i].MainWindowHandle != IntPtr.Zero) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static void SetDefaultFirstLastIndices() {
|
||||
if (FirstIndex == -1) {
|
||||
FirstIndex = FindFirstNonNullWindow();
|
||||
Console.WriteLine($"Set default FirstIndex to: {FirstIndex}");
|
||||
}
|
||||
if (LastIndex == -1) {
|
||||
LastIndex = FindLastNonNullWindow();
|
||||
Console.WriteLine($"Set default LastIndex to: {LastIndex}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateSequenceHotkey(Keys newKey) {
|
||||
Console.WriteLine($"UpdateSequenceHotkey called with new key: {newKey} (code: {(int)newKey})");
|
||||
Console.WriteLine($"Old SequenceKeybind before update: {SequenceKeybind} (code: {(int)SequenceKeybind})");
|
||||
@@ -95,6 +172,7 @@ namespace DD2Switcher {
|
||||
Console.WriteLine($"Registered new hotkey ID: {sequenceHotkeyId} for key: {newKey}");
|
||||
SequenceKeybind = newKey;
|
||||
Console.WriteLine($"New SequenceKeybind after update: {SequenceKeybind} (code: {(int)SequenceKeybind})");
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
// Static properties for first/last selection persistence
|
||||
@@ -293,6 +371,10 @@ namespace DD2Switcher {
|
||||
PushHistory(firstNullIndex);
|
||||
ActiveIndex = firstNullIndex;
|
||||
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
|
||||
|
||||
// Set default first/last indices if not set
|
||||
SetDefaultFirstLastIndices();
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
private static void TrackProcess() {
|
||||
@@ -371,6 +453,25 @@ namespace DD2Switcher {
|
||||
CleanWindows();
|
||||
Console.WriteLine($"Tab to window at index {index}");
|
||||
|
||||
// Find the next non-null window if the target is null
|
||||
int originalIndex = index;
|
||||
while (index < NumProc && (windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero)) {
|
||||
index++;
|
||||
}
|
||||
if (index >= NumProc) {
|
||||
// Try from the beginning
|
||||
index = 0;
|
||||
while (index < originalIndex &&
|
||||
(windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero)) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
if (index >= NumProc || windows[index] == null || windows[index].MainWindowHandle == IntPtr.Zero) {
|
||||
Console.WriteLine("No valid windows found to tab to");
|
||||
return;
|
||||
}
|
||||
|
||||
var window = windows[index];
|
||||
Console.WriteLine(
|
||||
$"Window at index {index}: {(window == null ? "NULL" : window.ProcessName + " PID:" + window.Id)}");
|
||||
@@ -454,6 +555,9 @@ namespace DD2Switcher {
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
|
||||
// Load settings
|
||||
LoadSettings();
|
||||
|
||||
var processes = Process.GetProcesses();
|
||||
var currentProcess = Process.GetCurrentProcess();
|
||||
|
||||
|
Reference in New Issue
Block a user