Fix the fucking first and last index selection logic

This commit is contained in:
2025-08-31 20:35:39 +02:00
parent c4e9007f2b
commit 0e3f2005d1
2 changed files with 54 additions and 32 deletions

View File

@@ -19,20 +19,25 @@ namespace DD2Switcher {
public static void UntrackWindow(int index) {
if (index >= 0 && index < NumProc) {
windows[index] = null;
// Compact the array by shifting non-null elements to the left
for (int i = index; i < NumProc - 1; i++) {
windows[i] = windows[i + 1];
}
windows[NumProc - 1] = null;
// Update ActiveIndex if needed
if (ActiveIndex == index) {
ActiveIndex = -1;
} else if (ActiveIndex > index) {
ActiveIndex--;
}
// Update first/last indices if needed
if (FirstIndex == index) {
FirstIndex = -1;
}
if (LastIndex == index) {
LastIndex = -1;
}
}
}
// Static properties for first/last selection persistence
public static int FirstIndex { get; set; } = -1;
public static int LastIndex { get; set; } = -1;
private static int ActiveIndex = -1;
private static bool AltPressed = false;

View File

@@ -8,8 +8,6 @@ using System.Windows.Forms;
namespace DD2Switcher {
public partial class SettingsForm : Form {
private FlowLayoutPanel windowsPanel;
private int firstIndex = -1;
private int lastIndex = -1;
public SettingsForm() {
InitializeComponent();
@@ -79,39 +77,58 @@ namespace DD2Switcher {
var windowPanelForm = new WindowPanelForm();
windowPanelForm.WindowIndex = i;
windowPanelForm.WindowProcess = window;
windowPanelForm.IsFirst = (i == firstIndex);
windowPanelForm.IsLast = (i == lastIndex);
windowPanelForm.IsFirst = (i == Program.FirstIndex);
windowPanelForm.IsLast = (i == Program.LastIndex);
windowPanelForm.UpdateDisplay();
windowPanelForm.PickClicked += (sender, index) => {
if (firstIndex == -1) {
if (Program.FirstIndex == -1) {
// First pick - set both first and last
firstIndex = index;
lastIndex = index;
} else if (lastIndex == -1) {
// Second pick - set last
lastIndex = index;
Program.FirstIndex = index;
Program.LastIndex = index;
} else if (Program.LastIndex == -1) {
// Second pick - set last (must be >= first)
if (index >= Program.FirstIndex) {
Program.LastIndex = index;
} else {
Program.LastIndex = Program.FirstIndex;
Program.FirstIndex = index;
}
} else {
// Subsequent picks - determine which becomes first
if (index < firstIndex) {
// New index is lower, so it becomes first
lastIndex = firstIndex;
firstIndex = index;
int distanceToFirst = Math.Abs(index - Program.FirstIndex);
int distanceToLast = Math.Abs(index - Program.LastIndex);
if (distanceToFirst <= distanceToLast) {
// New index is closer to first, so first moves
if (index <= Program.LastIndex) {
Program.FirstIndex = index;
} else {
Program.LastIndex = Program.FirstIndex;
Program.FirstIndex = index;
}
} else {
// New index is higher, so it becomes last
lastIndex = index;
// New index is closer to last, so last moves
if (index >= Program.FirstIndex) {
Program.LastIndex = index;
} else {
Program.FirstIndex = Program.LastIndex;
Program.LastIndex = index;
}
}
}
// Ensure last >= first
if (Program.LastIndex < Program.FirstIndex) {
int tmp = Program.LastIndex;
Program.LastIndex = Program.FirstIndex;
Program.FirstIndex = tmp;
}
RefreshWindowsList();
};
windowPanelForm.UntrackClicked += (sender, index) => {
UntrackWindow(index);
if (index == firstIndex) {
firstIndex = -1;
}
if (index == lastIndex) {
lastIndex = -1;
}
RefreshWindowsList();
};
@@ -120,11 +137,11 @@ namespace DD2Switcher {
}
private string GetFirstLastText(int index) {
if (index == firstIndex && index == lastIndex) {
if (index == Program.FirstIndex && index == Program.LastIndex) {
return "First & Last";
} else if (index == firstIndex) {
} else if (index == Program.FirstIndex) {
return "First";
} else if (index == lastIndex) {
} else if (index == Program.LastIndex) {
return "Last";
}
return "";