2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 ElmSharp.Wearable;
20 using System.Collections.Generic;
24 using Xamarin.Forms.Platform.Tizen;
25 using XForms = Xamarin.Forms.Forms;
27 [assembly: ExportEffect(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.TizenCircleSurfaceEffect), "CircleSurfaceEffect")]
28 namespace Tizen.Wearable.CircularUI.Forms.Renderer
30 public class TizenCircleSurfaceEffect : PlatformEffect
32 ElmSharp.Layout _surfaceLayout;
33 ElmSharp.Wearable.CircleSurface _surface;
35 EvasObjectEvent _showEvent;
36 EvasObjectEvent _hideEvent;
37 EvasObjectEvent _restackEvent;
38 EvasObjectEvent _moveEvent;
39 EvasObjectEvent _resizeEvent;
41 IRotaryEventReceiver _rotaryReceiver;
43 protected override void OnAttached()
45 var rect = Control.Geometry;
47 _surfaceLayout = new ElmSharp.Layout(XForms.NativeParent);
48 _surfaceLayout.Show();
49 _surface = new ElmSharp.Wearable.CircleSurface(_surfaceLayout);
50 _surfaceLayout.Geometry = rect;
51 _surfaceLayout.StackAbove(Control);
53 CircleSurfaceEffectBehavior.SetSurface(Element, _surface);
55 _showEvent = new EvasObjectEvent(Control, EvasObjectCallbackType.Show);
56 _hideEvent = new EvasObjectEvent(Control, EvasObjectCallbackType.Hide);
57 _restackEvent = new EvasObjectEvent(Control, EvasObjectCallbackType.Restack);
58 _moveEvent = new EvasObjectEvent(Control, EvasObjectCallbackType.Move);
59 _resizeEvent = new EvasObjectEvent(Control, EvasObjectCallbackType.Resize);
61 _showEvent.On += ControlShowed;
62 _hideEvent.On += ControlHided;
63 _restackEvent.On += ControlRestacked;
64 _moveEvent.On += ControlChanged;
65 _resizeEvent.On += ControlChanged;
67 Element.PropertyChanging += ElementPropertyChanging;
68 Element.PropertyChanged += ElementPropertyChanged;
72 (Element as Page).Appearing += (s, e) =>
74 var obj = CircleSurfaceEffectBehavior.GetRotaryFocusObject(Element);
75 ActivateRotaryFocusable(obj);
80 EcoreMainloop.Post(() =>
82 var obj = CircleSurfaceEffectBehavior.GetRotaryFocusObject(Element);
83 ActivateRotaryFocusable(obj);
88 protected override void OnDetached()
90 var obj = CircleSurfaceEffectBehavior.GetRotaryFocusObject(Element);
91 DeativateRotaryFocusable(obj);
95 _surfaceLayout.Unrealize();
96 _surfaceLayout = null;
97 CircleSurfaceEffectBehavior.SetSurface(Element, null);
100 _hideEvent.Dispose();
101 _restackEvent.Dispose();
102 _moveEvent.Dispose();
103 _resizeEvent.Dispose();
106 void ElementPropertyChanging(object sender, PropertyChangingEventArgs e)
108 if (e.PropertyName == CircleSurfaceEffectBehavior.RotaryFocusObjectProperty.PropertyName)
110 var obj = CircleSurfaceEffectBehavior.GetRotaryFocusObject(Element);
111 DeativateRotaryFocusable(obj);
115 void ElementPropertyChanged(object sender, global::System.ComponentModel.PropertyChangedEventArgs e)
117 if (e.PropertyName == CircleSurfaceEffectBehavior.RotaryFocusObjectProperty.PropertyName)
119 var obj = CircleSurfaceEffectBehavior.GetRotaryFocusObject(Element);
120 ActivateRotaryFocusable(obj);
124 void OnRotaryEventChanged(ElmSharp.Wearable.RotaryEventArgs e)
126 _rotaryReceiver?.Rotate(new RotaryEventArgs { IsClockwise = e.IsClockwise });
129 void ControlShowed(object sender, EventArgs e)
131 _surfaceLayout.Show();
134 void ControlHided(object sender, EventArgs e)
136 _surfaceLayout.Hide();
139 void ControlRestacked(object sender, EventArgs e)
141 _surfaceLayout.StackAbove(Control);
144 void ControlChanged(object sender, EventArgs e)
146 _surfaceLayout.Geometry = Control.Geometry;
149 void ActivateRotaryFocusable(IRotaryFocusable focusable)
151 if (focusable is IRotaryEventReceiver)
153 _rotaryReceiver = focusable as IRotaryEventReceiver;
154 RotaryEventManager.Rotated += OnRotaryEventChanged;
156 else if (focusable is IRotaryFocusable)
158 var consumer = focusable as BindableObject;
159 if (consumer != null)
161 var renderer = Xamarin.Forms.Platform.Tizen.Platform.GetRenderer(consumer);
162 (renderer?.NativeView as IRotaryActionWidget)?.Activate();
167 void DeativateRotaryFocusable(IRotaryFocusable focusable)
169 if (focusable is IRotaryEventReceiver)
171 _rotaryReceiver = null;
172 RotaryEventManager.Rotated -= OnRotaryEventChanged;
174 else if (focusable is IRotaryFocusable)
176 var consumer = focusable as BindableObject;
177 if (consumer != null)
179 var renderer = Xamarin.Forms.Platform.Tizen.Platform.GetRenderer(consumer);
180 (renderer?.NativeView as IRotaryActionWidget)?.Deactivate();