diff --git a/mRemoteV1/Tree/NodeSearcher.cs b/mRemoteV1/Tree/NodeSearcher.cs new file mode 100644 index 000000000..647ace718 --- /dev/null +++ b/mRemoteV1/Tree/NodeSearcher.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using System.Linq; +using mRemoteNG.Connection; + + +namespace mRemoteNG.Tree +{ + internal class NodeSearcher + { + private readonly ConnectionTreeModel _connectionTreeModel; + private ConnectionInfo _currentMatch; + + public List Matches { get; private set; } + + public NodeSearcher(ConnectionTreeModel connectionTreeModel) + { + _connectionTreeModel = connectionTreeModel; + } + + internal IEnumerable SearchByName(string searchText) + { + if (searchText == "") + ResetMatches(); + else + { + Matches = (List)_connectionTreeModel.GetRecursiveChildList().Where(node => node.Name.Contains(searchText)); + _currentMatch = Matches.First(); + } + return Matches; + } + + internal ConnectionInfo NextMatch() + { + var currentMatchIndex = Matches.IndexOf(_currentMatch); + if (currentMatchIndex < Matches.Count-1) + _currentMatch = Matches[currentMatchIndex + 1]; + return _currentMatch; + } + + internal ConnectionInfo PreviousMatch() + { + var currentMatchIndex = Matches.IndexOf(_currentMatch); + if (currentMatchIndex > 0) + _currentMatch = Matches[currentMatchIndex - 1]; + return _currentMatch; + } + + private void ResetMatches() + { + Matches = new List(); + _currentMatch = null; + } + } +} \ No newline at end of file diff --git a/mRemoteV1/UI/Window/ConnectionTreeWindow.cs b/mRemoteV1/UI/Window/ConnectionTreeWindow.cs index 5d598b6a4..e53d200ca 100644 --- a/mRemoteV1/UI/Window/ConnectionTreeWindow.cs +++ b/mRemoteV1/UI/Window/ConnectionTreeWindow.cs @@ -19,6 +19,7 @@ namespace mRemoteNG.UI.Window { private ConnectionTreeModel _connectionTreeModel; private ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler(); + private NodeSearcher _nodeSearcher; public ConnectionInfo SelectedNode => (ConnectionInfo) olvConnections.SelectedObject; @@ -135,6 +136,7 @@ namespace mRemoteNG.UI.Window private void PopulateTreeView() { olvConnections.Roots = ConnectionTreeModel.RootNodes; + _nodeSearcher = new NodeSearcher(ConnectionTreeModel); ExpandPreviouslyOpenedFolders(); ExpandRootConnectionNode(); OpenConnectionsFromLastSession(); @@ -817,11 +819,13 @@ namespace mRemoteNG.UI.Window } else if (e.KeyCode == Keys.Up) { - tvConnections.SelectedNode = tvConnections.SelectedNode.PrevVisibleNode; + //tvConnections.SelectedNode = tvConnections.SelectedNode.PrevVisibleNode; + olvConnections.SelectObject(_nodeSearcher.PreviousMatch()); } else if (e.KeyCode == Keys.Down) { - tvConnections.SelectedNode = tvConnections.SelectedNode.NextVisibleNode; + //tvConnections.SelectedNode = tvConnections.SelectedNode.NextVisibleNode; + olvConnections.SelectObject(_nodeSearcher.NextMatch()); } else { @@ -837,13 +841,8 @@ namespace mRemoteNG.UI.Window //TODO Fix for TreeListView private void txtSearch_TextChanged(object sender, EventArgs e) { - try - { - tvConnections.SelectedNode = ConnectionTree.Find(tvConnections.Nodes[0], txtSearch.Text); - } - catch (Exception) - { - } + var matches = _nodeSearcher.SearchByName(txtSearch.Text); + olvConnections.SelectObject(matches.First()); } //TODO Fix for TreeListView diff --git a/mRemoteV1/mRemoteV1.csproj b/mRemoteV1/mRemoteV1.csproj index 812761ceb..b7b553678 100644 --- a/mRemoteV1/mRemoteV1.csproj +++ b/mRemoteV1/mRemoteV1.csproj @@ -196,6 +196,7 @@ +