2 * Copyright(c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
21 namespace Tizen.NUI.Components.Extension
24 /// LottieButtonExtension is a ButtonExtension class that uses Lottie image for a Button icon.
26 /// This extension must be used within a class implementing LottieButtonStyle.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class LottieButtonExtension : ButtonExtension
33 /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL
35 [EditorBrowsable(EditorBrowsableState.Never)]
36 public LottieButtonExtension() : base()
38 LottieView = new LottieAnimationView();
42 /// The Lottie view that will be used as an icon part in a Button.
44 [EditorBrowsable(EditorBrowsableState.Never)]
45 protected LottieAnimationView LottieView { get; set; }
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public override ImageView OnCreateIcon(Button button, ImageView icon)
51 InitializeLottieView(button, LottieView);
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public override void OnControlStateChanged(Button button, View.ControlStateChangedEventArgs args)
60 UpdateLottieView(button, args.PreviousState, LottieView);
63 internal static void InitializeLottieView(Button button, LottieAnimationView lottieView)
65 if (button.Style as ILottieButtonStyle == null)
67 throw new Exception("LottieButtonExtension must be used within a ILottieButtonStyle or derived class.");
70 var lottieStyle = (ILottieButtonStyle)button.Style;
71 lottieView.URL = lottieStyle.LottieUrl;
72 lottieView.StopBehavior = LottieAnimationView.StopBehaviorType.MaximumFrame;
73 if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(ControlState.Normal, out var result))
75 result.Show(lottieView, true);
79 internal static void UpdateLottieView(Button button, ControlState previousState, LottieAnimationView lottieView)
81 var lottieStyle = ((ILottieButtonStyle)button.Style);
82 if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(button.ControlState, out var result))
84 result.Show(lottieView, !previousState.Contains(ControlState.Pressed));