[AT-SPI] Require ControlAccessible for Control
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / toggle-button-impl.cpp
1 /*
2  * Copyright (c) 2021 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
18 // CLASS HEADER
19 #include "toggle-button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/scripting/scripting.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/property-array.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/public-api/object/type-registry.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/devel-api/controls/control-devel.h>
31 #include <dali-toolkit/devel-api/controls/tooltip/tooltip-properties.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali-toolkit/public-api/align-enumerations.h>
34 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
35 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
36 #include <dali-toolkit/public-api/visuals/visual-properties.h>
37
38 #if defined(DEBUG_ENABLED)
39 extern Debug::Filter* gLogButtonFilter;
40 #endif
41
42 namespace Dali
43 {
44 namespace Toolkit
45 {
46 namespace Internal
47 {
48 namespace
49 {
50 BaseHandle Create()
51 {
52   return Toolkit::ToggleButton::New();
53 }
54
55 // Properties
56 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::ToggleButton, Toolkit::Button, Create)
57
58 DALI_PROPERTY_REGISTRATION(Toolkit, ToggleButton, "stateVisuals", ARRAY, STATE_VISUALS)
59 DALI_PROPERTY_REGISTRATION(Toolkit, ToggleButton, "tooltips", ARRAY, TOOLTIPS)
60 DALI_PROPERTY_REGISTRATION(Toolkit, ToggleButton, "currentStateIndex", INTEGER, CURRENT_STATE_INDEX)
61
62 DALI_TYPE_REGISTRATION_END()
63
64 } // unnamed namespace
65
66 Dali::Toolkit::ToggleButton ToggleButton::New()
67 {
68   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::New\n");
69   // Create the implementation, temporarily owned on stack
70   IntrusivePtr<ToggleButton> internalToggleButton = new ToggleButton();
71
72   // Pass ownership to CustomActor
73   Dali::Toolkit::ToggleButton toggleButton(*internalToggleButton);
74
75   // Second-phase init of the implementation
76   // This can only be done after the CustomActor connection has been made...
77   internalToggleButton->Initialize();
78
79   return toggleButton;
80 }
81
82 ToggleButton::ToggleButton()
83 : Button(),
84   mToggleStates(),
85   mToggleVisuals(),
86   mToggleSelectedVisuals(),
87   mToggleDisabledVisuals(),
88   mToggleDisabledSelectedVisuals(),
89   mToggleTooltips(),
90   mCurrentToggleIndex(0)
91 {
92   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::Constructor\n");
93   SetTogglableButton(false);
94 }
95
96 ToggleButton::~ToggleButton()
97 {
98 }
99
100 void ToggleButton::OnInitialize()
101 {
102   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnInitialize\n");
103   Button::OnInitialize();
104
105   // Toggle button requires the Leave event.
106   Actor self = Self();
107   self.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
108
109   DevelControl::SetAccessibilityConstructor(Self(), [](Dali::Actor actor) {
110     return std::make_unique<ToggleButtonAccessible>(actor, Dali::Accessibility::Role::TOGGLE_BUTTON);
111   });
112 }
113
114 void ToggleButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
115 {
116   Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast(Dali::BaseHandle(object));
117
118   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::SetProperty index[%d]\n", propertyIndex);
119
120   if(toggleButton)
121   {
122     ToggleButton& toggleButtonImpl(GetImplementation(toggleButton));
123
124     switch(propertyIndex)
125     {
126       case Toolkit::ToggleButton::Property::STATE_VISUALS:
127       {
128         Property::Array stateArray;
129         if(value.Get(stateArray))
130         {
131           toggleButtonImpl.SetToggleStates(stateArray);
132         }
133
134         break;
135       }
136       case Toolkit::ToggleButton::Property::TOOLTIPS:
137       {
138         const Property::Array* tipArray = value.GetArray();
139         if(tipArray)
140         {
141           std::vector<std::string> tips;
142           size_t                   tipsCount = tipArray->Count();
143           tips.resize(tipsCount);
144           for(size_t i = 0; i != tipsCount; ++i)
145           {
146             tipArray->GetElementAt(i).Get(tips[i]);
147           }
148           toggleButtonImpl.SetToggleTooltips(tips);
149         }
150         break;
151       }
152       default:
153       {
154         break;
155       }
156     } // end of switch
157   }
158 }
159
160 Property::Value ToggleButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
161 {
162   Property::Value value;
163
164   Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast(Dali::BaseHandle(object));
165
166   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::GetProperty index[%d]\n", propertyIndex);
167
168   if(toggleButton)
169   {
170     ToggleButton& toggleButtonImpl(GetImplementation(toggleButton));
171
172     switch(propertyIndex)
173     {
174       case Toolkit::ToggleButton::Property::STATE_VISUALS:
175       {
176         Property::Array array = toggleButtonImpl.GetToggleStates();
177         value                 = Property::Value(array);
178         break;
179       }
180       case Toolkit::ToggleButton::Property::TOOLTIPS:
181       {
182         Property::Value  value1(Property::ARRAY);
183         Property::Array* tipArray = value1.GetArray();
184
185         if(tipArray)
186         {
187           std::vector<std::string> tips = toggleButtonImpl.GetToggleTooltips();
188           size_t                   tipsCount(tips.size());
189           for(size_t i(0); i != tipsCount; ++i)
190           {
191             tipArray->PushBack(tips[i]);
192           }
193         }
194         value = value1;
195         break;
196       }
197       case Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX:
198       {
199         value = static_cast<int>(toggleButtonImpl.mCurrentToggleIndex);
200         break;
201       }
202     } // end of switch
203   }
204
205   return value;
206 }
207
208 void ToggleButton::CreateVisualsForAllStates(const Property::Array& states, std::vector<Toolkit::Visual::Base>& visuals)
209 {
210   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::CreateVisualsForAllStates\n");
211
212   visuals.clear();
213
214   for(unsigned int i = 0; i < states.Count(); i++)
215   {
216     Property::Value value(states[i]);
217
218     Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
219     Toolkit::Visual::Base  stateVisual;
220
221     if(value.GetType() == Property::MAP)
222     {
223       Property::Map* map = value.GetMap();
224       if(map && !map->Empty()) // Empty map results in current visual removal.
225       {
226         DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using Map\n");
227         stateVisual = visualFactory.CreateVisual(*map);
228       }
229     }
230     else if(value.GetType() == Property::STRING)
231     {
232       std::string imageUrl = value.Get<std::string>();
233       DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using image URL\n");
234       if(!imageUrl.empty())
235       {
236         stateVisual = visualFactory.CreateVisual(imageUrl, ImageDimensions());
237       }
238     }
239
240     if(stateVisual)
241     {
242       stateVisual.SetDepthIndex(DepthIndex::CONTENT);
243       visuals.push_back(stateVisual);
244     }
245   } // end of for
246   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals mToggleVisuals:%d\n", mToggleVisuals.size());
247 }
248
249 void ToggleButton::SetToggleStates(const Property::Array& states)
250 { //this should really be generalized to be either string or maps so that any visual can be created.
251   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::SetToggleStates\n");
252   if(!states.Empty())
253   {
254     mToggleStates.Clear();
255     mToggleStates = states;
256     /* New toggle button index from 0. */
257     mCurrentToggleIndex = 0;
258
259     // Create all visuals, save to mToggleVisuals.
260     CreateVisualsForAllStates(states, mToggleVisuals);
261     CreateVisualsForAllStates(states, mToggleSelectedVisuals);
262     CreateVisualsForAllStates(states, mToggleDisabledVisuals);
263     CreateVisualsForAllStates(states, mToggleDisabledSelectedVisuals);
264
265     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::Began to register visual.\n");
266
267     PrepareVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex]);
268     PrepareVisual(Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex]);
269     PrepareVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex]);
270     PrepareVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex]);
271
272     RelayoutRequest();
273   }
274 }
275
276 Property::Array ToggleButton::GetToggleStates() const
277 {
278   return mToggleStates;
279 }
280
281 void ToggleButton::SetToggleTooltips(std::vector<std::string>& tips)
282 {
283   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::SetToggleTooltips\n");
284   if(!tips.empty())
285   {
286     mToggleTooltips.clear();
287     mToggleTooltips.swap(tips);
288   }
289
290   if(!mToggleTooltips.empty() && (mCurrentToggleIndex < mToggleTooltips.size()))
291   {
292     Self().SetProperty(Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex]);
293   }
294
295   RelayoutRequest();
296 }
297
298 const std::vector<std::string>& ToggleButton::GetToggleTooltips() const
299 {
300   return mToggleTooltips;
301 }
302
303 void ToggleButton::PrepareVisual(Property::Index index, Toolkit::Visual::Base& visual)
304 {
305   bool enabled = false; // Disabled by default
306
307   // Unregister the visual with the given index if registered previously
308   if(DevelControl::GetVisual(*this, index))
309   {
310     // Check whether it was enabled to ensure we do the same with the new visual we're registering
311     enabled = DevelControl::IsVisualEnabled(*this, index);
312     DevelControl::UnregisterVisual(*this, index);
313   }
314
315   DevelControl::RegisterVisual(*this, index, visual, enabled);
316 }
317
318 void ToggleButton::RelayoutVisual(Property::Index index, const Vector2& size)
319 {
320   Toolkit::Visual::Base visual = DevelControl::GetVisual(*this, index);
321   if(visual)
322   {
323     Size    visualSize     = Size::ZERO;
324     Vector2 visualPosition = Vector2::ZERO;
325
326     visual.GetNaturalSize(visualSize);
327
328     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual size to(%f,%f)\n", visualSize.width, visualSize.height);
329     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual position to(%f,%f)\n", visualPosition.x, visualPosition.y);
330
331     Property::Map visualTransform;
332     visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, visualSize)
333       .Add(Toolkit::Visual::Transform::Property::OFFSET, visualPosition)
334       .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
335       .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
336       .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER)
337       .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER);
338
339     visual.SetTransformAndSize(visualTransform, size);
340   }
341 }
342
343 void ToggleButton::OnRelayout(const Vector2& size, RelayoutContainer& container)
344 {
345   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout targetSize(%f,%f) ptr(%p)\n", size.width, size.height, this);
346
347   RelayoutVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, size);
348   RelayoutVisual(Toolkit::Button::Property::SELECTED_VISUAL, size);
349   RelayoutVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, size);
350   RelayoutVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, size);
351 }
352
353 void ToggleButton::OnPressed()
354 {
355   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnPressed\n");
356   // State index will add 1 only when button is pressed.
357   mCurrentToggleIndex = (mCurrentToggleIndex + 1) % mToggleVisuals.size();
358
359   // Both create SelectedVisual and UnselectedVisual
360   PrepareVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex]);
361   PrepareVisual(Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex]);
362   PrepareVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex]);
363   PrepareVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex]);
364
365   //Need to check mCurrentToggleIndex, it must less than the size of mToggleTooltips.
366   if(!mToggleTooltips.empty() && (mCurrentToggleIndex < mToggleTooltips.size()))
367   {
368     Self().SetProperty(Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex]);
369   }
370
371   RelayoutRequest();
372 }
373
374 Dali::Accessibility::States ToggleButton::ToggleButtonAccessible::CalculateStates()
375 {
376   auto states = Button::ButtonAccessible::CalculateStates();
377   auto button = Toolkit::ToggleButton::DownCast(Self());
378   if(button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX))
379   {
380     states[Dali::Accessibility::State::CHECKED] = true;
381   }
382   return states;
383 }
384
385 std::string ToggleButton::ToggleButtonAccessible::GetDescriptionRaw() const
386 {
387   auto button   = Toolkit::ToggleButton::DownCast(Self());
388   auto index    = button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX);
389   auto tooltips = button.GetProperty<Property::Array>(Toolkit::ToggleButton::Property::TOOLTIPS);
390
391   return tooltips[index].Get<std::string>();
392 }
393
394 Property::Index ToggleButton::ToggleButtonAccessible::GetDescriptionPropertyIndex()
395 {
396   return Toolkit::ToggleButton::Property::TOOLTIPS;
397 }
398
399 void ToggleButton::OnStateChange(State newState)
400 {
401   // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
402   if((Self() == Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor()) && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
403   {
404     auto* accessible = GetAccessibleObject();
405
406     accessible->EmitStateChanged(Dali::Accessibility::State::CHECKED, mCurrentToggleIndex ? 1 : 0, 0);
407     accessible->Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
408   }
409 }
410
411 } // namespace Internal
412
413 } // namespace Toolkit
414
415 } // namespace Dali