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) { public static void UntrackWindow(int index) {
if (index >= 0 && index < NumProc) { if (index >= 0 && index < NumProc) {
windows[index] = null; 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 // Update ActiveIndex if needed
if (ActiveIndex == index) { if (ActiveIndex == index) {
ActiveIndex = -1; 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 int ActiveIndex = -1;
private static bool AltPressed = false; private static bool AltPressed = false;

View File

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