public TeXCommentAdornment(
            IWpfTextView textView,
            TeXCommentTag tag,
            LineSpan lineSpan,
            double lastLineWidthWithoutStartWhiteSpaces,
            TeXCommentAdornmentState initialState,
            Action <Span> refreshTags,
            Action <bool> setIsInEditModeForAllAdornmentsInDocument,
            Action <TeXCommentTag, string> addAttribute,
            IRenderingManager renderingManager,
            IVsSettings vsSettings)
        {
            ExtensionSettings.Instance.CustomZoomChanged += CustomZoomChanged;
            textView.Caret.PositionChanged += Caret_PositionChanged;

            this.DataTag     = tag;
            this.refreshTags = refreshTags;
            this.setIsInEditModeForAllAdornmentsInDocument = setIsInEditModeForAllAdornmentsInDocument;
            this.addAttribute = addAttribute;
            this.textView     = textView;
            this.lastLineWidthWithoutStartWhiteSpaces = lastLineWidthWithoutStartWhiteSpaces;
            this.renderingManager = renderingManager;
            VsSettings            = vsSettings;
            ResourcesManager      = ResourcesManager.GetOrCreate(textView);
            LineSpan = lineSpan;

            InitializeComponent();

            previewAdorner = new PreviewAdorner(this, ResourcesManager, vsSettings);
            Loaded        += (s, e) =>
            {
                if (previewAdorner.Parent == null)
                {
                    previewAdorner.OffsetX = -this.lastLineWidthWithoutStartWhiteSpaces; //'this' is important because of lambda closure
                    System.Windows.Documents.AdornerLayer.GetAdornerLayer(this).Add(previewAdorner);
                }
            };

            //for correctly working binding
            NameScope.SetNameScope(btnShow.ContextMenu, NameScope.GetNameScope(this));
            NameScope.SetNameScope(btnEdit.ContextMenu, NameScope.GetNameScope(this));
            NameScope.SetNameScope((ToolTip)imgError.ToolTip, NameScope.GetNameScope(this));

            CurrentState = initialState;
        }
        private unsafe void RenderInternal(Input input)
        {
            if (webBrowser.InvokeRequired)
            {
                //wait until MathJax is done with rendering
                while (!mathJaxRenderingDone)
                {
                    Thread.Sleep(WaitingIntervalMs);
                }
                webBrowser.Invoke(new Action <Input>(RenderInternal), input);
            }
            else
            {
                webBrowser.Width  = (int)(input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale * DefaultBrowserWidth);
                webBrowser.Height = (int)(input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale * DefaultBrowserHeight);

                const int ExtraMargin = 4;
                var       myDiv       = webBrowser.Document.GetElementById("myDiv");
                var       width       = (myDiv.OffsetRectangle.X + myDiv.ScrollRectangle.Width) + ExtraMargin;
                var       height      = (myDiv.OffsetRectangle.Y + myDiv.ScrollRectangle.Height) + ExtraMargin;

                webBrowser.Width              = width;
                webBrowser.Height             = height;
                webBrowser.Document.BackColor = Color.Transparent;

                const PixelFormat pixelFormat = PixelFormat.Format32bppArgb;
                using (var bitmap = new Bitmap(width, height, pixelFormat))
                {
                    if (webBrowser.Document.DomDocument is IViewObject viewObject)
                    {
                        var webBrowserRectangle = new TagRECT(myDiv.OffsetRectangle);
                        var bitmapRectangle     = new TagRECT(0, 0, width, height);

                        using (var gr = Graphics.FromImage(bitmap))
                        {
                            var hdc = gr.GetHdc();

                            try
                            {
                                int hr = viewObject.Draw(1 /*DVASPECT_CONTENT*/,
                                                         -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero,
                                                         hdc,
                                                         ref bitmapRectangle,
                                                         ref webBrowserRectangle,
                                                         IntPtr.Zero, 0);
                            }
                            finally
                            {
                                gr.ReleaseHdc();
                            }
                        }

                        var background = BGRA.From(input.Background);
                        using (var croppedBitmap = CropToContent(bitmap, background))
                        {
                            MakeBackgroundTransparent(croppedBitmap, background);

                            var bitmapSource = ResourcesManager.CreateBitmapSourceWithCurrentDpi(croppedBitmap);

                            var cacheInfo = new HtmlRendererCache.Info(
                                currentContent,
                                input.Foreground,
                                input.Background,
                                input.Font,
                                input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale,
                                CacheVersion);

                            var errors          = objectForScripting.Errors.Count == 0 ? Array.Empty <string>() : objectForScripting.Errors.ToArray();
                            var cachedImagePath = errors.Length == 0 ? cache.Add(cacheInfo, croppedBitmap) : null;
                            rendererResult = new RendererResult(bitmapSource, cachedImagePath, errors);
                        }
                    }
                }
            }
        }