refactor(Program.cs): simplify sequence state machine by removing PROCESSING state

This commit is contained in:
2025-08-31 22:10:11 +02:00
parent 833355cd60
commit df936e187d

View File

@@ -61,7 +61,6 @@ namespace DD2Switcher {
} }
if (sequenceFirstIndex >= 0 && sequenceLastIndex >= 0 && sequenceFirstIndex <= sequenceLastIndex) { if (sequenceFirstIndex >= 0 && sequenceLastIndex >= 0 && sequenceFirstIndex <= sequenceLastIndex) {
CurrentState = SequenceState.PROCESSING;
CurrentSequenceIndex = sequenceFirstIndex; CurrentSequenceIndex = sequenceFirstIndex;
Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}"); Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}");
TabTo(CurrentSequenceIndex + 1); // Tab to first window TabTo(CurrentSequenceIndex + 1); // Tab to first window
@@ -194,7 +193,7 @@ namespace DD2Switcher {
public static int LastIndex { get; set; } = -1; public static int LastIndex { get; set; } = -1;
// Sequence mode state engine // Sequence mode state engine
public enum SequenceState { INACTIVE, WAITING_FOR_EVENT, PROCESSING, WAITING_TO_ADVANCE } public enum SequenceState { INACTIVE, WAITING_FOR_EVENT, WAITING_TO_ADVANCE, ADVANCE }
public static SequenceState CurrentState { get; set; } = SequenceState.INACTIVE; public static SequenceState CurrentState { get; set; } = SequenceState.INACTIVE;
public static int CurrentSequenceIndex { get; set; } = -1; public static int CurrentSequenceIndex { get; set; } = -1;
@@ -207,8 +206,8 @@ namespace DD2Switcher {
private static readonly Dictionary<SequenceState, Action> stateHandlers = new Dictionary<SequenceState, Action> { private static readonly Dictionary<SequenceState, Action> stateHandlers = new Dictionary<SequenceState, Action> {
{ SequenceState.INACTIVE, HandleInactive }, { SequenceState.INACTIVE, HandleInactive },
{ SequenceState.WAITING_FOR_EVENT, HandleWaitingForEvent }, { SequenceState.WAITING_FOR_EVENT, HandleWaitingForEvent },
{ SequenceState.PROCESSING, HandleProcessing }, { SequenceState.WAITING_TO_ADVANCE, HandleWaitingToAdvance },
{ SequenceState.WAITING_TO_ADVANCE, HandleWaitingToAdvance } { SequenceState.ADVANCE, HandleAdvance }
}; };
private static void HandleInactive() { private static void HandleInactive() {
@@ -216,37 +215,27 @@ namespace DD2Switcher {
} }
private static void HandleWaitingForEvent() { private static void HandleWaitingForEvent() {
Console.WriteLine($"Event detected in state: {CurrentState}, advancing to next step"); // Event happened, transition to waiting to advance
Console.WriteLine($"Event detected in state: {CurrentState}, transitioning to WAITING_TO_ADVANCE");
CurrentState = SequenceState.PROCESSING; CurrentState = SequenceState.WAITING_TO_ADVANCE;
CurrentSequenceIndex++;
Console.WriteLine($"Advanced to index {CurrentSequenceIndex}");
// Get the actual last index for this sequence (computed or user-set)
int sequenceLastIndex = (LastIndex != -1) ? LastIndex : FindLastNonNullWindow();
if (CurrentSequenceIndex > sequenceLastIndex) {
// End of sequence - tab back to first
int sequenceFirstIndex = (FirstIndex != -1) ? FirstIndex : FindFirstNonNullWindow();
Console.WriteLine("End of sequence reached, tabbing back to first window");
TabTo(sequenceFirstIndex + 1);
ExitSequenceMode();
} else {
Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}");
TabTo(CurrentSequenceIndex + 1);
CurrentState = SequenceState.WAITING_FOR_EVENT;
Console.WriteLine($"State changed to: {CurrentState}"); Console.WriteLine($"State changed to: {CurrentState}");
} }
}
private static void HandleProcessing() {
// Processing state - do nothing, just a transition state
}
private static void HandleWaitingToAdvance() { private static void HandleWaitingToAdvance() {
Console.WriteLine($"Advancing sequence from WAITING_TO_ADVANCE state"); // Start timer to advance after N milliseconds
Console.WriteLine($"Starting 500ms timer in WAITING_TO_ADVANCE state");
Task.Delay(500).ContinueWith(_ => {
if (CurrentState == SequenceState.WAITING_TO_ADVANCE) {
CurrentState = SequenceState.ADVANCE;
Console.WriteLine($"Timer expired, transitioning to ADVANCE state");
HandleSequenceEvent();
}
});
}
private static void HandleAdvance() {
Console.WriteLine($"Advancing sequence from ADVANCE state");
CurrentState = SequenceState.PROCESSING;
CurrentSequenceIndex++; CurrentSequenceIndex++;
Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); Console.WriteLine($"Advanced to index {CurrentSequenceIndex}");
@@ -322,8 +311,7 @@ namespace DD2Switcher {
if (wParamInt == WM_LBUTTONDOWN || wParamInt == WM_RBUTTONDOWN || wParamInt == WM_MBUTTONDOWN) { if (wParamInt == WM_LBUTTONDOWN || wParamInt == WM_RBUTTONDOWN || wParamInt == WM_MBUTTONDOWN) {
if (CurrentState == SequenceState.WAITING_FOR_EVENT) { if (CurrentState == SequenceState.WAITING_FOR_EVENT) {
Console.WriteLine($"Mouse click detected in state: {CurrentState}"); Console.WriteLine($"Mouse click detected in state: {CurrentState}");
CurrentState = SequenceState.WAITING_TO_ADVANCE; HandleSequenceEvent();
Console.WriteLine($"State changed to: {CurrentState}");
} }
} }
} }