Example #1
0
        public void SetImageBackgroundColor(DependencyObject o, object themeKey)
        {
            if (_coreShell.IsUnitTestEnvironment)
            {
                return;
            }

            Color?color = null;

            if (themeKey is ThemeResourceKey)
            {
                // VS theme colors
                var themeColor = VSColorTheme.GetThemedColor(themeKey as ThemeResourceKey);
                color = Color.FromArgb(themeColor.A, themeColor.R, themeColor.G, themeColor.B);
            }
            else if (themeKey is ResourceKey)
            {
                // High contrast or system colors
                var obj = (o as FrameworkElement)?.TryFindResource(themeKey as ResourceKey);
                if (obj is Color)
                {
                    color = (Color)obj;
                }
            }

            Debug.Assert(color.HasValue, "SetImageBackgroundColor: Unknown resource key type or color not found");
            if (color.HasValue)
            {
                ImageThemingUtilities.SetImageBackgroundColor(o, color.Value);
            }
        }
        public void SetTheme(FrameworkElement element)
        {
            if (element is Control control)
            {
                control.SetResourceReference(Control.BackgroundProperty, ThemedDialogColors.WindowPanelBrushKey);
                control.SetResourceReference(Control.ForegroundProperty, ThemedDialogColors.WindowPanelTextBrushKey);
                control.SetResourceReference(Control.BorderBrushProperty, ThemedDialogColors.WindowBorderBrushKey);
                control.SetResourceReference(TasExplorerView.ListItemMouseOverBrushProperty, ThemedDialogColors.ListItemMouseOverBrushKey);
                control.SetResourceReference(TasExplorerView.WizardFooterBrushProperty, ThemedDialogColors.WizardFooterBrushKey);
                control.SetResourceReference(DeploymentDialogView.HyperlinkBrushProperty, ThemedDialogColors.HyperlinkBrushKey);
                control.SetResourceReference(LoginView.HyperlinkBrushProperty, ThemedDialogColors.HyperlinkBrushKey);
                control.SetResourceReference(OutputView.ListItemMouseOverBrushProperty, ThemedDialogColors.ListItemMouseOverBrushKey);
                control.SetResourceReference(OutputView.WindowButtonDownBorderBrushProperty, ThemedDialogColors.WindowButtonDownBorderBrushKey);
                control.SetResourceReference(OutputView.WindowButtonDownHoverBrushProperty, ThemedDialogColors.WindowButtonHoverBrushKey);
                control.SetResourceReference(OutputView.WindowPanelBrushProperty, ThemedDialogColors.WindowPanelBrushKey);
            }

            ThemedDialogStyleLoader.SetUseDefaultThemedDialogStyles(element, true);
            ImageThemingUtilities.SetThemeScrollBars(element, true);
            if (!element.IsInitialized)
            {
                element.Initialized += OnElementInitialized;
            }
            else
            {
                MergeStyles(element);
            }
        }
Example #3
0
 public void SetThemeScrollBars(DependencyObject o)
 {
     if (TestEnvironment.Current == null)
     {
         ImageThemingUtilities.SetThemeScrollBars(o, true);
     }
 }
Example #4
0
 public void SetThemeScrollBars(DependencyObject o)
 {
     if (!_coreShell.IsUnitTestEnvironment)
     {
         ImageThemingUtilities.SetThemeScrollBars(o, true);
     }
 }
Example #5
0
        protected override void OnExecute(object parameter)
        {
            var s = new Bitmap(Properties.Resources.png_undo_16_16);

            var w = new TestWindow
            {
                Background = Brushes.Green,
                Grid       =
                {
                    Margin     = new Thickness(50),
                    Background = Brushes.Red
                }
            };


            var b = ImageThemingUtilities.GetThemedBitmap(s, ImageThemingUtilities.GetImageBackgroundColor(w.Grid).ToRgba());

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                b.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromWidthAndHeight(b.Width, b.Height));

            w.Icon = bs;


            w.ShowDialog();


            //new FolderBrowserDialog().ShowDialog();
        }
Example #6
0
        protected void AddCommands(ToolBar toolBar, int imageId, string tooltip, Func <CommandContext, IEnumerable <CommandItem> > getItemsHandler)
        {
            var b = CreateButton(imageId, tooltip);

            b.ContextMenu = new ContextMenu().SetStyleResourceProperty("EditorContextMenu");
            void ButtonEventHandler(Button btn, CommandContext ctx)
            {
                var m = btn.ContextMenu;

                ImageThemingUtilities.SetImageBackgroundColor(m, ThemeHelper.TitleBackgroundColor);
                m.Foreground      = ThemeHelper.ToolWindowTextBrush;
                m.IsEnabled       = true;
                m.PlacementTarget = btn;
                m.Placement       = System.Windows.Controls.Primitives.PlacementMode.Bottom;
                m.IsOpen          = true;
                if (m.Tag == null || (bool)m.Tag != ctx.RightClick)
                {
                    m.Items.Clear();
                    foreach (var item in getItemsHandler(ctx))
                    {
                        m.Items.Add(new CommandMenuItem(this, item));
                    }
                    m.Tag = ctx.RightClick;
                }
            }

            b.Click += (s, args) => {
                ButtonEventHandler(s as Button, new CommandContext(this, s as Control, args));
            };
            b.MouseRightButtonUp += (s, args) => {
                ButtonEventHandler(s as Button, new CommandContext(this, s as Control, args, true));
                args.Handled = true;
            };
            toolBar.Items.Add(b);
        }
