88using System . Windows ;
99using System . Windows . Controls ;
1010using System . Windows . Input ;
11+ using System . Windows . Threading ;
1112
1213namespace SQLScriptsExplorer . Addin . Commands . ToolWindow
1314{
@@ -16,8 +17,13 @@ namespace SQLScriptsExplorer.Addin.Commands.ToolWindow
1617 /// </summary>
1718 public partial class MainToolWindowControl : UserControl
1819 {
19- ISettingsRepository settingsRepository = null ;
20- ITreeNodeRepository treeNodeRepository = null ;
20+ private ISettingsRepository settingsRepository = null ;
21+ private ITreeNodeRepository treeNodeRepository = null ;
22+
23+ // DispatcherTimer to debounce the search
24+ private DispatcherTimer searchTimer = null ;
25+ // You can adjust how long to wait before performing the search (e.g., 500ms)
26+ private readonly TimeSpan SearchDelay = TimeSpan . FromMilliseconds ( 300 ) ;
2127
2228 public bool IsSearchResultsView
2329 {
@@ -34,6 +40,11 @@ public MainToolWindowControl()
3440 settingsRepository = new SettingsRepository ( ) ;
3541 treeNodeRepository = new TreeNodeRepository ( ) ;
3642
43+ // Initialize the search debounce timer
44+ searchTimer = new DispatcherTimer ( ) ;
45+ searchTimer . Interval = SearchDelay ;
46+ searchTimer . Tick += SearchTimer_Tick ;
47+
3748 RefreshTreeView ( ) ;
3849 }
3950
@@ -55,7 +66,8 @@ private void btnRefresh_Click(object sender, RoutedEventArgs e)
5566
5667 if ( IsSearchResultsView )
5768 {
58- txtSearch_KeyUp ( sender , null ) ;
69+ // If currently in Search view, re-run the search after refresh
70+ PerformSearch ( ) ;
5971 }
6072 }
6173
@@ -77,13 +89,11 @@ private void btnCollapseAll_Click(object sender, RoutedEventArgs e)
7789 if ( IsSearchResultsView )
7890 {
7991 TreeViewHelper . CollapseTreeView ( treeNodeRepository . TreeSearchResult ) ;
80-
8192 FileExplorerSearchResults . RefreshTreeView ( ) ;
8293 }
8394 else
8495 {
8596 TreeViewHelper . CollapseTreeView ( treeNodeRepository . Tree ) ;
86-
8797 FileExplorerAll . RefreshTreeView ( ) ;
8898 }
8999 }
@@ -93,13 +103,11 @@ private void btnExpandAll_Click(object sender, RoutedEventArgs e)
93103 if ( IsSearchResultsView )
94104 {
95105 TreeViewHelper . ExpandTreeView ( treeNodeRepository . TreeSearchResult ) ;
96-
97106 FileExplorerSearchResults . RefreshTreeView ( ) ;
98107 }
99108 else
100109 {
101110 TreeViewHelper . ExpandTreeView ( treeNodeRepository . Tree ) ;
102-
103111 FileExplorerAll . RefreshTreeView ( ) ;
104112 }
105113 }
@@ -129,7 +137,30 @@ public void RefreshTreeView()
129137 FileExplorerAll . RefreshTreeView ( ) ;
130138 }
131139
140+ /// <summary>
141+ /// KeyUp event: we reset/restart the timer, so that we only search
142+ /// after user stops typing for a brief moment.
143+ /// </summary>
132144 private void txtSearch_KeyUp ( object sender , KeyEventArgs e )
145+ {
146+ // Stop and restart the timer each time a key is pressed.
147+ searchTimer . Stop ( ) ;
148+ searchTimer . Start ( ) ;
149+ }
150+
151+ /// <summary>
152+ /// Called when the search timer elapses (no more key presses for the set interval).
153+ /// </summary>
154+ private void SearchTimer_Tick ( object sender , EventArgs e )
155+ {
156+ searchTimer . Stop ( ) ; // Prevent multiple triggers
157+ PerformSearch ( ) ;
158+ }
159+
160+ /// <summary>
161+ /// Actually performs the search in the repository, updates the UI accordingly.
162+ /// </summary>
163+ private void PerformSearch ( )
133164 {
134165 if ( ! string . IsNullOrWhiteSpace ( txtSearch . Text ) )
135166 {
@@ -151,6 +182,7 @@ private void txtSearch_KeyUp(object sender, KeyEventArgs e)
151182 FileExplorerAll . Visibility = Visibility . Visible ;
152183 }
153184
185+ // Update Filter property
154186 FileExplorerSearchResults . Filter = txtSearch . Text ;
155187 }
156188
@@ -162,7 +194,10 @@ private void FileExplorerSearchResults_TreeNodeAdded(object sender, System.Event
162194 var treeNode = treeNodeSearchResults . Clone ( treeNodeParent ) ;
163195
164196 treeNodeParent . Children . Add ( treeNode ) ;
165- treeNodeParent . Children = treeNodeParent . Children . OrderBy ( p => p . Type ) . ThenBy ( p => p . FileName ) . ToList ( ) ;
197+ treeNodeParent . Children = treeNodeParent . Children
198+ . OrderBy ( p => p . Type )
199+ . ThenBy ( p => p . FileName )
200+ . ToList ( ) ;
166201
167202 FileExplorerAll . DataSourceDictionary . Add ( treeNode . Id , treeNode ) ;
168203
@@ -175,7 +210,6 @@ private void FileExplorerSearchResults_TreeNodeDeleted(object sender, System.Eve
175210 var treeNode = FileExplorerAll . DataSourceDictionary [ treeNodeSearchResults . Id ] ;
176211
177212 treeNode . Parent . Children . Remove ( treeNode ) ;
178-
179213 FileExplorerAll . DataSourceDictionary . Remove ( treeNode . Id ) ;
180214
181215 FileExplorerAll . RefreshTreeView ( ) ;
@@ -189,7 +223,10 @@ private void FileExplorerSearchResults_TreeNodeRenamed(object sender, System.Eve
189223 treeNode . FileName = treeNodeSearchResults . FileName ;
190224 treeNode . FileFullPath = treeNodeSearchResults . FileFullPath ;
191225
192- treeNode . Parent . Children = treeNode . Parent . Children . OrderBy ( p => p . Type ) . ThenBy ( p => p . FileName ) . ToList ( ) ;
226+ treeNode . Parent . Children = treeNode . Parent . Children
227+ . OrderBy ( p => p . Type )
228+ . ThenBy ( p => p . FileName )
229+ . ToList ( ) ;
193230
194231 FileExplorerAll . RefreshTreeView ( ) ;
195232 }
@@ -215,4 +252,4 @@ private void ToolBar_Loaded(object sender, RoutedEventArgs e)
215252
216253 #endregion
217254 }
218- }
255+ }
0 commit comments