// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Threading; namespace ICSharpCode.SharpDevelop.Gui { /// /// Represents a target where an active task reports progress to. /// /// /// This interface is not thread-safe, but it can be used to report progress from background threads which will be /// automatically sent to the correct GUI thread. /// Using a progress monitor from multiple threads is possible if the user synchronizes the access. /// public interface IProgressMonitor : IDisposable { /// /// Gets/Sets the amount of work already done within this task. /// Always uses a scale from 0 to 1 local to this progress monitor. /// double Progress { get; set; } /// /// Creates a nested task. /// /// The amount of work this sub-task performs in relation to the work of this task. /// That means, this parameter is used as a scaling factor for work performed within the subtask. /// A new progress monitor representing the sub-task. /// Multiple child progress monitors can be used at once; even concurrently on multiple threads. IProgressMonitor CreateSubTask(double workAmount); /// /// Gets/Sets the name to show while the task is active. /// string TaskName { get; set; } /// /// Gets/sets if the task current shows a modal dialog. Set this property to true to make progress /// dialogs windows temporarily invisible while your modal dialog is showing. /// bool ShowingDialog { // TODO: get rid of this. Don't mix calculations and UI! get; set; } /// /// Gets the cancellation token. /// CancellationToken CancellationToken { get; } /// /// Gets/Sets the operation status. /// /// Note: the status of the whole operation is the most severe status of all nested monitors. /// The more severe value persists even if the child monitor gets disposed. /// OperationStatus Status { get; set; } } /// /// Represents the status of a operation with progress monitor. /// public enum OperationStatus : byte { /// /// Everything is normal. /// Normal, /// /// There was at least one warning. /// Warning, /// /// There was at least one error. /// Error } /// /// Adapter IDomProgressMonitor -> IProgressMonitor /// public sealed class DomProgressMonitor : Dom.IDomProgressMonitor { IProgressMonitor monitor; private DomProgressMonitor(IProgressMonitor monitor) { if (monitor == null) throw new ArgumentNullException("monitor"); this.monitor = monitor; } public static Dom.IDomProgressMonitor Wrap(IProgressMonitor monitor) { if (monitor == null) return null; else return new DomProgressMonitor(monitor); } public bool ShowingDialog { get { return monitor.ShowingDialog; } set { monitor.ShowingDialog = value; } } } /// /// Dummy progress monitor implementation that does not report the progress anywhere. /// public sealed class DummyProgressMonitor : IProgressMonitor { public string TaskName { get; set; } public bool ShowingDialog { get; set; } public OperationStatus Status { get; set; } public CancellationToken CancellationToken { get; set; } public double Progress { get; set; } public IProgressMonitor CreateSubTask(double workAmount) { return new DummyProgressMonitor() { CancellationToken = this.CancellationToken }; } public void Dispose() { } } }