summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Utilities
diff options
context:
space:
mode:
authorScott <[email protected]>2015-09-26 21:05:40 +0100
committerScott <[email protected]>2015-09-26 21:30:32 +0100
commit1320d77d36f096c7aa5b1697d3aaf9aa35d5e199 (patch)
tree1a9964eac27b7689b39a2e5adbf019a7295b6a9b /win/CS/HandBrakeWPF/Utilities
parente703a7961f12a3e02c475754862a1f4a57a04646 (diff)
App Services Tidyup Contd
Moving all the helper and utility classes to the gui project
Diffstat (limited to 'win/CS/HandBrakeWPF/Utilities')
-rw-r--r--win/CS/HandBrakeWPF/Utilities/EnumHelper.cs160
-rw-r--r--win/CS/HandBrakeWPF/Utilities/Execute.cs153
-rw-r--r--win/CS/HandBrakeWPF/Utilities/ExtensionMethods.cs45
-rw-r--r--win/CS/HandBrakeWPF/Utilities/Interfaces/INotifyPropertyChangedEx.cs37
-rw-r--r--win/CS/HandBrakeWPF/Utilities/PropertyChangedBase.cs121
5 files changed, 516 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
new file mode 100644
index 000000000..29202efe6
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
@@ -0,0 +1,160 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="EnumHelper.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Enum Helpers
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.ObjectModel;
+ using System.ComponentModel;
+ using System.ComponentModel.DataAnnotations;
+ using System.Diagnostics;
+ using System.Linq;
+ using System.Reflection;
+
+ using HandBrake.ApplicationServices.Attributes;
+
+ /// <summary>
+ /// Enum Helpers
+ /// </summary>
+ /// <typeparam name="T">
+ /// The Type Parameter
+ /// </typeparam>
+ public class EnumHelper<T>
+ {
+ /// <summary>
+ /// Get the description of an Enum
+ /// </summary>
+ /// <param name="value">
+ /// The value.
+ /// </param>
+ /// <returns>
+ /// The Description string
+ /// </returns>
+ public static string GetDescription(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ DescriptionAttribute[] attributes =
+ (DescriptionAttribute[])fieldInfo.GetCustomAttributes(
+ typeof(DescriptionAttribute), false);
+ return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
+ }
+
+ /// <summary>
+ /// Get the Display Value of the Enum Model
+ /// </summary>
+ /// <param name="value">An Enum with Display Attributes</param>
+ /// <returns>A string name</returns>
+ public static string GetDisplay(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ DisplayAttribute[] attributes = (DisplayAttribute[])fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false);
+
+ return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
+ }
+
+ /// <summary>
+ /// Get the Enumeration for a given Enum Description
+ /// </summary>
+ /// <param name="description">The String description</param>
+ /// <returns>The Enum Value</returns>
+ public static T GetValue(string description)
+ {
+ return GetValue(description, false);
+ }
+
+ /// <summary>
+ /// Get the Enumeration for a given Enum Description
+ /// </summary>
+ /// <param name="description">The String description</param>
+ /// <param name="insensitiveCase">Turn of sensitivity to cases.</param>
+ /// <returns>The Enum Value</returns>
+ public static T GetValue(string description, bool insensitiveCase)
+ {
+ foreach (T val in Enum.GetValues(typeof(T)))
+ {
+ string currDescription = GetDescription(val);
+ string currDisplay = GetDisplay(val);
+ string shortName = GetShortName(val);
+ if (currDescription == description || currDisplay == description || shortName == description)
+ {
+ return val;
+ }
+
+ if (insensitiveCase && (currDescription.ToLower() == description.ToLower() || currDisplay.ToLower() == description.ToLower() || shortName.ToLower() == description.ToLower()))
+ {
+ return val;
+ }
+ }
+
+ Debug.WriteLine("EnumHelper.GetValue: The Description for the enum was not recognized: " + description);
+
+ return default(T);
+ }
+
+ /// <summary>
+ /// The get short name.
+ /// </summary>
+ /// <param name="value">
+ /// The value.
+ /// </param>
+ /// <returns>
+ /// The <see cref="string"/>.
+ /// </returns>
+ public static string GetShortName(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ ShortName[] attributes = (ShortName[])fieldInfo.GetCustomAttributes(typeof(ShortName), false);
+
+ return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
+ }
+
+ /// <summary>
+ /// Return a list of all the enum values.
+ /// </summary>
+ /// <returns>
+ /// An Enum Oject List
+ /// </returns>
+ public static IEnumerable<T> GetEnumList()
+ {
+ return Enum.GetValues(typeof(T)).Cast<T>().ToList();
+ }
+
+ /// <summary>
+ /// Get a list of string names for each enum value.
+ /// </summary>
+ /// <param name="enumType">
+ /// The enum type.
+ /// </param>
+ /// <returns>
+ /// A collection of strings that represent all the enum values
+ /// </returns>
+ public static IEnumerable<string> GetEnumDisplayValues(Type enumType)
+ {
+ var strings = new Collection<string>();
+ foreach (T e in Enum.GetValues(enumType))
+ strings.Add(GetDisplay(e));
+ return strings;
+ }
+
+ /// <summary>
+ /// Get a list of string names for each enum value passed in.
+ /// </summary>
+ /// <param name="items">
+ /// The items.
+ /// </param>
+ /// <returns>
+ /// A collection of strings that represent all the enum values
+ /// </returns>
+ public static IEnumerable<string> GetEnumDisplayValuesSubset(IEnumerable<T> items)
+ {
+ return items.Select(GetDisplay).ToList();
+ }
+ }
+}
diff --git a/win/CS/HandBrakeWPF/Utilities/Execute.cs b/win/CS/HandBrakeWPF/Utilities/Execute.cs
new file mode 100644
index 000000000..34fdf3a44
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/Execute.cs
@@ -0,0 +1,153 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright company="HandBrake Project (http://handbrake.fr)" file="Execute.cs">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Enables easy marshalling of code to the UI thread.
+// Borrowed from Caliburn Micro.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+ using System;
+ using System.ComponentModel;
+ using System.Diagnostics;
+ using System.Threading.Tasks;
+ using System.Windows;
+ using System.Windows.Threading;
+
+ /// <summary>
+ /// Enables easy marshalling of code to the UI thread.
+ /// </summary>
+ public static class Execute
+ {
+ private static System.Action<System.Action> executor = (System.Action<System.Action>)(action => action());
+ private static Dispatcher dispatcher;
+ private static bool? inDesignMode;
+
+ /// <summary>
+ /// Gets a value indicating whether or not the framework is in design-time mode.
+ /// </summary>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Initializes the framework using the current dispatcher.
+ /// </summary>
+ public static void InitializeWithDispatcher()
+ {
+ Execute.dispatcher = Dispatcher.CurrentDispatcher;
+ Execute.executor = (System.Action<System.Action>)null;
+ }
+
+ /// <summary>
+ /// Resets the executor to use a non-dispatcher-based action executor.
+ /// </summary>
+ public static void ResetWithoutDispatcher()
+ {
+ executor = (System.Action<System.Action>)(action => action());
+ dispatcher = (Dispatcher)null;
+ }
+
+ /// <summary>
+ /// Sets a custom UI thread marshaller.
+ /// </summary>
+ /// <param name="marshaller">The marshaller.</param>
+ [Obsolete]
+ public static void SetUIThreadMarshaller(System.Action<System.Action> marshaller)
+ {
+ Execute.executor = marshaller;
+ Execute.dispatcher = (Dispatcher)null;
+ }
+
+ /// <summary>
+ /// The validate dispatcher.
+ /// </summary>
+ /// <exception cref="InvalidOperationException">
+ /// Not initialized with dispatcher.
+ /// </exception>
+ private static void ValidateDispatcher()
+ {
+ if (Execute.dispatcher == null)
+ throw new InvalidOperationException("Not initialized with dispatcher.");
+ }
+
+ /// <summary>
+ /// Executes the action on the UI thread asynchronously.
+ /// </summary>
+ /// <param name="action">The action to execute.</param>
+ public static void BeginOnUIThread(this System.Action action)
+ {
+ Execute.ValidateDispatcher();
+ Execute.dispatcher.BeginInvoke((Delegate)action);
+ }
+
+ /// <summary>
+ /// Executes the action on the UI thread asynchronously.
+ /// </summary>
+ /// <param name="action">
+ /// The action to execute.
+ /// </param>
+ /// <returns>
+ /// The <see cref="Task"/>.
+ /// </returns>
+ public static Task OnUIThreadAsync(this System.Action action)
+ {
+ Execute.ValidateDispatcher();
+ TaskCompletionSource<object> taskSource = new TaskCompletionSource<object>();
+ 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;
+ }
+
+ /// <summary>
+ /// The check access.
+ /// </summary>
+ /// <returns>
+ /// The <see cref="bool"/>.
+ /// </returns>
+ private static bool CheckAccess()
+ {
+ if (Execute.dispatcher != null)
+ return Execute.dispatcher.CheckAccess();
+ return true;
+ }
+
+ /// <summary>
+ /// Executes the action on the UI thread.
+ /// </summary>
+ /// <param name="action">The action to execute.</param>
+ 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/HandBrakeWPF/Utilities/ExtensionMethods.cs b/win/CS/HandBrakeWPF/Utilities/ExtensionMethods.cs
new file mode 100644
index 000000000..f25b8be8e
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/ExtensionMethods.cs
@@ -0,0 +1,45 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="ExtensionMethods.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// The extension methods.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+ using System.Linq.Expressions;
+ using System.Reflection;
+
+ /// <summary>
+ /// The extension methods.
+ /// </summary>
+ public static class ExtensionMethods
+ {
+ /// <summary>
+ /// Converts an expression into a <see cref="MemberInfo"/>.
+ /// </summary>
+ /// <param name="expression">
+ /// The expression to convert.
+ /// </param>
+ /// <returns>
+ /// The member info.
+ /// </returns>
+ public static MemberInfo GetMemberInfo(this Expression expression)
+ {
+ var lambda = (LambdaExpression)expression;
+
+ MemberExpression memberExpression;
+ if (lambda.Body is UnaryExpression)
+ {
+ var unaryExpression = (UnaryExpression)lambda.Body;
+ memberExpression = (MemberExpression)unaryExpression.Operand;
+ }
+ else
+ memberExpression = (MemberExpression)lambda.Body;
+
+ return memberExpression.Member;
+ }
+ }
+}
diff --git a/win/CS/HandBrakeWPF/Utilities/Interfaces/INotifyPropertyChangedEx.cs b/win/CS/HandBrakeWPF/Utilities/Interfaces/INotifyPropertyChangedEx.cs
new file mode 100644
index 000000000..0030ee7b7
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/Interfaces/INotifyPropertyChangedEx.cs
@@ -0,0 +1,37 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="INotifyPropertyChangedEx.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Extends <see cref="T:System.ComponentModel.INotifyPropertyChanged" /> such that the change event can be raised by external parties.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities.Interfaces
+{
+ using System.ComponentModel;
+
+ /// <summary>
+ /// Extends <see cref="T:System.ComponentModel.INotifyPropertyChanged"/> such that the change event can be raised by external parties.
+ /// </summary>
+ public interface INotifyPropertyChangedEx : INotifyPropertyChanged
+ {
+ /// <summary>
+ /// Enables/Disables property change notification.
+ /// </summary>
+ bool IsNotifying { get; set; }
+
+ /// <summary>
+ /// Notifies subscribers of the property change.
+ /// </summary>
+ /// <param name="propertyName">
+ /// Name of the property.
+ /// </param>
+ void NotifyOfPropertyChange(string propertyName);
+
+ /// <summary>
+ /// Raises a change notification indicating that all bindings should be refreshed.
+ /// </summary>
+ void Refresh();
+ }
+}
diff --git a/win/CS/HandBrakeWPF/Utilities/PropertyChangedBase.cs b/win/CS/HandBrakeWPF/Utilities/PropertyChangedBase.cs
new file mode 100644
index 000000000..ae3a401b2
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/PropertyChangedBase.cs
@@ -0,0 +1,121 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright company="HandBrake Project (http://handbrake.fr)" file="PropertyChangedBase.cs">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
+// Borrowed from Caliburn Micro
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+ using System;
+ using System.ComponentModel;
+ using System.Linq.Expressions;
+ using System.Runtime.Serialization;
+
+ using INotifyPropertyChangedEx = HandBrakeWPF.Utilities.Interfaces.INotifyPropertyChangedEx;
+
+ /// <summary>
+ /// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
+ /// </summary>
+ [Serializable]
+ public class PropertyChangedBase : INotifyPropertyChangedEx, INotifyPropertyChanged
+ {
+ [NonSerialized]
+ private bool isNotifying;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether the Enables/Disables property change notification.
+ /// </summary>
+ [Browsable(false)]
+ public bool IsNotifying
+ {
+ get
+ {
+ return this.isNotifying;
+ }
+ set
+ {
+ this.isNotifying = value;
+ }
+ }
+
+ /// <summary>
+ /// Occurs when a property value changes.
+ /// </summary>
+ public event PropertyChangedEventHandler PropertyChanged = (param0, param1) => { };
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="PropertyChangedBase"/> class.
+ /// Creates an instance of <see cref="T:HandBrakeWPF.Utilities.PropertyChangedBase"/>.
+ /// </summary>
+ public PropertyChangedBase()
+ {
+ this.IsNotifying = true;
+ }
+
+ /// <summary>
+ /// Raises a change notification indicating that all bindings should be refreshed.
+ /// </summary>
+ public void Refresh()
+ {
+ this.NotifyOfPropertyChange(string.Empty);
+ }
+
+ /// <summary>
+ /// Notifies subscribers of the property change.
+ /// </summary>
+ /// <param name="propertyName">Name of the property.</param>
+ public virtual void NotifyOfPropertyChange(string propertyName)
+ {
+ if (!this.IsNotifying)
+ return;
+ Execute.OnUIThread((System.Action)(() => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName))));
+ }
+
+ /// <summary>
+ /// Notifies subscribers of the property change.
+ /// </summary>
+ /// <typeparam name="TProperty">The type of the property.</typeparam><param name="property">The property expression.</param>
+ public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
+ {
+ this.NotifyOfPropertyChange(ExtensionMethods.GetMemberInfo((Expression)property).Name);
+ }
+
+ /// <summary>
+ /// Raises the <see cref="E:PropertyChanged"/> event directly.
+ /// </summary>
+ /// <param name="e">The <see cref="T:System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected void OnPropertyChanged(PropertyChangedEventArgs e)
+ {
+ PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
+ if (changedEventHandler == null)
+ return;
+ changedEventHandler((object)this, e);
+ }
+
+ /// <summary>
+ /// Called when the object is deserialized.
+ /// </summary>
+ /// <param name="c">The streaming context.</param>
+ [OnDeserialized]
+ public void OnDeserialized(StreamingContext c)
+ {
+ this.IsNotifying = true;
+ }
+
+ /// <summary>
+ /// Used to indicate whether or not the IsNotifying property is serialized to Xml.
+ /// </summary>
+ /// <returns>
+ /// Whether or not to serialize the IsNotifying property. The default is false.
+ /// </returns>
+ public virtual bool ShouldSerializeIsNotifying()
+ {
+ return false;
+ }
+ }
+}