private static Brush GetBackgroundColor(IEditorFormatMap map)
        {
            var properties = map.GetProperties(EditorFormatDefinitionNames.ConflictingKeyBindingMargin);
            var key = EditorFormatDefinition.BackgroundColorId;
            var color = ConflictingKeyBindingMarginFormatDefinition.DefaultColor;
            if (properties != null && properties.Contains(key))
            {
                color = (Color)properties[key];
            }

            return new SolidColorBrush(color);
        }
示例#2
0
            private static Brush GetBrush(IEditorFormatMap map, string name, string resource = EditorFormatDefinition.BackgroundBrushId)
            {
                var formatProperties = map.GetProperties(name);
                if (formatProperties != null && formatProperties.Contains(resource))
                {
                    var brushValue = formatProperties[resource] as Brush;
                    if (brushValue != null)
                    {
                        Debug.Assert(brushValue.IsFrozen);

                        return brushValue;
                    }
                }

                return null;
            }
示例#3
0
 protected override Color? GetColor(
     IWpfTextView view, IEditorFormatMap editorFormatMap)
 {
     var property = editorFormatMap.GetProperties(SuggestionTagFormat.ResourceName)["ForegroundColor"];
     return property as Color?;
 }
示例#4
0
        internal void CreateAndSetPainter(string category, ref ISelectionPainter painter, SKColor defaultColor)
        {
            if (painter != null)
            {
                painter.Dispose();
            }

            painter = BrushSelectionPainter.CreatePainter(this, _selectionAdornmentLayer, _editorFormatMap.GetProperties(category), defaultColor);
        }
示例#5
0
 void ReinitializeCache()
 {
     toClassificationInfo.Clear();
     ClassificationFontUtils.CopyTo(defaultResourceDictionary, editorFormatMap.GetProperties(EditorFormatMapConstants.PlainText));
     defaultTextFormattingRunProperties = ClassificationFontUtils.Create(defaultResourceDictionary);
 }
