From 4333d6f8a38f1044feb7273d9978af268044cbc3 Mon Sep 17 00:00:00 2001 From: WMJ Date: Thu, 5 Dec 2024 15:32:17 +0800 Subject: [PATCH] ! Make Quick Info background change according to tooltip text color --- .../QuickInfoBackgroundController.cs | 26 +++++++++++++++++++ Codist/SyntaxHighlight/FormatStore.cs | 5 ---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Codist/QuickInfo/QuickInfoBackgroundController.cs b/Codist/QuickInfo/QuickInfoBackgroundController.cs index bd0009cb..762fb02a 100644 --- a/Codist/QuickInfo/QuickInfoBackgroundController.cs +++ b/Codist/QuickInfo/QuickInfoBackgroundController.cs @@ -6,11 +6,32 @@ using System.Windows.Media; using CLR; using Microsoft.VisualStudio.Language.Intellisense; +using Microsoft.VisualStudio.Text.Classification; namespace Codist.QuickInfo { sealed class QuickInfoBackgroundController : SingletonQuickInfoSource { + #region Adaptive background brightness + static readonly IClassificationFormatMap __ToolTipFormatMap = InitFormatMap(); + static bool __Init; + + static IClassificationFormatMap InitFormatMap() { + var m = ServicesHelper.Instance.ClassificationFormatMap.GetClassificationFormatMap("tooltip"); + m.ClassificationFormatMappingChanged += (s, args) => AdaptBackgroundBrightness(__ToolTipFormatMap); + return m; + } + + static void AdaptBackgroundBrightness(IClassificationFormatMap formatMap) { + QuickInfoConfig c = Config.Instance.QuickInfo; + if (c.BackColor.A != 0 + && formatMap.DefaultTextProperties.ForegroundBrush is SolidColorBrush b + && b.Color.IsDark() == c.BackColor.IsDark()) { + c.BackColor = c.BackColor.InvertBrightness(); + } + } + #endregion + SolidColorBrush _Background; public QuickInfoBackgroundController() { @@ -20,11 +41,16 @@ public QuickInfoBackgroundController() { void ConfigUpdated(ConfigUpdatedEventArgs args) { if (args.UpdatedFeature.MatchFlags(Features.SuperQuickInfo)) { + AdaptBackgroundBrightness(__ToolTipFormatMap); UpdateBackgroundBrush(); } } void UpdateBackgroundBrush() { + if (__Init == false) { + __Init = true; + AdaptBackgroundBrightness(__ToolTipFormatMap); + } var bc = Config.Instance.QuickInfo.BackColor; var o = bc.A; if (o != 0) { diff --git a/Codist/SyntaxHighlight/FormatStore.cs b/Codist/SyntaxHighlight/FormatStore.cs index 282197a7..5e8afa1b 100644 --- a/Codist/SyntaxHighlight/FormatStore.cs +++ b/Codist/SyntaxHighlight/FormatStore.cs @@ -806,11 +806,6 @@ void InvertColorBrightness() { } } - var qi = Config.Instance.QuickInfo; - if (qi.BackColor.A != 0) { - qi.BackColor = qi.BackColor.InvertBrightness(); - Config.Instance.FireConfigChangedEvent(Features.SuperQuickInfo); - } var sm = Config.Instance.SymbolReferenceMarkerSettings; if (sm.ReferenceMarker.A != 0 || sm.WriteMarker.A != 0 || sm.SymbolDefinition.A != 0) { if (sm.ReferenceMarker.A != 0) {