[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / toggle-button-impl.cpp
index 1697b20..1479e35 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -106,10 +106,12 @@ void ToggleButton::OnInitialize()
   Actor self = Self();
   self.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
 
-  DevelControl::SetAccessibilityConstructor(Self(), [](Dali::Actor actor) {
-    return std::unique_ptr<Dali::Accessibility::Accessible>(
-      new AccessibleImpl(actor, Dali::Accessibility::Role::TOGGLE_BUTTON));
-  });
+  self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::TOGGLE_BUTTON);
+}
+
+DevelControl::ControlAccessible* ToggleButton::CreateAccessibleObject()
+{
+  return new ToggleButtonAccessible(Self());
 }
 
 void ToggleButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
@@ -142,11 +144,22 @@ void ToggleButton::SetProperty(BaseObject* object, Property::Index propertyIndex
           std::vector<std::string> tips;
           size_t                   tipsCount = tipArray->Count();
           tips.resize(tipsCount);
+
+          bool valid = true;
           for(size_t i = 0; i != tipsCount; ++i)
           {
-            tipArray->GetElementAt(i).Get(tips[i]);
+            if(DALI_UNLIKELY(!tipArray->GetElementAt(i).Get(tips[i])))
+            {
+              // Given array is invalid. Fast out.
+              valid = false;
+              break;
+            }
+          }
+
+          if(DALI_LIKELY(valid))
+          {
+            toggleButtonImpl.SetToggleTooltips(tips);
           }
-          toggleButtonImpl.SetToggleTooltips(tips);
         }
         break;
       }
@@ -174,8 +187,7 @@ Property::Value ToggleButton::GetProperty(BaseObject* object, Property::Index pr
     {
       case Toolkit::ToggleButton::Property::STATE_VISUALS:
       {
-        Property::Array array = toggleButtonImpl.GetToggleStates();
-        value                 = Property::Value(array);
+        value = toggleButtonImpl.GetToggleStates();
         break;
       }
       case Toolkit::ToggleButton::Property::TOOLTIPS:
@@ -355,7 +367,7 @@ void ToggleButton::OnPressed()
 {
   DALI_LOG_INFO(gLogButtonFilter, Debug::General, "ToggleButton::OnPressed\n");
   // State index will add 1 only when button is pressed.
-  mCurrentToggleIndex = (mCurrentToggleIndex + 1) % mToggleVisuals.size();
+  mCurrentToggleIndex = (mCurrentToggleIndex + 1) % static_cast<uint32_t>(mToggleVisuals.size());
 
   // Both create SelectedVisual and UnselectedVisual
   PrepareVisual(Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex]);
@@ -372,9 +384,9 @@ void ToggleButton::OnPressed()
   RelayoutRequest();
 }
 
-Dali::Accessibility::States ToggleButton::AccessibleImpl::CalculateStates()
+Dali::Accessibility::States ToggleButton::ToggleButtonAccessible::CalculateStates()
 {
-  auto states = Button::AccessibleImpl::CalculateStates();
+  auto states = Button::ButtonAccessible::CalculateStates();
   auto button = Toolkit::ToggleButton::DownCast(Self());
   if(button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX))
   {
@@ -383,7 +395,7 @@ Dali::Accessibility::States ToggleButton::AccessibleImpl::CalculateStates()
   return states;
 }
 
-std::string ToggleButton::AccessibleImpl::GetDescriptionRaw()
+std::string ToggleButton::ToggleButtonAccessible::GetDescriptionRaw() const
 {
   auto button   = Toolkit::ToggleButton::DownCast(Self());
   auto index    = button.GetProperty<int>(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX);
@@ -392,7 +404,7 @@ std::string ToggleButton::AccessibleImpl::GetDescriptionRaw()
   return tooltips[index].Get<std::string>();
 }
 
-Property::Index ToggleButton::AccessibleImpl::GetDescriptionPropertyIndex()
+Property::Index ToggleButton::ToggleButtonAccessible::GetDescriptionPropertyIndex()
 {
   return Toolkit::ToggleButton::Property::TOOLTIPS;
 }
@@ -400,11 +412,14 @@ Property::Index ToggleButton::AccessibleImpl::GetDescriptionPropertyIndex()
 void ToggleButton::OnStateChange(State newState)
 {
   // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
-  if(Dali::Accessibility::IsUp() && (Self() == Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor())
-     && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
+  if(newState == SELECTED_STATE || newState == UNSELECTED_STATE)
   {
-    Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(Dali::Accessibility::State::CHECKED, mCurrentToggleIndex ? 1 : 0, 0);
-    Dali::Accessibility::Accessible::Get(Self())->Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
+    auto* accessible = GetAccessibleObject();
+    if(DALI_LIKELY(accessible) && accessible->IsHighlighted())
+    {
+      accessible->EmitStateChanged(Dali::Accessibility::State::CHECKED, mCurrentToggleIndex ? 1 : 0, 0);
+      accessible->Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
+    }
   }
 }