示例#6
0
        private Brush GetEditorTagBorderBrush(string tagId)
        {
            var properties = _textFormattingMap.GetProperties(tagId);

            return((Brush)(properties["Foreground"] ?? ((Pen)properties["MarkerFormatDefinition/BorderId"]).Brush));
        }
        private void UpdateBrushes()
        {
            var map = _editorFormatMap.GetProperties(ControlCharFormatDefinition.Name);

            _foregroundBrush = map.GetForegroundBrush(ControlCharFormatDefinition.DefaultForegroundBrush);
        }
        private void ApplyNumbers()
        {
            // Toggle visibility
            var isLineNumberOn = (bool)textView.Options.GetOptionValue(DefaultTextViewHostOptions.LineNumberMarginName);

            this.Visibility = isLineNumberOn ? Visibility.Visible : Visibility.Hidden;

            // Get the visual styles
            var lineNumberColorScheme = formatMap.GetProperties("Line Number");
            var backColor             = (SolidColorBrush)lineNumberColorScheme[EditorFormatDefinition.BackgroundBrushId];
            var foreColor             = (SolidColorBrush)lineNumberColorScheme[EditorFormatDefinition.ForegroundBrushId];
            var fontFamily            = textView.FormattedLineSource.DefaultTextProperties.Typeface.FontFamily;
            var fontSize = textView.FormattedLineSource.DefaultTextProperties.FontRenderingEmSize;

            var currentLineDefinition = formatMap.GetProperties(CurrentLineFormatDefinition.Name);
            var currentLineBackColor  = currentLineDefinition.GetValue <SolidColorBrush>(EditorFormatDefinition.BackgroundBrushId, defaultValue: backColor);
            var currentLineForeColor  = currentLineDefinition.GetValue <SolidColorBrush>(EditorFormatDefinition.ForegroundBrushId, defaultValue: foreColor);

            // Setup line indexes
            var currentCursorLineNumber   = CursorLineNumber;
            var viewTotalLines            = textView.TextViewLines.Count;
            var totalLineCount            = textView.TextSnapshot.LineCount;
            var numberCharactersLineCount = (totalLineCount == 0) ? 1 : (int)Math.Log10(totalLineCount) + 1 + 1;

            var formattedWidth = CalculateWidth(FormatNumber(numberCharactersLineCount, totalLineCount), fontFamily, fontSize);

            this.Width      = isLineNumberOn ? formattedWidth : 0.0;
            this.Background = backColor;

            // Bail when line numbers are off
            if (!isLineNumberOn)
            {
                return;
            }

            foreach (TextBlock textblock in Children)
            {
                textBlockPool.PutObject(textblock);
            }
            Children.Clear();

            var lineNumbers       = BuildLineNumbers(currentCursorLineNumber, textView.VisualSnapshot.LineCount);
            var viewPortFirstLine = textView.TextSnapshot.GetLineNumberFromPosition(textView.TextViewLines.FirstVisibleLine.Start);
            var viewPortLastLine  = textView.TextSnapshot.GetLineNumberFromPosition(textView.TextViewLines.LastVisibleLine.End) + 1;
            var cursorAbove       = currentCursorLineNumber < viewPortFirstLine;
            var cursorBelow       = currentCursorLineNumber > viewPortLastLine;

            int offset;

            if (cursorAbove)
            {
                var hiddenLines = CountHiddenLines(textView.Caret.Position.BufferPosition, textView.TextViewLines.FirstVisibleLine.Start);
                offset = viewPortFirstLine - hiddenLines - 1;
            }
            else if (cursorBelow)
            {
                var hiddenLines = CountHiddenLines(textView.TextViewLines.FirstVisibleLine.Start, textView.Caret.Position.BufferPosition);
                offset = viewPortFirstLine + hiddenLines - 1;
            }
            else
            {
                viewPortFirstLine = viewPortFirstLine == 0 ? 1 : viewPortFirstLine;
                var cursorViewPortLineIndex = currentCursorLineNumber - viewPortFirstLine;
                var hiddenLines             = CountHiddenLines(textView.TextViewLines.FirstVisibleLine.Start, textView.Caret.ContainingTextViewLine.Start);
                offset = currentCursorLineNumber - cursorViewPortLineIndex + hiddenLines - 1;
            }

            offset = offset < 0 ? 0 : offset;

            var previousLineNumber = -1;
            var counter            = 0;

            for (var i = 0; i < viewTotalLines; i++)
            {
                var lineForeColor         = foreColor;
                var lineBackColor         = backColor;
                var width                 = numberCharactersLineCount;
                var currentLoopLine       = textView.TextSnapshot.GetLineFromPosition(textView.TextViewLines[i].Start);
                var currentLoopLineNumber = currentLoopLine.LineNumber;

                int?displayNumber;
                if (previousLineNumber == currentLoopLineNumber)
                {
                    // line wrapped
                    displayNumber = null;
                }
                else if (currentLoopLineNumber + 1 == currentCursorLineNumber || !HasFocus)
                {
                    var indx = offset + counter;
                    displayNumber = lineNumbers.Count - 1 >= indx ? lineNumbers[indx] : lineNumbers[i];
                    width         = HasFocus ? numberCharactersLineCount * -1 : numberCharactersLineCount;
                    counter      += 1;

                    if (HasFocus)
                    {
                        lineForeColor = currentLineForeColor;
                        lineBackColor = currentLineBackColor;
                    }
                }
                else
                {
                    // cursor line - display real line number
                    var indx = offset + counter;
                    displayNumber = lineNumbers[indx];
                    counter      += 1;
                }

                var lineNumber = ConstructLineNumber(displayNumber, width, fontFamily, fontSize, lineForeColor, lineBackColor);
                previousLineNumber = currentLoopLineNumber;

                var top = (textView.TextViewLines[i].TextTop - textView.ViewportTop);
                SetTop(lineNumber, top);
                Children.Add(lineNumber);
            }
        }
示例#9
0
 public ResourceDictionary GetProperties(string key)
 {
     viewProps.Add(key);
     return(categoryMap.GetProperties(key));
 }
        private void UpdateBrushes()
        {
            ResourceDictionary resourceDictionary = _editorFormatMap.GetProperties("Track Changes before save");

            if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundColorId))
            {
                Color color = (Color)resourceDictionary[EditorFormatDefinition.BackgroundColorId];

                _brushes[3] = new SolidColorBrush(color);

                if (_brushes[3].CanFreeze)
                {
                    _brushes[3].Freeze();
                }
            }
            else if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundBrushId))
            {
                _brushes[3] = (Brush)resourceDictionary[EditorFormatDefinition.BackgroundBrushId];

                if (_brushes[3].CanFreeze)
                {
                    _brushes[3].Freeze();
                }
            }
            resourceDictionary = _editorFormatMap.GetProperties("Track Changes after save");
            if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundColorId))
            {
                Color color = (Color)resourceDictionary[EditorFormatDefinition.BackgroundColorId];

                _brushes[1] = new SolidColorBrush(color);

                if (_brushes[1].CanFreeze)
                {
                    _brushes[1].Freeze();
                }
            }
            else if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundBrushId))
            {
                _brushes[1] = (Brush)resourceDictionary[EditorFormatDefinition.BackgroundBrushId];

                if (_brushes[1].CanFreeze)
                {
                    _brushes[1].Freeze();
                }
            }
            resourceDictionary = _editorFormatMap.GetProperties("Track reverted changes");
            if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundColorId))
            {
                Color color = (Color)resourceDictionary[EditorFormatDefinition.BackgroundColorId];

                _brushes[2] = new SolidColorBrush(color);

                if (_brushes[2].CanFreeze)
                {
                    _brushes[2].Freeze();
                }
            }
            else if (resourceDictionary.Contains(EditorFormatDefinition.BackgroundBrushId))
            {
                _brushes[2] = (Brush)resourceDictionary[EditorFormatDefinition.BackgroundBrushId];

                if (_brushes[2].CanFreeze)
                {
                    _brushes[2].Freeze();
                }
            }
        }
