From 99ad0b8920e0aca2c5773e2a7ec5707ef8d38b9f Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 19 Feb 2021 22:36:02 +0800 Subject: [PATCH] Refactor Profile Feat Multiple Rows ProfileTable Cut Remove Profile Confirm --- Netch/Forms/MainForm.cs | 144 +++++++++++++++++++++------------------- Netch/Models/Profile.cs | 7 +- Netch/Models/Setting.cs | 6 ++ 3 files changed, 85 insertions(+), 72 deletions(-) diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index aee9f13d..63bd5fb4 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -122,6 +122,8 @@ namespace Netch.Forms _configurationGroupBoxHeight = ConfigurationGroupBox.Height; _profileConfigurationHeight = ConfigurationGroupBox.Controls[0].Height / 3; // 因为 AutoSize, 所以得到的是Controls的总高度 + _profileGroupBoxPaddingHeight = ProfileGroupBox.Height - ProfileTable.Height; + _profileTableHeight = ProfileTable.Height; } private void InitText() @@ -875,44 +877,50 @@ namespace Netch.Forms private int _configurationGroupBoxHeight; private int _profileConfigurationHeight; + private int _profileGroupBoxPaddingHeight; + private int _profileTableHeight; private void InitProfile() { // Clear - foreach (var button in ProfileButtons) - button.Dispose(); + foreach (var button in ProfileTable.Controls) + ((Button) button).Dispose(); - ProfileButtons.Clear(); + ProfileTable.Controls.Clear(); ProfileTable.ColumnStyles.Clear(); ProfileTable.RowStyles.Clear(); - var numProfile = Global.Settings.ProfileCount; - if (numProfile == 0) + var profileCount = Global.Settings.ProfileCount; + if (profileCount == 0) { // Hide Profile GroupBox, Change window size configLayoutPanel.RowStyles[2].SizeType = SizeType.Percent; configLayoutPanel.RowStyles[2].Height = 0; ProfileGroupBox.Visible = false; - ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight - _profileConfigurationHeight); + ConfigurationGroupBox.Height = _configurationGroupBoxHeight - _profileConfigurationHeight; } else { // Load Profiles - ProfileTable.ColumnCount = numProfile; - while (Global.Settings.Profiles.Count < numProfile) - Global.Settings.Profiles.Add(new Profile()); + var columnCount = Global.Settings.ProfileTableColumnCount; - for (var i = 0; i < numProfile; ++i) + ProfileTable.ColumnCount = profileCount >= columnCount ? columnCount : profileCount; + ProfileTable.RowCount = (int) Math.Ceiling(profileCount / (float) columnCount); + + for (var i = 0; i < profileCount; ++i) { - var b = new Button(); - b.Click += ProfileButton_Click; - b.Dock = DockStyle.Fill; - b.Text = !Global.Settings.Profiles[i].IsDummy ? Global.Settings.Profiles[i].ProfileName : i18N.Translate("None"); + var profile = Global.Settings.Profiles.SingleOrDefault(p => p.Index == i); + var b = new Button + { + Dock = DockStyle.Fill, + Text = profile?.ProfileName ?? i18N.Translate("None"), + Tag = profile + }; - ProfileTable.Controls.Add(b, i, 0); - ProfileButtons.Add(b); + b.Click += ProfileButton_Click; + ProfileTable.Controls.Add(b, i % columnCount, i / columnCount); } // equal column @@ -924,21 +932,18 @@ namespace Netch.Forms configLayoutPanel.RowStyles[2].SizeType = SizeType.AutoSize; ProfileGroupBox.Visible = true; - ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight); + ProfileGroupBox.Height = ProfileTable.RowCount * _profileTableHeight + _profileGroupBoxPaddingHeight; + ConfigurationGroupBox.Height = _configurationGroupBoxHeight; } } - private void LoadProfile(int index) + private void LoadProfile(Profile profile) { - var p = Global.Settings.Profiles[index]; - ProfileNameText.Text = p.ProfileName; + ProfileNameText.Text = profile.ProfileName; ModeComboBox.ResetCompletionList(); - if (p.IsDummy) - throw new Exception("Profile not found."); - - var server = ServerComboBox.Items.Cast().FirstOrDefault(s => s.Remark.Equals(p.ServerRemark)); - var mode = ModeComboBox.Items.Cast().FirstOrDefault(m => m.Remark.Equals(p.ModeRemark)); + var server = ServerComboBox.Items.Cast().FirstOrDefault(s => s.Remark.Equals(profile.ServerRemark)); + var mode = ModeComboBox.Items.Cast().FirstOrDefault(m => m.Remark.Equals(profile.ModeRemark)); if (server == null) throw new Exception("Server not found."); @@ -950,67 +955,70 @@ namespace Netch.Forms ModeComboBox.SelectedItem = mode; } - private void SaveProfile(int index) + private Profile CreateProfileAtIndex(int index) { - var selectedServer = (Server) ServerComboBox.SelectedItem; - var selectedMode = (Models.Mode) ModeComboBox.SelectedItem; + var server = (Server) ServerComboBox.SelectedItem; + var mode = (Models.Mode) ModeComboBox.SelectedItem; var name = ProfileNameText.Text; - Global.Settings.Profiles[index] = new Profile(selectedServer, selectedMode, name); + Profile profile; + if ((profile = Global.Settings.Profiles.SingleOrDefault(p => p.Index == index)) != null) + Global.Settings.Profiles.Remove(profile); + profile = new Profile(server, mode, name, index); + Global.Settings.Profiles.Add(profile); + return profile; } - private void RemoveProfile(int index) - { - Global.Settings.Profiles[index] = new Profile(); - } - - private readonly List