PerspexObjectExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive;
  5. using System.Reactive.Disposables;
  6. using System.Reactive.Linq;
  7. using System.Reactive.Subjects;
  8. using Perspex.Data;
  9. using Perspex.Reactive;
  10. namespace Perspex
  11. {
  12. /// <summary>
  13. /// Provides extension methods for <see cref="PerspexObject"/> and related classes.
  14. /// </summary>
  15. public static class PerspexObjectExtensions
  16. {
  17. /// <summary>
  18. /// Gets an observable for a <see cref="PerspexProperty"/>.
  19. /// </summary>
  20. /// <param name="o">The object.</param>
  21. /// <param name="property">The property.</param>
  22. /// <returns>An observable.</returns>
  23. public static IObservable<object> GetObservable(this IPerspexObject o, PerspexProperty property)
  24. {
  25. Contract.Requires<ArgumentNullException>(o != null);
  26. Contract.Requires<ArgumentNullException>(property != null);
  27. return new PerspexObservable<object>(
  28. observer =>
  29. {
  30. EventHandler<PerspexPropertyChangedEventArgs> handler = (s, e) =>
  31. {
  32. if (e.Property == property)
  33. {
  34. observer.OnNext(e.NewValue);
  35. }
  36. };
  37. observer.OnNext(o.GetValue(property));
  38. o.PropertyChanged += handler;
  39. return Disposable.Create(() =>
  40. {
  41. o.PropertyChanged -= handler;
  42. });
  43. },
  44. GetDescription(o, property));
  45. }
  46. /// <summary>
  47. /// Gets an observable for a <see cref="PerspexProperty"/>.
  48. /// </summary>
  49. /// <param name="o">The object.</param>
  50. /// <typeparam name="T">The property type.</typeparam>
  51. /// <param name="property">The property.</param>
  52. /// <returns>An observable.</returns>
  53. public static IObservable<T> GetObservable<T>(this IPerspexObject o, PerspexProperty<T> property)
  54. {
  55. Contract.Requires<ArgumentNullException>(o != null);
  56. Contract.Requires<ArgumentNullException>(property != null);
  57. return o.GetObservable((PerspexProperty)property).Cast<T>();
  58. }
  59. /// <summary>
  60. /// Gets an observable for a <see cref="PerspexProperty"/>.
  61. /// </summary>
  62. /// <param name="o">The object.</param>
  63. /// <typeparam name="T">The type of the property.</typeparam>
  64. /// <param name="property">The property.</param>
  65. /// <returns>
  66. /// An observable which when subscribed pushes the old and new values of the property each
  67. /// time it is changed.
  68. /// </returns>
  69. public static IObservable<Tuple<T, T>> GetObservableWithHistory<T>(
  70. this IPerspexObject o,
  71. PerspexProperty<T> property)
  72. {
  73. Contract.Requires<ArgumentNullException>(o != null);
  74. Contract.Requires<ArgumentNullException>(property != null);
  75. return new PerspexObservable<Tuple<T, T>>(
  76. observer =>
  77. {
  78. EventHandler<PerspexPropertyChangedEventArgs> handler = (s, e) =>
  79. {
  80. if (e.Property == property)
  81. {
  82. observer.OnNext(Tuple.Create((T)e.OldValue, (T)e.NewValue));
  83. }
  84. };
  85. o.PropertyChanged += handler;
  86. return Disposable.Create(() =>
  87. {
  88. o.PropertyChanged -= handler;
  89. });
  90. },
  91. GetDescription(o, property));
  92. }
  93. /// <summary>
  94. /// Gets a subject for a <see cref="PerspexProperty"/>.
  95. /// </summary>
  96. /// <param name="o">The object.</param>
  97. /// <param name="property">The property.</param>
  98. /// <param name="priority">
  99. /// The priority with which binding values are written to the object.
  100. /// </param>
  101. /// <returns>
  102. /// An <see cref="ISubject{Object}"/> which can be used for two-way binding to/from the
  103. /// property.
  104. /// </returns>
  105. public static ISubject<object> GetSubject(
  106. this IPerspexObject o,
  107. PerspexProperty property,
  108. BindingPriority priority = BindingPriority.LocalValue)
  109. {
  110. // TODO: Subject.Create<T> is not yet in stable Rx : once it is, remove the
  111. // AnonymousSubject classes and use Subject.Create<T>.
  112. var output = new Subject<object>();
  113. var result = new AnonymousSubject<object>(
  114. Observer.Create<object>(
  115. x => output.OnNext(x),
  116. e => output.OnError(e),
  117. () => output.OnCompleted()),
  118. o.GetObservable(property));
  119. o.Bind(property, output, priority);
  120. return result;
  121. }
  122. /// <summary>
  123. /// Gets a subject for a <see cref="PerspexProperty"/>.
  124. /// </summary>
  125. /// <typeparam name="T">The property type.</typeparam>
  126. /// <param name="o">The object.</param>
  127. /// <param name="property">The property.</param>
  128. /// <param name="priority">
  129. /// The priority with which binding values are written to the object.
  130. /// </param>
  131. /// <returns>
  132. /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the
  133. /// property.
  134. /// </returns>
  135. public static ISubject<T> GetSubject<T>(
  136. this IPerspexObject o,
  137. PerspexProperty<T> property,
  138. BindingPriority priority = BindingPriority.LocalValue)
  139. {
  140. // TODO: Subject.Create<T> is not yet in stable Rx : once it is, remove the
  141. // AnonymousSubject classes from this file and use Subject.Create<T>.
  142. var output = new Subject<T>();
  143. var result = new AnonymousSubject<T>(
  144. Observer.Create<T>(
  145. x => output.OnNext(x),
  146. e => output.OnError(e),
  147. () => output.OnCompleted()),
  148. o.GetObservable(property));
  149. o.Bind(property, output, priority);
  150. return result;
  151. }
  152. /// <summary>
  153. /// Binds a property on an <see cref="IPerspexObject"/> to an <see cref="IBinding"/>.
  154. /// </summary>
  155. /// <param name="o">The object.</param>
  156. /// <param name="property">The property to bind.</param>
  157. /// <param name="binding">The binding.</param>
  158. /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
  159. public static IDisposable Bind(
  160. this IPerspexObject o,
  161. PerspexProperty property,
  162. IBinding binding)
  163. {
  164. Contract.Requires<ArgumentNullException>(o != null);
  165. Contract.Requires<ArgumentNullException>(property != null);
  166. Contract.Requires<ArgumentNullException>(binding != null);
  167. var mode = binding.Mode;
  168. if (mode == BindingMode.Default)
  169. {
  170. mode = property.DefaultBindingMode;
  171. }
  172. return o.Bind(
  173. property,
  174. binding.CreateSubject(o, property),
  175. mode,
  176. binding.Priority);
  177. }
  178. /// <summary>
  179. /// Binds a property to a subject according to a <see cref="BindingMode"/>.
  180. /// </summary>
  181. /// <param name="o">The object.</param>
  182. /// <param name="property">The property to bind.</param>
  183. /// <param name="source">The binding source.</param>
  184. /// <param name="mode">The binding mode.</param>
  185. /// <param name="priority">The binding priority.</param>
  186. /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
  187. public static IDisposable Bind(
  188. this IPerspexObject o,
  189. PerspexProperty property,
  190. ISubject<object> source,
  191. BindingMode mode,
  192. BindingPriority priority = BindingPriority.LocalValue)
  193. {
  194. Contract.Requires<ArgumentNullException>(o != null);
  195. Contract.Requires<ArgumentNullException>(property != null);
  196. Contract.Requires<ArgumentNullException>(source != null);
  197. switch (mode)
  198. {
  199. case BindingMode.Default:
  200. case BindingMode.OneWay:
  201. return o.Bind(property, source, priority);
  202. case BindingMode.TwoWay:
  203. return new CompositeDisposable(
  204. o.Bind(property, source, priority),
  205. o.GetObservable(property).Subscribe(source));
  206. case BindingMode.OneTime:
  207. return source.Take(1).Subscribe(x => o.SetValue(property, x, priority));
  208. case BindingMode.OneWayToSource:
  209. return o.GetObservable(property).Subscribe(source);
  210. default:
  211. throw new ArgumentException("Invalid binding mode.");
  212. }
  213. }
  214. /// <summary>
  215. /// Subscribes to a property changed notifications for changes that originate from a
  216. /// <typeparamref name="TTarget"/>.
  217. /// </summary>
  218. /// <typeparam name="TTarget">The type of the property change sender.</typeparam>
  219. /// <param name="observable">The property changed observable.</param>
  220. /// <param name="action">
  221. /// The method to call. The parameters are the sender and the event args.
  222. /// </param>
  223. /// <returns>A disposable that can be used to terminate the subscription.</returns>
  224. public static IDisposable AddClassHandler<TTarget>(
  225. this IObservable<PerspexPropertyChangedEventArgs> observable,
  226. Action<TTarget, PerspexPropertyChangedEventArgs> action)
  227. where TTarget : PerspexObject
  228. {
  229. return observable.Subscribe(e =>
  230. {
  231. if (e.Sender is TTarget)
  232. {
  233. action((TTarget)e.Sender, e);
  234. }
  235. });
  236. }
  237. /// <summary>
  238. /// Subscribes to a property changed notifications for changes that originate from a
  239. /// <typeparamref name="TTarget"/>.
  240. /// </summary>
  241. /// <typeparam name="TTarget">The type of the property change sender.</typeparam>
  242. /// <param name="observable">The property changed observable.</param>
  243. /// <param name="handler">Given a TTarget, returns the handler.</param>
  244. /// <returns>A disposable that can be used to terminate the subscription.</returns>
  245. public static IDisposable AddClassHandler<TTarget>(
  246. this IObservable<PerspexPropertyChangedEventArgs> observable,
  247. Func<TTarget, Action<PerspexPropertyChangedEventArgs>> handler)
  248. where TTarget : class
  249. {
  250. return observable.Subscribe(e => SubscribeAdapter(e, handler));
  251. }
  252. /// <summary>
  253. /// Gets a description of a property that van be used in observables.
  254. /// </summary>
  255. /// <param name="o">The object.</param>
  256. /// <param name="property">The property</param>
  257. /// <returns>The description.</returns>
  258. private static string GetDescription(IPerspexObject o, PerspexProperty property)
  259. {
  260. return $"{o.GetType().Name}.{property.Name}";
  261. }
  262. /// <summary>
  263. /// Observer method for <see cref="AddClassHandler{TTarget}(IObservable{PerspexPropertyChangedEventArgs},
  264. /// Func{TTarget, Action{PerspexPropertyChangedEventArgs}})"/>.
  265. /// </summary>
  266. /// <typeparam name="TTarget">The sender type to accept.</typeparam>
  267. /// <param name="e">The event args.</param>
  268. /// <param name="handler">Given a TTarget, returns the handler.</param>
  269. private static void SubscribeAdapter<TTarget>(
  270. PerspexPropertyChangedEventArgs e,
  271. Func<TTarget, Action<PerspexPropertyChangedEventArgs>> handler)
  272. where TTarget : class
  273. {
  274. var target = e.Sender as TTarget;
  275. if (target != null)
  276. {
  277. handler(target)(e);
  278. }
  279. }
  280. }
  281. }