bool IsEmptySelectionContext(IVsTrackSelectionEx selCtx) { ThreadHelper.ThrowIfNotOnUIThread(); Validate.IsNotNull(selCtx, "selCtx"); IVsMonitorSelection2 monitorSelection2 = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection2; monitorSelection2.GetEmptySelectionContext(out var emptySelCtxt); return(ComUtilities.IsSameComObject(selCtx, emptySelCtxt)); }
/// <summary> /// Create the list of selection elements for the selection view. This includes the core slots as well as any /// registered extension provided selection elenents /// </summary> void InitializeSelectionItems() { ThreadHelper.ThrowIfNotOnUIThread(); IVsMonitorSelection selectionMonitor = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection; object elementValue; string elementName; // Initialize the current selection objects IVsHierarchy hierarchy = null; ISelectionContainer selectionContainer = null; selectionMonitor.GetCurrentSelection(out var pHierarchy, out var itemID, out var multiSelect, out var pSelectionContainer); if (pHierarchy != IntPtr.Zero) { hierarchy = Marshal.GetObjectForIUnknown(pHierarchy) as IVsHierarchy; } if (pSelectionContainer != IntPtr.Zero) { selectionContainer = Marshal.GetObjectForIUnknown(pSelectionContainer) as ISelectionContainer; } // Sync descriptions with selection by calling OnSelectionChanged SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.Hierarchy, description: string.Empty, owner: string.Empty)); SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.ItemID, description: string.Empty, owner: string.Empty)); SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.SelectionContainer, description: string.Empty, owner: string.Empty)); SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)SelectionItemInfo.SpecialElement.MultiItemSelect, description: string.Empty, owner: string.Empty)); OnSelectionChanged(null, 0, null, null, hierarchy, itemID, multiSelect, selectionContainer); // Initialize the current selection data with the element values VSConstants.SelectionElement[] elements = { VSConstants.SelectionElement.WindowFrame, VSConstants.SelectionElement.DocumentFrame, VSConstants.SelectionElement.UndoManager, VSConstants.SelectionElement.StartupProject, VSConstants.SelectionElement.UserContext, VSConstants.SelectionElement.PropertyBrowserSID, VSConstants.SelectionElement.ResultList, VSConstants.SelectionElement.LastWindowFrame }; foreach (VSConstants.SelectionElement element in elements) { selectionMonitor.GetCurrentElementValue((uint)element, out elementValue); object valueOwner = GetOwnerForSelectedElement(element); SelectionItems.Add(new SelectionItemInfo(element, GetSelectionElementDescription(elementValue), GetSelectionElementDescription(valueOwner))); } // Read the VSIP registered selection elements // Skip this one, it is not supported yet and will always be empty Guid surfaceSelectionElement = new Guid("{64db9e55-5614-44b3-93c9-e617b95eeb5f}"); IVsMonitorSelection2 selectionMonitor2 = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection2; using RegistryKey rootKey = GetRegistryRoot(); using RegistryKey elementsKey = rootKey.OpenSubKey("SelectionElements"); if (elementsKey != null) { string[] elementGuids = elementsKey.GetSubKeyNames(); foreach (string guidString in elementGuids) { using RegistryKey elementKey = elementsKey.OpenSubKey(guidString); Guid elementGuid = new Guid(guidString); selectionMonitor2.GetElementID(ref elementGuid, out var selElem); selectionMonitor.GetCurrentElementValue(selElem, out elementValue); elementName = (string)elementKey.GetValue("Name", defaultValue: string.Empty); // Skip this one, it is not supported yet and will always be empty if (elementGuid == surfaceSelectionElement) { continue; } object contextOwner = GetOwnerForSelectedElement((VSConstants.SelectionElement)selElem); SelectionItems.Add(new SelectionItemInfo((VSConstants.SelectionElement)selElem, elementName, GetSelectionElementDescription(elementValue), GetSelectionElementDescription(contextOwner))); } } }