示例#11
0
        Brush GetBackgroundBrush()
        {
            var props = editorFormatMap.GetProperties(IsActive ? ThemeClassificationTypeNameKeys.SelectedText : ThemeClassificationTypeNameKeys.InactiveSelectedText);

            return(ResourceDictionaryUtilities.GetBackgroundBrush(props, IsActive ? SystemColors.HighlightBrush : SystemColors.GrayTextBrush));
        }
示例#12
0
        /// <summary>
        /// Creates a GraphicsResult object which is the error block based on the geometry and formatting set for the item.
        /// </summary>
        public override GraphicsResult GetGraphics(IWpfTextView view, Geometry bounds, TextFormattingRunProperties format)
        {
            var block = new TextBlock
            {
                FontFamily = format.Typeface.FontFamily,
                FontSize   = 0.75 * format.FontRenderingEmSize,
                FontStyle  = FontStyles.Normal,
                Foreground = format.ForegroundBrush,
                Padding    = new Thickness(left: 2, top: 0, right: 2, bottom: 0),
            };

            var idRun = GetRunForId(out var hyperlink);

            if (hyperlink is null)
            {
                block.Inlines.Add(idRun);
            }
            else
            {
                // Match the hyperlink color to what the classification is set to by the user
                var linkColor = _classificationFormatMap.GetTextProperties(_classificationType);
                hyperlink.Foreground = linkColor.ForegroundBrush;

                block.Inlines.Add(hyperlink);
                hyperlink.RequestNavigate += HandleRequestNavigate;
            }

            block.Inlines.Add(": " + _diagnostic.Message);

            var lineHeight = Math.Floor(format.Typeface.FontFamily.LineSpacing * block.FontSize);
            var image      = new CrispImage
            {
                Moniker   = GetMoniker(),
                MaxHeight = lineHeight,
                Margin    = new Thickness(1, 0, 5, 0)
            };

            var border = new Border
            {
                BorderBrush     = format.BackgroundBrush,
                BorderThickness = new Thickness(1),
                Background      = Brushes.Transparent,
                Child           = new StackPanel
                {
                    Height      = lineHeight,
                    Orientation = Orientation.Horizontal,
                    Children    = { image, block }
                },
                CornerRadius = new CornerRadius(2),
                // Highlighting lines are 2px buffer. So shift us up by one from the bottom so we feel centered between them.
                Margin  = new Thickness(10, top: 0, right: 0, bottom: 1),
                Padding = new Thickness(1)
            };

            // This is used as a workaround to the moniker issues in blue theme
            var editorBackground = (Color)_editorFormatMap.GetProperties("TextView Background")["BackgroundColor"];

            ImageThemingUtilities.SetImageBackgroundColor(border, editorBackground);

            border.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            view.ViewportWidthChanged += ViewportWidthChangedHandler;
            view.LayoutChanged        += View_LayoutChanged;

            return(new GraphicsResult(border, dispose:
                                      () =>
            {
                if (hyperlink is not null)
                {
                    hyperlink.RequestNavigate -= HandleRequestNavigate;
                }

                view.ViewportWidthChanged -= ViewportWidthChangedHandler;
                view.LayoutChanged -= View_LayoutChanged;
            }));

            // The tag listens to the width changing to allow the diagnostic UI to move with
            // the window as it gets moved.
            // The InlineDiagnosticsAdornmentManager listens to the viewport width
            // changing to deal with diagnostics intersecting with the text in the editor.
            void ViewportWidthChangedHandler(object s, EventArgs e)
            {
                if (Location is InlineDiagnosticsLocations.PlacedAtEndOfEditor)
                {
                    Canvas.SetLeft(border, view.ViewportRight - border.DesiredSize.Width);
                }
            }

            void View_LayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
            {
                if (Location is InlineDiagnosticsLocations.PlacedAtEndOfEditor)
                {
                    Canvas.SetLeft(border, view.ViewportRight - border.DesiredSize.Width);
                }
            }

            void HandleRequestNavigate(object sender, RoutedEventArgs e)
            {
                var uri = hyperlink.NavigateUri;

                _         = _navigateToLinkService.TryNavigateToLinkAsync(uri, CancellationToken.None);
                e.Handled = true;
            }

            Run GetRunForId(out Hyperlink?link)
            {
                var id = new Run(_diagnostic.Id);

                link = null;

                var helpLinkUri = _diagnostic.GetValidHelpLinkUri();

                if (helpLinkUri != null)
                {
                    link = new Hyperlink(id)
                    {
                        NavigateUri = helpLinkUri
                    };
                }

                return(id);
            }
        }
