Skip to content

Commit

Permalink
PointerHelper class and device family info APIs added, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jul 12, 2017
1 parent 0f310fa commit 11cc001
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 4 deletions.
2 changes: 1 addition & 1 deletion UICompositionAnimations/CompositionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ public static SpriteVisual AttachVisualShadow(
shadow.Opacity = opacity;
sprite.Shadow = shadow;
sprite.Size = new Vector2(width ?? (float)element.Width, height ?? (float)element.Height);
sprite.Offset = new Vector3(-0.5f, -0.5f, 0);
sprite.Offset = new Vector3(offsetX, offsetY, 0);

// Clip it and add it to the visual tree
InsetClip clip = compositor.CreateInsetClip(
Expand Down
58 changes: 58 additions & 0 deletions UICompositionAnimations/Helpers/ApiInformaitonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation.Collections;
using Windows.Foundation.Metadata;
using Windows.System.Profile;
using Windows.UI.Composition;
Expand Down Expand Up @@ -184,5 +186,61 @@ public static bool AreBackdropEffectsAvailable
public static bool IsRequiresPointerAvailable => ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Control", nameof(Control.RequiresPointer));

#endregion

#region Device family

private static bool? _IsMobileDevice;

/// <summary>
/// Gets whether or not the device is a mobile phone
/// </summary>
public static bool IsMobileDevice
{
get
{
if (_IsMobileDevice == null)
{
try
{
IObservableMap<String, String> qualifiers = ResourceContext.GetForCurrentView().QualifierValues;
_IsMobileDevice = qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Mobile";
}
catch (UnauthorizedAccessException)
{
// No idea why this should happen
return ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");
}
}
return _IsMobileDevice.Value;
}
}

private static bool? _IsDesktop;

/// <summary>
/// Gets whether or not the device is running Windows 10 Desktop
/// </summary>
public static bool IsDesktop
{
get
{
if (_IsDesktop == null)
{
try
{
IObservableMap<String, String> qualifiers = ResourceContext.GetForCurrentView().QualifierValues;
_IsDesktop = qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Desktop";
}
catch (UnauthorizedAccessException)
{
// Weird crash, but still...
return false;
}
}
return _IsDesktop.Value;
}
}

#endregion
}
}
84 changes: 84 additions & 0 deletions UICompositionAnimations/Helpers/PointerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using Windows.Devices.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;

namespace UICompositionAnimations.Helpers
{
/// <summary>
/// A static class with some extension methods to manage different pointer states
/// </summary>
public static class PointerHelper
{
/// <summary>
/// Adds an event handler to all the pointer events of the target control
/// </summary>
/// <param name="control">The control to monitor</param>
/// <param name="action">An action to call every time a pointer event is raised. The bool parameter
/// indicates whether the pointer is moving to or from the control</param>
public static void ManageControlPointerStates(this UIElement control, Action<PointerDeviceType, bool> action)
{
// Nested functions that adds the actual handlers
void AddHandler(RoutedEvent @event, bool state, Func<PointerDeviceType, bool> predicate)
{
control.AddHandler(@event, new PointerEventHandler((_, e) =>
{
if (predicate == null || predicate(e.Pointer.PointerDeviceType))
{
action(e.Pointer.PointerDeviceType, state);
}
}), true);
}

// Add handlers
AddHandler(UIElement.PointerExitedEvent, false, null);
AddHandler(UIElement.PointerCaptureLostEvent, false, null);
AddHandler(UIElement.PointerCanceledEvent, false, null);
AddHandler(UIElement.PointerEnteredEvent, true, p => p != PointerDeviceType.Touch);
AddHandler(UIElement.PointerReleasedEvent, false, p => p == PointerDeviceType.Touch);
}

/// <summary>
/// Adds an event handler to all the pointer events of the target element
/// </summary>
/// <param name="element">The element to monitor</param>
/// <param name="action">An action to call every time a pointer event is raised. The bool parameter
/// indicates whether the pointer is moving to or from the control</param>
public static void ManageHostPointerStates(this UIElement element, Action<PointerDeviceType, bool> action)
{
// Nested functions that adds the actual handlers
void AddHandler(RoutedEvent @event, bool state, Func<PointerDeviceType, bool> predicate)
{
element.AddHandler(@event, new PointerEventHandler((_, e) =>
{
if (predicate == null || predicate(e.Pointer.PointerDeviceType))
{
action(e.Pointer.PointerDeviceType, state);
}
}), true);
}

// Add handlers
AddHandler(UIElement.PointerExitedEvent, false, null);
AddHandler(UIElement.PointerMovedEvent, true, p => p != PointerDeviceType.Touch);
}

/// <summary>
/// Adds the appropriate handlers to a control to help setup the light effects (skipped when on a mobile phone)
/// </summary>
/// <param name="element">The element to monitor</param>
/// <param name="action">An action to call every time the light effects state should be changed</param>
public static void ManageLightsPointerStates(this UIElement element, Action<bool> action)
{
// Platform check
if (ApiInformationHelper.IsMobileDevice) return;

// Nested functions that adds the actual handlers
element.ManageHostPointerStates((pointer, value) =>
{
if (pointer != PointerDeviceType.Mouse) return;
action(value);
});
}
}
}
2 changes: 1 addition & 1 deletion UICompositionAnimations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.0.0")]
[assembly: AssemblyVersion("2.8.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
1 change: 1 addition & 0 deletions UICompositionAnimations/UICompositionAnimations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<Compile Include="Helpers\ColorConverter.cs" />
<Compile Include="Helpers\DispatcherHelper.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="Helpers\PointerHelper.cs" />
<Compile Include="Lights\PointerPositionSpotLight.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Enums\TranslationAxis.cs" />
Expand Down
4 changes: 2 additions & 2 deletions UICompositionAnimations/UICompositionAnimations.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<package >
<metadata>
<id>Sergio0694.UWP.UICompositionAnimations</id>
<version>2.7.0.0</version>
<version>2.8.0.0</version>
<title>UICompositionAnimations</title>
<description>A wrapper UWP PCL to work with Windows.UI.Composition and XAML animations, and Win2D effects</description>
<authors>Sergio Pedri</authors>
<owners>Sergio Pedri</owners>
<projectUrl>https://github.com/Sergio0694/UICompositionAnimations</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>Added XAML custom acrylic brush</releaseNotes>
<releaseNotes>PointerHelper class added, minor bug fixes and improvements</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>uwp composition animations xaml csharp windows winrt universal app ui win2d graphics</tags>
</metadata>
Expand Down

0 comments on commit 11cc001

Please sign in to comment.