Example #7
0
 public void SetImageBackgroundColor(DependencyObject o, object themeKey)
 {
     if (!VsAppShell.Current.IsUnitTestEnvironment)
     {
         Debug.Assert(themeKey is ThemeResourceKey);
         var color = VSColorTheme.GetThemedColor(themeKey as ThemeResourceKey);
         ImageThemingUtilities.SetImageBackgroundColor(o, Color.FromArgb(color.A, color.R, color.G, color.B));
     }
 }
Example #8
0
 private void Image_Loaded(object sender, RoutedEventArgs e)
 {
     if (sender is Image img && img.Source is DrawingImage drawingImage)
     {
         img.Source = new DrawingImage
         {
             Drawing = ImageThemingUtilities.TransformDrawing(drawingImage.Drawing, IconThemeSelector.KnownThemes.Dark.GetIconTheme())
         };
     }
 }
Example #9
0
        public static ImageSource ConvertCore(ImageSource inputImage, Color backgroundColor, bool isEnabled, bool isHighContrast, object parameter)
        {
            if (!(inputImage is BitmapSource inputImage1) || backgroundColor.A == 0 & isEnabled)
            {
                return(inputImage);
            }
            var biasColor = GrayscaleBitmapSourceConverter.GetBiasColor(parameter);

            return(ImageThemingUtilities.GetOrCreateThemedBitmapSource(inputImage1, backgroundColor, isEnabled, biasColor, isHighContrast));
        }
Example #10
0
        static Button CreateButton(int imageId, string tooltip)
        {
            var b = new Button {
                Content = ThemeHelper.GetImage(imageId, Config.Instance.SmartBarButtonSize),
                ToolTip = tooltip,
                Cursor  = Cursors.Hand
            };

            ImageThemingUtilities.SetImageBackgroundColor(b, ThemeHelper.TitleBackgroundColor);
            return(b);
        }
Example #11
0
        void UpdateBackgroundColor()
        {
            // set background color for children
            var properties = editorFormatMap.GetProperties(marginPropertiesName);

            if (properties.Contains("BackgroundColor"))
            {
                var backgroundColor = (Color)properties["BackgroundColor"];
                ImageThemingUtilities.SetImageBackgroundColor(glyphMarginGrid, backgroundColor);
            }
        }
Example #12
0
 public override object ProvideValue(IServiceProvider serviceProvider)
 {
     if (Source is DrawingImage drawingImage)
     {
         return(new DrawingImage
         {
             Drawing = ImageThemingUtilities.TransformDrawing(drawingImage.Drawing, Theme)
         });
     }
     else
     {
         return(Source);
     }
 }
Example #13
0
        private static Bitmap GetInvertedBitmap(Microsoft.VisualStudio.Shell.Interop.IVsUIShell5 shell5,
            Bitmap inputBitmap, Color transparentColor, uint backgroundColor)
#endif
        {
            Bitmap outputBitmap = null;
            try
            {
#if VS2015
                outputBitmap = ImageThemingUtilities.GetThemedBitmap(inputBitmap, backgroundColor);
#else
                byte[] outputBytes;
                Rectangle rect;
                System.Drawing.Imaging.BitmapData bitmapData;
                IntPtr sourcePointer;
                int length;

                outputBitmap = new Bitmap(inputBitmap);

                outputBitmap.MakeTransparent(transparentColor);

                rect = new Rectangle(0, 0, outputBitmap.Width, outputBitmap.Height);

                bitmapData = outputBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, outputBitmap.PixelFormat);

                sourcePointer = bitmapData.Scan0;

                length = (Math.Abs(bitmapData.Stride) * outputBitmap.Height);

                outputBytes = new byte[length];

                Marshal.Copy(sourcePointer, outputBytes, 0, length);

                shell5.ThemeDIBits( (UInt32)outputBytes.Length, outputBytes, (UInt32)outputBitmap.Width,
                                   (UInt32)outputBitmap.Height, true, backgroundColor);

                Marshal.Copy(outputBytes, 0, sourcePointer, length);

                outputBitmap.UnlockBits(bitmapData);
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return outputBitmap;

        }
