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
        internal static void AdjustFontSize(IServiceProvider serviceProvider, Guid category, short change)
        {
            IVsFontAndColorStorage   storage   = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(SVsFontAndColorStorage));
            IVsFontAndColorUtilities utilities = storage as IVsFontAndColorUtilities;

            LOGFONTW[] pLOGFONT = new LOGFONTW[1];
            FontInfo[] pInfo    = new FontInfo[1];

            ErrorHandler.ThrowOnFailure(storage.OpenCategory(category, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES)));
            try
            {
                if (!ErrorHandler.Succeeded(storage.GetFont(pLOGFONT, pInfo)))
                {
                    return;
                }

                pInfo[0].wPointSize = checked ((ushort)(pInfo[0].wPointSize + change));
                ErrorHandler.ThrowOnFailure(storage.SetFont(pInfo));
                ErrorHandler.ThrowOnFailure(utilities.FreeFontInfo(pInfo));
            }
            finally
            {
                storage.CloseCategory();
            }
        }
Ejemplo n.º 4
0
        private void VSColorTheme_ThemeChanged(ThemeChangedEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (_classificationFormatMapService == null || _classificationRegistry == null)
            {
                var componentModel = _serviceProvider.GetService <SComponentModel, IComponentModel>();

                _classificationFormatMapService ??= componentModel?.GetService <IClassificationFormatMapService>();
                _classificationRegistry ??= componentModel?.GetService <IClassificationTypeRegistryService>();
            }

            if (_classificationFormatMapService == null || _classificationRegistry == null)
            {
                return;
            }

            var logger = AcuminatorVSPackage.Instance?.AcuminatorLogger;

            _fontAndColorStorage ??= _serviceProvider.GetService <SVsFontAndColorStorage, IVsFontAndColorStorage>();
            _fontAndColorCacheManager ??= _serviceProvider.GetService <SVsFontAndColorCacheManager, IVsFontAndColorCacheManager>();
            IClassificationFormatMap formatMap = _classificationFormatMapService.GetClassificationFormatMap(category: TextCategory);

            if (_fontAndColorStorage == null || _fontAndColorCacheManager == null || formatMap == null)
            {
                return;
            }

            _fontAndColorCacheManager.CheckCache(ref _mefItemsGuid, out int _);
            int openCategoryResult = _fontAndColorStorage.OpenCategory(ref _mefItemsGuid, (uint)__FCSTORAGEFLAGS.FCSF_READONLY);

            if (openCategoryResult != VSConstants.S_OK)
            {
                logger?.LogMessage($"Error on opening category in the registry during the theme change. The error code is {openCategoryResult}", Logger.LogMode.Error);
            }

            try
            {
                var acuminatorThemeChangedEventArgs = new AcuminatorThemeChangedEventArgs(_fontAndColorStorage, _classificationRegistry, formatMap);

                formatMap.BeginBatchUpdate();
                AcuminatorThemeChanged?.Invoke(this, acuminatorThemeChangedEventArgs);
            }
            catch (Exception exception)
            {
                logger?.LogException(exception, logOnlyFromAcuminatorAssemblies: false, Logger.LogMode.Error);
            }
            finally
            {
                formatMap.EndBatchUpdate();
                int refreshCacheResult = _fontAndColorCacheManager.RefreshCache(ref _mefItemsGuid);

                if (refreshCacheResult != VSConstants.S_OK)
                {
                    logger?.LogMessage($"Error on the refresh of MEF Items cache in the registry during the theme change. The error code is {refreshCacheResult}", Logger.LogMode.Error);
                }

                _fontAndColorStorage.CloseCategory();
            }
        }
Ejemplo n.º 5
0
        FontInfo?GetTextEditorFontInfo()
        {
            IVsFontAndColorStorage fontStorage = (IVsFontAndColorStorage)VSAsmPackage.GetGlobalService(typeof(SVsFontAndColorStorage));

            if (fontStorage == null)
            {
                return(null);
            }

            if (fontStorage.OpenCategory(TextEditorFontGuid, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) != VSConstants.S_OK)
            {
                return(null);
            }

            FontInfo[] info   = new FontInfo[1];
            int        result = fontStorage.GetFont(null, info);

            fontStorage.CloseCategory();

            if (result != VSConstants.S_OK)
            {
                return(null);
            }

            return(info[0]);
        }
Ejemplo n.º 6
0
        public FontAndColorProvider()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            _storage = (IVsFontAndColorStorage)GetGlobalService(typeof(SVsFontAndColorStorage));
            Assumes.Present(_storage);
            ErrorHandler.ThrowOnFailure(_storage.OpenCategory(Constants.FontAndColorsCategoryGuid, _storageFlags));

            _service = (FontAndColorService)GetGlobalService(typeof(FontAndColorService));
            _service.ItemsChanged += FontAndColorItemsChanged;
            FontAndColorState      = new FontAndColorState(this);
        }
