Merge "Add a callback for navigation policy in web engine." into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / accessibility.cpp
1 /*
2  * Copyright 2020  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9
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.
15  */
16
17 // EXTERNAL INCLUDES
18 #include <dali/integration-api/debug.h>
19 #include <dali/public-api/actors/actor.h>
20 #include <dali/public-api/actors/layer.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <dali/public-api/object/object-registry.h>
23 #include <dali/public-api/object/type-info.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <string_view>
26 #include <unordered_map>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
30 #include <dali/devel-api/adaptor-framework/actor-accessible.h>
31 #include <dali/devel-api/adaptor-framework/proxy-accessible.h>
32 #include <dali/devel-api/adaptor-framework/window-devel.h>
33 #include <dali/devel-api/atspi-interfaces/accessible.h>
34 #include <dali/devel-api/atspi-interfaces/collection.h>
35 #include <dali/devel-api/atspi-interfaces/component.h>
36 #include <dali/internal/adaptor/common/adaptor-impl.h>
37 #include <dali/public-api/dali-adaptor-common.h>
38
39 using namespace Dali::Accessibility;
40 using namespace Dali;
41
42 const std::string& Dali::Accessibility::Address::GetBus() const
43 {
44   return mBus.empty() && Bridge::GetCurrentBridge() ? Bridge::GetCurrentBridge()->GetBusName() : mBus;
45 }
46
47 std::string Accessible::GetLocalizedRoleName() const
48 {
49   return GetRoleName();
50 }
51
52 std::string Accessible::GetRoleName() const
53 {
54   static const std::unordered_map<Role, std::string_view> roleMap{
55     {Role::INVALID, "invalid"},
56     {Role::ACCELERATOR_LABEL, "accelerator label"},
57     {Role::ALERT, "alert"},
58     {Role::ANIMATION, "animation"},
59     {Role::ARROW, "arrow"},
60     {Role::CALENDAR, "calendar"},
61     {Role::CANVAS, "canvas"},
62     {Role::CHECK_BOX, "check box"},
63     {Role::CHECK_MENU_ITEM, "check menu item"},
64     {Role::COLOR_CHOOSER, "color chooser"},
65     {Role::COLUMN_HEADER, "column header"},
66     {Role::COMBO_BOX, "combo box"},
67     {Role::DATE_EDITOR, "date editor"},
68     {Role::DESKTOP_ICON, "desktop icon"},
69     {Role::DESKTOP_FRAME, "desktop frame"},
70     {Role::DIAL, "dial"},
71     {Role::DIALOG, "dialog"},
72     {Role::DIRECTORY_PANE, "directory pane"},
73     {Role::DRAWING_AREA, "drawing area"},
74     {Role::FILE_CHOOSER, "file chooser"},
75     {Role::FILLER, "filler"},
76     {Role::FOCUS_TRAVERSABLE, "focus traversable"},
77     {Role::FONT_CHOOSER, "font chooser"},
78     {Role::FRAME, "frame"},
79     {Role::GLASS_PANE, "glass pane"},
80     {Role::HTML_CONTAINER, "html container"},
81     {Role::ICON, "icon"},
82     {Role::IMAGE, "image"},
83     {Role::INTERNAL_FRAME, "internal frame"},
84     {Role::LABEL, "label"},
85     {Role::LAYERED_PANE, "layered pane"},
86     {Role::LIST, "list"},
87     {Role::LIST_ITEM, "list item"},
88     {Role::MENU, "menu"},
89     {Role::MENU_BAR, "menu bar"},
90     {Role::MENU_ITEM, "menu item"},
91     {Role::OPTION_PANE, "option pane"},
92     {Role::PAGE_TAB, "page tab"},
93     {Role::PAGE_TAB_LIST, "page tab list"},
94     {Role::PANEL, "panel"},
95     {Role::PASSWORD_TEXT, "password text"},
96     {Role::POPUP_MENU, "popup menu"},
97     {Role::PROGRESS_BAR, "progress bar"},
98     {Role::PUSH_BUTTON, "push button"},
99     {Role::RADIO_BUTTON, "radio button"},
100     {Role::RADIO_MENU_ITEM, "radio menu item"},
101     {Role::ROOT_PANE, "root pane"},
102     {Role::ROW_HEADER, "row header"},
103     {Role::SCROLL_BAR, "scroll bar"},
104     {Role::SCROLL_PANE, "scroll pane"},
105     {Role::SEPARATOR, "separator"},
106     {Role::SLIDER, "slider"},
107     {Role::SPIN_BUTTON, "spin button"},
108     {Role::SPLIT_PANE, "split pane"},
109     {Role::STATUS_BAR, "status bar"},
110     {Role::TABLE, "table"},
111     {Role::TABLE_CELL, "table cell"},
112     {Role::TABLE_COLUMN_HEADER, "table column header"},
113     {Role::TABLE_ROW_HEADER, "table row header"},
114     {Role::TEAROFF_MENU_ITEM, "tearoff menu item"},
115     {Role::TERMINAL, "terminal"},
116     {Role::TEXT, "text"},
117     {Role::TOGGLE_BUTTON, "toggle button"},
118     {Role::TOOL_BAR, "tool bar"},
119     {Role::TOOL_TIP, "tool tip"},
120     {Role::TREE, "tree"},
121     {Role::TREE_TABLE, "tree table"},
122     {Role::UNKNOWN, "unknown"},
123     {Role::VIEWPORT, "viewport"},
124     {Role::WINDOW, "window"},
125     {Role::EXTENDED, "extended"},
126     {Role::HEADER, "header"},
127     {Role::FOOTER, "footer"},
128     {Role::PARAGRAPH, "paragraph"},
129     {Role::RULER, "ruler"},
130     {Role::APPLICATION, "application"},
131     {Role::AUTOCOMPLETE, "autocomplete"},
132     {Role::EDITBAR, "edit bar"},
133     {Role::EMBEDDED, "embedded"},
134     {Role::ENTRY, "entry"},
135     {Role::CHART, "chart"},
136     {Role::CAPTION, "caution"},
137     {Role::DOCUMENT_FRAME, "document frame"},
138     {Role::HEADING, "heading"},
139     {Role::PAGE, "page"},
140     {Role::SECTION, "section"},
141     {Role::REDUNDANT_OBJECT, "redundant object"},
142     {Role::FORM, "form"},
143     {Role::LINK, "link"},
144     {Role::INPUT_METHOD_WINDOW, "input method window"},
145     {Role::TABLE_ROW, "table row"},
146     {Role::TREE_ITEM, "tree item"},
147     {Role::DOCUMENT_SPREADSHEET, "document spreadsheet"},
148     {Role::DOCUMENT_PRESENTATION, "document presentation"},
149     {Role::DOCUMENT_TEXT, "document text"},
150     {Role::DOCUMENT_WEB, "document web"},
151     {Role::DOCUMENT_EMAIL, "document email"},
152     {Role::COMMENT, "comment"},
153     {Role::LIST_BOX, "list box"},
154     {Role::GROUPING, "grouping"},
155     {Role::IMAGE_MAP, "image map"},
156     {Role::NOTIFICATION, "notification"},
157     {Role::INFO_BAR, "info bar"},
158     {Role::LEVEL_BAR, "level bar"},
159     {Role::TITLE_BAR, "title bar"},
160     {Role::BLOCK_QUOTE, "block quote"},
161     {Role::AUDIO, "audio"},
162     {Role::VIDEO, "video"},
163     {Role::DEFINITION, "definition"},
164     {Role::ARTICLE, "article"},
165     {Role::LANDMARK, "landmark"},
166     {Role::LOG, "log"},
167     {Role::MARQUEE, "marquee"},
168     {Role::MATH, "math"},
169     {Role::RATING, "rating"},
170     {Role::TIMER, "timer"},
171     {Role::STATIC, "static"},
172     {Role::MATH_FRACTION, "math fraction"},
173     {Role::MATH_ROOT, "math root"},
174     {Role::SUBSCRIPT, "subscript"},
175     {Role::SUPERSCRIPT, "superscript"},
176   };
177
178   auto it = roleMap.find(GetRole());
179
180   if(it == roleMap.end())
181   {
182     return {};
183   }
184
185   return std::string{it->second};
186 }
187
188 Dali::Actor Accessible::GetCurrentlyHighlightedActor()
189 {
190   return IsUp() ? Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor : Dali::Actor{};
191 }
192
193 void Accessible::SetCurrentlyHighlightedActor(Dali::Actor actor)
194 {
195   if(IsUp())
196   {
197     Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor = actor;
198   }
199 }
200
201 Dali::Actor Accessible::GetHighlightActor()
202 {
203   return IsUp() ? Bridge::GetCurrentBridge()->mData->mHighlightActor : Dali::Actor{};
204 }
205
206 void Accessible::SetHighlightActor(Dali::Actor actor)
207 {
208   if(IsUp())
209   {
210     Bridge::GetCurrentBridge()->mData->mHighlightActor = actor;
211   }
212 }
213
214 void Bridge::ForceDown()
215 {
216   auto highlighted = Accessible::GetCurrentlyHighlightedActor();
217   if(highlighted)
218   {
219     auto component = dynamic_cast<Component*>(Accessible::Get(highlighted));
220     if(component)
221     {
222       component->ClearHighlight();
223     }
224   }
225   mData = {};
226 }
227
228 void Bridge::SetIsOnRootLevel(Accessible* owner)
229 {
230   owner->mIsOnRootLevel = true;
231 }
232
233 namespace
234 {
235 class AdaptorAccessible : public ActorAccessible
236 {
237 protected:
238   bool mRoot = false;
239
240 public:
241   AdaptorAccessible(Dali::Actor actor, bool isRoot)
242   : ActorAccessible(actor),
243     mRoot(isRoot)
244   {
245   }
246
247   bool GrabFocus() override
248   {
249     return false;
250   }
251
252   bool GrabHighlight() override
253   {
254     return false;
255   }
256
257   bool ClearHighlight() override
258   {
259     return false;
260   }
261
262   Role GetRole() const override
263   {
264     return mRoot ? Role::WINDOW : Role::REDUNDANT_OBJECT;
265   }
266
267   States GetStates() override
268   {
269     States state;
270     if(mRoot)
271     {
272       auto window             = Dali::DevelWindow::Get(Self());
273       auto visible            = window.IsVisible();
274       state[State::ENABLED]   = true;
275       state[State::SENSITIVE] = true;
276       state[State::SHOWING]   = visible;
277       state[State::VISIBLE]   = true;
278       state[State::ACTIVE]    = visible;
279     }
280     else
281     {
282       auto parentState      = GetParent()->GetStates();
283       state[State::SHOWING] = parentState[State::SHOWING];
284       state[State::VISIBLE] = parentState[State::VISIBLE];
285     }
286     return state;
287   }
288
289   Attributes GetAttributes() const override
290   {
291     Dali::TypeInfo type;
292     Self().GetTypeInfo(type);
293     return {
294       {"class", type.GetName()},
295     };
296   }
297
298   bool DoGesture(const GestureInfo& gestureInfo) override
299   {
300     return false;
301   }
302
303   std::vector<Relation> GetRelationSet() override
304   {
305     return {};
306   }
307 }; // AdaptorAccessible
308
309 using AdaptorAccessiblesType = std::unordered_map<const Dali::RefObject*, std::unique_ptr<AdaptorAccessible> >;
310
311 // Save RefObject from an Actor in Accessible::Get()
312 AdaptorAccessiblesType gAdaptorAccessibles;
313
314 std::function<Accessible*(Dali::Actor)> convertingFunctor = [](Dali::Actor) -> Accessible* {
315   return nullptr;
316 };
317
318 ObjectRegistry objectRegistry;
319 } // namespace
320
321 void Accessible::SetObjectRegistry(ObjectRegistry registry)
322 {
323   objectRegistry = registry;
324   objectRegistry.ObjectDestroyedSignal().Connect([](const Dali::RefObject* obj) {
325     gAdaptorAccessibles.erase(obj);
326   });
327 }
328
329 void Accessible::RegisterExternalAccessibleGetter(std::function<Accessible*(Dali::Actor)> functor)
330 {
331   convertingFunctor = functor;
332 }
333
334 Accessible* Accessible::Get(Dali::Actor actor, bool isRoot)
335 {
336   if(!actor)
337   {
338     return nullptr;
339   }
340
341   auto accessible = convertingFunctor(actor);
342   if(!accessible)
343   {
344     auto pair = gAdaptorAccessibles.emplace(&actor.GetBaseObject(), nullptr);
345     if(pair.second)
346     {
347       pair.first->second.reset(new AdaptorAccessible(actor, isRoot));
348     }
349     accessible = pair.first->second.get();
350   }
351
352   return accessible;
353 }