示例#13
0
        private void DrawLineNumbers()
        {
            int lineCount   = _textView.TextViewLines.Count;
            int notFoundVal = Int32.MaxValue;

            List <int> rlnList = new List <int>();

            // Get the index from the line collection where the cursor is currently sitting
            int cursorLineIndex = _textView.TextViewLines.GetIndexOfTextLine(_textView.Caret.ContainingTextViewLine);

            if (cursorLineIndex > -1)
            {
                _lineMap.Clear();
                for (int i = 0; i < lineCount; i++)
                {
                    int relLineNr = cursorLineIndex - i;
                    rlnList.Add(relLineNr);
                    _lineMap[GetLineNumber(i)] = relLineNr;
                }
            }
            else
            {
                // Cursor is off the screen. Extrapolate relative line numbers.
                for (int i = 0; i < lineCount; i++)
                {
                    int relLineNr = 0;

                    // Try to get relative line number value for this line from the map.
                    if (!_lineMap.TryGetValue(GetLineNumber(i), out relLineNr))
                    {
                        relLineNr = notFoundVal;
                    }
                    rlnList.Add(relLineNr);
                }

                // Extrapolate missing relative line number values
                for (int i = 0; i < lineCount; i++)
                {
                    if (rlnList[0] != notFoundVal)
                    {
                        rlnList[i] = rlnList[0] - i;
                    }
                    else if (rlnList[rlnList.Count - 1] != notFoundVal)
                    {
                        rlnList[lineCount - 1 - i] = rlnList[lineCount - 1] + i;
                    }
                }
            }

            // Clear existing text boxes
            if (_canvas.Children.Count > 0)
            {
                _canvas.Children.Clear();
            }

            ResourceDictionary rd         = _formatMap.GetProperties("Relative Line Numbers");
            SolidColorBrush    fgBrush    = (SolidColorBrush)rd[EditorFormatDefinition.ForegroundBrushId];
            FontWeight         fontWeight = Convert.ToBoolean(rd[ClassificationFormatDefinition.IsBoldId]) ?
                                            FontWeights.Bold : FontWeights.Normal;

            this.Background = (SolidColorBrush)rd[EditorFormatDefinition.BackgroundBrushId];

            string notFoundTxt = "~ ";

            for (int i = 0; i < lineCount; i++)
            {
                int relLineNumber = rlnList[i];
                _lineMap[GetLineNumber(i)] = relLineNumber;

                TextBlock tb = new TextBlock();
                tb.Text       = string.Format("{0,2}", relLineNumber == notFoundVal ? notFoundTxt : Math.Abs(relLineNumber).ToString());
                tb.FontFamily = _fontFamily;
                tb.FontSize   = _fontEmSize;
                tb.Foreground = fgBrush;
                tb.FontWeight = fontWeight;
                Canvas.SetLeft(tb, _labelOffsetX);
                Canvas.SetTop(tb, _textView.TextViewLines[i].TextTop - _textView.ViewportTop);
                _canvas.Children.Add(tb);
            }

            // Ajdust margin width
            int    maxVal = Math.Max(Math.Abs(rlnList[0]), Math.Abs(rlnList[rlnList.Count - 1]));
            string sample = maxVal == notFoundVal ? notFoundTxt : maxVal.ToString();

            this.Width = GetMarginWidth(new Typeface(_fontFamily.Source), _fontEmSize, sample) + 2 * _labelOffsetX;
        }
示例#14
0
        Brush GetBrush(string name, string resource)
        {
            var rd = _EditorFormatMap.GetProperties(name);

            return(rd.Contains(resource) ? rd[resource] as Brush : null);
        }