<commit_before>﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.Generic;
using GitHub.InlineReviews.Glyph;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text.Classification;
using GitHub.InlineReviews.Services;
using GitHub.Models;

namespace GitHub.InlineReviews.Tags
{
    class InlineCommentGlyphFactory : IGlyphFactory<InlineCommentTag>
    {
        readonly IInlineCommentPeekService peekService;
        readonly ITextView textView;
        readonly BrushesManager brushesManager;

        public InlineCommentGlyphFactory(
            IInlineCommentPeekService peekService,
            ITextView textView,
            IEditorFormatMap editorFormatMap)
        {
            this.peekService = peekService;
            this.textView = textView;

            brushesManager = new BrushesManager(editorFormatMap);
        }

        class BrushesManager
        {
            const string AddBackgroundColorKey = "deltadiff.add.word";
            const string DeleteBackgroundColorKey = "deltadiff.remove.word";
            const string NoneBackgroundColorKey = "Indicator Margin";

            readonly IEditorFormatMap editorFormatMap;
            readonly SolidColorBrush addBackground;
            readonly SolidColorBrush deleteBackground;
            readonly SolidColorBrush noneBackground;

            internal BrushesManager(IEditorFormatMap editorFormatMap)
            {
                this.editorFormatMap = editorFormatMap;

                addBackground = new SolidColorBrush();
                deleteBackground = new SolidColorBrush();
                noneBackground = new SolidColorBrush();
                UpdateBrushColors();

                editorFormatMap.FormatMappingChanged += EditorFormatMap_FormatMappingChanged;
            }

            internal Brush GetBrush(DiffChangeType diffChangeType)
            {
                switch (diffChangeType)
                {
                    case DiffChangeType.Add:
                        return addBackground;
                    case DiffChangeType.Delete:
                        return deleteBackground;
                    case DiffChangeType.None:
                    default:
                        return noneBackground;
                }
            }

            void EditorFormatMap_FormatMappingChanged(object sender, FormatItemsEventArgs e)
            {
                foreach (var key in e.ChangedItems)
                {
                    UpdateBrushColors(key);
                }
            }

            void UpdateBrushColors(string key = null)
            {
                if (key == null || key == AddBackgroundColorKey)
                {
                    addBackground.Color = TryGetBackgroundColor(AddBackgroundColorKey);
                }

                if (key == null || key == DeleteBackgroundColorKey)
                {
                    deleteBackground.Color = TryGetBackgroundColor(DeleteBackgroundColorKey);
                }

                if (key == null || key == "Indicator Margin")
                {
                    noneBackground.Color = TryGetBackgroundColor(NoneBackgroundColorKey);
                }
            }

            Color TryGetBackgroundColor(string key)
            {
                try
                {
                    var properties = editorFormatMap.GetProperties(key);
                    return (Color)properties?["BackgroundColor"];
                }
                catch
                {
                    return Colors.Transparent;
                }
            }
        }

        public UIElement GenerateGlyph(IWpfTextViewLine line, InlineCommentTag tag)<commit_msg>Revert "Refresh glyph background colors when theme changes"<commit_after>﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.Generic;
using GitHub.InlineReviews.Glyph;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text.Classification;
using GitHub.InlineReviews.Services;
using GitHub.Models;

namespace GitHub.InlineReviews.Tags
{
    class InlineCommentGlyphFactory : IGlyphFactory<InlineCommentTag>
    {
        readonly IInlineCommentPeekService peekService;
        readonly ITextView textView;
        readonly BrushesManager brushesManager;

        public InlineCommentGlyphFactory(
            IInlineCommentPeekService peekService,
            ITextView textView,
            IEditorFormatMap editorFormatMap)
        {
            this.peekService = peekService;
            this.textView = textView;

            brushesManager = new BrushesManager(editorFormatMap);
        }

        class BrushesManager
        {
            readonly Brush addBackground;
            readonly Brush deleteBackground;
            readonly Brush noneBackground;

            internal BrushesManager(IEditorFormatMap editorFormatMap)
            {
                addBackground = TryGetValue<Brush>(editorFormatMap, "deltadiff.add.word", "Background");
                deleteBackground = TryGetValue<Brush>(editorFormatMap, "deltadiff.remove.word", "Background");
                noneBackground = TryGetValue<Brush>(editorFormatMap, "Indicator Margin", "Background");
            }

            T TryGetValue<T>(IEditorFormatMap editorFormatMap, string key, string name) where T : class
            {
                var properties = editorFormatMap.GetProperties(key);
                return properties?[name] as T;
            }

            internal Brush GetBrush(DiffChangeType diffChangeType)
            {
                switch (diffChangeType)
                {
                    case DiffChangeType.Add:
                        return addBackground;
                    case DiffChangeType.Delete:
                        return deleteBackground;
                    case DiffChangeType.None:
                    default:
                        return noneBackground;
                }
            }
        }

        public UIElement GenerateGlyph(IWpfTextViewLine line, InlineCommentTag tag)
{'fraction_non_alphanumeric': 0.05955056179775281, 'fraction_numerical': 0.0, 'mean_word_length': 1.8297002724795641, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140033', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1372, 'n_words': 295}