Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace SQLScriptsExplorer.Addin.Commands.ToolWindow
{
Expand All @@ -16,8 +17,13 @@ namespace SQLScriptsExplorer.Addin.Commands.ToolWindow
/// </summary>
public partial class MainToolWindowControl : UserControl
{
ISettingsRepository settingsRepository = null;
ITreeNodeRepository treeNodeRepository = null;
private ISettingsRepository settingsRepository = null;
private ITreeNodeRepository treeNodeRepository = null;

// DispatcherTimer to debounce the search
private DispatcherTimer searchTimer = null;
// You can adjust how long to wait before performing the search (e.g., 500ms)
private readonly TimeSpan SearchDelay = TimeSpan.FromMilliseconds(300);

public bool IsSearchResultsView
{
Expand All @@ -34,6 +40,11 @@ public MainToolWindowControl()
settingsRepository = new SettingsRepository();
treeNodeRepository = new TreeNodeRepository();

// Initialize the search debounce timer
searchTimer = new DispatcherTimer();
searchTimer.Interval = SearchDelay;
searchTimer.Tick += SearchTimer_Tick;

RefreshTreeView();
}

Expand All @@ -55,7 +66,8 @@ private void btnRefresh_Click(object sender, RoutedEventArgs e)

if (IsSearchResultsView)
{
txtSearch_KeyUp(sender, null);
// If currently in Search view, re-run the search after refresh
PerformSearch();
}
}

Expand All @@ -77,13 +89,11 @@ private void btnCollapseAll_Click(object sender, RoutedEventArgs e)
if (IsSearchResultsView)
{
TreeViewHelper.CollapseTreeView(treeNodeRepository.TreeSearchResult);

FileExplorerSearchResults.RefreshTreeView();
}
else
{
TreeViewHelper.CollapseTreeView(treeNodeRepository.Tree);

FileExplorerAll.RefreshTreeView();
}
}
Expand All @@ -93,13 +103,11 @@ private void btnExpandAll_Click(object sender, RoutedEventArgs e)
if (IsSearchResultsView)
{
TreeViewHelper.ExpandTreeView(treeNodeRepository.TreeSearchResult);

FileExplorerSearchResults.RefreshTreeView();
}
else
{
TreeViewHelper.ExpandTreeView(treeNodeRepository.Tree);

FileExplorerAll.RefreshTreeView();
}
}
Expand Down Expand Up @@ -129,7 +137,30 @@ public void RefreshTreeView()
FileExplorerAll.RefreshTreeView();
}

/// <summary>
/// KeyUp event: we reset/restart the timer, so that we only search
/// after user stops typing for a brief moment.
/// </summary>
private void txtSearch_KeyUp(object sender, KeyEventArgs e)
{
// Stop and restart the timer each time a key is pressed.
searchTimer.Stop();
searchTimer.Start();
}

/// <summary>
/// Called when the search timer elapses (no more key presses for the set interval).
/// </summary>
private void SearchTimer_Tick(object sender, EventArgs e)
{
searchTimer.Stop(); // Prevent multiple triggers
PerformSearch();
}

/// <summary>
/// Actually performs the search in the repository, updates the UI accordingly.
/// </summary>
private void PerformSearch()
{
if (!string.IsNullOrWhiteSpace(txtSearch.Text))
{
Expand All @@ -151,6 +182,7 @@ private void txtSearch_KeyUp(object sender, KeyEventArgs e)
FileExplorerAll.Visibility = Visibility.Visible;
}

// Update Filter property
FileExplorerSearchResults.Filter = txtSearch.Text;
}

Expand All @@ -162,7 +194,10 @@ private void FileExplorerSearchResults_TreeNodeAdded(object sender, System.Event
var treeNode = treeNodeSearchResults.Clone(treeNodeParent);

treeNodeParent.Children.Add(treeNode);
treeNodeParent.Children = treeNodeParent.Children.OrderBy(p => p.Type).ThenBy(p => p.FileName).ToList();
treeNodeParent.Children = treeNodeParent.Children
.OrderBy(p => p.Type)
.ThenBy(p => p.FileName)
.ToList();

FileExplorerAll.DataSourceDictionary.Add(treeNode.Id, treeNode);

Expand All @@ -175,7 +210,6 @@ private void FileExplorerSearchResults_TreeNodeDeleted(object sender, System.Eve
var treeNode = FileExplorerAll.DataSourceDictionary[treeNodeSearchResults.Id];

treeNode.Parent.Children.Remove(treeNode);

FileExplorerAll.DataSourceDictionary.Remove(treeNode.Id);

FileExplorerAll.RefreshTreeView();
Expand All @@ -189,7 +223,10 @@ private void FileExplorerSearchResults_TreeNodeRenamed(object sender, System.Eve
treeNode.FileName = treeNodeSearchResults.FileName;
treeNode.FileFullPath = treeNodeSearchResults.FileFullPath;

treeNode.Parent.Children = treeNode.Parent.Children.OrderBy(p => p.Type).ThenBy(p => p.FileName).ToList();
treeNode.Parent.Children = treeNode.Parent.Children
.OrderBy(p => p.Type)
.ThenBy(p => p.FileName)
.ToList();

FileExplorerAll.RefreshTreeView();
}
Expand All @@ -215,4 +252,4 @@ private void ToolBar_Loaded(object sender, RoutedEventArgs e)

#endregion
}
}
}