private void CreateVisual(CaretAdornmentData nodeData, int caretLineOffset, int caretOffset, IUserIdentity userIdentity)
        {
            if (View.IsClosed)
            {
                return;
            }

            if (RepoDocument != null && nodeData.RelativeToServerSource)
            {
                var remoteFileText = SourceControlRepo.GetRemoteFileLines(View.TextBuffer.GetTextDocumentFilePath());

                if (remoteFileText != null)
                {
                    string localFileText = View.TextSnapshot.GetText();

                    caretLineOffset = LineNumberTranslator.GetLineNumber(localFileText.Split(new[] { "\r\n" }, StringSplitOptions.None), remoteFileText, caretLineOffset, FileNumberBasis.Local);
                }
            }

            var caretLineNumber = View.TextSnapshot.GetLineNumberFromPosition(nodeData.SpanStart) + caretLineOffset;

            var caretPosition = View.TextSnapshot.GetLineFromLineNumber(Math.Min(caretLineNumber, View.TextSnapshot.LineCount - 1)).Start.Position + caretOffset;

            if (caretPosition < nodeData.NonWhiteSpaceStart)
            {
                caretPosition = nodeData.NonWhiteSpaceStart;
            }
            else if (caretPosition > nodeData.SpanEnd)
            {
                caretPosition = nodeData.SpanEnd;
            }

            var atEnd           = caretPosition >= View.TextSnapshot.Length;
            var remoteCaretSpan = new SnapshotSpan(View.TextSnapshot, atEnd ? View.TextSnapshot.Length - 1 : caretPosition, 1);
            var onSameLineAsEnd = remoteCaretSpan.Start.GetContainingLine().LineNumber == View.TextSnapshot.GetLineNumberFromPosition(View.TextSnapshot.Length);

            Geometry characterGeometry = View.TextViewLines.IsValid ? View.TextViewLines.GetMarkerGeometry(remoteCaretSpan) : null;

            if (characterGeometry != null)
            {
                var caretGeometry = new LineGeometry(atEnd && onSameLineAsEnd ? characterGeometry.Bounds.TopRight : characterGeometry.Bounds.TopLeft,
                                                     atEnd && onSameLineAsEnd ? characterGeometry.Bounds.BottomRight : characterGeometry.Bounds.BottomLeft);
                var drawing = new GeometryDrawing(null, UserColours.GetUserPen(userIdentity), caretGeometry);
                drawing.Freeze();

                var drawingImage = new DrawingImage(drawing);
                drawingImage.Freeze();

                var image = new Image
                {
                    Source = drawingImage,
                };

                // Align the image with the top of the bounds of the text geometry
                var bounds = caretGeometry.Bounds;
                Canvas.SetLeft(image, bounds.Left);
                Canvas.SetTop(image, bounds.Top);

                Layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, remoteCaretSpan, null, image, null);

                FrameworkElement userControl = null;
                if (UserAvatars.ContainsKey(userIdentity.Id) && UserAvatars[userIdentity.Id].Count != 0)
                {
                    userControl = UserAvatars[userIdentity.Id].Dequeue();
                }
                if (TeamCodingPackage.Current.Settings.UserSettings.UserCodeDisplay == Options.UserSettings.UserDisplaySetting.Colour)
                {
                    if (userControl == null)
                    {
                        userControl = new Border()
                        {
                            Tag          = userIdentity.Id,
                            Background   = UserColours.GetUserBrush(userIdentity),
                            CornerRadius = new CornerRadius(bounds.Height / 2 / 2)
                        };
                    }
                    userControl.Width  = bounds.Height / 2;
                    userControl.Height = bounds.Height / 2;
                    Canvas.SetTop(userControl, bounds.Top - (userControl.Height * 0.75));
                }
                else
                {
                    if (userControl == null)
                    {
                        userControl = TeamCodingPackage.Current.UserImages.CreateUserIdentityControl(userIdentity);
                    }
                    userControl.Width  = bounds.Height / 1.25f;
                    userControl.Height = bounds.Height / 1.25f;
                    Canvas.SetTop(userControl, bounds.Top - userControl.Height);
                }
                userControl.ToolTip = userIdentity.DisplayName ?? userIdentity.Id;
                Canvas.SetLeft(userControl, bounds.Left - userControl.Width / 2);
                Layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, remoteCaretSpan, null, userControl, AdornmentRemoved);
            }
        }
Esempio n. 2
0
 public Color GetUserColour()
 {
     return(UserColours.GetUserColour(this));
 }