Example #14
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var image = new Image { Source = source };
            if (source is DrawingImage drawingImage)
            {
                image.Source = new DrawingImage()
                {
                    Drawing = ImageThemingUtilities.TransformDrawing((source as DrawingImage)?.Drawing, IconThemeSelector.KnownThemes.Dark.GetIconTheme())
                };
            }

            RenderOptions.SetBitmapScalingMode(image, scalingMode);
            if (width >= 0 && height >= 0)
            {
                image.Width = width;
                image.Height = height;
            }
            return image;
        }
Example #15
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var image = new Image {
                Source = source
            };

            if (source is DrawingImage drawingImage)
            {
                image.Source = new DrawingImage()
                {
                    Drawing = ImageThemingUtilities.TransformDrawing((source as DrawingImage)?.Drawing, ThemeController.CurrentTheme.GetThemeBase().GetIconTheme())
                };
            }

            RenderOptions.SetBitmapScalingMode(image, scalingMode);
            if (width >= 0 && height >= 0)
            {
                image.Width  = width;
                image.Height = height;
            }
            return(image);
        }
        private static void UseVsThemePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!DesignerProperties.GetIsInDesignMode(d))
            {
                if (d is FrameworkElement element)
                {
                    if ((bool)e.NewValue)
                    {
                        OverrideProperty(element, Control.BackgroundProperty, _originalBackgroundProperty, ThemedDialogColors.WindowPanelBrushKey);
                        OverrideProperty(element, Control.ForegroundProperty, _originalForegroundProperty, ThemedDialogColors.WindowPanelTextBrushKey);
                        ThemedDialogStyleLoader.SetUseDefaultThemedDialogStyles(element, true);
                        ImageThemingUtilities.SetThemeScrollBars(element, true);

                        // Only merge the styles after the element has been initialized.
                        // If the element hasn't been initialized yet, add an event handler
                        // so that we can merge the styles once it has been initialized.
                        if (!element.IsInitialized)
                        {
                            element.Initialized += OnElementInitialized;
                        }
                        else
                        {
                            MergeStyles(element);
                        }
                    }
                    else
                    {
                        if (_themeResources is not null)
                        {
                            element.Resources.MergedDictionaries.Remove(_themeResources);
                        }
                        ImageThemingUtilities.SetThemeScrollBars(element, null);
                        ThemedDialogStyleLoader.SetUseDefaultThemedDialogStyles(element, false);
                        RestoreProperty(element, Control.ForegroundProperty, _originalForegroundProperty);
                        RestoreProperty(element, Control.BackgroundProperty, _originalBackgroundProperty);
                    }
                }
            }
        }
Example #17
0
        private static unsafe BitmapSource ConvertToGrayScale(BitmapSource image, Color biasColor)
        {
            Validate.IsNotNull(image, nameof(image));
            if (image.Format != PixelFormats.Bgra32)
            {
                throw new ArgumentException();
            }
            int stride = image.PixelWidth * 4;
            int num    = image.PixelWidth * image.PixelHeight * 4;

            using (ReusableResourceHolder <byte[]> reusableResourceHolder = ImageThemingUtilities.AcquireConversionBuffer(num))
            {
                byte[] resource = reusableResourceHolder.Resource;
                image.CopyPixels(resource, stride, 0);
                ImageThemingUtilities.GrayscaleDIBits(resource, num, biasColor);
                byte[]       numArray;
                BitmapSource bitmapSource;
                fixed(void *_ = numArray = resource)
                {
                    void *voidPtr;

                    if (numArray.Length == 0)
                    {
                        voidPtr = null;
                    }
                    else
                        fixed(void *tmp = &numArray[0])
                        {
                            voidPtr = tmp;
                        }
                    bitmapSource = BitmapSource.Create(image.PixelWidth, image.PixelHeight, image.DpiX, image.DpiY, PixelFormats.Bgra32, image.Palette, (IntPtr)voidPtr, num, stride);
                }

                bitmapSource.Freeze();
                return(bitmapSource);
            }
        }
Example #18
0
 public static void SetBackgroundForCrispImage(this System.Windows.DependencyObject target, WpfColor color)
 {
     ImageThemingUtilities.SetImageBackgroundColor(target, color);
 }
Example #19
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 unused, 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.LayoutChanged += View_LayoutChanged;

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

                view.LayoutChanged -= View_LayoutChanged;
            }));

            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);
            }
        }
Example #20
0
        public static ImageSource ThemeImage(DependencyObject owner, BitmapSource source)
        {
            Color background = ImageThemingUtilities.GetImageBackgroundColor(owner);

            return(ImageThemingUtilities.GetOrCreateThemedBitmapSource(source, background, true, Colors.Black, false));
        }