Ejemplo n.º 7
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.º 8
0
        private FontFamily GetFontFamily(FontAndColorsResourceKey key)
        {
            const __FCSTORAGEFLAGS flags = __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS |
                                           __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS;

            fncStorage.OpenCategory(key.Category, (uint)flags);
            try {
                var fontInfos = new FontInfo[1];
                if (fncStorage.GetFont(null, fontInfos) != VSConstants.S_OK)
                {
                    return(null);
                }

                if (fontInfos[0].bFaceNameValid == 1)
                {
                    return(new FontFamily(fontInfos[0].bstrFaceName));
                }

                return(null);
            } finally {
                fncStorage.CloseCategory();
            }
        }
        static void InCategory(IVsFontAndColorStorage storage, Guid category, Action callback)
        {
            var hresult = storage.OpenCategory(ref category, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS));

            try
            {
                if (hresult == 0)
                    callback();
            }
            finally
            {
                storage.CloseCategory();
            }
        }
        static void InCategory(IVsFontAndColorStorage storage, Guid category, Action callback)
        {
            var hresult = storage.OpenCategory(ref category, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS));

            try
            {
                if (hresult == 0)
                {
                    callback();
                }
            }
            finally
            {
                storage.CloseCategory();
            }
        }
Ejemplo n.º 11
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.º 12
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();
            }
        }
Ejemplo n.º 13
0
        public static async Task AdjustFontSizeAsync(string categoryGuid, short change)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsFontAndColorStorage storage = await VS.Shell.GetFontAndColorStorageAsync();

            Assumes.Present(storage);
            // ReSharper disable once SuspiciousTypeConversion.Global
            var utilities = storage as IVsFontAndColorUtilities;

            if (utilities == null)
            {
                return;
            }

            var pLOGFONT = new LOGFONTW[1];
            var pInfo    = new FontInfo[1];
            var category = new Guid(categoryGuid);

            ErrorHandler.ThrowOnFailure(storage.OpenCategory(category, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES)));
            try
            {
                if (!ErrorHandler.Succeeded(storage.GetFont(pLOGFONT, pInfo)))
                {
                    return;
                }

                pInfo[0].wPointSize = checked ((ushort)(pInfo[0].wPointSize + change));
                ErrorHandler.ThrowOnFailure(storage.SetFont(pInfo));
                ErrorHandler.ThrowOnFailure(utilities.FreeFontInfo(pInfo));
            }
            finally
            {
                storage.CloseCategory();
            }
        }
Ejemplo n.º 14
0
        public void ApplyColorSet(ThemeAsset colorSet)
        {
            // these guids are like windows, a lot of different windows have plain text
            // this one is the main category, other categories may be printers etc
            m_pStorage.OpenCategory(new Guid("{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}"), (uint)__FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES);
            {
                SetForegroundColor("Plain Text", colorSet.GetColor(eThemeColor.PlainText_FG));
                SetBackgroundColor("Plain Text", colorSet.GetColor(eThemeColor.PlainText_BG));

                SetForegroundColor("Selected Text", colorSet.GetColor(eThemeColor.SelectedText_FG));
                SetBackgroundColor("Selected Text", colorSet.GetColor(eThemeColor.SelectedText_BG));

                SetForegroundColor("Inactive Selected Text", colorSet.GetColor(eThemeColor.InactiveSelectedText_FG));
                SetBackgroundColor("Inactive Selected Text", colorSet.GetColor(eThemeColor.InactiveSelectedText_BG));
            }
            m_pStorage.CloseCategory();

            // {358463D0-D084-400F-997E-A34FC570BC72}
            // changed text, selected text, text

            // {8259ACED-490A-41B3-A0FB-64C842CCDC80}
            // changed text, selected text, text

            // {A7EE6BEE-D0AA-4B2F-AD9D-748276A725F6}
            // changed text, selected text, text

            // {40660F54-80FA-4375-89A3-8D06AA954EBA}
            // these change when "all text tool windows" changes
            // inactive selected text, plain text, selected text

            // {9E632E6E-D786-4F9A-8D3E-B9398836C784}
            // inactive selected text, plain text, selected text

            // {6BB65C5A-2F31-4BDE-9F48-8A38DC0C63E7}
            // inactive selected text, plain text, selected text

            // {CE2ECED5-C21C-464C-9B45-15E10E9F9EF9}
            // inactive selected text, plain text, selected text

            // {EE1BE240-4E81-4BEB-8EEA-54322B6B1BF5}
            // inactive selected text, plain text, selected text

            // {5C48B2CB-0366-4FBF-9786-0BB37E945687}
            // "all text tool windows"
            // inactive selected text, plain text, selected text, current list location

            // {9973EFDF-317D-431C-8BC1-5E88CBFD4F7F}
            // inactive selected text, plain text, selected text, current list location



            // what is this
            m_pStorage.OpenCategory(new Guid("{CE2ECED5-C21C-464C-9B45-15E10E9F9EF9}"), (uint)__FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES);
            {
                SetForegroundColor("Plain Text", colorSet.GetColor(eThemeColor.PlainText_FG));
                SetBackgroundColor("Plain Text", colorSet.GetColor(eThemeColor.PlainText_BG));

                SetForegroundColor("Selected Text", colorSet.GetColor(eThemeColor.SelectedText_FG));
                SetBackgroundColor("Selected Text", colorSet.GetColor(eThemeColor.SelectedText_BG));

                SetForegroundColor("Inactive Selected Text", colorSet.GetColor(eThemeColor.InactiveSelectedText_FG));
                SetBackgroundColor("Inactive Selected Text", colorSet.GetColor(eThemeColor.InactiveSelectedText_BG));
            }
            m_pStorage.CloseCategory();
        }
Ejemplo n.º 15
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}.";
            }
        }