From 229390eb6e663ce2e69b9cadba9ea168ae1f7681 Mon Sep 17 00:00:00 2001 From: Jakub Majocha Date: Sat, 18 Nov 2023 22:14:07 +0100 Subject: [PATCH 1/2] detect dark mode by looking at background --- .../ClassificationDefinitions.fs | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs b/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs index 02b6c3c52f9..d5d5063ec6d 100644 --- a/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs +++ b/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs @@ -11,7 +11,6 @@ open Microsoft.VisualStudio.Editor open Microsoft.VisualStudio.PlatformUI open Microsoft.VisualStudio.Shell open Microsoft.VisualStudio.Shell.Interop -open Microsoft.Internal.VisualStudio.Shell.Interop open Microsoft.VisualStudio.Language.StandardClassification open Microsoft.VisualStudio.Text.Classification open Microsoft.VisualStudio.Utilities @@ -91,23 +90,7 @@ module internal ClassificationDefinitions = [)>] serviceProvider: IServiceProvider ) = - let (|LightTheme|DarkTheme|UnknownTheme|) id = - if - id = KnownColorThemes.Light - || id = KnownColorThemes.Blue - || id = Guids.blueHighContrastThemeId - then - LightTheme - elif id = KnownColorThemes.Dark then - DarkTheme - else - UnknownTheme - - let getCurrentThemeId () = - let themeService = - serviceProvider.GetService(typeof) :?> IVsColorThemeService - - themeService.CurrentTheme.ThemeId + let mutable isDarkBackground = false let customColorData = [ @@ -143,7 +126,16 @@ module internal ClassificationDefinitions = |} ] + let setIsDarkBackground () = + isDarkBackground <- + VSColorTheme + .GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey) + .GetBrightness() < 0.5f + let setColors _ = + + setIsDarkBackground() + let fontAndColorStorage = serviceProvider.GetService(typeof) :?> IVsFontAndColorStorage @@ -172,10 +164,9 @@ module internal ClassificationDefinitions = let oldProps = formatMap.GetTextProperties(ict) let newProps = - match getCurrentThemeId () with - | LightTheme -> oldProps.SetForeground item.LightThemeColor - | DarkTheme -> oldProps.SetForeground item.DarkThemeColor - | UnknownTheme -> oldProps + if isDarkBackground + then oldProps.SetForeground item.DarkThemeColor + else oldProps.SetForeground item.LightThemeColor formatMap.SetTextProperties(ict, newProps) @@ -187,15 +178,13 @@ module internal ClassificationDefinitions = do VSColorTheme.add_ThemeChanged handler + do setIsDarkBackground () + member _.GetColor(ctype) = match customColorData |> List.tryFindV (fun item -> item.ClassificationName = ctype) with | ValueSome item -> let light, dark = item.LightThemeColor, item.DarkThemeColor - - match getCurrentThemeId () with - | LightTheme -> Nullable light - | DarkTheme -> Nullable dark - | UnknownTheme -> Nullable() + if isDarkBackground then Nullable dark else Nullable light | ValueNone -> Nullable() interface ISetThemeColors with From 737c484d81f1ccf21db34306cfec40494c24aa59 Mon Sep 17 00:00:00 2001 From: Jakub Majocha Date: Sat, 18 Nov 2023 22:28:08 +0100 Subject: [PATCH 2/2] fantomas --- .../Classification/ClassificationDefinitions.fs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs b/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs index d5d5063ec6d..085d769aeea 100644 --- a/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs +++ b/vsintegration/src/FSharp.Editor/Classification/ClassificationDefinitions.fs @@ -134,7 +134,7 @@ module internal ClassificationDefinitions = let setColors _ = - setIsDarkBackground() + setIsDarkBackground () let fontAndColorStorage = serviceProvider.GetService(typeof) :?> IVsFontAndColorStorage @@ -164,9 +164,10 @@ module internal ClassificationDefinitions = let oldProps = formatMap.GetTextProperties(ict) let newProps = - if isDarkBackground - then oldProps.SetForeground item.DarkThemeColor - else oldProps.SetForeground item.LightThemeColor + if isDarkBackground then + oldProps.SetForeground item.DarkThemeColor + else + oldProps.SetForeground item.LightThemeColor formatMap.SetTextProperties(ict, newProps)