2025-04-14 11:01:01 +08:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
2025-04-14 14:20:35 +08:00
|
|
|
|
// 1. 解除原生绑定
|
|
|
|
|
|
BindingOperations.ClearBinding(_searchBox, TextBox.TextProperty);
|
2025-04-14 11:01:01 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 阻止Explorer内部处理搜索
|
|
|
|
|
|
AssociatedObject.AllSearchPattern = null;
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 确保初始状态
|
|
|
|
|
|
_searchBox.Text = "";
|
2025-04-14 14:20:35 +08:00
|
|
|
|
// 3. 设置我们自己的绑定
|
|
|
|
|
|
_searchBox.SetBinding(TextBox.TextProperty, new Binding
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = this,
|
|
|
|
|
|
Path = new PropertyPath("SearchText"),
|
|
|
|
|
|
Mode = BindingMode.TwoWay,
|
|
|
|
|
|
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
|
|
|
|
|
|
});
|
2025-04-14 11:01:01 +08:00
|
|
|
|
// 5. 只保留我们的KeyDown处理
|
|
|
|
|
|
_searchBox.KeyDown -= OnSearchKeyDown;
|
|
|
|
|
|
_searchBox.KeyDown += OnSearchKeyDown;
|
|
|
|
|
|
}
|
2025-04-14 14:20:35 +08:00
|
|
|
|
// 使用依赖属性来管理搜索文本
|
|
|
|
|
|
public string SearchText
|
2025-04-14 11:01:01 +08:00
|
|
|
|
{
|
2025-04-14 14:20:35 +08:00
|
|
|
|
get { return (string)GetValue(SearchTextProperty); }
|
|
|
|
|
|
set { SetValue(SearchTextProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty SearchTextProperty =
|
|
|
|
|
|
DependencyProperty.Register("SearchText", typeof(string), typeof(MinIOSearchBehavior),
|
|
|
|
|
|
new PropertyMetadata(null, OnSearchTextChanged));
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnSearchTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
2025-04-14 11:01:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnSearchKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
|
{
|
2025-04-15 09:14:45 +08:00
|
|
|
|
if (e.Key != Key.Enter || _minioBehavior == null || !_minioBehavior.UseMinIO)
|
2025-04-14 11:01:01 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
2025-04-15 09:14:45 +08:00
|
|
|
|
// 直接从依赖属性获取值
|
|
|
|
|
|
string searchText = this.SearchText?.Trim();
|
|
|
|
|
|
string currentPath = AssociatedObject.CurrentPath ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(searchText))
|
2025-04-14 11:01:01 +08:00
|
|
|
|
{
|
2025-04-15 09:14:45 +08:00
|
|
|
|
_minioBehavior.RefreshMinIOPath(currentPath);
|
2025-04-14 11:01:01 +08:00
|
|
|
|
}
|
2025-04-15 09:14:45 +08:00
|
|
|
|
else
|
2025-04-14 11:01:01 +08:00
|
|
|
|
{
|
2025-04-15 09:14:45 +08:00
|
|
|
|
_minioBehavior.RefreshMinIOPath(currentPath, searchText);
|
2025-04-14 11:01:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
AssociatedObject.Loaded -= OnExplorerLoaded;
|
|
|
|
|
|
base.OnDetaching();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|