diff --git a/src/TestCentric/testcentric.gui/Controls/StretchToolStripTextBox.cs b/src/TestCentric/testcentric.gui/Controls/StretchToolStripTextBox.cs new file mode 100644 index 00000000..1cd1522d --- /dev/null +++ b/src/TestCentric/testcentric.gui/Controls/StretchToolStripTextBox.cs @@ -0,0 +1,35 @@ +// *********************************************************************** +// Copyright (c) Charlie Poole and TestCentric contributors. +// Licensed under the MIT License. See LICENSE file in root directory. +// *********************************************************************** + +using System.Drawing; +using System.Windows.Forms; +using System; + +namespace TestCentric.Gui.Controls +{ + /// + /// This class is required to stretch a ToolStripTextBox control within a ToolStrip to fill the available space and to resize when the control resizes. + /// The implementation is from the Microsoft Windows Forms documentation, but simplified to the current use case. + /// "How to: Stretch a ToolStripTextBox to Fill the Remaining Width of a ToolStrip" + /// https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/stretch-a-toolstriptextbox-to-fill-the-remaining-width-of-a-toolstrip-wf?view=netframeworkdesktop-4.8 + /// + internal class StretchToolStripTextBox : ToolStripTextBox + { + public override Size GetPreferredSize(Size constrainingSize) + { + // Get width of the owning ToolStrip + int textBoxMargin = 2; + Int32 width = Owner.DisplayRectangle.Width - textBoxMargin; + + // If the available width is less than the default width, use the default width + if (width < DefaultSize.Width) width = DefaultSize.Width; + + // Retrieve the preferred size from the base class, but change the width to the calculated width. + Size size = base.GetPreferredSize(constrainingSize); + size.Width = width; + return size; + } + } +} diff --git a/src/TestCentric/testcentric.gui/Elements/IChanged.cs b/src/TestCentric/testcentric.gui/Elements/IChanged.cs new file mode 100644 index 00000000..fb8911e3 --- /dev/null +++ b/src/TestCentric/testcentric.gui/Elements/IChanged.cs @@ -0,0 +1,19 @@ +// *********************************************************************** +// Copyright (c) Charlie Poole and TestCentric contributors. +// Licensed under the MIT License. See LICENSE file in root directory. +// *********************************************************************** + +namespace TestCentric.Gui.Elements +{ + /// + /// The IChanged interface represents a IViewElement. + /// If the IViewElement changes, it will raise the Changed event. + /// + public interface IChanged : IViewElement + { + /// + /// Event raised when the element is changed by the user + /// + event CommandHandler Changed; + } +} diff --git a/src/TestCentric/testcentric.gui/Elements/ToolStripTextBoxElement.cs b/src/TestCentric/testcentric.gui/Elements/ToolStripTextBoxElement.cs new file mode 100644 index 00000000..787c2d27 --- /dev/null +++ b/src/TestCentric/testcentric.gui/Elements/ToolStripTextBoxElement.cs @@ -0,0 +1,96 @@ +// *********************************************************************** +// Copyright (c) Charlie Poole and TestCentric contributors. +// Licensed under the MIT License. See LICENSE file in root directory. +// *********************************************************************** + + +using System; +using System.Windows.Forms; + +namespace TestCentric.Gui.Elements +{ + /// + /// This class implements the IChanged interface for a ToolStripTextBox control. It provides this additional functionality: + /// - show a PlaceHoder text if there's no text input + /// - Invoke the Changed event as soon as no further input is made within a short period of time. + /// + public class ToolStripTextBoxElement : ToolStripElement, IChanged + { + private Timer _typingTimer; + + public event CommandHandler Changed; + + public ToolStripTextBoxElement(ToolStripTextBox textBox, string placeHolderText) + : base(textBox) + { + TextBox = textBox; + PlaceHolderText = placeHolderText; + TextBox.TextChanged += OnTextChanged; + + TextBox.LostFocus += OnTextBoxLostFocus; + TextBox.GotFocus += OnTextBoxGotFocus; + + // Call LostFocus to set initial text and color + OnTextBoxLostFocus(null, EventArgs.Empty); + } + + private string PlaceHolderText { get; set; } + + private ToolStripTextBox TextBox { get; } + + private bool IsPlaceHolderTextShown { get; set; } + + private void OnTextBoxGotFocus(object sender, EventArgs e) + { + // If the PlaceHolderText is shown, replace it with an empty text + if (IsPlaceHolderTextShown) + { + TextBox.Text = ""; + TextBox.ForeColor = System.Drawing.Color.Black; + IsPlaceHolderTextShown = false; + } + } + + private void OnTextBoxLostFocus(object sender, EventArgs e) + { + // If there's no text input, show the PlaceHolderText instead + string searchText = TextBox.Text; + if (string.IsNullOrEmpty(searchText) && !string.IsNullOrEmpty(PlaceHolderText)) + { + IsPlaceHolderTextShown = true; + TextBox.Text = PlaceHolderText; + TextBox.ForeColor = System.Drawing.Color.LightGray; + } + } + + private void OnTextChanged(object sender, EventArgs e) + { + if (IsPlaceHolderTextShown) + return; + + if (_typingTimer == null) + { + _typingTimer = new Timer(); + _typingTimer.Interval = 600; + _typingTimer.Tick += TypingTimerTimeout; + } + + _typingTimer.Stop(); + _typingTimer.Start(); + } + + private void TypingTimerTimeout(object sender, EventArgs e) + { + var timer = sender as Timer; + if (timer == null) + return; + + // The timer must be stopped! + timer.Stop(); + if (Changed != null) + Changed(); + + TextBox.Focus(); + } + } +} diff --git a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs index 722cb285..bb08aecc 100644 --- a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs +++ b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs @@ -257,6 +257,12 @@ private void WireUpEvents() _model.TestCentricTestFilter.OutcomeFilter = filter; }; + _view.TextFilter.Changed += () => + { + var text = _view.TextFilter.Text; + _model.TestCentricTestFilter.TextFilter = text; + }; + // Node selected in tree //_treeView.SelectedNodesChanged += (nodes) => //{ diff --git a/src/TestCentric/testcentric.gui/Views/ITestTreeView.cs b/src/TestCentric/testcentric.gui/Views/ITestTreeView.cs index 1bd1d1a1..806608e5 100644 --- a/src/TestCentric/testcentric.gui/Views/ITestTreeView.cs +++ b/src/TestCentric/testcentric.gui/Views/ITestTreeView.cs @@ -53,6 +53,8 @@ public interface ITestTreeView : IView // Test Filter related properties / methods IMultiSelection OutcomeFilter { get; } + IChanged TextFilter { get; } + void SetTestFilterVisibility(bool visible); // Tree-related Methods diff --git a/src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs b/src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs index 9c3a8696..ab902a21 100644 --- a/src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs +++ b/src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs @@ -1,5 +1,7 @@ namespace TestCentric.Gui.Views { + using TestCentric.Gui.Controls; + partial class TestTreeView { /// @@ -37,6 +39,8 @@ private void InitializeComponent() this.filterOutcomeFailedButton = new System.Windows.Forms.ToolStripButton(); this.filterOutcomeWarningButton = new System.Windows.Forms.ToolStripButton(); this.filterOutcomeNotRunButton = new System.Windows.Forms.ToolStripButton(); + this.filterTextToolStrip = new System.Windows.Forms.ToolStrip(); + this.filterTextBox = new StretchToolStripTextBox(); this.testTreeContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); this.runMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.debugMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -128,6 +132,22 @@ private void InitializeComponent() this.filterToolStrip.Size = new System.Drawing.Size(744, 24); this.filterToolStrip.TabIndex = 0; // + // filterTextBox + // + this.filterTextBox.Name = "filterTextBox"; + // + // filterTextToolStrip + // + this.filterTextToolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.filterTextBox, + }); + this.filterTextToolStrip.Location = new System.Drawing.Point(0, 0); + this.filterTextToolStrip.Name = "filterTextToolStrip"; + this.filterTextToolStrip.Size = new System.Drawing.Size(744, 24); + this.filterTextToolStrip.Stretch = true; + this.filterTextToolStrip.Visible = false; + this.filterTextToolStrip.TabIndex = 1; + // // testTreeContextMenu // this.testTreeContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -228,6 +248,7 @@ private void InitializeComponent() this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.treeView); + this.Controls.Add(this.filterTextToolStrip); this.Controls.Add(this.filterToolStrip); this.Name = "TestTreeView"; this.Size = new System.Drawing.Size(191, 246); @@ -246,6 +267,8 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripButton filterOutcomeFailedButton; private System.Windows.Forms.ToolStripButton filterOutcomeWarningButton; private System.Windows.Forms.ToolStripButton filterOutcomeNotRunButton; + private System.Windows.Forms.ToolStrip filterTextToolStrip; + private System.Windows.Forms.ToolStripTextBox filterTextBox; private System.Windows.Forms.ToolStripMenuItem runMenuItem; private System.Windows.Forms.ToolStripMenuItem expandAllMenuItem; private System.Windows.Forms.ToolStripMenuItem collapseAllMenuItem; diff --git a/src/TestCentric/testcentric.gui/Views/TestTreeView.cs b/src/TestCentric/testcentric.gui/Views/TestTreeView.cs index 981281d0..ef0eeed5 100644 --- a/src/TestCentric/testcentric.gui/Views/TestTreeView.cs +++ b/src/TestCentric/testcentric.gui/Views/TestTreeView.cs @@ -49,6 +49,7 @@ public TestTreeView() TestPropertiesCommand = new CommandMenuElement(testPropertiesMenuItem); ViewAsXmlCommand = new CommandMenuElement(viewAsXmlMenuItem); OutcomeFilter = new MultiCheckedToolStripButtonGroup(new[] { filterOutcomePassedButton, filterOutcomeFailedButton, filterOutcomeWarningButton, filterOutcomeNotRunButton }); + TextFilter = new ToolStripTextBoxElement(filterTextBox, "Filter..."); TreeView = treeView; // NOTE: We use MouseDown here rather than MouseUp because @@ -127,6 +128,8 @@ public bool CheckBoxes public IMultiSelection OutcomeFilter { get; private set; } + public IChanged TextFilter { get; private set; } + public TreeNode ContextNode { get; private set; } public ContextMenuStrip TreeContextMenu => TreeView.ContextMenuStrip; @@ -205,6 +208,7 @@ public void InvokeIfRequired(MethodInvoker _delegate) public void SetTestFilterVisibility(bool isVisible) { filterToolStrip.Visible = isVisible; + filterTextToolStrip.Visible = isVisible; } public void LoadAlternateImages(string imageSet) diff --git a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs index 93f2d24e..a873eb96 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs @@ -249,6 +249,19 @@ public void OutcomeFilterChanged_ApplyFilter() _model.TestCentricTestFilter.Received().OutcomeFilter = selectedItems; } + [Test] + public void TextFilterChanged_ApplyFilter() + { + // 1. Arrange + _view.TextFilter.Text.Returns("TestA"); + + // 2. Act + _view.TextFilter.Changed += Raise.Event(); + + // 3. Assert + _model.TestCentricTestFilter.Received().TextFilter = "TestA"; + } + // TODO: Version 1 Test - Make it work if needed. //[Test] //public void WhenContextNodeIsNotNull_RunCommandExecutesThatTest()