From 658e141dd5c8eb38e76bf3a91e313874efd2fabe Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Sun, 27 Jul 2025 15:49:36 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=85=AC=E6=8B=9B=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=BD=BF=E7=94=A8=E5=8A=A8=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Constants/UILogColor.cs | 2 + src/MaaWpfGui/Helper/ToolTipHelper.cs | 34 +++++++++----- .../ViewModels/UI/RecognizerViewModel.cs | 45 +++++++++++-------- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/MaaWpfGui/Constants/UILogColor.cs b/src/MaaWpfGui/Constants/UILogColor.cs index f47489f9ca..75f35ac508 100644 --- a/src/MaaWpfGui/Constants/UILogColor.cs +++ b/src/MaaWpfGui/Constants/UILogColor.cs @@ -20,6 +20,8 @@ namespace MaaWpfGui.Constants /// public static class UiLogColor { + public const string Text = "PrimaryTextBrush"; + /// /// The recommended color for error logs. /// diff --git a/src/MaaWpfGui/Helper/ToolTipHelper.cs b/src/MaaWpfGui/Helper/ToolTipHelper.cs index 2b8370f313..5d9eb2d4ef 100644 --- a/src/MaaWpfGui/Helper/ToolTipHelper.cs +++ b/src/MaaWpfGui/Helper/ToolTipHelper.cs @@ -94,17 +94,31 @@ namespace MaaWpfGui.Helper Inline CloneInline(Inline inline) { - return inline switch + switch (inline) { - Run run => new Run(run.Text) - { - Foreground = run.Foreground, - FontWeight = run.FontWeight, - FontStyle = run.FontStyle, - }, - LineBreak => new LineBreak(), - _ => new Run(), - }; + case Run run: + var newRun = new Run(run.Text) + { + FontWeight = run.FontWeight, + FontStyle = run.FontStyle, + }; + + if (run.Tag is string resourceKey) + { + newRun.SetResourceReference(TextElement.ForegroundProperty, resourceKey); + newRun.Tag = resourceKey; + } + else + { + newRun.Foreground = run.Foreground; + } + + return newRun; + case LineBreak: + return new LineBreak(); + default: + return new Run(); + } } } diff --git a/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs b/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs index b390847663..d43b19b62e 100644 --- a/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs @@ -116,12 +116,13 @@ namespace MaaWpfGui.ViewModels.UI foreach (var combs in resultArray ?? []) { int tagLevel = (int)(combs["level"] ?? -1); - recruitResultInlines.Add(new Run($"{tagLevel}★ Tags: ")); + var tagStr = $"{tagLevel}★ Tags: "; + tagStr = ((JArray?)combs["tags"] ?? []).Aggregate(tagStr, (current, tag) => current + $"{tag} "); + var tagRun = new Run(tagStr); + tagRun.SetResourceReference(TextElement.ForegroundProperty, UiLogColor.Text); + tagRun.Tag = UiLogColor.Text; - foreach (var tag in (JArray?)combs["tags"] ?? []) - { - recruitResultInlines.Add(new Run($"{tag} ")); - } + recruitResultInlines.Add(tagRun); recruitResultInlines.Add(new LineBreak()); @@ -175,10 +176,21 @@ namespace MaaWpfGui.ViewModels.UI } var run = new Run($"{operName}{potentialText} "); - var foreground = GetColorByStarWithOpacity(operLevel, isMaxPot ? 0.4 : 1.0); - if (foreground != null) + var opacity = isMaxPot ? 0.4 : 1.0; + + if (Math.Abs(opacity - 1.0) < 1e-4) { - run.Foreground = foreground; + var brushKey = GetBrushKeyByStar(operLevel); + run.SetResourceReference(TextElement.ForegroundProperty, brushKey); + run.Tag = brushKey; + } + else + { + var brush = GetBrushWithOpacityByStar(operLevel, opacity); + if (brush != null) + { + run.Foreground = brush; + } } recruitResultInlines.Add(run); @@ -197,9 +209,9 @@ namespace MaaWpfGui.ViewModels.UI return new(Color.FromArgb(alpha, baseColor.R, baseColor.G, baseColor.B)); } - Brush? GetColorByStarWithOpacity(int level, double opacity = 1.0) + string GetBrushKeyByStar(int level) { - var brushKey = level switch + return level switch { >= 6 => UiLogColor.Star6Operator, 5 => UiLogColor.Star5Operator, @@ -208,15 +220,12 @@ namespace MaaWpfGui.ViewModels.UI 2 => UiLogColor.Star2Operator, _ => UiLogColor.Star1Operator, }; + } - var brush = Application.Current.TryFindResource(brushKey) as SolidColorBrush; - if (brush == null) - { - return null; - } - - var baseColor = brush.Color; - return GetBrushWithOpacity(baseColor, opacity); + Brush? GetBrushWithOpacityByStar(int level, double opacity) + { + var brushKey = GetBrushKeyByStar(level); + return Application.Current.TryFindResource(brushKey) is not SolidColorBrush brush ? null : GetBrushWithOpacity(brush.Color, opacity); } }