From 7ea30b7136f3e9feec3a61d74cd609bbe97c2ef9 Mon Sep 17 00:00:00 2001 From: DHancock Date: Mon, 1 Jun 2020 20:26:13 +0100 Subject: [PATCH] Tidy up code for next release A bit of code cleaning and add Control+W and Alt+F4 keyboard sequences to close the chooser dialog. --- Source/CharacterChooserExt.cs | 50 +++++++++------------------------- Source/CharacterChooserForm.cs | 39 ++++++++++++-------------- 2 files changed, 31 insertions(+), 58 deletions(-) diff --git a/Source/CharacterChooserExt.cs b/Source/CharacterChooserExt.cs index d00aeda..c56ba08 100644 --- a/Source/CharacterChooserExt.cs +++ b/Source/CharacterChooserExt.cs @@ -19,18 +19,17 @@ You should have received a copy of the GNU General Public License using System; using System.Collections.Generic; -using System.Diagnostics; using System.Drawing; using System.Windows.Forms; -#if DEBUG using System.IO; -#endif using KeePass.Plugins; using KeePass.Resources; using KeePassLib; using KeePassLib.Security; +using CharacterChooser.Properties; + namespace CharacterChooser { @@ -45,17 +44,8 @@ public CharacterChooserExt() MenuItems = new List(); } - /// - /// The Initialize method is called by KeePass when - /// you should initialize your plug in. - /// - /// Plug in host interface. Through this - /// interface you can access the KeePass main window, the - /// currently opened database, etc. - /// You must return true in order to signal - /// successful initialization. If you return false, - /// KeePass unloads your plug in (without calling the - /// Terminate method of your plug in). + + public override bool Initialize(IPluginHost host) { if (host == null) @@ -71,11 +61,11 @@ private void MainWindow_UIStateUpdated(object sender, EventArgs e) { if (MenuItems.Count > 0) { + // only enable if one entry is selected and it has a valid field bool enable = false; if (Host.MainWindow.GetSelectedEntriesCount() == 1) { - // only enable if one of the selected entry's relevant fields has some data PwEntry entry = Host.MainWindow.GetSelectedEntry(true); foreach (KeyValuePair kvp in entry.Strings) @@ -96,9 +86,6 @@ private void MainWindow_UIStateUpdated(object sender, EventArgs e) private static bool FieldIsValid(KeyValuePair field) { - Debug.Assert(field.Value != null); - Debug.Assert(field.Key != null); - if (field.Value.IsEmpty) return false; @@ -124,7 +111,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t) // In KeePass 2.41 this is called once and the single menu item added to an // entry's context menu. In KeePass 2.42.1 it is called twice, the menus are // added to the context menu and the new Entry drop down menu on the main window. - ToolStripMenuItem menuItem = new ToolStripMenuItem(Properties.Resources.menu_text, null, new ToolStripMenuItem()); + ToolStripMenuItem menuItem = new ToolStripMenuItem(Resources.menu_text, null, new ToolStripMenuItem()); menuItem.DropDownOpening += delegate (object sender, EventArgs e) @@ -134,7 +121,6 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t) // rebuild current... List userFields = new List(); - PwEntry entry = Host.MainWindow.GetSelectedEntry(true); foreach (KeyValuePair kvp in entry.Strings) @@ -143,7 +129,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t) { ToolStripMenuItem subMenuItem = new ToolStripMenuItem { - Image = Properties.Resources.Menu_16x, + Image = Resources.Menu_16x, Tag = kvp.Value }; @@ -165,9 +151,6 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t) } } - // the plugin's menu item shouldn't have been enabled - Debug.Assert((menuItem.DropDownItems.Count > 0) || (userFields.Count > 0)); - if (userFields.Count > 0) menuItem.DropDownItems.AddRange(userFields.ToArray()); }; @@ -191,8 +174,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t) /// - /// URL of a version information file. See - /// https://keepass.info/help/v2_dev/plg_index.html#upd + /// URL of a version information file /// public override string UpdateUrl { @@ -210,34 +192,28 @@ public override string UpdateUrl /// - /// Get a handle to a 16x16 icon representing the plugin. - /// This icon is shown in the plugin management window of - /// KeePass for example. + /// a 16x16 icon representing the plugin /// public override Image SmallIcon { - get { return Properties.Resources.Plugin_16x; } + get { return Resources.Plugin_16x; } } + /// - /// The Terminate function is called by KeePass when - /// you should free all resources, close open files/streams, - /// etc. It is also recommended that you remove all your - /// plugin menu items from the KeePass menu. + /// Clean up when this plugin is unloaded /// public override void Terminate() { Host.MainWindow.UIStateUpdated -= MainWindow_UIStateUpdated; foreach (ToolStripMenuItem menuItem in MenuItems) - { + { ToolStrip parent = menuItem.GetCurrentParent(); if (parent != null) parent.Items.Remove(menuItem); - - menuItem.Dispose(); } MenuItems.Clear(); diff --git a/Source/CharacterChooserForm.cs b/Source/CharacterChooserForm.cs index c82bac8..82fafdd 100644 --- a/Source/CharacterChooserForm.cs +++ b/Source/CharacterChooserForm.cs @@ -57,10 +57,6 @@ private CharacterChooserForm() internal CharacterChooserForm(IPluginHost host, ToolStripItemCollection fields, ToolStripItem startField) : this() { - Debug.Assert(host != null); - Debug.Assert(fields != null); - Debug.Assert(startField != null); - Host = host; // the standard KeePass app icon @@ -143,7 +139,7 @@ private void AdjustLayoutForPasswordFont() { Font passwordFont = KeePass.Program.Config.UI.PasswordFont.ToFont(); - if (!Utils.FontEquals(passwordFont, charTextBox1.Font)) + if (!Utils.EqivalentFont(passwordFont, charTextBox1.Font)) { int verticalOffset = 0; @@ -262,8 +258,6 @@ private void FieldComboBox_SelectedIndexChanged(object sender, EventArgs e) private void IndexComboBox_SelectedIndexChanged(object sender, EventArgs e) { - Debug.Assert(sender is ComboBox); - bool entryProtected = false; bool characterSelected = false; @@ -284,7 +278,7 @@ private bool LoadCharacterTextBox(int selectedIndex, TextBox textBox) { ProtectedString pString = (ProtectedString)((ToolStripItem)fieldComboBox.SelectedItem).Tag; - Debug.Assert(selectedIndex <= pString.Length); + // the first combo box item is a dash indicating that there is no character selected if (selectedIndex < 1) textBox.Text = string.Empty; @@ -307,7 +301,10 @@ private bool LoadCharacterTextBox(int selectedIndex, TextBox textBox) [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { - if ((keyData == Keys.Escape) || (keyData == Keys.Enter)) + if ((keyData == Keys.Escape) || + (keyData == Keys.Enter) || + (keyData == (Keys.Control | Keys.W)) || + (keyData == (Keys.Alt | Keys.F4))) { Close(); return true; @@ -348,9 +345,7 @@ private void ProtectButtonEnabledStateChanged(object sender, EventArgs e) private void CharTextBox_TextChanged(object sender, EventArgs e) { - Debug.Assert(sender is TextBox); - - TextBox tb = sender as TextBox; + TextBox tb = (TextBox)sender; if (string.IsNullOrEmpty(tb.Text)) tb.Enabled = false; @@ -365,8 +360,7 @@ private void CharTextBox_TextChanged(object sender, EventArgs e) private void ToolTip_Popup(object sender, PopupEventArgs e) { - Debug.Assert(e.AssociatedControl is TextBox); - e.Cancel = (e.AssociatedControl as TextBox).UseSystemPasswordChar; + e.Cancel = ((TextBox)e.AssociatedControl).UseSystemPasswordChar; } @@ -429,9 +423,6 @@ private sealed class CharacterSelectorRow public CharacterSelectorRow(ComboBox comboBox, TextBox textBox) { - Debug.Assert(comboBox != null); - Debug.Assert(textBox != null); - IndexComboBox = comboBox; CharTextBox = textBox; } @@ -458,13 +449,19 @@ public static int CalculateDropDownWidth(ComboBox comboBox) return width; } + - - public static bool FontEquals(Font left, Font right) + // Check fonts are the same. The Font.Equals() method + // is too strict. + public static bool EqivalentFont(Font left, Font right) { - Debug.Assert(left != null); - Debug.Assert(right != null); + if (ReferenceEquals(left, right)) + return true; + + if ((left == null) || (right == null)) + return false; + // the font family should never be null return (left.FontFamily.Equals(right.FontFamily)) && (left.SizeInPoints == right.SizeInPoints) && (left.Style == right.Style);