搜索功能完善

dev2.0
洁 任 2025-04-14 11:01:01 +08:00
parent 05e5fe75e0
commit 122a1f16f2
6 changed files with 284 additions and 52 deletions

View File

@ -582,7 +582,6 @@
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</h:ApplicationBase.Resources>

View File

@ -127,17 +127,6 @@ namespace Hopetry.Provider.Behaviors
{
dataGrid.BeginningEdit += DataGrid_BeginningEdit;
}
// 确保DataGrid正确初始化
//Dispatcher.BeginInvoke(new Action(() =>
//{
// var dataGrid = FindVisualChild<DataGrid>(AssociatedObject);
// if (dataGrid != null)
// {
// dataGrid.SelectionMode = DataGridSelectionMode.Extended;
// dataGrid.SelectionUnit = DataGridSelectionUnit.FullRow;
// }
//}), DispatcherPriority.Loaded);
AssociatedObject.Loaded += OnExplorerLoaded;
}
@ -210,7 +199,7 @@ namespace Hopetry.Provider.Behaviors
.Build();
}
public async void RefreshMinIOPath(string path)
public async void RefreshMinIOPath(string path,string searchText="")
{
if (AssociatedObject == null || _minioClient == null) return;
@ -219,9 +208,12 @@ namespace Hopetry.Provider.Behaviors
AssociatedObject.IsEnabled = false;
AssociatedObject.Cursor = System.Windows.Input.Cursors.Wait;
var items = await GetMinIOItemsAsync(path);
var items = await GetMinIOItemsAsync(path, searchText);
AssociatedObject.ItemsSource = items.ToObservable();
//var items = await GetMinIOItemsAsync(path);
//AssociatedObject.ItemsSource = items.ToObservable();
// 2. 刷新导航栏
var navigationBar = FindVisualChild<NavigationBar>(AssociatedObject);
if (navigationBar != null)
@ -250,7 +242,7 @@ namespace Hopetry.Provider.Behaviors
}
}
private async Task<IEnumerable<SystemInfoModel>> GetMinIOItemsAsync(string path)
private async Task<IEnumerable<SystemInfoModel>> GetMinIOItemsAsync(string path,string searchText)
{
var items = new List<SystemInfoModel>();
@ -298,7 +290,7 @@ namespace Hopetry.Provider.Behaviors
}
// 按修改时间降序排序(最新修改的排前面)
return items.OrderByDescending(x =>
return items.Where(x=>x.DisplayName.Contains(searchText)).OrderByDescending(x =>
{
if (x is MinIOFileModel fileModel)
{
@ -800,6 +792,10 @@ namespace Hopetry.Provider.Behaviors
navigationBar.ItemsSource = dirs.Select(l => new DirectoryModel(l));
}
#endregion
#region 搜索行为实现
#endregion
}

View File

@ -0,0 +1,263 @@
using HeBianGu.Base.WpfBase;
using HeBianGu.Control.Explorer;
using HeBianGu.General.WpfControlLib;
using Hopetry.Provider.Behaviors;
using Minio.DataModel.Args;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace Hopetry.Provider.Behaviors
{
public class MinIOSearchBehavior : Behavior<Explorer>
{
#region
//private ExplorerMinIOBehavior _minioBehavior;
//private TextBox _searchBox;
//private DispatcherTimer _searchDebounceTimer;
//protected override void OnAttached()
//{
// base.OnAttached();
// AssociatedObject.Loaded += (s, e) =>
// {
// _minioBehavior = Interaction.GetBehaviors(AssociatedObject)
// .OfType<ExplorerMinIOBehavior>()
// .FirstOrDefault();
// _searchBox = FindSearchBox();
// if (_searchBox != null)
// {
// // 移除原来的 TextChanged 事件
// //_searchBox.TextChanged -= OnSearchTextChanged;
// // 改为监听 KeyDown 事件
// _searchBox.KeyDown += OnSearchKeyDown;
// }
// };
//}
//private void OnSearchKeyDown(object sender, KeyEventArgs e)
//{
// // 如果按下的是 Enter 键,才执行搜索
// if (e.Key == Key.Enter)
// {
// if (_minioBehavior == null || !_minioBehavior.UseMinIO || _searchBox == null)
// return;
// string searchText = _searchBox.Text;
// if (!string.IsNullOrEmpty(searchText))
// {
// _minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath, searchText);
// }
// else
// {
// _minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath);
// }
// }
//}
//private TextBox FindSearchBox()
//{
// var searchBox = FindVisualChildren<TextBox>(AssociatedObject)
// .FirstOrDefault(tb =>
// tb.Style == AssociatedObject.FindResource(TextBoxKeys.ClearSingle) as Style &&
// BindingOperations.GetBindingExpression(tb, TextBox.TextProperty)?.ParentBinding?.Path?.Path == "AllSearchPattern"
// );
// if (searchBox != null)
// {
// // 1. 移除默认的 TextChanged 绑定
// BindingOperations.ClearBinding(searchBox, TextBox.TextProperty);
// // 2. 阻止 Explorer 内部更新 AllSearchPattern
// AssociatedObject.AllSearchPattern = null; // 清空默认值
// // 3. 改为手动处理 Enter 键搜索
// searchBox.KeyDown += OnSearchKeyDown;
// }
// return searchBox;
//}
//private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
//{
// if (depObj != null)
// {
// for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
// {
// DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
// if (child != null && child is T t)
// {
// yield return t;
// }
// foreach (T childOfChild in FindVisualChildren<T>(child))
// {
// yield return childOfChild;
// }
// }
// }
//}
//protected override void OnDetaching()
//{
// if (_searchBox != null)
// {
// _searchBox.KeyDown -= OnSearchKeyDown;
// }
// base.OnDetaching();
//}
#endregion
#region ceshi
private ExplorerMinIOBehavior _minioBehavior;
private TextBox _searchBox;
private bool _isHandlingSearch; // 防止递归调用
private bool _suppressTextChanged;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnExplorerLoaded;
}
private void OnExplorerLoaded(object sender, RoutedEventArgs e)
{
_minioBehavior = Interaction.GetBehaviors(AssociatedObject)
.OfType<ExplorerMinIOBehavior>()
.FirstOrDefault();
_searchBox = FindSearchBox();
if (_searchBox != null)
{
// 完全接管搜索框
TakeOverSearchBox();
}
}
private void TakeOverSearchBox()
{
// 2. 清除所有可能影响搜索的绑定
BindingOperations.ClearAllBindings(_searchBox);
// 2. 接管TextChanged事件
_searchBox.TextChanged -= OnSearchTextChanged;
_searchBox.TextChanged += OnSearchTextChanged;
// 3. 阻止Explorer内部处理搜索
AssociatedObject.AllSearchPattern = null;
// 4. 确保初始状态
_searchBox.Text = "";
// 5. 只保留我们的KeyDown处理
_searchBox.KeyDown -= OnSearchKeyDown;
_searchBox.KeyDown += OnSearchKeyDown;
// 4. 确保初始状态
_searchBox.Text = "";
// 确保不会触发默认搜索
//_searchBox.SetCurrentValue(TextBox.TextProperty, "");
}
private void OnSearchTextChanged(object sender, TextChangedEventArgs e)
{
string searchText = _searchBox.Text.Trim();
if (!string.IsNullOrEmpty(searchText))
{
_minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath, searchText);
}
else
{
_minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath);
}
}
private void OnSearchKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter || _isHandlingSearch)
return;
_isHandlingSearch = true;
try
{
if (_minioBehavior == null || !_minioBehavior.UseMinIO || _searchBox == null)
return;
string searchText = _searchBox.Text.Trim();
// 确保CurrentPath有效
if (string.IsNullOrEmpty(AssociatedObject.CurrentPath))
{
// 设置一个默认路径,避免显示根目录
AssociatedObject.CurrentPath = ""; // 替换为你的MinIO默认路径
}
if (!string.IsNullOrEmpty(searchText))
{
_minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath, searchText);
}
else
{
_minioBehavior.RefreshMinIOPath(AssociatedObject.CurrentPath);
}
}
finally
{
_isHandlingSearch = false;
}
}
private TextBox FindSearchBox()
{
return FindVisualChildren<TextBox>(AssociatedObject)
.FirstOrDefault(tb =>
tb.Style == AssociatedObject.FindResource(TextBoxKeys.ClearSingle) as Style &&
BindingOperations.GetBindingExpression(tb, TextBox.TextProperty)?.ParentBinding?.Path?.Path == "AllSearchPattern"
);
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) yield break;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child is T t)
yield return t;
foreach (var childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
protected override void OnDetaching()
{
if (_searchBox != null)
{
_searchBox.KeyDown -= OnSearchKeyDown;
_searchBox.TextChanged -= OnSearchTextChanged;
}
AssociatedObject.Loaded -= OnExplorerLoaded;
base.OnDetaching();
}
#endregion
}
}

