Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jul 13, 2017
1 parent b220026 commit 336b1aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
where T : FrameworkElement
{
// Get the compositor
Visual visual = await element.Dispatcher.GetFromUIThreadAsync(element.GetVisual);
Visual visual = await element.Dispatcher.GetAsync(element.GetVisual);
Compositor compositor = visual.Compositor;

// Create the saturation effect and the effect factory
Expand All @@ -450,7 +450,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
SpriteVisual sprite = compositor.CreateSpriteVisual();
sprite.Brush = effectBrush;
await AddToTreeAndBindSizeAsync(visual, element, sprite);
if (initiallyVisible) await element.Dispatcher.RunOnUIThreadAsync(() => element.Opacity = 1);
if (initiallyVisible) await element.Dispatcher.RunAsync(() => element.Opacity = 1);
return new AttachedAnimatableCompositionEffect<T>(element, sprite, new CompositionAnimationParameters(animationPropertyName, on, off), disposeOnUnload);
}

Expand All @@ -468,7 +468,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
[NotNull] this T element, float on, float off, bool initiallyVisible, bool disposeOnUnload = false) where T : FrameworkElement
{
// Get the compositor
Visual visual = await element.Dispatcher.GetFromUIThreadAsync(element.GetVisual);
Visual visual = await element.Dispatcher.GetAsync(element.GetVisual);
Compositor compositor = visual.Compositor;

// Create the blur effect and the effect factory
Expand All @@ -491,7 +491,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
SpriteVisual sprite = compositor.CreateSpriteVisual();
sprite.Brush = effectBrush;
await AddToTreeAndBindSizeAsync(visual, element, sprite);
if (initiallyVisible) await element.Dispatcher.RunOnUIThreadAsync(() => element.Opacity = 1);
if (initiallyVisible) await element.Dispatcher.RunAsync(() => element.Opacity = 1);
return new AttachedAnimatableCompositionEffect<T>(element, sprite, new CompositionAnimationParameters(animationPropertyName, on, off), disposeOnUnload);
}

Expand Down Expand Up @@ -522,7 +522,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
where T : FrameworkElement
{
// Get the compositor
Visual visual = await element.Dispatcher.GetFromUIThreadAsync(element.GetVisual);
Visual visual = await element.Dispatcher.GetAsync(element.GetVisual);
Compositor compositor = visual.Compositor;

// Create the blur effect and the effect factory
Expand Down Expand Up @@ -561,7 +561,7 @@ public static async Task<AttachedAnimatableCompositionEffect<T>> AttachCompositi
SpriteVisual sprite = compositor.CreateSpriteVisual();
sprite.Brush = effectBrush;
await AddToTreeAndBindSizeAsync(target.GetVisual(), target, sprite);
if (initiallyVisible) await element.Dispatcher.RunOnUIThreadAsync(() => element.Opacity = 1);
if (initiallyVisible) await element.Dispatcher.RunAsync(() => element.Opacity = 1);
return new AttachedAnimatableCompositionEffect<T>(target, sprite, new CompositionAnimationParameters(animationPropertyName, on, off), disposeOnUnload);
}

