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 System.Collections.Generic;
20 using Tizen.NUI.BaseComponents;
25 /// BackKeyManager is a class to manager back key event.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public sealed class BackKeyManager
30 private static readonly BackKeyManager instance = new BackKeyManager();
33 /// BackKeyManager construct.
35 private BackKeyManager()
37 NUIApplication.GetDefaultWindow().KeyEvent += OnWindowKeyEvent;
43 [EditorBrowsable(EditorBrowsableState.Never)]
44 public List<View> Subscriber { get; } = new List<View>();
47 /// BackKeyManager static instance.
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 public static BackKeyManager Instance { get { return instance; } }
52 private Container FindParent(Container child, int step)
54 Container result = child;
56 for (int i = 0; i < step; i++)
58 result = result?.GetParent();
64 // If false, comparison1 is on top and if true, comparison2 is on top.
65 private bool CompareElevation(Container comparison1, Container comparison2)
69 Container parent1 = comparison1?.GetParent();
70 Container parent2 = comparison2?.GetParent();
72 if ((parent1 == null && parent2 != null) || (parent2 == null && parent1 != null))
74 // One is in DefaultLayer but the other is not
77 // parent1 is in DefaultLayer.
78 Layer comparison2AsLayer = comparison2 as Layer;
79 if (comparison2AsLayer != null)
81 result = comparison2AsLayer.Depth > NUIApplication.GetDefaultWindow().GetDefaultLayer().Depth;
86 // parent2 is in DefaultLayer.
87 Layer comparison1AsLayer = comparison1 as Layer;
88 if (comparison1AsLayer != null)
90 result = NUIApplication.GetDefaultWindow().GetDefaultLayer().Depth < comparison1AsLayer.Depth;
96 // If they have same parent, ready to compare!
97 if (parent1 == parent2)
100 if (comparison1.GetType().FullName.Contains("Layer"))
102 // If comparison1 is Layer, comparison2 is also Layer because only window can have Layer as child in NUI.
104 Layer comparison1AsLayer = comparison1 as Layer;
105 Layer comparison2AsLayer = comparison2 as Layer;
106 if (comparison1AsLayer != null && comparison2AsLayer != null)
108 result = comparison1AsLayer.Depth < comparison2AsLayer.Depth;
113 // If comparison1 is View, comparison2 is also View because only window can have Layer as child in NUI.
114 // Compare SiblingOrder
115 View comparison1AsView = comparison1 as View;
116 View comparison2AsView = comparison2 as View;
117 if (comparison1AsView != null && comparison2AsView != null)
119 result = comparison1AsView.SiblingOrder < comparison2AsView.SiblingOrder;
126 result = CompareElevation(parent1, parent2);
133 private void OnWindowKeyEvent(object source, Window.KeyEventArgs args)
135 if (args.Key.State == Key.StateType.Up && (args.Key.KeyPressedName == "Back" || args.Key.KeyPressedName == "XF86Back"))
139 for (int i = 0; i < Subscriber.Count; i++)
142 if (Subscriber[i].Visibility && Subscriber[i].IsOnWindow)
144 // Initialize first top
152 if (top.HierarchyDepth != Subscriber[i].HierarchyDepth)
154 Container compare1 = top;
155 Container compare2 = Subscriber[i];
157 // If their depth is different, sync.
158 if (top.HierarchyDepth > Subscriber[i].HierarchyDepth)
160 compare1 = FindParent(compare1, top.HierarchyDepth - Subscriber[i].HierarchyDepth);
164 compare2 = FindParent(compare2, Subscriber[i].HierarchyDepth - top.HierarchyDepth);
167 if (compare1 == compare2)
169 // One is descendant of the other. Descendant is above ancestor.
170 top = top.HierarchyDepth > Subscriber[i].HierarchyDepth ? top : Subscriber[i];
175 top = CompareElevation(compare1, compare2) ? Subscriber[i] : top;
180 top = CompareElevation(top, Subscriber[i]) ? Subscriber[i] : top;
186 top?.EmitBackKeyPressed();