TransitioningContentControlPageViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Avalonia;
  8. using Avalonia.Animation;
  9. using Avalonia.Media;
  10. using Avalonia.Media.Imaging;
  11. using Avalonia.Platform;
  12. using Avalonia.Styling;
  13. using Avalonia.VisualTree;
  14. using MiniMvvm;
  15. namespace ControlCatalog.ViewModels
  16. {
  17. public class TransitioningContentControlPageViewModel : ViewModelBase
  18. {
  19. public TransitioningContentControlPageViewModel()
  20. {
  21. var assetLoader = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
  22. var images = new string[]
  23. {
  24. "delicate-arch-896885_640.jpg",
  25. "hirsch-899118_640.jpg",
  26. "maple-leaf-888807_640.jpg"
  27. };
  28. foreach (var image in images)
  29. {
  30. var path = $"avares://ControlCatalog/Assets/{image}";
  31. Images.Add(new Bitmap(assetLoader.Open(new Uri(path))));
  32. }
  33. SetupTransitions();
  34. _SelectedTransition = PageTransitions[1];
  35. _SelectedImage = Images[0];
  36. }
  37. public List<PageTransition> PageTransitions { get; } = new List<PageTransition>();
  38. public List<Bitmap> Images { get; } = new List<Bitmap>();
  39. private Bitmap _SelectedImage;
  40. private bool _Reversed;
  41. /// <summary>
  42. /// Gets or Sets the selected image
  43. /// </summary>
  44. public Bitmap SelectedImage
  45. {
  46. get { return _SelectedImage; }
  47. set { this.RaiseAndSetIfChanged(ref _SelectedImage, value); }
  48. }
  49. private PageTransition _SelectedTransition;
  50. /// <summary>
  51. /// Gets or sets the transition to play
  52. /// </summary>
  53. public PageTransition SelectedTransition
  54. {
  55. get { return _SelectedTransition; }
  56. set { this.RaiseAndSetIfChanged(ref _SelectedTransition, value); }
  57. }
  58. private bool _ClipToBounds;
  59. /// <summary>
  60. /// Gets or sets if the content should be clipped to bounds
  61. /// </summary>
  62. public bool ClipToBounds
  63. {
  64. get { return _ClipToBounds; }
  65. set { this.RaiseAndSetIfChanged(ref _ClipToBounds, value); }
  66. }
  67. private int _Duration = 500;
  68. /// <summary>
  69. /// Gets or Sets the duration
  70. /// </summary>
  71. public int Duration
  72. {
  73. get { return _Duration; }
  74. set
  75. {
  76. this.RaiseAndSetIfChanged(ref _Duration , value);
  77. SetupTransitions();
  78. }
  79. }
  80. /// <summary>
  81. /// Gets or sets a value indicating whether the animation is reversed.
  82. /// </summary>
  83. public bool Reversed
  84. {
  85. get => _Reversed;
  86. set => this.RaiseAndSetIfChanged(ref _Reversed, value);
  87. }
  88. private void SetupTransitions()
  89. {
  90. if (PageTransitions.Count == 0)
  91. {
  92. PageTransitions.AddRange(new[]
  93. {
  94. new PageTransition("None"),
  95. new PageTransition("CrossFade"),
  96. new PageTransition("Slide horizontally"),
  97. new PageTransition("Slide vertically"),
  98. new PageTransition("Composite"),
  99. new PageTransition("Custom")
  100. });
  101. }
  102. PageTransitions[1].Transition = new CrossFade(TimeSpan.FromMilliseconds(Duration));
  103. PageTransitions[2].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Horizontal);
  104. PageTransitions[3].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Vertical);
  105. var compositeTransition = new CompositePageTransition();
  106. compositeTransition.PageTransitions.Add(PageTransitions[1].Transition!);
  107. compositeTransition.PageTransitions.Add(PageTransitions[2].Transition!);
  108. compositeTransition.PageTransitions.Add(PageTransitions[3].Transition!);
  109. PageTransitions[4].Transition = compositeTransition;
  110. PageTransitions[5].Transition = new CustomTransition(TimeSpan.FromMilliseconds(Duration));
  111. }
  112. public void NextImage()
  113. {
  114. Reversed = false;
  115. var index = Images.IndexOf(SelectedImage) + 1;
  116. if (index >= Images.Count)
  117. {
  118. index = 0;
  119. }
  120. SelectedImage = Images[index];
  121. }
  122. public void PrevImage()
  123. {
  124. Reversed = true;
  125. var index = Images.IndexOf(SelectedImage) - 1;
  126. if (index < 0)
  127. {
  128. index = Images.Count-1;
  129. }
  130. SelectedImage = Images[index];
  131. }
  132. }
  133. public class PageTransition : ViewModelBase
  134. {
  135. public PageTransition(string displayTitle)
  136. {
  137. DisplayTitle = displayTitle;
  138. }
  139. public string DisplayTitle { get; }
  140. private IPageTransition? _Transition;
  141. /// <summary>
  142. /// Gets or sets the transition
  143. /// </summary>
  144. public IPageTransition? Transition
  145. {
  146. get { return _Transition; }
  147. set { this.RaiseAndSetIfChanged(ref _Transition, value); }
  148. }
  149. public override string ToString()
  150. {
  151. return DisplayTitle;
  152. }
  153. }
  154. public class CustomTransition : IPageTransition
  155. {
  156. /// <summary>
  157. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  158. /// </summary>
  159. public CustomTransition()
  160. {
  161. }
  162. /// <summary>
  163. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  164. /// </summary>
  165. /// <param name="duration">The duration of the animation.</param>
  166. public CustomTransition(TimeSpan duration)
  167. {
  168. Duration = duration;
  169. }
  170. /// <summary>
  171. /// Gets the duration of the animation.
  172. /// </summary>
  173. public TimeSpan Duration { get; set; }
  174. public async Task Start(Visual? from, Visual? to, bool forward, CancellationToken cancellationToken)
  175. {
  176. if (cancellationToken.IsCancellationRequested)
  177. {
  178. return;
  179. }
  180. var tasks = new List<Task>();
  181. var parent = GetVisualParent(from, to);
  182. var scaleProperty = ScaleTransform.ScaleYProperty;
  183. if (from != null)
  184. {
  185. var animation = new Animation
  186. {
  187. Children =
  188. {
  189. new KeyFrame
  190. {
  191. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  192. Cue = new Cue(0d)
  193. },
  194. new KeyFrame
  195. {
  196. Setters =
  197. {
  198. new Setter
  199. {
  200. Property = scaleProperty,
  201. Value = 0d
  202. }
  203. },
  204. Cue = new Cue(1d)
  205. }
  206. },
  207. Duration = Duration
  208. };
  209. tasks.Add(animation.RunAsync(from, null, cancellationToken));
  210. }
  211. if (to != null)
  212. {
  213. to.IsVisible = true;
  214. var animation = new Animation
  215. {
  216. Children =
  217. {
  218. new KeyFrame
  219. {
  220. Setters =
  221. {
  222. new Setter
  223. {
  224. Property = scaleProperty,
  225. Value = 0d
  226. }
  227. },
  228. Cue = new Cue(0d)
  229. },
  230. new KeyFrame
  231. {
  232. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  233. Cue = new Cue(1d)
  234. }
  235. },
  236. Duration = Duration
  237. };
  238. tasks.Add(animation.RunAsync(to, null, cancellationToken));
  239. }
  240. await Task.WhenAll(tasks);
  241. if (from != null && !cancellationToken.IsCancellationRequested)
  242. {
  243. from.IsVisible = false;
  244. }
  245. }
  246. /// <summary>
  247. /// Gets the common visual parent of the two control.
  248. /// </summary>
  249. /// <param name="from">The from control.</param>
  250. /// <param name="to">The to control.</param>
  251. /// <returns>The common parent.</returns>
  252. /// <exception cref="ArgumentException">
  253. /// The two controls do not share a common parent.
  254. /// </exception>
  255. /// <remarks>
  256. /// Any one of the parameters may be null, but not both.
  257. /// </remarks>
  258. private static Visual GetVisualParent(Visual? from, Visual? to)
  259. {
  260. var p1 = (from ?? to)!.GetVisualParent();
  261. var p2 = (to ?? from)!.GetVisualParent();
  262. if (p1 != null && p2 != null && p1 != p2)
  263. {
  264. throw new ArgumentException("Controls for PageSlide must have same parent.");
  265. }
  266. return p1 ?? throw new InvalidOperationException("Cannot determine visual parent.");
  267. }
  268. }
  269. }