c695836c3b2e4d0ac7fcb1e4f2e5e6f315090371
[platform/core/csapi/xsf.git] / src / XSF / Xamarin.Forms.Core / SwipeView.cs
1 using System;
2 using System.ComponentModel;
3 using System.Runtime.CompilerServices;
4 using Xamarin.Forms.Platform;
5
6 namespace Xamarin.Forms
7 {
8         [ContentProperty("Content")]
9         [RenderWith(typeof(_SwipeViewRenderer))]
10         public class SwipeView : ContentView, IElementConfiguration<SwipeView>, ISwipeViewController
11         {
12                 readonly Lazy<PlatformConfigurationRegistry<SwipeView>> _platformConfigurationRegistry;
13
14                 public SwipeView()
15                 {
16                         _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<SwipeView>>(() => new PlatformConfigurationRegistry<SwipeView>(this));
17                 }
18
19                 [EditorBrowsable(EditorBrowsableState.Never)]
20                 public static void VerifySwipeViewFlagEnabled(
21                         string constructorHint = null,
22                         [CallerMemberName] string memberName = "")
23                 {
24                         ExperimentalFlags.VerifyFlagEnabled(nameof(SwipeView), ExperimentalFlags.SwipeViewExperimental, memberName: memberName);
25                 }
26
27                 public static readonly BindableProperty LeftItemsProperty =
28                         BindableProperty.Create(nameof(LeftItems), typeof(SwipeItems), typeof(SwipeView), null, BindingMode.OneWay, null, defaultValueCreator: SwipeItemsDefaultValueCreator,
29                                 propertyChanged: OnSwipeItemsChanged);
30
31                 public static readonly BindableProperty RightItemsProperty =
32                         BindableProperty.Create(nameof(RightItems), typeof(SwipeItems), typeof(SwipeView), null, BindingMode.OneWay, null, defaultValueCreator: SwipeItemsDefaultValueCreator);
33
34                 public static readonly BindableProperty TopItemsProperty =
35                         BindableProperty.Create(nameof(TopItems), typeof(SwipeItems), typeof(SwipeView), null, BindingMode.OneWay, null, defaultValueCreator: SwipeItemsDefaultValueCreator);
36
37                 public static readonly BindableProperty BottomItemsProperty =
38                         BindableProperty.Create(nameof(BottomItems), typeof(SwipeItems), typeof(SwipeView), null, BindingMode.OneWay, null, defaultValueCreator: SwipeItemsDefaultValueCreator);
39
40                 public SwipeItems LeftItems
41                 {
42                         get { return (SwipeItems)GetValue(LeftItemsProperty); }
43                         set { SetValue(LeftItemsProperty, value); }
44                 }
45
46                 public SwipeItems RightItems
47                 {
48                         get { return (SwipeItems)GetValue(RightItemsProperty); }
49                         set { SetValue(RightItemsProperty, value); }
50                 }
51
52                 public SwipeItems TopItems
53                 {
54                         get { return (SwipeItems)GetValue(TopItemsProperty); }
55                         set { SetValue(TopItemsProperty, value); }
56                 }
57
58                 public SwipeItems BottomItems
59                 {
60                         get { return (SwipeItems)GetValue(BottomItemsProperty); }
61                         set { SetValue(BottomItemsProperty, value); }
62                 }
63
64                 static void OnSwipeItemsChanged(BindableObject bindable, object oldValue, object newValue)
65                 {
66                         ((SwipeView)bindable).UpdateSwipeItemsParent((SwipeItems)newValue);
67                 }
68
69                 public event EventHandler<SwipeStartedEventArgs> SwipeStarted;
70                 public event EventHandler<SwipeChangingEventArgs> SwipeChanging;
71                 public event EventHandler<SwipeEndedEventArgs> SwipeEnded;
72                 public event EventHandler CloseRequested;
73
74                 public void Close()
75                 {
76                         CloseRequested?.Invoke(this, EventArgs.Empty);
77                 }
78
79                 void ISwipeViewController.SendSwipeStarted(SwipeStartedEventArgs args) => SwipeStarted?.Invoke(this, args);
80
81                 void ISwipeViewController.SendSwipeChanging(SwipeChangingEventArgs args) => SwipeChanging?.Invoke(this, args);
82
83                 void ISwipeViewController.SendSwipeEnded(SwipeEndedEventArgs args) => SwipeEnded?.Invoke(this, args);
84
85                 protected override void OnBindingContextChanged()
86                 {
87                         base.OnBindingContextChanged();
88
89                         object bc = BindingContext;
90
91                         if (LeftItems != null)
92                                 SetInheritedBindingContext(LeftItems, bc);
93
94                         if (RightItems != null)
95                                 SetInheritedBindingContext(RightItems, bc);
96
97                         if (TopItems != null)
98                                 SetInheritedBindingContext(TopItems, bc);
99
100                         if (BottomItems != null)
101                                 SetInheritedBindingContext(BottomItems, bc);
102                 }
103
104                 SwipeItems SwipeItemsDefaultValueCreator() => new SwipeItems();
105
106                 static object SwipeItemsDefaultValueCreator(BindableObject bindable)
107                 {
108                         return ((SwipeView)bindable).SwipeItemsDefaultValueCreator();
109                 }
110
111                 public IPlatformElementConfiguration<T, SwipeView> On<T>() where T : IConfigPlatform
112                 {
113                         return _platformConfigurationRegistry.Value.On<T>();
114                 }
115
116                 void UpdateSwipeItemsParent(SwipeItems swipeItems)
117                 {
118                         swipeItems.Parent = this;
119
120                         foreach (var swipeItem in swipeItems)
121                                 ((VisualElement)swipeItem).Parent = swipeItems;
122                 }
123         }
124 }