View File

@ -1,36 +0,0 @@
using HeBianGu.Control.Explorer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Hopetry.Provider
{
public static class NavigationBarExtensions
{
public static readonly DependencyProperty RootDisplayNameProperty =
DependencyProperty.RegisterAttached("RootDisplayName", typeof(string), typeof(NavigationBarExtensions),
new PropertyMetadata("全部文件", OnRootDisplayNameChanged));
public static string GetRootDisplayName(DependencyObject obj)
{
return (string)obj.GetValue(RootDisplayNameProperty);
}
public static void SetRootDisplayName(DependencyObject obj, string value)
{
obj.SetValue(RootDisplayNameProperty, value);
}
private static void OnRootDisplayNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is NavigationBar navigationBar)
{
// 这里可以通过反射或其他方式修改内部显示逻辑
// 或者依赖行为来处理实际修改
}
}
}
}

View File

@ -50,7 +50,9 @@
Split="0.02"
StartValue="0" />-->
<behaviors:ExplorerMinIOBehavior UseMinIO="True" RootDisplayName="全部文件" />
<behaviors:MinIOSearchBehavior/>
</h:Interaction.Behaviors>
<!--<i:Interaction.Behaviors>
<behaviors:ExplorerMinIOBehavior UseMinIO="True" />
</i:Interaction.Behaviors>-->

View File

@ -32,6 +32,7 @@ using Minio.DataModel;
using Minio.DataModel.Args;
using Timer = System.Timers.Timer;
using System.Collections.ObjectModel;
using Hopetry.Provider;
namespace HeBianGu.App.Disk
{
@ -126,6 +127,7 @@ namespace HeBianGu.App.Disk
private int _uploadCount = 0;
private int _completeCount = 0;
private ExplorerMinIOBehavior _explorerBehavior;
private MinIOSearchBehavior _minioBehavior;
public ICommand UploadCommand { get; }
public ICommand UploadCommand1 { get; }
@ -154,6 +156,11 @@ namespace HeBianGu.App.Disk
{
_explorerBehavior = behavior;
}
public void SetExplorerBehavior(MinIOSearchBehavior behavior)
{
_minioBehavior = behavior;
}
private void UpdateProgress(object sender, System.Timers.ElapsedEventArgs e)
{
lock (_timerLock)
@ -634,6 +641,7 @@ namespace HeBianGu.App.Disk
_explorerBehavior.RefreshMinIOPath(CurrentMinIOPath);
MessageBox.Show($"已成功删除 {selectedItems.Count} 个项目");
SelectedItems.Clear();
}
}
catch (Exception ex)