From abcbdbca5fc445a4eac9024a41d803cd4e62f9ab Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 14 Mar 2015 17:34:12 +0000 Subject: WinGui: Fixed the temp Property Changed Base Impl. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6987 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../HandBrake.ApplicationServices.csproj | 2 + .../Utilities/Execute.cs | 136 +++++++++++++++++++++ .../Interfaces/INotifyPropertyChangedEx.cs | 37 ++++++ .../Utilities/PropertyChangedBase.cs | 92 +++++++++----- .../HandBrakeWPF/Services/PrePostActionService.cs | 2 + win/CS/HandBrakeWPF/Services/QueueProcessor.cs | 1 + win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 1 + win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs | 2 + 8 files changed, 244 insertions(+), 29 deletions(-) create mode 100644 win/CS/HandBrake.ApplicationServices/Utilities/Execute.cs create mode 100644 win/CS/HandBrake.ApplicationServices/Utilities/Interfaces/INotifyPropertyChangedEx.cs (limited to 'win') diff --git a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index a507cfdc8..e84331529 100644 --- a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -190,7 +190,9 @@ + + diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/Execute.cs b/win/CS/HandBrake.ApplicationServices/Utilities/Execute.cs new file mode 100644 index 000000000..7f5889891 --- /dev/null +++ b/win/CS/HandBrake.ApplicationServices/Utilities/Execute.cs @@ -0,0 +1,136 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Enables easy marshalling of code to the UI thread. +// Borrowed from Caliburn Micro. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.ApplicationServices.Utilities +{ + using System; + using System.ComponentModel; + using System.Diagnostics; + using System.Threading.Tasks; + using System.Windows; + using System.Windows.Threading; + + /// + /// Enables easy marshalling of code to the UI thread. + /// + public static class Execute + { + private static System.Action executor = (System.Action)(action => action()); + private static Dispatcher dispatcher; + private static bool? inDesignMode; + + /// + /// Indicates whether or not the framework is in design-time mode. + /// + public static bool InDesignMode + { + get + { + if (!Execute.inDesignMode.HasValue) + { + Execute.inDesignMode = new bool?((bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue); + if (!Execute.inDesignMode.GetValueOrDefault(false) && Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal)) + Execute.inDesignMode = new bool?(true); + } + return Execute.inDesignMode.GetValueOrDefault(false); + } + } + + /// + /// Initializes the framework using the current dispatcher. + /// + public static void InitializeWithDispatcher() + { + Execute.dispatcher = Dispatcher.CurrentDispatcher; + Execute.executor = (System.Action)null; + } + + /// + /// Resets the executor to use a non-dispatcher-based action executor. + /// + public static void ResetWithoutDispatcher() + { + executor = (System.Action)(action => action()); + dispatcher = (Dispatcher)null; + } + + /// + /// Sets a custom UI thread marshaller. + /// + /// The marshaller. + [Obsolete] + public static void SetUIThreadMarshaller(System.Action marshaller) + { + Execute.executor = marshaller; + Execute.dispatcher = (Dispatcher)null; + } + + private static void ValidateDispatcher() + { + if (Execute.dispatcher == null) + throw new InvalidOperationException("Not initialized with dispatcher."); + } + + /// + /// Executes the action on the UI thread asynchronously. + /// + /// The action to execute. + public static void BeginOnUIThread(this System.Action action) + { + Execute.ValidateDispatcher(); + Execute.dispatcher.BeginInvoke((Delegate)action); + } + + /// + /// Executes the action on the UI thread asynchronously. + /// + /// The action to execute. + public static Task OnUIThreadAsync(this System.Action action) + { + Execute.ValidateDispatcher(); + TaskCompletionSource taskSource = new TaskCompletionSource(); + System.Action action1 = (System.Action)(() => + { + try + { + action(); + taskSource.SetResult((object)null); + } + catch (Exception ex) + { + taskSource.SetException(ex); + } + }); + Execute.dispatcher.BeginInvoke((Delegate)action1); + return (Task)taskSource.Task; + } + + private static bool CheckAccess() + { + if (Execute.dispatcher != null) + return Execute.dispatcher.CheckAccess(); + return true; + } + + /// + /// Executes the action on the UI thread. + /// + /// The action to execute. + public static void OnUIThread(this System.Action action) + { + if (Execute.executor != null) + Execute.executor(action); + else if (Execute.CheckAccess()) + action(); + else + Execute.OnUIThreadAsync(action).Wait(); + } + } +} diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/Interfaces/INotifyPropertyChangedEx.cs b/win/CS/HandBrake.ApplicationServices/Utilities/Interfaces/INotifyPropertyChangedEx.cs new file mode 100644 index 000000000..add37626b --- /dev/null +++ b/win/CS/HandBrake.ApplicationServices/Utilities/Interfaces/INotifyPropertyChangedEx.cs @@ -0,0 +1,37 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Extends such that the change event can be raised by external parties. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.ApplicationServices.Utilities.Interfaces +{ + using System.ComponentModel; + + /// + /// Extends such that the change event can be raised by external parties. + /// + public interface INotifyPropertyChangedEx : INotifyPropertyChanged + { + /// + /// Enables/Disables property change notification. + /// + bool IsNotifying { get; set; } + + /// + /// Notifies subscribers of the property change. + /// + /// + /// Name of the property. + /// + void NotifyOfPropertyChange(string propertyName); + + /// + /// Raises a change notification indicating that all bindings should be refreshed. + /// + void Refresh(); + } +} diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/PropertyChangedBase.cs b/win/CS/HandBrake.ApplicationServices/Utilities/PropertyChangedBase.cs index 9809def57..b5de5dd22 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/PropertyChangedBase.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/PropertyChangedBase.cs @@ -1,10 +1,10 @@ // -------------------------------------------------------------------------------------------------------------------- -// +// // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling. -// This class is a modified version of the caliburn micro +// Borrowed from Caliburn Micro // // -------------------------------------------------------------------------------------------------------------------- @@ -15,72 +15,106 @@ namespace HandBrake.ApplicationServices.Utilities using System.Linq.Expressions; using System.Runtime.Serialization; + using HandBrake.ApplicationServices.Utilities.Interfaces; + /// - /// Property Changed Base implimentation. + /// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling. /// - [DataContract] - public class PropertyChangedBase : INotifyPropertyChanged + [Serializable] + public class PropertyChangedBase : INotifyPropertyChangedEx, INotifyPropertyChanged { + [NonSerialized] + private bool isNotifying; + /// - /// Creates an instance of . + /// Enables/Disables property change notification. /// - public PropertyChangedBase() + [Browsable(false)] + public bool IsNotifying { - IsNotifying = true; + get + { + return this.isNotifying; + } + set + { + this.isNotifying = value; + } } /// /// Occurs when a property value changes. /// - public event PropertyChangedEventHandler PropertyChanged = delegate { }; + public event PropertyChangedEventHandler PropertyChanged = (param0, param1) => { }; /// - /// Enables/Disables property change notification. + /// Creates an instance of . /// - public bool IsNotifying { get; set; } + public PropertyChangedBase() + { + this.IsNotifying = true; + } /// /// Raises a change notification indicating that all bindings should be refreshed. /// - public virtual void Refresh() + public void Refresh() { - NotifyOfPropertyChange(string.Empty); + this.NotifyOfPropertyChange(string.Empty); } /// /// Notifies subscribers of the property change. /// - /// Name of the property. - public virtual void NotifyOfPropertyChange(string propertyName = null) + /// Name of the property. + public virtual void NotifyOfPropertyChange(string propertyName) { - if (IsNotifying) - { - OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); - } + if (!this.IsNotifying) + return; + Execute.OnUIThread((System.Action)(() => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)))); } /// /// Notifies subscribers of the property change. /// - /// The type of the property. - /// The property expression. + /// The type of the property.The property expression. public void NotifyOfPropertyChange(Expression> property) { - NotifyOfPropertyChange(property.GetMemberInfo().Name); + this.NotifyOfPropertyChange(ExtensionMethods.GetMemberInfo((Expression)property).Name); } /// - /// Raises the event directly. + /// Raises the event directly. /// - /// The instance containing the event data. + /// The instance containing the event data. [EditorBrowsable(EditorBrowsableState.Never)] protected void OnPropertyChanged(PropertyChangedEventArgs e) { - var handler = PropertyChanged; - if (handler != null) - { - handler(this, e); - } + PropertyChangedEventHandler changedEventHandler = this.PropertyChanged; + if (changedEventHandler == null) + return; + changedEventHandler((object)this, e); + } + + /// + /// Called when the object is deserialized. + /// + /// The streaming context. + [OnDeserialized] + public void OnDeserialized(StreamingContext c) + { + this.IsNotifying = true; + } + + /// + /// Used to indicate whether or not the IsNotifying property is serialized to Xml. + /// + /// + /// Whether or not to serialize the IsNotifying property. The default is false. + /// + public virtual bool ShouldSerializeIsNotifying() + { + return false; } } } diff --git a/win/CS/HandBrakeWPF/Services/PrePostActionService.cs b/win/CS/HandBrakeWPF/Services/PrePostActionService.cs index b8501b999..5bb8ca0ad 100644 --- a/win/CS/HandBrakeWPF/Services/PrePostActionService.cs +++ b/win/CS/HandBrakeWPF/Services/PrePostActionService.cs @@ -22,6 +22,8 @@ namespace HandBrakeWPF.Services using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.ViewModels.Interfaces; + using Execute = Caliburn.Micro.Execute; + /// /// The when done service. /// diff --git a/win/CS/HandBrakeWPF/Services/QueueProcessor.cs b/win/CS/HandBrakeWPF/Services/QueueProcessor.cs index a6dfe83c4..0507f7307 100644 --- a/win/CS/HandBrakeWPF/Services/QueueProcessor.cs +++ b/win/CS/HandBrakeWPF/Services/QueueProcessor.cs @@ -24,6 +24,7 @@ namespace HandBrakeWPF.Services using HandBrake.ApplicationServices.Services.Encode.Interfaces; using HandBrake.ApplicationServices.Utilities; + using Execute = Caliburn.Micro.Execute; using IQueueProcessor = HandBrakeWPF.Services.Interfaces.IQueueProcessor; using QueueCompletedEventArgs = HandBrakeWPF.EventArgs.QueueCompletedEventArgs; using QueueProgressEventArgs = HandBrakeWPF.EventArgs.QueueProgressEventArgs; diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index 88aa0c9e9..c35d6d7de 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -52,6 +52,7 @@ namespace HandBrakeWPF.ViewModels using Ookii.Dialogs.Wpf; + using Execute = Caliburn.Micro.Execute; using IQueueProcessor = HandBrakeWPF.Services.Interfaces.IQueueProcessor; /// diff --git a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs index 31841a335..2e73657cb 100644 --- a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs @@ -28,6 +28,8 @@ namespace HandBrakeWPF.ViewModels using Ookii.Dialogs.Wpf; + using Execute = Caliburn.Micro.Execute; + /// /// The Options View Model /// -- cgit v1.2.3