/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.ComponentModel.Composition; using Microsoft.VisualStudio.Utilities; namespace dnSpy.Contracts.Documents.Tabs.DocViewer { /// /// Notifies listeners when certain events occur. You can /// manually import this instance and hook the events or you can export an /// public interface IDocumentViewerService { /// /// Raised when a new instance has been created /// event EventHandler? Added; /// /// Raised when a instance has been closed /// event EventHandler? Removed; /// /// Raised when the instance gets new content /// (its /// method was called). It's only raised if the new content is different from the current /// content. I.e., calling it twice in a row with the same content won't raise this event /// the second time. /// event EventHandler? GotNewContent; } /// /// Gets notified when a document viewer event occurs. Use /// to export an instance. /// public interface IDocumentViewerListener { /// /// Raised when some event occurred /// /// Event arguments void OnEvent(DocumentViewerEventArgs e); } /// Metadata public interface IDocumentViewerListenerMetadata { /// See double Order { get; } } /// /// Exports a instance /// [MetadataAttribute, AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public sealed class ExportDocumentViewerListenerAttribute : ExportAttribute, IDocumentViewerListenerMetadata { /// Constructor public ExportDocumentViewerListenerAttribute() : this(DocumentViewerListenerConstants.ORDER_DEFAULT) { } /// Constructor /// Order of this instance, eg. public ExportDocumentViewerListenerAttribute(double order) : base(typeof(IDocumentViewerListener)) => Order = order; /// /// Order of this instance /// public double Order { get; set; } } /// /// event /// public enum DocumentViewerEvent { /// /// Raised when a new instance has been created /// Added, /// /// Raised when a instance has been closed /// Removed, /// /// Raised after the instance got new content /// (its /// method was called). It's only raised if the new content is different from the current /// content. I.e., calling it twice in a row with the same content won't raise this event /// the second time. /// GotNewContent, } /// /// Document viewer event args base class /// public abstract class DocumentViewerEventArgs : EventArgs { /// /// Gets the event type /// public abstract DocumentViewerEvent EventType { get; } /// /// Gets the instance /// public IDocumentViewer DocumentViewer { get; } /// /// Constructor /// /// instance protected DocumentViewerEventArgs(IDocumentViewer documentViewer) => DocumentViewer = documentViewer ?? throw new ArgumentNullException(nameof(documentViewer)); } /// /// Document viewer added event args /// public sealed class DocumentViewerAddedEventArgs : DocumentViewerEventArgs { /// /// Returns the event type, which is /// public override DocumentViewerEvent EventType => DocumentViewerEvent.Added; /// /// Constructor /// /// instance public DocumentViewerAddedEventArgs(IDocumentViewer documentViewer) : base(documentViewer) { } } /// /// Document viewer removed event args /// public sealed class DocumentViewerRemovedEventArgs : DocumentViewerEventArgs { /// /// Returns the event type, which is /// public override DocumentViewerEvent EventType => DocumentViewerEvent.Removed; /// /// Constructor /// /// instance public DocumentViewerRemovedEventArgs(IDocumentViewer documentViewer) : base(documentViewer) { } } /// /// New content event args /// public sealed class DocumentViewerGotNewContentEventArgs : DocumentViewerEventArgs { /// /// Returns the event type, which is /// public override DocumentViewerEvent EventType => DocumentViewerEvent.GotNewContent; /// /// New content /// public DocumentViewerContent Content { get; } /// /// New content type /// public IContentType ContentType { get; } /// /// Constructor /// /// Document viewer /// New content /// Content type public DocumentViewerGotNewContentEventArgs(IDocumentViewer documentViewer, DocumentViewerContent content, IContentType contentType) : base(documentViewer) { Content = content ?? throw new ArgumentNullException(nameof(content)); ContentType = contentType ?? throw new ArgumentNullException(nameof(contentType)); } } }