Пример #1
0
        int IVsSelectionEvents.OnElementValueChanged(uint elementId, object oldValue, object newValue)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (newValue is IVsWindowFrame frame &&
                IsFrameElement() &&
                IsDocumentFrame())
            {
                var textDocument = textDocumentProvider.GetFromFrame(frame);

                if (textDocument != null)
                {
                    OnDocumentFocused?.Invoke(this, new DocumentFocusedEventArgs(textDocument));
                }
            }

            return(VSConstants.S_OK);

            bool IsFrameElement()
            {
                return(elementId == (uint)VSConstants.VSSELELEMID.SEID_WindowFrame);
            }

            bool IsDocumentFrame()
            {
                return(ErrorHandler.Succeeded(frame.GetProperty((int)__VSFPROPID.VSFPROPID_Type, out var frameType)) &&
                       (int)frameType == (int)__WindowFrameTypeFlags.WINDOWFRAMETYPE_Document);
            }
        }
        public ITextDocument FindActiveDocument()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Get the current doc frame, if there is one
            monitorSelection.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out var pvarValue);
            if (!(pvarValue is IVsWindowFrame frame))
            {
                return(null);
            }

            return(textDocumentProvider.GetFromFrame(frame));
        }
        int IVsSelectionEvents.OnElementValueChanged(uint elementId, object oldValue, object newValue)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Note: this notification can fire multiple times with different elementId value
            // when the doc frame changes. Two cases:
            // [ tool or doc window ] -> [ tool or doc window ] => elementId == SEID_WindowFrame
            // [ doc window ] -> [ doc window ] => elementId == SEID_DocumentFrame
            // We are only interested in the second case. See bugs #2079 and #2091.
            if (elementId == (uint)VSConstants.VSSELELEMID.SEID_DocumentFrame)
            {
                ITextDocument activeTextDoc = null;
                if (newValue != null && newValue is IVsWindowFrame frame)
                {
                    activeTextDoc = textDocumentProvider.GetFromFrame(frame);
                }

                // The "active document" will be null if the last document has just been closed
                ActiveDocumentChanged?.Invoke(this, new ActiveDocumentChangedEventArgs(activeTextDoc));
            }

            return(VSConstants.S_OK);
        }