Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Extensions / TextSlideEffect.cs
1 using System.Linq;
2 using Xamarin.Forms;
3
4 namespace Tizen.Xamarin.Forms.Extension
5 {
6     /// <summary>
7     /// Enumeration for the slide mode of a text.
8     /// </summary>
9     public enum TextSlideMode
10     {
11         /// <summary>
12         /// The text appears on the left of the view and slides to the right to show the overflow. When all of the text has been shown, the position is reset.
13         /// </summary>
14         Short = 1,
15
16         /// <summary>
17         /// The entire text appears from the right of the screen and slides until it disappears to the left of the screen(reappearing on the right again).
18         /// </summary>
19         Long,
20
21         /// <summary>
22         /// The text appears on the left of the view and slides to the right to show the overflow. When all of the text has been shown, the animation reverses, moving the text to the left.
23         /// </summary>
24         Bounce
25     }
26
27     /// <summary>
28     /// An effect that can make text sliding.
29     /// </summary>
30     /// <example>
31     /// <code>
32     /// new Label
33     /// {
34     ///     Text = "Test Slide Text",
35     ///     LineBreakMode =
36     /// }
37     /// </code>
38     /// </example>
39     public static class TextSlideEffect
40     {
41         /// <summary>
42         /// BindableProperty. Implements the attached property that represents the style about how the text slides.
43         /// </summary>
44         public static readonly BindableProperty ModeProperty = BindableProperty.CreateAttached("Mode", typeof(TextSlideMode), typeof(TextSlideEffect), TextSlideMode.Long);
45
46         /// <summary>
47         /// BindableProperty. Implements the attached property that represents the slide mode.
48         /// </summary>
49         public static readonly BindableProperty IsAlwaysOnProperty = BindableProperty.CreateAttached("IsAlwaysOn", typeof(bool), typeof(TextSlideEffect), false);
50
51         /// <summary>
52         /// BindableProperty. Implements the attached property that represents the slide duration.
53         /// </summary>
54         public static readonly BindableProperty DurationProperty = BindableProperty.CreateAttached("Duration", typeof(int), typeof(TextSlideEffect), 3000);
55
56         /// <summary>
57         /// BindableProperty. Implements the attached property that represents the style when the text slides.
58         /// </summary>
59         public static readonly BindableProperty IsTextSlidingProperty = BindableProperty.CreateAttached("IsTextSliding", typeof(bool), typeof(TextSlideEffect), false, propertyChanged: OnIsTextSlidingChanged);
60
61         /// <summary>
62         /// Gets the TextSlideMode of the bindable element.
63         /// </summary>
64         public static TextSlideMode GetMode(BindableObject view)
65         {
66             return (TextSlideMode)view.GetValue(ModeProperty);
67         }
68
69         /// <summary>
70         /// Sets the TextSlideMode of the bindable element.
71         /// </summary>
72         public static void SetMode(BindableObject view, TextSlideMode value)
73         {
74             view.SetValue(ModeProperty, value);
75         }
76
77         /// <summary>
78         /// Gets the IsAlwaysOn of the bindable element.
79         /// </summary>
80         public static bool GetIsAlwaysOn(BindableObject view)
81         {
82             return (bool)view.GetValue(IsAlwaysOnProperty);
83         }
84
85         /// <summary>
86         /// Sets the IsAlwaysOn of the bindable element.
87         /// </summary>
88         /// <remarks>
89         /// When the value is set to false, text will only slide when the length of the text is longer a View.
90         /// Otherwise, the text will always slide.
91         /// </remarks>
92         public static void SetIsAlwaysOn(BindableObject view, bool value)
93         {
94             view.SetValue(IsAlwaysOnProperty, value);
95         }
96
97         /// <summary>
98         /// Gets the Duration of the bindable element.
99         /// </summary>
100         public static int GetDuration(BindableObject view)
101         {
102             return (int)view.GetValue(DurationProperty);
103         }
104
105         /// <summary>
106         /// Sets the Duration(in milliseconds) of the bindable element.
107         /// </summary>
108         public static void SetDuration(BindableObject view, int value)
109         {
110             view.SetValue(DurationProperty, value);
111         }
112
113         /// <summary>
114         /// Gets the sliding status of the bindable element.
115         /// </summary>
116         public static bool GetIsTextSliding(BindableObject view)
117         {
118             return (bool)view.GetValue(IsTextSlidingProperty);
119         }
120
121         /// <summary>
122         /// Sets the sliding status of the bindable element.
123         /// </summary>
124         /// <remarks>
125         /// This method only works properly when the text is not wrapped.
126         /// For example, if the Label.LineBreakMode is set to any options other than NoWrap,
127         /// the Text will not be sliding.
128         /// </remarks>
129         /// <seealso cref="SetIsAlwaysOn(BindableObject, bool)"/>
130         public static void SetIsTextSliding(BindableObject view, bool value)
131         {
132             view.SetValue(IsTextSlidingProperty, value);
133         }
134
135         static void OnIsTextSlidingChanged(BindableObject bindable, object oldValue, object newValue)
136         {
137             var label = bindable as Label;
138             if (label == null)
139                 return;
140
141             bool isEnable = (bool)newValue;
142             if (isEnable)
143             {
144                 label.Effects.Add(new LabelSlideEffect());
145             }
146             else
147             {
148                 var toRemove = label.Effects.FirstOrDefault(e => e is LabelSlideEffect);
149                 if (toRemove != null)
150                     label.Effects.Remove(toRemove);
151             }
152         }
153
154         class LabelSlideEffect : RoutingEffect
155         {
156             public LabelSlideEffect() : base("Tizen.LabelSlideEffect")
157             {
158             }
159         }
160     }
161 }