[dali_2.3.19] Merge branch 'devel/master'
[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::unique_ptr<Dali::Accessibility::Accessible>(
111       new AccessibleImpl(actor, Dali::Accessibility::Role::TOGGLE_BUTTON));
112   });
113 }
114
115 void ToggleButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
116 {
117   Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast(Dali::BaseHandle(object));
118
119   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::SetProperty index[%d]\n", propertyIndex);
120
121   if(toggleButton)
122   {
123     ToggleButton& toggleButtonImpl(GetImplementation(toggleButton));
124
125     switch(propertyIndex)
126     {
127       case Toolkit::ToggleButton::Property::STATE_VISUALS:
128       {
129         Property::Array stateArray;
130         if(value.Get(stateArray))
131         {
132           toggleButtonImpl.SetToggleStates(stateArray);
133         }
134
135         break;
136       }
137       case Toolkit::ToggleButton::Property::TOOLTIPS:
138       {
139         const Property::Array* tipArray = value.GetArray();
140         if(tipArray)
141         {
142           std::vector<std::string> tips;
143           size_t                   tipsCount = tipArray->Count();
144           tips.resize(tipsCount);
145           for(size_t i = 0; i != tipsCount; ++i)
146           {
147             tipArray->GetElementAt(i).Get(tips[i]);
148           }
149           toggleButtonImpl.SetToggleTooltips(tips);
150         }
151         break;
152       }
153       default:
154       {
155         break;
156       }
157     } // end of switch
158   }
159 }
160
161 Property::Value ToggleButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
162 {
163   Property::Value value;
164
165   Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast(Dali::BaseHandle(object));
166
167   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::GetProperty index[%d]\n", propertyIndex);
168
169   if(toggleButton)
170   {
171     ToggleButton& toggleButtonImpl(GetImplementation(toggleButton));
172
173     switch(propertyIndex)
174     {
175       case Toolkit::ToggleButton::Property::STATE_VISUALS:
176       {
177         Property::Array array = toggleButtonImpl.GetToggleStates();
178         value                 = Property::Value(array);
179         break;
180       }
181       case Toolkit::ToggleButton::Property::TOOLTIPS:
182       {
183         Property::Value  value1(Property::ARRAY);
184         Property::Array* tipArray = value1.GetArray();
185
186         if(tipArray)
187         {
188           std::vector<std::string> tips = toggleButtonImpl.GetToggleTooltips();
189           size_t                   tipsCount(tips.size());
190           for(size_t i(0); i != tipsCount; ++i)
191           {
192             tipArray->PushBack(tips[i]);
193           }
194         }
195         value = value1;
196         break;
197       }
198       case Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX:
199       {
200         value = static_cast<int>(toggleButtonImpl.mCurrentToggleIndex);
201         break;
202       }
203     } // end of switch
204   }
205
206   return value;
207 }
208
209 void ToggleButton::CreateVisualsForAllStates(const Property::Array& states, std::vector<Toolkit::Visual::Base>& visuals)
210 {
211   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::CreateVisualsForAllStates\n");
212
213   visuals.clear();
214
215   for(unsigned int i = 0; i < states.Count(); i++)
216   {
217     Property::Value value(states[i]);
218
219     Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
220     Toolkit::Visual::Base  stateVisual;
221
222     if(value.GetType() == Property::MAP)
223     {
224       Property::Map* map = value.GetMap();
225       if(map && !map->Empty()) // Empty map results in current visual removal.
226       {
227         DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using Map\n");
228         stateVisual = visualFactory.CreateVisual(*map);
229       }
230     }
231     else if(value.GetType() == Property::STRING)
232     {
233       std::string imageUrl = value.Get<std::string>();
234       DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using image URL\n");
235       if(!imageUrl.empty())
236       {
237         stateVisual = visualFactory.CreateVisual(imageUrl, ImageDimensions());
238       }
239     }
240
241     if(stateVisual)
242     {
243       stateVisual.SetDepthIndex(DepthIndex::CONTENT);
244       visuals.push_back(stateVisual);
245     }
246   } // end of for
247   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals mToggleVisuals:%d\n", mToggleVisuals.size());
248 }
249
250 void ToggleButton::SetToggleStates(const Property::Array& states)
251 { //this should really be generalized to be either string or maps so that any visual can be created.
252   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::SetToggleStates\n");
253   if(!states.Empty())
254   {
255     mToggleStates.Clear();
256     mToggleStates = states;
257     /* New toggle button index from 0. */
258     mCurrentToggleIndex = 0;
259
260     // Create all visuals, save to mToggleVisuals.
261     CreateVisualsForAllStates(states, mToggleVisuals);
262     CreateVisualsForAllStates(states, mToggleSelectedVisuals);
263     CreateVisualsForAllStates(states, mToggleDisabledVisuals);
264     CreateVisualsForAllStates(states, mToggleDisabledSelectedVisuals);
265
266     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::Began to register visual.\n");
267
268     PrepareVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex]);
269     PrepareVisual(Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex]);
270     PrepareVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex]);
271     PrepareVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex]);
272
273     RelayoutRequest();
274   }
275 }
276
277 Property::Array ToggleButton::GetToggleStates() const
278 {
279   return mToggleStates;
280 }
281
282 void ToggleButton::SetToggleTooltips(std::vector<std::string>& tips)
283 {
284   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::SetToggleTooltips\n");
285   if(!tips.empty())
286   {
287     mToggleTooltips.clear();
288     mToggleTooltips.swap(tips);
289   }
290
291   if(!mToggleTooltips.empty() && (mCurrentToggleIndex < mToggleTooltips.size()))
292   {
293     Self().SetProperty(Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex]);
294   }
295
296   RelayoutRequest();
297 }
298
299 const std::vector<std::string>& ToggleButton::GetToggleTooltips() const
300 {
301   return mToggleTooltips;
302 }
303
304 void ToggleButton::PrepareVisual(Property::Index index, Toolkit::Visual::Base& visual)
305 {
306   bool enabled = false; // Disabled by default
307
308   // Unregister the visual with the given index if registered previously
309   if(DevelControl::GetVisual(*this, index))
310   {
311     // Check whether it was enabled to ensure we do the same with the new visual we're registering
312     enabled = DevelControl::IsVisualEnabled(*this, index);
313     DevelControl::UnregisterVisual(*this, index);
314   }
315
316   DevelControl::RegisterVisual(*this, index, visual, enabled);
317 }
318
319 void ToggleButton::RelayoutVisual(Property::Index index, const Vector2& size)
320 {
321   Toolkit::Visual::Base visual = DevelControl::GetVisual(*this, index);
322   if(visual)
323   {
324     Size    visualSize     = Size::ZERO;
325     Vector2 visualPosition = Vector2::ZERO;
326
327     visual.GetNaturalSize(visualSize);
328
329     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual size to(%f,%f)\n", visualSize.width, visualSize.height);
330     DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual position to(%f,%f)\n", visualPosition.x, visualPosition.y);
331
332     Property::Map visualTransform;
333     visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, visualSize)
334       .Add(Toolkit::Visual::Transform::Property::OFFSET, visualPosition)
335       .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
336       .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
337       .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER)
338       .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER);
339
340     visual.SetTransformAndSize(visualTransform, size);
341   }
342 }
343
344 void ToggleButton::OnRelayout(const Vector2& size, RelayoutContainer& container)
345 {
346   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout targetSize(%f,%f) ptr(%p)\n", size.width, size.height, this);
347
348   RelayoutVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, size);
349   RelayoutVisual(Toolkit::Button::Property::SELECTED_VISUAL, size);
350   RelayoutVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, size);
351   RelayoutVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, size);
352 }
353
354 void ToggleButton::OnPressed()
355 {
356   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnPressed\n");
357   // State index will add 1 only when button is pressed.
358   mCurrentToggleIndex = (mCurrentToggleIndex + 1) % mToggleVisuals.size();
359
360   // Both create SelectedVisual and UnselectedVisual
361   PrepareVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex]);
362   PrepareVisual(Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex]);
363   PrepareVisual(Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex]);
364   PrepareVisual(Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex]);
365
366   //Need to check mCurrentToggleIndex, it must less than the size of mToggleTooltips.
367   if(!mToggleTooltips.empty() && (mCurrentToggleIndex < mToggleTooltips.size()))
368   {
369     Self().SetProperty(Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex]);
370   }
371
372   RelayoutRequest();
373 }
374
375 Dali::Accessibility::States ToggleButton::AccessibleImpl::CalculateStates()
376 {
377   auto states = Button::AccessibleImpl::CalculateStates();
378   auto button = Toolkit::ToggleButton::DownCast(Self());
379   if(button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX))
380     states[Dali::Accessibility::State::CHECKED] = true;
381   return states;
382 }
383
384 std::string ToggleButton::AccessibleImpl::GetDescriptionRaw()
385 {
386   auto button   = Toolkit::ToggleButton::DownCast(Self());
387   auto index    = button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX);
388   auto tooltips = button.GetProperty<Property::Array>(Toolkit::ToggleButton::Property::TOOLTIPS);
389
390   return tooltips[index].Get<std::string>();
391 }
392
393 Property::Index ToggleButton::AccessibleImpl::GetDescriptionPropertyIndex()
394 {
395   return Toolkit::ToggleButton::Property::TOOLTIPS;
396 }
397
398 void ToggleButton::OnStateChange(State newState)
399 {
400   // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
401   if(Dali::Accessibility::IsUp() && (Self() == Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor())
402      && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
403   {
404     Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
405       Dali::Accessibility::State::CHECKED, mCurrentToggleIndex ? 1 : 0, 0);
406       Dali::Accessibility::Accessible::Get(Self())->Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
407   }
408 }
409
410 } // namespace Internal
411
412 } // namespace Toolkit
413
414 } // namespace Dali