2 * Copyright(c) 2019 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.
19 using System.Runtime.InteropServices;
20 using Tizen.NUI.BaseComponents;
21 using System.ComponentModel;
26 /// Provides the functionality of handling keyboard navigation and maintaining the two-dimensional keyboard focus chain.<br />
27 /// It provides functionality of setting the focus and moving the focus in four directions( i.e., left, right, up, and down).<br />
28 /// It also draws a highlight for the focused view and sends an event when the focus is changed.<br />
30 /// <since_tizen> 3 </since_tizen>
31 public class FocusManager : BaseHandle
33 private static readonly FocusManager instance = FocusManager.Get();
34 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
35 private CustomAlgorithmInterfaceWrapper _customAlgorithmInterfaceWrapper;
37 private EventHandlerWithReturnType<object, PreFocusChangeEventArgs, View> _preFocusChangeEventHandler;
38 private PreFocusChangeEventCallback _preFocusChangeCallback;
40 private EventHandler<FocusChangedEventArgs> _focusChangedEventHandler;
41 private FocusChangedEventCallback _focusChangedEventCallback;
43 private EventHandler<FocusGroupChangedEventArgs> _focusGroupChangedEventHandler;
44 private FocusGroupChangedEventCallback _focusGroupChangedEventCallback;
46 private EventHandler<FocusedViewActivatedEventArgs> _focusedViewEnterKeyEventHandler;
47 private FocusedViewEnterKeyEventCallback _focusedViewEnterKeyEventCallback;
49 private EventHandler<FocusedViewActivatedEventArgs> _focusedViewEnterKeyEventHandler2;
50 private FocusedViewEnterKeyEventCallback2 _focusedViewEnterKeyEventCallback2;
52 internal FocusManager(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.FocusManager.FocusManager_SWIGUpcast(cPtr), cMemoryOwn)
54 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
57 internal FocusManager() : this(Interop.FocusManager.new_FocusManager(), true)
59 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
62 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
63 internal delegate IntPtr PreFocusChangeEventCallback(IntPtr current, IntPtr proposed, View.FocusDirection direction);
65 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
66 internal delegate void FocusChangedEventCallback(IntPtr current, IntPtr next);
68 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
69 private delegate void FocusGroupChangedEventCallback(IntPtr current, bool forwardDirection);
71 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
72 private delegate void FocusedViewEnterKeyEventCallback(IntPtr view);
74 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
75 private delegate void FocusedViewEnterKeyEventCallback2(IntPtr view);
78 /// PreFocusChange will be triggered before the focus is going to be changed.<br />
79 /// The FocusManager makes the best guess for which view to focus towards the given direction, but applications might want to change that.<br />
80 /// By connecting with this event, they can check the proposed view to focus and return a different view if they wish.<br />
81 /// This event is only triggered when the navigation key is pressed and KeyboardFocusManager tries to move the focus automatically.<br />
82 /// It won't be emitted for focus movement by calling the SetCurrentFocusView directly.<br />
84 /// <since_tizen> 3 </since_tizen>
85 public event EventHandlerWithReturnType<object, PreFocusChangeEventArgs, View> PreFocusChange
89 if (_preFocusChangeEventHandler == null)
91 _preFocusChangeCallback = OnPreFocusChange;
92 PreFocusChangeSignal().Connect(_preFocusChangeCallback);
94 _preFocusChangeEventHandler += value;
98 _preFocusChangeEventHandler -= value;
99 if (_preFocusChangeEventHandler == null && PreFocusChangeSignal().Empty() == false)
101 PreFocusChangeSignal().Disconnect(_preFocusChangeCallback);
107 /// The FocusGroupChanged will be triggered after the current focused view has been changed.
109 /// <since_tizen> 3 </since_tizen>
110 public event EventHandler<FocusChangedEventArgs> FocusChanged
114 if (_focusChangedEventCallback == null)
116 _focusChangedEventCallback = OnFocusChanged;
117 FocusChangedSignal().Connect(_focusChangedEventCallback);
119 _focusChangedEventHandler += value;
123 _focusChangedEventHandler -= value;
125 if (_focusChangedEventCallback == null && FocusChangedSignal().Empty() == false)
127 FocusChangedSignal().Disconnect(_focusChangedEventCallback);
133 /// The FocusGroupChanged will be triggered when the focus group has been changed.<br />
134 /// If the current focus group has a parent layout control, the FocusManager will make the best guess for the next focus group to move the focus to in the given direction (forward or backward).<br />
135 /// If not, the application has to set the new focus.<br />
137 /// <since_tizen> 3 </since_tizen>
138 public event EventHandler<FocusGroupChangedEventArgs> FocusGroupChanged
142 if (_focusGroupChangedEventCallback == null)
144 _focusGroupChangedEventCallback = OnFocusGroupChanged;
145 FocusGroupChangedSignal().Connect(_focusGroupChangedEventCallback);
147 _focusGroupChangedEventHandler += value;
151 _focusGroupChangedEventHandler -= value;
153 if (_focusGroupChangedEventCallback == null && FocusGroupChangedSignal().Empty() == false)
155 FocusGroupChangedSignal().Disconnect(_focusGroupChangedEventCallback);
161 /// The FocusedViewActivated will be triggered when the current focused view has the enter key pressed on it.
163 /// <since_tizen> 3 </since_tizen>
164 public event EventHandler<FocusedViewActivatedEventArgs> FocusedViewActivated
168 if (_focusedViewEnterKeyEventCallback == null)
170 _focusedViewEnterKeyEventCallback = OnFocusedViewEnterKey;
171 FocusedViewEnterKeySignal().Connect(_focusedViewEnterKeyEventCallback);
173 _focusedViewEnterKeyEventHandler += value;
177 _focusedViewEnterKeyEventHandler -= value;
179 if (_focusedViewEnterKeyEventCallback != null && FocusedViewEnterKeySignal().Empty() == false)
181 FocusedViewEnterKeySignal().Disconnect(_focusedViewEnterKeyEventCallback);
187 /// [Obsolete("Please do not use! this will be deprecated")]
189 /// <since_tizen> 3 </since_tizen>
190 /// Please do not use! this will be deprecated!
191 /// Instead please use FocusedViewActivated.
192 [Obsolete("Please do not use! This will be deprecated! Please use FocusManager.FocusedViewActivated instead! " +
194 "FocusManager.Instance.FocusedViewActivated = OnFocusedViewActivated; " +
195 "private void OnFocusedViewActivated(object source, FocusManager.FocusedViewActivatedEventArgs args) {...}")]
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 public event EventHandler<FocusedViewActivatedEventArgs> FocusedViewEnterKeyPressed
201 if (_focusedViewEnterKeyEventCallback2 == null)
203 _focusedViewEnterKeyEventCallback2 = OnFocusedViewEnterKey2;
204 FocusedViewEnterKeySignal().Connect(_focusedViewEnterKeyEventCallback2);
206 _focusedViewEnterKeyEventHandler2 += value;
210 _focusedViewEnterKeyEventHandler2 -= value;
212 if (_focusedViewEnterKeyEventCallback2 != null && FocusedViewEnterKeySignal().Empty() == false)
214 FocusedViewEnterKeySignal().Disconnect(_focusedViewEnterKeyEventCallback2);
220 /// ICustomFocusAlgorithm is used to provide the custom keyboard focus algorithm for retrieving the next focusable view.<br />
221 /// The application can implement the interface and override the keyboard focus behavior.<br />
222 /// If the focus is changing within a layout container, then the layout container is queried first to provide the next focusable view.<br />
223 /// If this does not provide a valid view, then the Keyboard FocusManager will check focusable properties to determine the next focusable actor.<br />
224 /// If focusable properties are not set, then the keyboard FocusManager calls the GetNextFocusableView() method of this interface.<br />
226 /// <since_tizen> 3 </since_tizen>
227 public interface ICustomFocusAlgorithm
230 /// Get the next focus actor.
232 /// <param name="current">The current focus view.</param>
233 /// <param name="proposed">The proposed focus view</param>
234 /// <param name="direction">The focus move direction</param>
235 /// <returns>The next focus actor.</returns>
236 /// <since_tizen> 3 </since_tizen>
237 View GetNextFocusableView(View current, View proposed, View.FocusDirection direction);
241 /// Gets or sets the status of whether the focus movement should be looped within the same focus group.<br />
242 /// The focus movement is not looped by default.<br />
244 /// <since_tizen> 3 </since_tizen>
245 public bool FocusGroupLoop
249 SetFocusGroupLoop(value);
253 return GetFocusGroupLoop();
258 /// Gets or sets the focus indicator view.<br />
259 /// This will replace the default focus indicator view in the FocusManager and will be added to the focused view as a highlight.<br />
261 /// <since_tizen> 3 </since_tizen>
262 public View FocusIndicator
266 SetFocusIndicatorView(value);
270 return GetFocusIndicatorView();
275 /// Gets the singleton of the FocusManager object.
277 /// <since_tizen> 3 </since_tizen>
278 public static FocusManager Instance
287 /// Moves the keyboard focus to the given view.<br />
288 /// Only one view can be focused at the same time.<br />
289 /// The view must be in the stage already and keyboard focusable.<br />
291 /// <param name="view">The view to be focused.</param>
292 /// <returns>Whether the focus is successful or not.</returns>
293 /// <since_tizen> 3 </since_tizen>
294 public bool SetCurrentFocusView(View view)
298 throw new ArgumentNullException("the target view should not be null");
301 bool ret = Interop.FocusManager.FocusManager_SetCurrentFocusActor(swigCPtr, View.getCPtr(view));
302 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
307 /// Gets the current focused view.
309 /// <returns>A handle to the current focused view or an empty handle if no view is focused.</returns>
310 /// <since_tizen> 3 </since_tizen>
311 public View GetCurrentFocusView()
313 //to fix memory leak issue, match the handle count with native side.
314 IntPtr cPtr = Interop.FocusManager.FocusManager_GetCurrentFocusActor(swigCPtr);
315 View ret = this.GetInstanceSafely<View>(cPtr);
320 /// Moves the focus to the next focusable view in the focus chain in the given direction (according to the focus traversal order).
322 /// <param name="direction">The direction of the focus movement.</param>
323 /// <returns>True if the movement was successful.</returns>
324 /// <since_tizen> 3 </since_tizen>
325 public bool MoveFocus(View.FocusDirection direction)
327 bool ret = Interop.FocusManager.FocusManager_MoveFocus(swigCPtr, (int)direction);
328 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
333 /// Clears the focus from the current focused view if any, so that no view is focused in the focus chain.<br />
334 /// It will emit the FocusChanged event without the current focused view.<br />
336 /// <since_tizen> 3 </since_tizen>
337 public void ClearFocus()
339 Interop.FocusManager.FocusManager_ClearFocus(swigCPtr);
340 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
344 /// Move the focus to previous focused view.
346 /// <since_tizen> 3 </since_tizen>
347 public void MoveFocusBackward()
349 Interop.FocusManager.FocusManager_MoveFocusBackward(swigCPtr);
350 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
354 /// Sets whether the view is a focus group that can limit the scope of the focus movement to its child views in the focus chain.<br />
355 /// Layout controls set themselves as focus groups by default.<br />
357 /// <param name="view">The view to be set as a focus group.</param>
358 /// <param name="isFocusGroup">Whether to set the view as a focus group or not.</param>
359 /// <since_tizen> 3 </since_tizen>
360 public void SetAsFocusGroup(View view, bool isFocusGroup)
362 Interop.FocusManager.FocusManager_SetAsFocusGroup(swigCPtr, View.getCPtr(view), isFocusGroup);
363 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
367 /// Checks whether the view is set as a focus group or not.
369 /// <param name="view">The view to be checked.</param>
370 /// <returns>Whether the view is set as a focus group.</returns>
371 /// <since_tizen> 3 </since_tizen>
372 public bool IsFocusGroup(View view)
374 bool ret = Interop.FocusManager.FocusManager_IsFocusGroup(swigCPtr, View.getCPtr(view));
375 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
380 /// Returns the closest ancestor of the given view that is a focus group.
382 /// <param name="view">The view to be checked for its focus group.</param>
383 /// <returns>The focus group the given view belongs to or an empty handle if the given view.</returns>
384 /// <since_tizen> 3 </since_tizen>
385 public View GetFocusGroup(View view)
387 //to fix memory leak issue, match the handle count with native side.
388 IntPtr cPtr = Interop.FocusManager.FocusManager_GetFocusGroup(swigCPtr, View.getCPtr(view));
389 View ret = this.GetInstanceSafely<View>(cPtr);
394 /// Provides the implementation of a custom focus algorithm interface to allow the application to define the focus logic.<br />
396 /// <param name="arg0">The user's implementation of ICustomFocusAlgorithm.</param>
397 /// <since_tizen> 3 </since_tizen>
398 public void SetCustomAlgorithm(ICustomFocusAlgorithm arg0)
402 _customAlgorithmInterfaceWrapper = new CustomAlgorithmInterfaceWrapper();
403 _customAlgorithmInterfaceWrapper.SetFocusAlgorithm(arg0);
405 Interop.NDalic.SetCustomAlgorithm(swigCPtr, CustomAlgorithmInterface.getCPtr(_customAlgorithmInterfaceWrapper));
406 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410 Interop.NDalic.SetCustomAlgorithm(swigCPtr, new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero));
411 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FocusManager obj)
417 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
420 internal static FocusManager Get()
422 FocusManager ret = new FocusManager(Interop.FocusManager.FocusManager_Get(), true);
423 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
427 internal void SetFocusGroupLoop(bool enabled)
429 Interop.FocusManager.FocusManager_SetFocusGroupLoop(swigCPtr, enabled);
430 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433 internal bool GetFocusGroupLoop()
435 bool ret = Interop.FocusManager.FocusManager_GetFocusGroupLoop(swigCPtr);
436 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
440 internal void SetFocusIndicatorView(View indicator)
442 Interop.FocusManager.FocusManager_SetFocusIndicatorActor(swigCPtr, View.getCPtr(indicator));
443 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
446 internal View GetFocusIndicatorView()
448 //to fix memory leak issue, match the handle count with native side.
449 IntPtr cPtr = Interop.FocusManager.FocusManager_GetFocusIndicatorActor(swigCPtr);
450 View ret = this.GetInstanceSafely<View>(cPtr);
454 internal PreFocusChangeSignal PreFocusChangeSignal()
456 PreFocusChangeSignal ret = new PreFocusChangeSignal(Interop.FocusManager.FocusManager_PreFocusChangeSignal(swigCPtr), false);
457 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
461 internal FocusChangedSignal FocusChangedSignal()
463 FocusChangedSignal ret = new FocusChangedSignal(Interop.FocusManager.FocusManager_FocusChangedSignal(swigCPtr), false);
464 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
468 internal FocusGroupChangedSignal FocusGroupChangedSignal()
470 FocusGroupChangedSignal ret = new FocusGroupChangedSignal(Interop.FocusManager.FocusManager_FocusGroupChangedSignal(swigCPtr), false);
471 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
475 internal ViewSignal FocusedViewEnterKeySignal()
477 ViewSignal ret = new ViewSignal(Interop.FocusManager.FocusManager_FocusedActorEnterKeySignal(swigCPtr), false);
478 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482 private IntPtr OnPreFocusChange(IntPtr current, IntPtr proposed, View.FocusDirection direction)
485 PreFocusChangeEventArgs e = new PreFocusChangeEventArgs();
487 if (current != global::System.IntPtr.Zero)
489 e.CurrentView = Registry.GetManagedBaseHandleFromNativePtr(current) as View;
491 if (proposed != global::System.IntPtr.Zero)
493 e.ProposedView = Registry.GetManagedBaseHandleFromNativePtr(proposed) as View;
495 e.Direction = direction;
497 if (_preFocusChangeEventHandler != null)
499 view = _preFocusChangeEventHandler(this, e);
504 return view.GetPtrfromView();
508 if (e.ProposedView) return proposed;
513 private void OnFocusChanged(IntPtr current, IntPtr next)
515 FocusChangedEventArgs e = new FocusChangedEventArgs();
517 e.CurrentView = Registry.GetManagedBaseHandleFromNativePtr(current) as View;
518 e.NextView = Registry.GetManagedBaseHandleFromNativePtr(next) as View;
520 if (_focusChangedEventHandler != null)
522 _focusChangedEventHandler(this, e);
526 private void OnFocusGroupChanged(IntPtr current, bool forwardDirection)
528 FocusGroupChangedEventArgs e = new FocusGroupChangedEventArgs();
530 e.CurrentView = Registry.GetManagedBaseHandleFromNativePtr(current) as View;
531 e.ForwardDirection = forwardDirection;
533 if (_focusGroupChangedEventHandler != null)
535 _focusGroupChangedEventHandler(this, e);
539 private void OnFocusedViewEnterKey(IntPtr view)
541 FocusedViewActivatedEventArgs e = new FocusedViewActivatedEventArgs();
543 e.View = Registry.GetManagedBaseHandleFromNativePtr(view) as View;
545 if (_focusedViewEnterKeyEventHandler != null)
547 _focusedViewEnterKeyEventHandler(this, e);
552 /// Please do not use! this will be deprecated!
554 /// Please do not use! this will be deprecated!
555 /// Instead please use OnFocusedViewEnterKey.
556 [Obsolete("Please do not use! This will be deprecated! Please use FocusManager.OnFocusedViewEnterKey instead!")]
557 [EditorBrowsable(EditorBrowsableState.Never)]
558 private void OnFocusedViewEnterKey2(IntPtr view)
560 FocusedViewActivatedEventArgs e = new FocusedViewActivatedEventArgs();
562 e.View = Registry.GetManagedBaseHandleFromNativePtr(view) as View;
564 if (_focusedViewEnterKeyEventHandler != null)
566 _focusedViewEnterKeyEventHandler(this, e);
571 /// Event arguments that passed via the PreFocusChange signal.
573 /// <since_tizen> 3 </since_tizen>
574 public class PreFocusChangeEventArgs : EventArgs
576 private View _current;
577 private View _proposed;
578 private View.FocusDirection _direction;
581 /// The current focus view.
583 /// <since_tizen> 3 </since_tizen>
584 public View CurrentView
597 /// The proposed view.
599 /// <since_tizen> 3 </since_tizen>
600 public View ProposedView
613 /// The focus move direction.
615 /// <since_tizen> 3 </since_tizen>
616 public View.FocusDirection Direction
630 /// Event arguments that passed via the FocusChanged signal.
632 /// <since_tizen> 3 </since_tizen>
633 public class FocusChangedEventArgs : EventArgs
635 private View _current;
639 /// The current focus view.
641 /// <since_tizen> 3 </since_tizen>
642 public View CurrentView
654 /// The next focus view.
656 /// <since_tizen> 3 </since_tizen>
671 /// Event arguments that passed via the FocusGroupChanged signal.
673 /// <since_tizen> 3 </since_tizen>
674 public class FocusGroupChangedEventArgs : EventArgs
676 private View _current;
677 private bool _forwardDirection;
680 /// The current focus view.
682 /// <since_tizen> 3 </since_tizen>
683 public View CurrentView
696 /// The forward direction.
698 /// <since_tizen> 3 </since_tizen>
699 public bool ForwardDirection
703 return _forwardDirection;
707 _forwardDirection = value;
713 /// Event arguments that passed via the FocusedViewEnterKey signal.
715 /// <since_tizen> 3 </since_tizen>
716 public class FocusedViewActivatedEventArgs : EventArgs
723 /// <since_tizen> 3 </since_tizen>
738 /// Please do not use! this will be deprecated
740 /// <since_tizen> 3 </since_tizen>
741 /// Please do not use! this will be deprecated.
742 /// Instead please use FocusedViewActivatedEventArgs.
743 [Obsolete("Please do not use! This will be deprecated! Please use FocusedViewActivatedEventArgs instead! " +
745 "FocusManager.Instance.FocusedViewActivated = OnFocusedViewActivated; " +
746 "private void OnFocusedViewActivated(object source, FocusManager.FocusedViewActivatedEventArgs arg)" +
748 [EditorBrowsable(EditorBrowsableState.Never)]
749 public class FocusedViewEnterKeyEventArgs : EventArgs
756 /// <since_tizen> 3 </since_tizen>
770 private class CustomAlgorithmInterfaceWrapper : CustomAlgorithmInterface
772 private FocusManager.ICustomFocusAlgorithm _customFocusAlgorithm;
774 public CustomAlgorithmInterfaceWrapper()
778 public void SetFocusAlgorithm(FocusManager.ICustomFocusAlgorithm customFocusAlgorithm)
780 _customFocusAlgorithm = customFocusAlgorithm;
783 public override View GetNextFocusableView(View current, View proposed, View.FocusDirection direction)
785 return _customFocusAlgorithm.GetNextFocusableView(current, proposed, direction);