public bool GetTextItemInfo(
            Guid category, string name, out Color foreground, out Color background)
        {
            foreground = Colors.Transparent;
            background = Colors.Transparent;

            if (fncStorage == null)
            {
                return(false);
            }

            if (ErrorHandler.Failed(fncStorage.OpenCategory(category, OpenFlags)))
            {
                return(false);
            }

            try {
                var itemInfo = new ColorableItemInfo[1];
                if (ErrorHandler.Failed(fncStorage.GetItem(name, itemInfo)))
                {
                    return(false);
                }

                DecodePlainTextColors(ref itemInfo[0]);

                foreground = GetWpfColor(itemInfo[0].crForeground);
                background = GetWpfColor(itemInfo[0].crBackground);

                return(true);
            } finally {
                fncStorage.CloseCategory();
            }
        }
        private async Task LoadSystemTextSettingsAsync(CancellationToken cancellationToken)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            try
            {
                IVsFontAndColorStorage storage = (IVsFontAndColorStorage)GetGlobalService(typeof(IVsFontAndColorStorage));

                var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0");

                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK)
                {
#pragma warning disable SA1129 // Do not use default value type constructor
                    LOGFONTW[] fnt  = new LOGFONTW[] { new LOGFONTW() };
                    FontInfo[] info = new FontInfo[] { new FontInfo() };
#pragma warning restore SA1129 // Do not use default value type constructor

                    if (storage.GetFont(fnt, info) == Microsoft.VisualStudio.VSConstants.S_OK)
                    {
                        var fontSize = info[0].wPointSize;

                        if (fontSize > 0)
                        {
                            ResourceAdornmentManager.TextSize = fontSize;
                        }
                    }
                }

                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK)
                {
                    var info = new ColorableItemInfo[1];

                    // Get the color value configured for regular string display
                    if (storage.GetItem("String", info) == Microsoft.VisualStudio.VSConstants.S_OK)
                    {
                        var win32Color = (int)info[0].crForeground;

                        int r = win32Color & 0x000000FF;
                        int g = (win32Color & 0x0000FF00) >> 8;
                        int b = (win32Color & 0x00FF0000) >> 16;

                        var textColor = Color.FromRgb((byte)r, (byte)g, (byte)b);

                        ResourceAdornmentManager.TextForegroundColor = textColor;
                    }
                }
            }
            catch (Exception exc)
            {
                ExceptionHelper.Log(exc, "Error in LoadSystemTextSettingsAsync");
            }
        }
Ejemplo n.º 3
0
        private (Color fg, Color bg, bool bold) GetInfo(string item)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var colorInfo = new ColorableItemInfo[1];

            ErrorHandler.ThrowOnFailure(_storage.GetItem(item, colorInfo));

            var fg     = FontAndColorService.ReadVsColor(colorInfo[0].crForeground);
            var bg     = FontAndColorService.ReadVsColor(colorInfo[0].crBackground);
            var isBold = ((FONTFLAGS)colorInfo[0].dwFontFlags & FONTFLAGS.FF_BOLD) == FONTFLAGS.FF_BOLD;

            return(fg, bg, isBold);
        }
Ejemplo n.º 4
0
 private static void SaveColor(IVsFontAndColorStorage vsStorage, ColorKey colorKey, System.Drawing.Color color)
 {
     Debug.WriteLine($"GeneralOptions.SaveColoR keyName={colorKey.Name} color={color}");
     ThreadHelper.ThrowIfNotOnUIThread();
     ColorableItemInfo[] arr = new ColorableItemInfo[1];
     ErrorHandler.ThrowOnFailure(vsStorage.GetItem(colorKey.Name, arr));
     if (colorKey.IsForeground)
     {
         arr[0].bForegroundValid = 1;
         arr[0].crForeground     = (uint)ColorTranslator.ToWin32(color);
     }
     else
     {
         arr[0].bBackgroundValid = 1;
         arr[0].crBackground     = (uint)ColorTranslator.ToWin32(color);
     }
     ErrorHandler.ThrowOnFailure(vsStorage.SetItem(colorKey.Name, arr));
 }
