Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AnimationRegistry.cs
1using System;
2using System.Collections.Concurrent;
4using System.Text;
5
7{
11 public interface IAnimationRegistry
12 {
21
30
37 void Register(ViewAnimationDescriptor Descriptor, string? Platform = null, string? Theme = null);
38
45 void Register(TransitionAnimationDescriptor Descriptor, string? Platform = null, string? Theme = null);
46 }
47
52 {
53 private readonly ConcurrentDictionary<string, ViewAnimationDescriptor> viewDescriptors = new(StringComparer.Ordinal);
54 private readonly ConcurrentDictionary<string, TransitionAnimationDescriptor> transitionDescriptors = new(StringComparer.Ordinal);
55
58 {
59 ArgumentNullException.ThrowIfNull(Context);
60
61 ViewAnimationDescriptor? Descriptor = ResolveDescriptor(this.viewDescriptors, Key, Context);
62 if (Descriptor is null)
63 {
64 Animation = default!;
65 return false;
66 }
67
68 Animation = Descriptor.CreateExecutor(Context);
69 return true;
70 }
71
74 {
75 ArgumentNullException.ThrowIfNull(Context);
76
77 TransitionAnimationDescriptor? Descriptor = ResolveDescriptor(this.transitionDescriptors, Key, Context);
78 if (Descriptor is null)
79 {
80 Animation = default!;
81 return false;
82 }
83
84 Animation = Descriptor.CreateExecutor(Context);
85 return true;
86 }
87
89 public void Register(ViewAnimationDescriptor Descriptor, string? Platform = null, string? Theme = null)
90 {
91 ArgumentNullException.ThrowIfNull(Descriptor);
92 string Key = ComposeKey(Descriptor.Key, Platform, Theme);
93 this.viewDescriptors[Key] = Descriptor;
94 }
95
97 public void Register(TransitionAnimationDescriptor Descriptor, string? Platform = null, string? Theme = null)
98 {
99 ArgumentNullException.ThrowIfNull(Descriptor);
100 string Key = ComposeKey(Descriptor.Key, Platform, Theme);
101 this.transitionDescriptors[Key] = Descriptor;
102 }
103
104 private static TDescriptor? ResolveDescriptor<TDescriptor>(ConcurrentDictionary<string, TDescriptor> Descriptors, AnimationKey Key, IAnimationContext Context)
105 where TDescriptor : AnimationDescriptor
106 {
107 string BaseKey = Key.Value;
108 string PlatformKey = string.IsNullOrWhiteSpace(Context.Platform) ? string.Empty : Context.Platform;
109 string? ThemeKey = Context.Theme;
110
111 List<string> Candidates = new List<string>();
112
113 if (!string.IsNullOrWhiteSpace(PlatformKey) && !string.IsNullOrWhiteSpace(ThemeKey))
114 Candidates.Add(ComposeKey(BaseKey, PlatformKey, ThemeKey));
115
116 if (!string.IsNullOrWhiteSpace(PlatformKey))
117 Candidates.Add(ComposeKey(BaseKey, PlatformKey, null));
118
119 if (!string.IsNullOrWhiteSpace(ThemeKey))
120 Candidates.Add(ComposeKey(BaseKey, null, ThemeKey));
121
122 Candidates.Add(BaseKey);
123
124 foreach (string Candidate in Candidates)
125 {
126 if (Descriptors.TryGetValue(Candidate, out TDescriptor Descriptor))
127 return Descriptor;
128 }
129
130 return null;
131 }
132
133 private static string ComposeKey(AnimationKey Key, string? Platform, string? Theme)
134 {
135 return ComposeKey(Key.Value, Platform, Theme);
136 }
137
138 private static string ComposeKey(string BaseKey, string? Platform, string? Theme)
139 {
140 StringBuilder Builder = new StringBuilder(BaseKey);
141 if (!string.IsNullOrWhiteSpace(Platform))
142 {
143 Builder.Append('.');
144 Builder.Append(Platform);
145 }
146
147 if (!string.IsNullOrWhiteSpace(Theme))
148 {
149 Builder.Append('.');
150 Builder.Append(Theme);
151 }
152
153 return Builder.ToString();
154 }
155 }
156}
Base class for animation descriptors.
AnimationKey Key
Gets the descriptor key.
Default implementation of IAnimationRegistry.
void Register(ViewAnimationDescriptor Descriptor, string? Platform=null, string? Theme=null)
Registers a view animation descriptor.
void Register(TransitionAnimationDescriptor Descriptor, string? Platform=null, string? Theme=null)
Registers a transition animation descriptor.
bool TryCreateViewAnimation(AnimationKey Key, IAnimationContext Context, out IViewAnimation Animation)
Attempts to create a view animation for the given key and context. True if an animation could be crea...
bool TryCreateTransition(AnimationKey Key, IAnimationContext Context, out ITransitionAnimation Animation)
Attempts to create a transition animation for the given key and context. True if an animation could b...
Descriptor defining a reusable transition animation profile.
ITransitionAnimation CreateExecutor(IAnimationContext Context)
Creates an executor for the given context.
Descriptor defining a reusable view animation profile.
IViewAnimation CreateExecutor(IAnimationContext Context)
Creates an executor for the given context.
Represents contextual information required when executing an animation.
Registry abstraction for animation descriptors.
bool TryCreateViewAnimation(AnimationKey Key, IAnimationContext Context, out IViewAnimation Animation)
Attempts to create a view animation for the given key and context.
void Register(TransitionAnimationDescriptor Descriptor, string? Platform=null, string? Theme=null)
Registers a transition animation descriptor.
bool TryCreateTransition(AnimationKey Key, IAnimationContext Context, out ITransitionAnimation Animation)
Attempts to create a transition animation for the given key and context.
void Register(ViewAnimationDescriptor Descriptor, string? Platform=null, string? Theme=null)
Registers a view animation descriptor.
Defines a transition animation executor.
Defines a view-based animation executor.
Represents a strongly typed key used for identifying animations.
Definition: AnimationKey.cs:7