From 8723790a04b0af575e4a496ec01b31f02a9f3dc7 Mon Sep 17 00:00:00 2001 From: Jason Jarvis Date: Thu, 24 Sep 2015 22:18:19 -0700 Subject: [PATCH 1/2] Animations (PageSlide and CrossFade) can now be added in Xaml files. Added a TimeSpanTypeConverter to support that. --- .../XamlTestApplication/Views/MainWindow.paml | 7 ++-- .../Context/PerspexWiringContext.cs | 2 ++ .../Converters/TimeSpanTypeConverter.cs | 36 +++++++++++++++++++ .../Perspex.Markup.Xaml.csproj | 1 + src/Perspex.SceneGraph/Animation/CrossFade.cs | 25 +++++++++++-- src/Perspex.SceneGraph/Animation/PageSlide.cs | 25 +++++++++++-- 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs diff --git a/samples/XamlTestApplication/Views/MainWindow.paml b/samples/XamlTestApplication/Views/MainWindow.paml index 2c4525eb13..7a6b53a638 100644 --- a/samples/XamlTestApplication/Views/MainWindow.paml +++ b/samples/XamlTestApplication/Views/MainWindow.paml @@ -3,8 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Perspex Test Application" Height="350" Width="525" SizeToContent="WidthAndHeight" > - - + + + + + diff --git a/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs b/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs index 4da18ba27f..10520b85d5 100644 --- a/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs +++ b/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs @@ -1,6 +1,7 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -86,6 +87,7 @@ namespace Perspex.Markup.Xaml.Context new TypeConverterRegistration(typeof(RowDefinitions), new RowDefinitionsTypeConverter()), new TypeConverterRegistration(typeof(Thickness), new ThicknessTypeConverter()), new TypeConverterRegistration(typeof(Selector), new SelectorTypeConverter()), + new TypeConverterRegistration(typeof(TimeSpan), new TimeSpanConverter()), }; typeConverterProvider.AddAll(converters); diff --git a/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs b/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs new file mode 100644 index 0000000000..c090c0e891 --- /dev/null +++ b/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs @@ -0,0 +1,36 @@ +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Globalization; +using OmniXaml.TypeConversion; +using Perspex.Media; + +namespace Perspex.Markup.Xaml.Converters +{ + public class TimeSpanConverter : ITypeConverter + { + public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType) + { + return sourceType == typeof(string); + } + + public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType) + { + return false; + } + + public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value) + { + // TimeSpan does not parse seconds format directly, restrict + // syntax to seconds for now (ie. "0.25") + var secs = double.Parse((string)value); + return TimeSpan.FromSeconds(secs); + } + + public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj b/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj index 243e7b5e5a..2bce8946ac 100644 --- a/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj +++ b/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj @@ -48,6 +48,7 @@ + diff --git a/src/Perspex.SceneGraph/Animation/CrossFade.cs b/src/Perspex.SceneGraph/Animation/CrossFade.cs index d2dd1b800b..5335777014 100644 --- a/src/Perspex.SceneGraph/Animation/CrossFade.cs +++ b/src/Perspex.SceneGraph/Animation/CrossFade.cs @@ -8,11 +8,28 @@ using System.Threading.Tasks; namespace Perspex.Animation { + // TODO: Perhaps we want a common base for Transitions with Duration so that we can + // consolidate the property declarations, etc + // + /// /// Defines a cross-fade animation between two s. /// - public class CrossFade : IPageTransition + public class CrossFade : PerspexObject, IPageTransition { + /// + /// Defines the property. + /// + public static readonly PerspexProperty DurationProperty = + PerspexProperty.Register(nameof(Duration)); + + /// + /// Initializes a new instance of the class. + /// + public CrossFade() + { + } + /// /// Initializes a new instance of the class. /// @@ -25,7 +42,11 @@ namespace Perspex.Animation /// /// Gets the duration of the animation. /// - public TimeSpan Duration { get; } + public TimeSpan Duration + { + get { return GetValue(DurationProperty); } + set { SetValue(DurationProperty, value); } + } /// /// Starts the animation. diff --git a/src/Perspex.SceneGraph/Animation/PageSlide.cs b/src/Perspex.SceneGraph/Animation/PageSlide.cs index db6863db35..c0041d560c 100644 --- a/src/Perspex.SceneGraph/Animation/PageSlide.cs +++ b/src/Perspex.SceneGraph/Animation/PageSlide.cs @@ -9,11 +9,28 @@ using Perspex.Media; namespace Perspex.Animation { + // TODO: Perhaps we want a common base for Transitions with Duration so that we can + // consolidate the property declarations, etc + // + /// /// Transitions between two pages by sliding them horizontally. /// - public class PageSlide : IPageTransition + public class PageSlide : PerspexObject, IPageTransition { + /// + /// Defines the property. + /// + public static readonly PerspexProperty DurationProperty = + PerspexProperty.Register(nameof(Duration)); + + /// + /// Initializes a new instance of the class. + /// + public PageSlide() + { + } + /// /// Initializes a new instance of the class. /// @@ -26,7 +43,11 @@ namespace Perspex.Animation /// /// Gets the duration of the animation. /// - public TimeSpan Duration { get; } + public TimeSpan Duration + { + get { return GetValue(DurationProperty); } + set { SetValue(DurationProperty, value); } + } /// /// Starts the animation. From d561910554ddcf67af282f4206b9dce9c1ef17b7 Mon Sep 17 00:00:00 2001 From: Jason Jarvis Date: Fri, 25 Sep 2015 07:47:18 -0700 Subject: [PATCH 2/2] Updated TimeSpanTypeConverter (renamed) to support both full syntax as well as shorthand seconds only. Made CrossFade/PageSlide use simple Duration property, and removed PerspexObject base. Fixed some white space issues. --- .../XamlTestApplication/Views/MainWindow.paml | 2 +- .../Context/PerspexWiringContext.cs | 2 +- .../Converters/TimeSpanTypeConverter.cs | 15 ++++++++++----- src/Perspex.SceneGraph/Animation/CrossFade.cs | 18 ++---------------- src/Perspex.SceneGraph/Animation/PageSlide.cs | 18 ++---------------- 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/samples/XamlTestApplication/Views/MainWindow.paml b/samples/XamlTestApplication/Views/MainWindow.paml index 7a6b53a638..0f37faa54c 100644 --- a/samples/XamlTestApplication/Views/MainWindow.paml +++ b/samples/XamlTestApplication/Views/MainWindow.paml @@ -6,7 +6,7 @@ - + diff --git a/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs b/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs index 10520b85d5..b2ab8c06ae 100644 --- a/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs +++ b/src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs @@ -87,7 +87,7 @@ namespace Perspex.Markup.Xaml.Context new TypeConverterRegistration(typeof(RowDefinitions), new RowDefinitionsTypeConverter()), new TypeConverterRegistration(typeof(Thickness), new ThicknessTypeConverter()), new TypeConverterRegistration(typeof(Selector), new SelectorTypeConverter()), - new TypeConverterRegistration(typeof(TimeSpan), new TimeSpanConverter()), + new TypeConverterRegistration(typeof(TimeSpan), new TimeSpanTypeConverter()), }; typeConverterProvider.AddAll(converters); diff --git a/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs b/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs index c090c0e891..54920c7242 100644 --- a/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs +++ b/src/Markup/Perspex.Markup.Xaml/Converters/TimeSpanTypeConverter.cs @@ -8,7 +8,7 @@ using Perspex.Media; namespace Perspex.Markup.Xaml.Converters { - public class TimeSpanConverter : ITypeConverter + public class TimeSpanTypeConverter : ITypeConverter { public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType) { @@ -22,10 +22,15 @@ namespace Perspex.Markup.Xaml.Converters public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value) { - // TimeSpan does not parse seconds format directly, restrict - // syntax to seconds for now (ie. "0.25") - var secs = double.Parse((string)value); - return TimeSpan.FromSeconds(secs); + var valueStr = (string)value; + if (!valueStr.Contains(":")) + { + // shorthand seconds format (ie. "0.25") + var secs = double.Parse(valueStr); + return TimeSpan.FromSeconds(secs); + } + + return TimeSpan.Parse(valueStr); } public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType) diff --git a/src/Perspex.SceneGraph/Animation/CrossFade.cs b/src/Perspex.SceneGraph/Animation/CrossFade.cs index 5335777014..4f3486c06a 100644 --- a/src/Perspex.SceneGraph/Animation/CrossFade.cs +++ b/src/Perspex.SceneGraph/Animation/CrossFade.cs @@ -8,21 +8,11 @@ using System.Threading.Tasks; namespace Perspex.Animation { - // TODO: Perhaps we want a common base for Transitions with Duration so that we can - // consolidate the property declarations, etc - // - /// /// Defines a cross-fade animation between two s. /// - public class CrossFade : PerspexObject, IPageTransition + public class CrossFade : IPageTransition { - /// - /// Defines the property. - /// - public static readonly PerspexProperty DurationProperty = - PerspexProperty.Register(nameof(Duration)); - /// /// Initializes a new instance of the class. /// @@ -42,11 +32,7 @@ namespace Perspex.Animation /// /// Gets the duration of the animation. /// - public TimeSpan Duration - { - get { return GetValue(DurationProperty); } - set { SetValue(DurationProperty, value); } - } + public TimeSpan Duration { get; set; } /// /// Starts the animation. diff --git a/src/Perspex.SceneGraph/Animation/PageSlide.cs b/src/Perspex.SceneGraph/Animation/PageSlide.cs index c0041d560c..cf145286fc 100644 --- a/src/Perspex.SceneGraph/Animation/PageSlide.cs +++ b/src/Perspex.SceneGraph/Animation/PageSlide.cs @@ -9,21 +9,11 @@ using Perspex.Media; namespace Perspex.Animation { - // TODO: Perhaps we want a common base for Transitions with Duration so that we can - // consolidate the property declarations, etc - // - /// /// Transitions between two pages by sliding them horizontally. /// - public class PageSlide : PerspexObject, IPageTransition + public class PageSlide : IPageTransition { - /// - /// Defines the property. - /// - public static readonly PerspexProperty DurationProperty = - PerspexProperty.Register(nameof(Duration)); - /// /// Initializes a new instance of the class. /// @@ -43,11 +33,7 @@ namespace Perspex.Animation /// /// Gets the duration of the animation. /// - public TimeSpan Duration - { - get { return GetValue(DurationProperty); } - set { SetValue(DurationProperty, value); } - } + public TimeSpan Duration { get; set; } /// /// Starts the animation.