Ejemplo n.º 5
0
        private void UpdateColors()
        {
            var theme = themeEngine.GetCurrentTheme();

            // Did theme change?
            if (theme != currentTheme)
            {
                currentTheme = theme;
                var colors = themeColors[theme];

                if (fontAndColorStorage != null && fontAndColorUtilities != null)
                {
                    if (fontAndColorStorage.OpenCategory(Microsoft.VisualStudio.Editor.DefGuidList.guidTextEditorFontCategory, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES)) == VSConstants.S_OK)
                    {
                        try
                        {
                            foreach (var pair in colors)
                            {
                                var colorInfos = new ColorableItemInfo[1];
                                if (fontAndColorStorage.GetItem(pair.Key, colorInfos) == VSConstants.S_OK)
                                {
                                    if (pair.Value.ForegroundColor != null)
                                    {
                                        colorInfos[0].crForeground = (uint)(pair.Value.ForegroundColor.Value.R | (pair.Value.ForegroundColor.Value.G << 8) | (pair.Value.ForegroundColor.Value.B << 16));
                                    }

                                    if (pair.Value.BackgroundColor != null)
                                    {
                                        colorInfos[0].crBackground = (uint)(pair.Value.BackgroundColor.Value.R | (pair.Value.BackgroundColor.Value.G << 8) | (pair.Value.BackgroundColor.Value.B << 16));
                                    }

                                    fontAndColorStorage.SetItem(pair.Key, colorInfos);
                                }
                            }
                        }
                        finally
                        {
                            fontAndColorStorage.CloseCategory();
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private static System.Drawing.Color LoadColor(IServiceProvider Site, IVsFontAndColorStorage vsStorage, ColorKey colorKey)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var arr = new ColorableItemInfo[1];

            ErrorHandler.ThrowOnFailure(vsStorage.GetItem(colorKey.Name, arr));

            var isValid = colorKey.IsForeground ? arr[0].bForegroundValid : arr[0].bBackgroundValid;

            if (isValid == 0)
            {
                throw new Exception();
            }

            var colorRef = colorKey.IsForeground ? arr[0].crForeground : arr[0].crBackground;
            var color    = FromColorRef(Site, vsStorage, colorRef);

            Debug.WriteLine($"GeneralOptions.LoadColoR keyName={colorKey.Name} color={color}");
            return(color);
        }
Ejemplo n.º 7
0
        private Color LoadColor(IVsFontAndColorStorage vsStorage, ColorKey colorKey)
        {
            var array = new ColorableItemInfo[1];

            ErrorHandler.ThrowOnFailure(vsStorage.GetItem(colorKey.Name, array));

            int isValid = colorKey.IsForeground
                ? array[0].bForegroundValid
                : array[0].bBackgroundValid;

            if (isValid == 0)
            {
                throw new Exception();
            }

            uint colorRef = colorKey.IsForeground
                ? array[0].crForeground
                : array[0].crBackground;

            return(FromColorRef(vsStorage, colorRef));
        }
Ejemplo n.º 8
0
        public HighlightWordFormatDefinition()
        {
            // Default the BackgroundColor to a transparent grey
            BackgroundColor = Color.FromArgb(127, 170, 170, 170);
            DisplayName     = "Highlight Word";
            ZOrder          = 5;

            // If possible, set the Background color to match the Highlighted Reference color
            IVsFontAndColorStorage colorStorage = ServiceProvider.GlobalProvider.GetService(typeof(IVsFontAndColorStorage)) as IVsFontAndColorStorage;

            ColorableItemInfo[] itemInfoOut = new ColorableItemInfo[1];
            if (colorStorage.OpenCategory(ref _textEditorCategory, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == VSConstants.S_OK)
            {
                if (colorStorage.GetItem(_itemName, itemInfoOut) == VSConstants.S_OK)
                {
                    uint hexColor = itemInfoOut[0].crBackground;
                    BackgroundColor = Color.FromArgb(255, (byte)hexColor, (byte)(hexColor >> 8), (byte)(hexColor >> 16));
                }
                colorStorage.CloseCategory();
            }
        }
        static Tuple <Color?, Color?> TryGetItem(IVsFontAndColorStorage storage, string item)
        {
            Tuple <Color?, Color?> result = null;

            // load specific category to prevent our own format classifications being loaded
            InCategory(storage, Microsoft.VisualStudio.Editor.DefGuidList.guidTextEditorFontCategory, () =>
            {
                ColorableItemInfo[] colors = new ColorableItemInfo[1];
                var hresult = storage.GetItem(item, colors);
                if (hresult == 0)
                {
                    result = Tuple.Create <Color?, Color?>(ParseColor(colors[0].crForeground), ParseColor(colors[0].crBackground));
                }
                else
                {
                    result = Tuple.Create <Color?, Color?>(null, null);
                }
            });

            return(result);
        }
Ejemplo n.º 10
0
        private static void GetSize()
        {
            try
            {
                IVsFontAndColorStorage storage = (IVsFontAndColorStorage)Package.GetGlobalService(typeof(IVsFontAndColorStorage));
                var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0");
                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == VS.VSConstants.S_OK)
                {
                    LOGFONTW[] Fnt  = new LOGFONTW[] { new LOGFONTW() };
                    FontInfo[] Info = new FontInfo[] { new FontInfo() };
                    storage.GetFont(Fnt, Info);
                    _fontSize = (int)Info[0].wPointSize;
                }

                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == VS.VSConstants.S_OK)
                {
                    var info = new ColorableItemInfo[1];
                    storage.GetItem("Plain Text", info);
                    _backgroundColor = ConvertFromWin32Color((int)info[0].crBackground);
                }
            }
            catch { }
        }
Ejemplo n.º 11
0
        private uint GetRgbaColorValue(FontAndColorsResourceKey key)
        {
            const __FCSTORAGEFLAGS flags = __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS |
                                           __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS;

            fncStorage.OpenCategory(key.Category, (uint)flags);
            try {
                var itemInfos = new ColorableItemInfo[1];
                if (fncStorage.GetItem(key.Name, itemInfos) != VSConstants.S_OK)
                {
                    return(0xFF000000);
                }

                uint color;
                switch (key.KeyType)
                {
                case FontAndColorsResourceKeyType.ForegroundColor:
                case FontAndColorsResourceKeyType.ForegroundBrush:
                    color = itemInfos[0].crForeground;
                    break;

                case FontAndColorsResourceKeyType.BackgroundColor:
                case FontAndColorsResourceKeyType.BackgroundBrush:
                    color = itemInfos[0].crBackground;
                    break;

                default:
                    color = 0;
                    break;
                }

                color |= 0xFF000000;
                return(color);
            } finally {
                fncStorage.CloseCategory();
            }
        }
        static Tuple<Color?, Color?> TryGetItem(IVsFontAndColorStorage storage, string item)
        {
            Tuple<Color?, Color?> result = null;
            // load specific category to prevent our own format classifications being loaded
            InCategory(storage, Microsoft.VisualStudio.Editor.DefGuidList.guidTextEditorFontCategory, () =>
            {
                ColorableItemInfo[] colors = new ColorableItemInfo[1];
                var hresult = storage.GetItem(item, colors);
                if (hresult == 0)
                {
                    result = Tuple.Create<Color?, Color?>(ParseColor(colors[0].crForeground), ParseColor(colors[0].crBackground));
                }
                else
                {
                    result = Tuple.Create<Color?, Color?>(null, null);
                }
            });

            return result;
        }
Ejemplo n.º 13
0
        private Color LoadColor(IVsFontAndColorStorage vsStorage, ColorKey colorKey)
        {
            var array = new ColorableItemInfo[1];
            ErrorHandler.ThrowOnFailure(vsStorage.GetItem(colorKey.Name, array));

            int isValid = colorKey.IsForeground
                ? array[0].bForegroundValid
                : array[0].bBackgroundValid;
            if (isValid == 0)
            {
                throw new Exception();
            }

            uint colorRef = colorKey.IsForeground
                ? array[0].crForeground
                : array[0].crBackground;
            return FromColorRef(vsStorage, colorRef);
        }
Ejemplo n.º 14
0
        private async void HandleOpenSolution(object sender = null, EventArgs e = null)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync();

            //Use nested methods to avoid prompt (and need) for multiple MainThead checks/switches
            IEnumerable <ProjectItem> RecurseProjectItems(ProjectItems projItems)
            {
                if (projItems != null)
                {
                    foreach (ProjectItem item in projItems)
                    {
                        foreach (var subItem in RecurseProjectItem(item))
                        {
                            yield return(subItem);
                        }
                    }
                }
            }

            IEnumerable <ProjectItem> RecurseProjectItem(ProjectItem item)
            {
                yield return(item);

                foreach (var subItem in RecurseProjectItems(item.ProjectItems))
                {
                    yield return(subItem);
                }
            }

            IEnumerable <ProjectItem> GetProjectFiles(Project proj)
            {
                foreach (ProjectItem item in RecurseProjectItems(proj.ProjectItems))
                {
                    yield return(item);
                }
            }

            // TODO: handle res files being removed or added to a project - currently will be ignored. Issue #2
            // Get all resource files from the solution
            // Do this now, rather than in adornment manager for performance and to avoid thread issues
            if (await this.GetServiceAsync(typeof(DTE)) is DTE dte)
            {
                foreach (var project in dte.Solution.Projects)
                {
                    foreach (var solFile in GetProjectFiles((Project)project))
                    {
                        var filePath = solFile.FileNames[0];
                        var fileExt  = System.IO.Path.GetExtension(filePath);

                        // Only interested in resx files
                        if (fileExt.Equals(".resx"))
                        {
                            // Only want neutral language ones, not locale specific versions
                            if (!System.IO.Path.GetFileNameWithoutExtension(filePath).Contains("."))
                            {
                                ResourceAdornmentManager.ResourceFiles.Add(filePath);
                            }
                        }
                    }
                }

                IVsFontAndColorStorage storage = (IVsFontAndColorStorage)VSPackage.GetGlobalService(typeof(IVsFontAndColorStorage));

                var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0");

                // Seem like reasonabel defaults as should be visible on light & dark theme
                int   _fontSize  = 10;
                Color _textColor = Colors.Gray;

                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK)
                {
                    LOGFONTW[] Fnt  = new LOGFONTW[] { new LOGFONTW() };
                    FontInfo[] Info = new FontInfo[] { new FontInfo() };
                    storage.GetFont(Fnt, Info);

                    _fontSize = Info[0].wPointSize;
                }

                if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK)
                {
                    var info = new ColorableItemInfo[1];

                    // Get the color value configured for regular string display
                    storage.GetItem("String", info);

                    var win32Color = (int)info[0].crForeground;

                    int r = win32Color & 0x000000FF;
                    int g = (win32Color & 0x0000FF00) >> 8;
                    int b = (win32Color & 0x00FF0000) >> 16;

                    _textColor = Color.FromRgb((byte)r, (byte)g, (byte)b);
                }

                ResourceAdornmentManager.TextSize            = _fontSize;
                ResourceAdornmentManager.TextForegroundColor = _textColor;

                var plural = ResourceAdornmentManager.ResourceFiles.Count > 1 ? "s" : string.Empty;
                (await this.GetServiceAsync(typeof(DTE)) as DTE).StatusBar.Text = $"String Resource Visualizer initialized with {ResourceAdornmentManager.ResourceFiles.Count} resource file{plural}.";
            }
        }