From 1f314d5c4bdcf2f97c71bceefd0f02133c1166b7 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 31 Aug 2025 20:27:45 +0200 Subject: [PATCH] Refactor the individual window panel into a separate form --- DD2Switcher/DD2Switcher.csproj | 9 ++ DD2Switcher/SettingsForm.cs | 84 ++------------- DD2Switcher/WindowPanelForm.Designer.cs | 133 ++++++++++++++++++++++++ DD2Switcher/WindowPanelForm.cs | 47 +++++++++ DD2Switcher/WindowPanelForm.resx | 120 +++++++++++++++++++++ 5 files changed, 320 insertions(+), 73 deletions(-) create mode 100644 DD2Switcher/WindowPanelForm.Designer.cs create mode 100644 DD2Switcher/WindowPanelForm.cs create mode 100644 DD2Switcher/WindowPanelForm.resx diff --git a/DD2Switcher/DD2Switcher.csproj b/DD2Switcher/DD2Switcher.csproj index f366f03..ef91bc4 100644 --- a/DD2Switcher/DD2Switcher.csproj +++ b/DD2Switcher/DD2Switcher.csproj @@ -59,6 +59,12 @@ Form + + UserControl + + + WindowPanelForm.cs + @@ -75,6 +81,9 @@ SettingsForm.cs + + WindowPanelForm.cs + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/DD2Switcher/SettingsForm.cs b/DD2Switcher/SettingsForm.cs index ed77860..6697465 100644 --- a/DD2Switcher/SettingsForm.cs +++ b/DD2Switcher/SettingsForm.cs @@ -76,82 +76,20 @@ namespace DD2Switcher { if (window == null) continue; - var windowPanel = CreateWindowPanel(i, window); - windowsPanel.Controls.Add(windowPanel); + var windowPanelForm = new WindowPanelForm(); + windowPanelForm.WindowIndex = i; + windowPanelForm.WindowProcess = window; + windowPanelForm.IsFirst = (i == firstIndex); + windowPanelForm.IsLast = (i == lastIndex); + windowPanelForm.UpdateDisplay(); + + windowPanelForm.PickClicked += (sender, index) => PickButton_Click(sender, new EventArgs()); + windowPanelForm.UntrackClicked += (sender, index) => UntrackButton_Click(sender, new EventArgs()); + + windowsPanel.Controls.Add(windowPanelForm); } } - private Panel CreateWindowPanel(int index, Process window) { - var panel = new Panel(); - panel.Width = 520; - panel.Height = 80; - panel.BorderStyle = BorderStyle.FixedSingle; - panel.Margin = new Padding(5); - panel.BackColor = Color.LightGray; - - // Index label - var indexLabel = new Label(); - indexLabel.Text = $"Index: {index}"; - indexLabel.Location = new Point(10, 5); - indexLabel.AutoSize = true; - indexLabel.Font = new Font("Segoe UI", 9F, FontStyle.Bold); - panel.Controls.Add(indexLabel); - - // Process name - var nameLabel = new Label(); - nameLabel.Text = $"Name: {window.ProcessName}"; - nameLabel.Location = new Point(10, 25); - nameLabel.AutoSize = true; - nameLabel.Font = new Font("Segoe UI", 9F); - panel.Controls.Add(nameLabel); - - // PID - var pidLabel = new Label(); - pidLabel.Text = $"PID: {window.Id}"; - pidLabel.Location = new Point(10, 45); - pidLabel.AutoSize = true; - pidLabel.Font = new Font("Segoe UI", 9F); - panel.Controls.Add(pidLabel); - - // Window title - var titleLabel = new Label(); - titleLabel.Text = $"Title: {window.MainWindowTitle}"; - titleLabel.Location = new Point(200, 25); - titleLabel.AutoSize = true; - titleLabel.Font = new Font("Segoe UI", 9F); - titleLabel.MaximumSize = new Size(200, 0); - panel.Controls.Add(titleLabel); - - // First/Last indicator - var firstLastLabel = new Label(); - firstLastLabel.Text = GetFirstLastText(index); - firstLastLabel.Location = new Point(200, 45); - firstLastLabel.AutoSize = true; - firstLastLabel.Font = new Font("Segoe UI", 9F, FontStyle.Bold); - firstLastLabel.ForeColor = Color.DarkBlue; - panel.Controls.Add(firstLastLabel); - - // Pick button - var pickButton = new Button(); - pickButton.Text = "Pick"; - pickButton.Location = new Point(420, 10); - pickButton.Size = new Size(50, 25); - pickButton.Tag = index; - pickButton.Click += PickButton_Click; - panel.Controls.Add(pickButton); - - // Untrack button - var untrackButton = new Button(); - untrackButton.Text = "Untrack"; - untrackButton.Location = new Point(420, 45); - untrackButton.Size = new Size(50, 25); - untrackButton.Tag = index; - untrackButton.Click += UntrackButton_Click; - panel.Controls.Add(untrackButton); - - return panel; - } - private string GetFirstLastText(int index) { if (index == firstIndex && index == lastIndex) { return "First & Last"; diff --git a/DD2Switcher/WindowPanelForm.Designer.cs b/DD2Switcher/WindowPanelForm.Designer.cs new file mode 100644 index 0000000..87c4f65 --- /dev/null +++ b/DD2Switcher/WindowPanelForm.Designer.cs @@ -0,0 +1,133 @@ +namespace DD2Switcher { + partial class WindowPanelForm { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.indexLabel = new System.Windows.Forms.Label(); + this.nameLabel = new System.Windows.Forms.Label(); + this.pidLabel = new System.Windows.Forms.Label(); + this.titleLabel = new System.Windows.Forms.Label(); + this.firstLastLabel = new System.Windows.Forms.Label(); + this.pickButton = new System.Windows.Forms.Button(); + this.untrackButton = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // indexLabel + // + this.indexLabel.AutoSize = true; + this.indexLabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); + this.indexLabel.Location = new System.Drawing.Point(10, 5); + this.indexLabel.Name = "indexLabel"; + this.indexLabel.Size = new System.Drawing.Size(52, 15); + this.indexLabel.TabIndex = 0; + this.indexLabel.Text = "Index: 0"; + // + // nameLabel + // + this.nameLabel.AutoSize = true; + this.nameLabel.Font = new System.Drawing.Font("Segoe UI", 9F); + this.nameLabel.Location = new System.Drawing.Point(12, 35); + this.nameLabel.Name = "nameLabel"; + this.nameLabel.Size = new System.Drawing.Size(45, 15); + this.nameLabel.TabIndex = 1; + this.nameLabel.Text = "Name: "; + // + // pidLabel + // + this.pidLabel.AutoSize = true; + this.pidLabel.Font = new System.Drawing.Font("Segoe UI", 9F); + this.pidLabel.Location = new System.Drawing.Point(12, 50); + this.pidLabel.Name = "pidLabel"; + this.pidLabel.Size = new System.Drawing.Size(28, 15); + this.pidLabel.TabIndex = 2; + this.pidLabel.Text = "PID:"; + // + // titleLabel + // + this.titleLabel.AutoSize = true; + this.titleLabel.Font = new System.Drawing.Font("Segoe UI", 9F); + this.titleLabel.Location = new System.Drawing.Point(12, 20); + this.titleLabel.MaximumSize = new System.Drawing.Size(200, 0); + this.titleLabel.Name = "titleLabel"; + this.titleLabel.Size = new System.Drawing.Size(36, 15); + this.titleLabel.TabIndex = 3; + this.titleLabel.Text = "Title: "; + // + // firstLastLabel + // + this.firstLastLabel.AutoSize = true; + this.firstLastLabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); + this.firstLastLabel.ForeColor = System.Drawing.Color.DarkBlue; + this.firstLastLabel.Location = new System.Drawing.Point(200, 45); + this.firstLastLabel.Name = "firstLastLabel"; + this.firstLastLabel.Size = new System.Drawing.Size(0, 15); + this.firstLastLabel.TabIndex = 4; + // + // pickButton + // + this.pickButton.Location = new System.Drawing.Point(420, 10); + this.pickButton.Name = "pickButton"; + this.pickButton.Size = new System.Drawing.Size(88, 25); + this.pickButton.TabIndex = 5; + this.pickButton.Text = "Pick"; + this.pickButton.UseVisualStyleBackColor = true; + this.pickButton.Click += new System.EventHandler(this.pickButton_Click); + // + // untrackButton + // + this.untrackButton.Location = new System.Drawing.Point(420, 45); + this.untrackButton.Name = "untrackButton"; + this.untrackButton.Size = new System.Drawing.Size(88, 25); + this.untrackButton.TabIndex = 6; + this.untrackButton.Text = "Untrack"; + this.untrackButton.UseVisualStyleBackColor = true; + this.untrackButton.Click += new System.EventHandler(this.untrackButton_Click); + // + // WindowPanelForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.indexLabel); + this.Controls.Add(this.nameLabel); + this.Controls.Add(this.pidLabel); + this.Controls.Add(this.titleLabel); + this.Controls.Add(this.firstLastLabel); + this.Controls.Add(this.pickButton); + this.Controls.Add(this.untrackButton); + this.Name = "WindowPanelForm"; + this.Size = new System.Drawing.Size(520, 80); + this.ResumeLayout(false); + this.PerformLayout(); + } + + #endregion + + private System.Windows.Forms.Label indexLabel; + private System.Windows.Forms.Label nameLabel; + private System.Windows.Forms.Label pidLabel; + private System.Windows.Forms.Label titleLabel; + private System.Windows.Forms.Label firstLastLabel; + private System.Windows.Forms.Button pickButton; + private System.Windows.Forms.Button untrackButton; + } +} \ No newline at end of file diff --git a/DD2Switcher/WindowPanelForm.cs b/DD2Switcher/WindowPanelForm.cs new file mode 100644 index 0000000..8556f57 --- /dev/null +++ b/DD2Switcher/WindowPanelForm.cs @@ -0,0 +1,47 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using System.Windows.Forms; + +namespace DD2Switcher { + public partial class WindowPanelForm : UserControl { + public event EventHandler PickClicked; + public event EventHandler UntrackClicked; + + public int WindowIndex { get; set; } + public Process WindowProcess { get; set; } + public bool IsFirst { get; set; } + public bool IsLast { get; set; } + + public WindowPanelForm() { + InitializeComponent(); + } + + public void UpdateDisplay() { + if (WindowProcess != null) { + indexLabel.Text = $"Index: {WindowIndex}"; + nameLabel.Text = $"Name: {WindowProcess.ProcessName}"; + pidLabel.Text = $"PID: {WindowProcess.Id}"; + titleLabel.Text = $"Title: {WindowProcess.MainWindowTitle}"; + + if (IsFirst && IsLast) { + firstLastLabel.Text = "First & Last"; + } else if (IsFirst) { + firstLastLabel.Text = "First"; + } else if (IsLast) { + firstLastLabel.Text = "Last"; + } else { + firstLastLabel.Text = ""; + } + } + } + + private void pickButton_Click(object sender, EventArgs e) { + PickClicked?.Invoke(this, WindowIndex); + } + + private void untrackButton_Click(object sender, EventArgs e) { + UntrackClicked?.Invoke(this, WindowIndex); + } + } +} \ No newline at end of file diff --git a/DD2Switcher/WindowPanelForm.resx b/DD2Switcher/WindowPanelForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/DD2Switcher/WindowPanelForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file