Expand Down Expand Up @@ -596,7 +596,7 @@ public static async Task<AttachedCompositeAnimatableCompositionEffect<T>> Attach
where T : FrameworkElement
{
// Get the compositor
Visual visual = await element.Dispatcher.GetFromUIThreadAsync(element.GetVisual);
Visual visual = await element.Dispatcher.GetAsync(element.GetVisual);
Compositor compositor = visual.Compositor;

// Create the blur effect and the effect factory
Expand Down Expand Up @@ -648,7 +648,7 @@ public static async Task<AttachedCompositeAnimatableCompositionEffect<T>> Attach
SpriteVisual sprite = compositor.CreateSpriteVisual();
sprite.Brush = effectBrush;
await AddToTreeAndBindSizeAsync(target.GetVisual(), target, sprite);
if (initiallyVisible) await element.Dispatcher.RunOnUIThreadAsync(() => element.Opacity = 1);
if (initiallyVisible) await element.Dispatcher.RunAsync(() => element.Opacity = 1);
return new AttachedCompositeAnimatableCompositionEffect<T>(target, sprite,
new Dictionary<String, CompositionAnimationValueParameters>
{
Expand All @@ -675,7 +675,7 @@ public static async Task<AttachedCompositeAnimatableCompositionEffect<T>> Attach
where T : FrameworkElement
{
// Get the compositor
Visual visual = await element.Dispatcher.GetFromUIThreadAsync(element.GetVisual);
Visual visual = await element.Dispatcher.GetAsync(element.GetVisual);
Compositor compositor = visual.Compositor;

// Create the blur effect, the saturation effect and the effect factory
Expand Down Expand Up @@ -708,7 +708,7 @@ public static async Task<AttachedCompositeAnimatableCompositionEffect<T>> Attach
SpriteVisual sprite = compositor.CreateSpriteVisual();
sprite.Brush = effectBrush;
await AddToTreeAndBindSizeAsync(visual, element, sprite);
if (initiallyVisible) await element.Dispatcher.RunOnUIThreadAsync(() => element.Opacity = 1);
if (initiallyVisible) await element.Dispatcher.RunAsync(() => element.Opacity = 1);

// Prepare and return the wrapped effect
return new AttachedCompositeAnimatableCompositionEffect<T>(element, sprite,
Expand All @@ -732,7 +732,7 @@ public static async Task<AttachedCompositeAnimatableCompositionEffect<T>> Attach
private static async Task AddToTreeAndBindSizeAsync([NotNull] Visual host, [NotNull] UIElement element, [NotNull] Visual visual)
{
// Add the shadow as a child of the host in the visual tree
await element.Dispatcher.RunOnUIThreadAsync(() => ElementCompositionPreview.SetElementChildVisual(element, visual));
await element.Dispatcher.RunAsync(() => ElementCompositionPreview.SetElementChildVisual(element, visual));

// Make sure size of shadow host and shadow visual always stay in sync
ExpressionAnimation bindSizeAnimation = host.Compositor.CreateExpressionAnimation($"{nameof(host)}.Size");
Expand Down
24 changes: 12 additions & 12 deletions UICompositionAnimations/Helpers/DispatcherHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class DispatcherHelper
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="callback">The action to execute on the UI thread</param>
[PublicAPI]
public static void RunOnUIThread([NotNull] this CoreDispatcher dispatcher, [NotNull] Action callback)
public static void Run([NotNull] this CoreDispatcher dispatcher, [NotNull] Action callback)
{
if (dispatcher.HasThreadAccess) callback();
else
Expand All @@ -37,7 +37,7 @@ public static void RunOnUIThread([NotNull] this CoreDispatcher dispatcher, [NotN
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="asyncCallback">The action to execute on the UI thread</param>
[PublicAPI]
public static void RunOnUIThread([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task> asyncCallback)
public static void Run([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task> asyncCallback)
{
// Check the current thread
if (dispatcher.HasThreadAccess) asyncCallback();
Expand All @@ -55,7 +55,7 @@ public static void RunOnUIThread([NotNull] this CoreDispatcher dispatcher, [NotN
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="callback">The action to execute on the UI thread</param>
[PublicAPI]
public static async Task RunOnUIThreadAsync([NotNull] this CoreDispatcher dispatcher, [NotNull] Action callback)
public static async Task RunAsync([NotNull] this CoreDispatcher dispatcher, [NotNull] Action callback)
{
if (dispatcher.HasThreadAccess) callback();
else
Expand All @@ -76,7 +76,7 @@ public static async Task RunOnUIThreadAsync([NotNull] this CoreDispatcher dispat
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="asyncCallback">The action to execute on the UI thread</param>
[PublicAPI]
public static Task RunOnUIThreadAsync([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task> asyncCallback)
public static Task RunAsync([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task> asyncCallback)
{
// Check the current thread
if (dispatcher.HasThreadAccess) return asyncCallback();
Expand All @@ -97,7 +97,7 @@ public static Task RunOnUIThreadAsync([NotNull] this CoreDispatcher dispatcher,
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="function">The function to execute on the UI thread</param>
[PublicAPI]
public static async ValueTask<T> GetFromUIThreadAsync<T>([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<T> function)
public static async ValueTask<T> GetAsync<T>([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<T> function)
{
if (dispatcher.HasThreadAccess) return function();
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
Expand All @@ -116,7 +116,7 @@ public static async ValueTask<T> GetFromUIThreadAsync<T>([NotNull] this CoreDisp
/// <param name="dispatcher">The target dispatcher to use to schedule the callback execution</param>
/// <param name="function">The async function to execute on the UI thread</param>
[PublicAPI]
public static Task<T> GetFromUIThreadAsync<T>([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task<T>> function)
public static Task<T> GetAsync<T>([NotNull] this CoreDispatcher dispatcher, [NotNull] Func<Task<T>> function)
{
// Check the current thread
if (dispatcher.HasThreadAccess) return function();
Expand Down Expand Up @@ -152,44 +152,44 @@ public static Task<T> GetFromUIThreadAsync<T>([NotNull] this CoreDispatcher disp
/// </summary>
/// <param name="callback">The action to execute on the UI thread</param>
[PublicAPI]
public static void RunOnUIThread([NotNull] Action callback) => CoreDispatcher.RunOnUIThread(callback);
public static void RunOnUIThread([NotNull] Action callback) => CoreDispatcher.Run(callback);

/// <summary>
/// Executes a given async action on the UI thread without awaiting the operation
/// </summary>
/// <param name="asyncCallback">The action to execute on the UI thread</param>
[PublicAPI]
public static void RunOnUIThread([NotNull] Func<Task> asyncCallback) => CoreDispatcher.RunOnUIThread(asyncCallback);
public static void RunOnUIThread([NotNull] Func<Task> asyncCallback) => CoreDispatcher.Run(asyncCallback);

/// <summary>
/// Executes a given action on the UI thread and waits for it to be completed
/// </summary>
/// <param name="callback">The action to execute on the UI thread</param>
[PublicAPI]
public static Task RunOnUIThreadAsync([NotNull] Action callback) => CoreDispatcher.RunOnUIThreadAsync(callback);
public static Task RunOnUIThreadAsync([NotNull] Action callback) => CoreDispatcher.RunAsync(callback);

/// <summary>
/// Executes a given action on the UI thread and waits for it to be completed
/// </summary>
/// <param name="asyncCallback">The action to execute on the UI thread</param>
[PublicAPI]
public static Task RunOnUIThreadAsync([NotNull] Func<Task> asyncCallback) => CoreDispatcher.RunOnUIThreadAsync(asyncCallback);
public static Task RunOnUIThreadAsync([NotNull] Func<Task> asyncCallback) => CoreDispatcher.RunAsync(asyncCallback);

/// <summary>
/// Executes a given function on the UI thread and returns its result
/// </summary>
/// <typeparam name="T">The return type</typeparam>
/// <param name="function">The function to execute on the UI thread</param>
[PublicAPI]
public static ValueTask<T> GetFromUIThreadAsync<T>([NotNull] Func<T> function) => CoreDispatcher.GetFromUIThreadAsync(function);
public static ValueTask<T> GetFromUIThreadAsync<T>([NotNull] Func<T> function) => CoreDispatcher.GetAsync(function);

/// <summary>
/// Executes a given async function on the UI thread and returns its result
/// </summary>
/// <typeparam name="T">The return type</typeparam>
/// <param name="function">The async function to execute on the UI thread</param>
[PublicAPI]
public static Task<T> GetFromUIThreadAsync<T>([NotNull] Func<Task<T>> function) => CoreDispatcher.GetFromUIThreadAsync(function);
public static Task<T> GetFromUIThreadAsync<T>([NotNull] Func<Task<T>> function) => CoreDispatcher.GetAsync(function);

#endregion
}
Expand Down

0 comments on commit 336b1aa

Please sign in to comment.