Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5c8e65e2b | ||
|
|
4309899c79 | ||
|
|
cf42cf19e3 | ||
|
|
c16ef849e6 | ||
|
|
1ee19b4023 | ||
|
|
da5069856e |
No files matched your search
@@ -318,6 +318,13 @@ public static class EnumerableExtensions {
|
||||
foreach (var item in itemsToRemove) source.Remove(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从集合中移除另一个集合中的所有元素。
|
||||
/// </summary>
|
||||
public static void RemoveAll<T>(this ICollection<T> source, IEnumerable<T> itemsToRemove) {
|
||||
foreach (var item in itemsToRemove) source.Remove(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仅保留满足条件的元素,移除不满足条件的元素。
|
||||
/// </summary>
|
||||
|
||||
@@ -127,7 +127,7 @@ public static class ExceptionExtensions {
|
||||
}
|
||||
if (webEx.Status is WebExceptionStatus.NameResolutionFailure or WebExceptionStatus.ConnectFailure or WebExceptionStatus.ReceiveFailure or
|
||||
WebExceptionStatus.SendFailure or WebExceptionStatus.ConnectionClosed or WebExceptionStatus.KeepAliveFailure or WebExceptionStatus.Timeout or
|
||||
WebExceptionStatus.ProxyNameResolutionFailure or WebExceptionStatus.SecureChannelFailure) return true;
|
||||
WebExceptionStatus.ProxyNameResolutionFailure or WebExceptionStatus.SecureChannelFailure or WebExceptionStatus.TrustFailure) return true;
|
||||
} else if (currentEx is SocketException socketEx) {
|
||||
if (socketEx.SocketErrorCode is SocketError.NetworkDown or SocketError.NetworkUnreachable or SocketError.NetworkReset or SocketError.ConnectionAborted or
|
||||
SocketError.ConnectionReset or SocketError.TimedOut or SocketError.ConnectionRefused or SocketError.HostUnreachable or
|
||||
|
||||
@@ -169,7 +169,7 @@ public static class DirectoryUtils {
|
||||
folder = PathUtils.ForCompare(folder);
|
||||
if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder)))
|
||||
throw new UnauthorizedAccessException($"不应操作磁盘根目录:{folder}");
|
||||
if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(f, folder)))
|
||||
if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(folder, f)))
|
||||
throw new UnauthorizedAccessException($"不应操作文件夹:{folder}");
|
||||
}
|
||||
private static readonly Lazy<HashSet<string>> criticalFolders = new(() => new(new[] {
|
||||
|
||||
@@ -75,6 +75,18 @@ public class DirectoryUtilsTest : TestWithFolder {
|
||||
public void 剪切_安全检查()
|
||||
=> Assert.Throws<UnauthorizedAccessException>(() => DirectoryUtils.Move(Paths.Base, Path.Combine(tempFolder, "SafetyCheckDest")));
|
||||
|
||||
[Test]
|
||||
public async Task 删除_安全检查_允许程序目录下的子文件夹() {
|
||||
var folder = Path.Combine(Paths.Base, $"MeloongTest_{Guid.NewGuid():N}");
|
||||
try {
|
||||
DirectoryUtils.Create(folder);
|
||||
DirectoryUtils.Delete(folder);
|
||||
await Assert.That(DirectoryUtils.Exists(folder)).IsFalse();
|
||||
} finally {
|
||||
if (Directory.Exists(PathUtils.ForApi(folder))) Directory.Delete(PathUtils.ForApi(folder), true);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// 此文件主体由 AI 生成,应作为独立模块,尽量减少与其他内容的耦合。
|
||||
// 使用 ModernTooltipService 接管后,TooltipService 仅有 IsEnabled、ShowOnDisabled、InitialShowDelay 仍然有效,其他属性不再生效。
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
@@ -60,6 +61,8 @@ public static class ModernTooltipService {
|
||||
private static readonly Brush backgroundBrush = new SolidColorBrush(Colors.White);
|
||||
private static readonly Brush borderBrush = new SolidColorBrush(Color.FromRgb(0xD6, 0xD6, 0xD6));
|
||||
private static readonly Brush foregroundBrush = new SolidColorBrush(Color.FromRgb(0x52, 0x52, 0x52));
|
||||
private static readonly DependencyPropertyDescriptor toolTipPropertyDescriptor =
|
||||
DependencyPropertyDescriptor.FromProperty(FrameworkElement.ToolTipProperty, typeof(FrameworkElement));
|
||||
|
||||
// 状态字段
|
||||
private static readonly DispatcherTimer openTimer = new();
|
||||
@@ -70,6 +73,7 @@ public static class ModernTooltipService {
|
||||
private static Border? popupCard;
|
||||
private static ScaleTransform? popupScale;
|
||||
private static FrameworkElement? currentOwner;
|
||||
private static FrameworkElement? observedToolTipOwner;
|
||||
private static ToolTip? borrowedToolTip;
|
||||
private static object? borrowedContent;
|
||||
|
||||
@@ -195,7 +199,11 @@ public static class ModernTooltipService {
|
||||
return;
|
||||
}
|
||||
|
||||
if (owner is not null) BeginShow(owner, Mouse.GetPosition(owner));
|
||||
if (owner is not null) {
|
||||
BeginShow(owner, Mouse.GetPosition(owner));
|
||||
} else if (currentOwner is { IsEnabled: false }) {
|
||||
Close(true); // 禁用的控件不会触发 MouseLeave,需要由其他元素收到的 MouseMove 主动关闭
|
||||
}
|
||||
}
|
||||
|
||||
private static FrameworkElement? FindCurrentTooltipOwner(FrameworkElement reference) {
|
||||
@@ -223,7 +231,9 @@ public static class ModernTooltipService {
|
||||
private static bool TryGetToolTip(FrameworkElement owner, out object? content, out ToolTip? sourceToolTip) {
|
||||
var raw = owner.ToolTip;
|
||||
sourceToolTip = raw as ToolTip;
|
||||
content = sourceToolTip?.Content ?? raw;
|
||||
content = sourceToolTip is null
|
||||
? raw
|
||||
: sourceToolTip.Content ?? (ReferenceEquals(sourceToolTip, borrowedToolTip) ? borrowedContent : null);
|
||||
return content is not null && (content is not string text || text.Length > 0);
|
||||
}
|
||||
|
||||
@@ -317,9 +327,21 @@ public static class ModernTooltipService {
|
||||
}
|
||||
|
||||
popup!.PlacementTarget = owner;
|
||||
UpdatePosition(owner, point);
|
||||
ObserveToolTip(owner);
|
||||
UpdateContent(owner, content!, sourceToolTip);
|
||||
|
||||
animationToken++;
|
||||
popupCard!.Opacity = 0;
|
||||
popupScale!.ScaleX = closedScale;
|
||||
popupScale.ScaleY = closedScale;
|
||||
popup.IsOpen = true;
|
||||
Animate(1, 1, null);
|
||||
}
|
||||
|
||||
private static void UpdateContent(FrameworkElement owner, object content, ToolTip? sourceToolTip) {
|
||||
popupCard!.DataContext = sourceToolTip?.DataContext ?? owner.DataContext;
|
||||
popupCard.FlowDirection = owner.FlowDirection;
|
||||
UpdatePosition(owner, point);
|
||||
|
||||
// 先还原上一次借出的 Visual,再挂载新的内容,避免同一元素拥有两个逻辑父级。
|
||||
popupCard.Child = null;
|
||||
@@ -341,18 +363,35 @@ public static class ModernTooltipService {
|
||||
Content = content, ContentTemplate = sourceToolTip?.ContentTemplate, ContentTemplateSelector = sourceToolTip?.ContentTemplateSelector,
|
||||
ContentStringFormat = sourceToolTip?.ContentStringFormat, Margin = contentMargin, MaxWidth = contentMaxWidth
|
||||
};
|
||||
}
|
||||
|
||||
animationToken++;
|
||||
popupCard.Opacity = 0;
|
||||
popupScale!.ScaleX = closedScale;
|
||||
popupScale.ScaleY = closedScale;
|
||||
popup.IsOpen = true;
|
||||
Animate(1, 1, null);
|
||||
private static void ObserveToolTip(FrameworkElement owner) {
|
||||
if (ReferenceEquals(observedToolTipOwner, owner)) return;
|
||||
StopObservingToolTip();
|
||||
observedToolTipOwner = owner;
|
||||
toolTipPropertyDescriptor.AddValueChanged(owner, OnToolTipChanged);
|
||||
}
|
||||
|
||||
private static void StopObservingToolTip() {
|
||||
if (observedToolTipOwner is null) return;
|
||||
toolTipPropertyDescriptor.RemoveValueChanged(observedToolTipOwner, OnToolTipChanged);
|
||||
observedToolTipOwner = null;
|
||||
}
|
||||
|
||||
private static void OnToolTipChanged(object? sender, EventArgs e) {
|
||||
if (sender is not FrameworkElement owner || !ReferenceEquals(owner, currentOwner) || popup is not { IsOpen: true }) return;
|
||||
if (!CanShowModernTooltip(owner) || !TryGetToolTip(owner, out var content, out var sourceToolTip)) {
|
||||
Close(true);
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateContent(owner, content!, sourceToolTip);
|
||||
}
|
||||
|
||||
private static void Close(bool animated) {
|
||||
openTimer.Stop();
|
||||
currentOwner = null;
|
||||
StopObservingToolTip();
|
||||
|
||||
if (popup is not { IsOpen: true } || popupCard is null || !animated) {
|
||||
HideImmediately();
|
||||
@@ -369,6 +408,7 @@ public static class ModernTooltipService {
|
||||
private static void HideImmediately() {
|
||||
openTimer.Stop();
|
||||
animationToken++;
|
||||
StopObservingToolTip();
|
||||
|
||||
if (popup is not null) popup.IsOpen = false;
|
||||
if (popupCard is not null) {
|
||||
|
||||
Reference in new issue
Block a user