/// <summary> /// Updates the cache that contains the result of laying out the label's text. /// </summary> /// <param name="availableSize">The size of the space that is available for laying out text.</param> private void UpdateTextLayoutResult(Size2D availableSize) { textLayoutCommands.Clear(); if (textParserResult.Count > 0 && Font.IsLoaded) { var unconstrainedWidth = Double.IsPositiveInfinity(availableSize.Width) && HorizontalAlignment != HorizontalAlignment.Stretch; var unconstrainedHeight = Double.IsPositiveInfinity(availableSize.Height) && VerticalAlignment != VerticalAlignment.Stretch; var constraintX = unconstrainedWidth ? null : (Int32?)Math.Ceiling(Display.DipsToPixels(availableSize.Width)); var constraintY = unconstrainedHeight ? null : (Int32?)Math.Ceiling(Display.DipsToPixels(availableSize.Height)); var cursorpos = textLayoutCommands.CursorPosition; var textRenderingMode = TextOptions.GetTextRenderingMode(this); var textScript = TextOptions.GetTextScript(this); var textLanguage = TextOptions.GetTextLanguage(this); var textDirection = FlowDirection == FlowDirection.RightToLeft ? TextDirection.RightToLeft : TextDirection.LeftToRight; var options = (textRenderingMode == TextRenderingMode.Shaped) ? TextLayoutOptions.Shape : TextLayoutOptions.None; var flags = LayoutUtil.ConvertAlignmentsToTextFlags(HorizontalContentAlignment, VerticalContentAlignment); var settings = new TextLayoutSettings(Font, constraintX, constraintY, flags, options, textDirection, textScript, FontStyle, null, textLanguage); View.Resources.TextRenderer.CalculateLayout(textParserResult, textLayoutCommands, settings); View.Resources.TextRenderer.UpdateCursor(textLayoutCommands, cursorpos); } }
protected override void OnPreviewKeyDown(KeyEventArgs e) { base.OnPreviewKeyDown(e); if (!e.Handled && e.Key == Key.D && e.KeyboardDevice.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)) { enableFocusDebugOutput = !enableFocusDebugOutput; StringWriter output = new StringWriter(); output.WriteLine("Keyboard.FocusedElement = " + GetElementName(Keyboard.FocusedElement)); output.WriteLine("ActiveContent = " + GetElementName(this.ActiveContent)); output.WriteLine("ActiveViewContent = " + GetElementName(this.ActiveViewContent)); output.WriteLine("ActiveWorkbenchWindow = " + GetElementName(this.ActiveWorkbenchWindow)); ((AvalonDockLayout)workbenchLayout).WriteState(output); LoggingService.Debug(output.ToString()); e.Handled = true; } if (!e.Handled && e.Key == Key.F && e.KeyboardDevice.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)) { if (TextOptions.GetTextFormattingMode(this) == TextFormattingMode.Display) { TextOptions.SetTextFormattingMode(this, TextFormattingMode.Ideal); } else { TextOptions.SetTextFormattingMode(this, TextFormattingMode.Display); } this.StatusBar.SetMessage("TextFormattingMode=" + TextOptions.GetTextFormattingMode(this)); } if (!e.Handled && e.Key == Key.R && e.KeyboardDevice.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)) { switch (TextOptions.GetTextRenderingMode(this)) { case TextRenderingMode.Auto: case TextRenderingMode.ClearType: TextOptions.SetTextRenderingMode(this, TextRenderingMode.Grayscale); break; case TextRenderingMode.Grayscale: TextOptions.SetTextRenderingMode(this, TextRenderingMode.Aliased); break; default: TextOptions.SetTextRenderingMode(this, TextRenderingMode.ClearType); break; } this.StatusBar.SetMessage("TextRenderingMode=" + TextOptions.GetTextRenderingMode(this)); } if (!e.Handled && e.Key == Key.G && e.KeyboardDevice.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); this.StatusBar.SetMessage("Total memory = " + (GC.GetTotalMemory(true) / 1024 / 1024f).ToString("f1") + " MB"); } }
private void PushTextRenderingMode() { if (_popupRoot == null || Child == null) { return; } var vs = DependencyPropertyHelper.GetValueSource(Child, TextOptions.TextRenderingModeProperty); if (vs.BaseValueSource <= BaseValueSource.Inherited) { TextOptions.SetTextRenderingMode(_popupRoot, TextOptions.GetTextRenderingMode(this)); } }
/// <summary> /// Updates the cache which contains the element's laid-out text. /// </summary> /// <param name="availableSize">The amount of space in which the element's text can be laid out.</param> private void UpdateTextLayoutCache(Size2D availableSize) { if (textLayoutCommands != null) { textLayoutCommands.Clear(); } if (View == null) { return; } var content = Content; var contentElement = content as UIElement; if (contentElement == null) { if (textLayoutCommands == null) { textLayoutCommands = new TextLayoutCommandStream(); } var font = GetValue <SourcedResource <UltravioletFont> >(TextElement.FontProperty); var fontStyle = GetValue <UltravioletFontStyle>(TextElement.FontStyleProperty); if (font.IsLoaded) { var availableSizeInPixels = Display.DipsToPixels(availableSize); var cursorpos = textLayoutCommands.CursorPosition; var textRenderingMode = TextOptions.GetTextRenderingMode(this); var textScript = TextOptions.GetTextScript(this); var textLanguage = TextOptions.GetTextLanguage(this); var textDirection = FlowDirection == FlowDirection.RightToLeft ? TextDirection.RightToLeft : TextDirection.LeftToRight; var options = (textRenderingMode == TextRenderingMode.Shaped) ? TextLayoutOptions.Shape : TextLayoutOptions.None; var flags = LayoutUtil.ConvertAlignmentsToTextFlags(HorizontalAlignment, VerticalAlignment); var settings = new TextLayoutSettings(font, (Int32)Math.Ceiling(availableSizeInPixels.Width), (Int32)Math.Ceiling(availableSizeInPixels.Height), flags, options, textDirection, textScript, fontStyle, null, textLanguage); View.Resources.TextRenderer.CalculateLayout(textParserResult, textLayoutCommands, settings); View.Resources.TextRenderer.UpdateCursor(textLayoutCommands, cursorpos); } } }
private static FrameworkElement CreateElement(string text, IWpfTextView textView, TextFormattingRunProperties format) { // Constructs the hint block which gets assigned parameter name and fontstyles according to the options // page. Calculates a font size 1/4 smaller than the font size of the rest of the editor var block = new TextBlock { FontFamily = format.Typeface.FontFamily, FontSize = format.FontRenderingEmSize - (0.25 * format.FontRenderingEmSize), FontStyle = FontStyles.Normal, Foreground = format.ForegroundBrush, // Adds a little bit of padding to the left of the text relative to the border // to make the text seem more balanced in the border Padding = new Thickness(left: 1, top: 0, right: 0, bottom: 0), Text = text + ":", VerticalAlignment = VerticalAlignment.Center, }; // Encapsulates the textblock within a border. Sets the height of the border to be 3/4 of the original // height. Gets foreground/background colors from the options menu. The margin is the distance from the // adornment to the text and pushing the adornment upwards to create a separation when on a specific line var border = new Border { Background = format.BackgroundBrush, Child = block, CornerRadius = new CornerRadius(2), Height = textView.LineHeight - (0.25 * textView.LineHeight), HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(left: 0, top: -0.20 * textView.LineHeight, right: 5, bottom: 0), Padding = new Thickness(1), // Need to set SnapsToDevicePixels and UseLayoutRounding to avoid unnecessary reformatting SnapsToDevicePixels = textView.VisualElement.SnapsToDevicePixels, UseLayoutRounding = textView.VisualElement.UseLayoutRounding, VerticalAlignment = VerticalAlignment.Center }; // Need to set these properties to avoid unnecessary reformatting because some dependancy properties // affect layout TextOptions.SetTextFormattingMode(border, TextOptions.GetTextFormattingMode(textView.VisualElement)); TextOptions.SetTextHintingMode(border, TextOptions.GetTextHintingMode(textView.VisualElement)); TextOptions.SetTextRenderingMode(border, TextOptions.GetTextRenderingMode(textView.VisualElement)); border.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return(border); }
private static FrameworkElement CreateElement( ImmutableArray <TaggedText> taggedTexts, IWpfTextView textView, TextFormattingRunProperties format, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, bool classify) { // Constructs the hint block which gets assigned parameter name and fontstyles according to the options // page. Calculates a font size 1/4 smaller than the font size of the rest of the editor var block = new TextBlock { FontFamily = format.Typeface.FontFamily, FontSize = format.FontRenderingEmSize - (0.25 * format.FontRenderingEmSize), FontStyle = FontStyles.Normal, Foreground = format.ForegroundBrush, // Adds a little bit of padding to the left of the text relative to the border // to make the text seem more balanced in the border Padding = new Thickness(left: 1, top: 0, right: 1, bottom: 0), VerticalAlignment = VerticalAlignment.Center, }; var(trimmedTexts, leftPadding, rightPadding) = Trim(taggedTexts); foreach (var taggedText in trimmedTexts) { var run = new Run(taggedText.ToVisibleDisplayString(includeLeftToRightMarker: true)); if (classify && taggedText.Tag != TextTags.Text) { var properties = formatMap.GetTextProperties(typeMap.GetClassificationType(taggedText.Tag.ToClassificationTypeName())); var brush = properties.ForegroundBrush.Clone(); run.Foreground = brush; } block.Inlines.Add(run); } // Encapsulates the textblock within a border. Sets the height of the border to be 3/4 of the original // height. Gets foreground/background colors from the options menu. The margin is the distance from the // adornment to the text and pushing the adornment upwards to create a separation when on a specific line // If the tag is started or followed by a space, we trim that off but represent the space as buffer on hte // left or right side. var left = leftPadding * 5; var right = rightPadding * 5; var border = new Border { Background = format.BackgroundBrush, Child = block, CornerRadius = new CornerRadius(2), Height = textView.LineHeight - (0.25 * textView.LineHeight), HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(left, top: -0.20 * textView.LineHeight, right, bottom: 0), Padding = new Thickness(1), // Need to set SnapsToDevicePixels and UseLayoutRounding to avoid unnecessary reformatting SnapsToDevicePixels = textView.VisualElement.SnapsToDevicePixels, UseLayoutRounding = textView.VisualElement.UseLayoutRounding, VerticalAlignment = VerticalAlignment.Center }; // Need to set these properties to avoid unnecessary reformatting because some dependancy properties // affect layout TextOptions.SetTextFormattingMode(border, TextOptions.GetTextFormattingMode(textView.VisualElement)); TextOptions.SetTextHintingMode(border, TextOptions.GetTextHintingMode(textView.VisualElement)); TextOptions.SetTextRenderingMode(border, TextOptions.GetTextRenderingMode(textView.VisualElement)); border.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return(border); }
private static FrameworkElement CreateElement( ImmutableArray <TaggedText> taggedTexts, IWpfTextView textView, TextFormattingRunProperties format, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, bool classify) { // Constructs the hint block which gets assigned parameter name and fontstyles according to the options // page. Calculates a inline tag that will be 3/4s the size of a normal line. This shrink size tends to work // well with VS at any zoom level or font size. var block = new TextBlock { FontFamily = format.Typeface.FontFamily, FontSize = 0.75 * format.FontRenderingEmSize, FontStyle = FontStyles.Normal, Foreground = format.ForegroundBrush, // Adds a little bit of padding to the left of the text relative to the border to make the text seem // more balanced in the border Padding = new Thickness(left: 2, top: 0, right: 2, bottom: 0) }; var(trimmedTexts, leftPadding, rightPadding) = Trim(taggedTexts); foreach (var taggedText in trimmedTexts) { var run = new Run(taggedText.ToVisibleDisplayString(includeLeftToRightMarker: true)); if (classify && taggedText.Tag != TextTags.Text) { var properties = formatMap.GetTextProperties(typeMap.GetClassificationType(taggedText.Tag.ToClassificationTypeName())); var brush = properties.ForegroundBrush.Clone(); run.Foreground = brush; } block.Inlines.Add(run); } block.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); // Encapsulates the textblock within a border. Gets foreground/background colors from the options menu. // If the tag is started or followed by a space, we trim that off but represent the space as buffer on hte // left or right side. var left = leftPadding * 5; var right = rightPadding * 5; var border = new Border { Background = format.BackgroundBrush, Child = block, CornerRadius = new CornerRadius(2), VerticalAlignment = VerticalAlignment.Bottom, Margin = new Thickness(left, top: 0, right, bottom: 0), }; // gets pixel distance of baseline to top of the font height var dockPanelHeight = format.Typeface.FontFamily.Baseline * format.FontRenderingEmSize; var dockPanel = new DockPanel { Height = dockPanelHeight, LastChildFill = false, // VerticalAlignment is set to Top because it will rest to the top relative to the stackpanel VerticalAlignment = VerticalAlignment.Top }; dockPanel.Children.Add(border); DockPanel.SetDock(border, Dock.Bottom); var stackPanel = new StackPanel { // Height set to align the baseline of the text within the TextBlock with the baseline of text in the editor Height = dockPanelHeight + (block.DesiredSize.Height - (block.FontFamily.Baseline * block.FontSize)), Orientation = Orientation.Vertical }; stackPanel.Children.Add(dockPanel); // Need to set these properties to avoid unnecessary reformatting because some dependancy properties // affect layout TextOptions.SetTextFormattingMode(stackPanel, TextOptions.GetTextFormattingMode(textView.VisualElement)); TextOptions.SetTextHintingMode(stackPanel, TextOptions.GetTextHintingMode(textView.VisualElement)); TextOptions.SetTextRenderingMode(stackPanel, TextOptions.GetTextRenderingMode(textView.VisualElement)); return(stackPanel); }
void UpdateViewLayoutOptionsCache(FrameworkElement view) { m_textRenderingMode = TextOptions.GetTextRenderingMode(view); m_textHintingMode = TextOptions.GetTextHintingMode(view); m_textFormattingMode = TextOptions.GetTextFormattingMode(view); }
Boolean AreViewLayoutOptionsChanged(FrameworkElement view) { return((TextOptions.GetTextRenderingMode(view) != m_textRenderingMode) || (TextOptions.GetTextHintingMode(view) != m_textHintingMode) || (TextOptions.GetTextFormattingMode(view) != m_textFormattingMode)); }
private static FrameworkElement CreateElement( ImmutableArray <TaggedText> taggedTexts, IWpfTextView textView, TextFormattingRunProperties format, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, bool classify) { // Constructs the hint block which gets assigned parameter name and fontstyles according to the options // page. Calculates a inline tag that will be 3/4s the size of a normal line. This shrink size tends to work // well with VS at any zoom level or font size. var block = new TextBlock { FontFamily = format.Typeface.FontFamily, FontSize = 0.75 * format.FontRenderingEmSize, FontStyle = FontStyles.Normal, Foreground = format.ForegroundBrush, // Adds a little bit of padding to the left of the text relative to the border // to make the text seem more balanced in the border Padding = new Thickness(left: 1, top: 0, right: 1, bottom: 0), VerticalAlignment = VerticalAlignment.Center, }; var(trimmedTexts, leftPadding, rightPadding) = Trim(taggedTexts); foreach (var taggedText in trimmedTexts) { var run = new Run(taggedText.ToVisibleDisplayString(includeLeftToRightMarker: true)); if (classify && taggedText.Tag != TextTags.Text) { var properties = formatMap.GetTextProperties(typeMap.GetClassificationType(taggedText.Tag.ToClassificationTypeName())); var brush = properties.ForegroundBrush.Clone(); run.Foreground = brush; } block.Inlines.Add(run); } // Encapsulates the textblock within a border. Gets foreground/background colors from the options menu. // If the tag is started or followed by a space, we trim that off but represent the space as buffer on hte // left or right side. var left = leftPadding * 5; var right = rightPadding * 5; var border = new Border { Background = format.BackgroundBrush, Child = block, CornerRadius = new CornerRadius(2), // Place 3 pixels above/below the border object. This works well as the highlighting lines are 2px // each, giving us 1 px of space on both side of the inline tag and them. This gives the inline tag // an appropriate floating-halfway feeling on the line. Margin = new Thickness(left, top: 3, right, bottom: 3), }; // Need to set these properties to avoid unnecessary reformatting because some dependancy properties // affect layout TextOptions.SetTextFormattingMode(border, TextOptions.GetTextFormattingMode(textView.VisualElement)); TextOptions.SetTextHintingMode(border, TextOptions.GetTextHintingMode(textView.VisualElement)); TextOptions.SetTextRenderingMode(border, TextOptions.GetTextRenderingMode(textView.VisualElement)); border.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return(border); }