/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/common/dom/dom_stepper_item.h" #include "core/components/focus_animation/focus_animation_theme.h" #include "core/components/stepper/stepper_theme.h" #include "frameworks/bridge/common/utils/utils.h" namespace OHOS::Ace::Framework { namespace { constexpr double DEFAULT_OPACITY = 1.0; } // namespace DOMStepperItem::DOMStepperItem(NodeId nodeId, const std::string& nodeName, int32_t index) : DOMNode(nodeId, nodeName) { itemIndex_ = index; stepperItemComponent_ = AceType::MakeRefPtr(RefPtr()); } bool DOMStepperItem::SetSpecializedStyle(const std::pair& style) { static const LinearMapNode stepperItemStyleOperators[] = { { DOM_ALIGN_ITEMS, [](const std::string& val, DOMStepperItem& stepperItem) { stepperItem.flexCrossAlign_ = ConvertStrToFlexAlign(val); } }, { DOM_TEXT_ALLOW_SCALE, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetAllowScale(StringToBool(value)); } }, { DOM_STEPPER_TEXT_COLOR, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetTextColor(stepperItem.ParseColor(value)); } }, { DOM_FLEX_DIRECTION, [](const std::string& val, DOMStepperItem& stepperItem) { stepperItem.flexDirection_ = ConvertStrToFlexDirection(val); } }, { DOM_STEPPER_FONT_FAMILY, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetFontFamilies(ConvertStrToFontFamilies(value)); } }, { DOM_STEPPER_FONT_SIZE, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetFontSize(stepperItem.ParseDimension(value)); } }, { DOM_STEPPER_FONT_STYLE, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetFontStyle(ConvertStrToFontStyle(value)); } }, { DOM_STEPPER_FONT_WEIGHT, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetFontWeight(ConvertStrToFontWeight(value)); } }, { DOM_JUSTIFY_CONTENT, [](const std::string& val, DOMStepperItem& stepperItem) { stepperItem.flexMainAlign_ = ConvertStrToFlexAlign(val); } }, { DOM_STEPPER_TEXT_DECORATION, [](const std::string& value, DOMStepperItem& stepperItem) { stepperItem.textStyle_.SetTextDecoration(ConvertStrToTextDecoration(value)); } }, }; auto operatorIter = BinarySearchFindIndex(stepperItemStyleOperators, ArraySize(stepperItemStyleOperators), style.first.c_str()); if (operatorIter != -1) { stepperItemStyleOperators[operatorIter].value(style.second, *this); return true; } return false; } void DOMStepperItem::InitializeStyle() { auto theme = GetTheme(); if (!theme) { return; } textStyle_ = theme->GetTextStyle(); textStyle_.SetAdaptTextSize(theme->GetTextStyle().GetFontSize(), theme->GetMinFontSize()); textStyle_.SetMaxLines(theme->GetTextMaxLines()); textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS); } bool DOMStepperItem::AddSpecializedEvent(int32_t pageId, const std::string& event) { if (event == DOM_STEPPER_ITEM_EVENT_APPEAR) { appearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId); stepperItemComponent_->SetAppearEventId(appearEventId_); return true; } else if (event == DOM_STEPPER_ITEM_EVENT_DISAPPEAR) { disappearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId); stepperItemComponent_->SetDisappearEventId(disappearEventId_); return true; } else { return false; } } void DOMStepperItem::AddStepperItem(const RefPtr& node, int32_t slot) { const auto& childComponent = node->GetRootComponent(); if (!flexComponent_) { flexComponent_ = AceType::MakeRefPtr( flexDirection_, flexMainAlign_, flexCrossAlign_, std::list>()); } flexComponent_->InsertChild(slot, childComponent); Component::MergeRSNode(childComponent, flexComponent_); } void DOMStepperItem::RemoveStepperItem(const RefPtr& node) { const auto& childComponent = node->GetRootComponent(); if (flexComponent_) { flexComponent_->RemoveChild(childComponent); } } void DOMStepperItem::OnChildNodeAdded(const RefPtr& child, int32_t slot) { if (child) { AddStepperItem(child, slot); } } void DOMStepperItem::OnChildNodeRemoved(const RefPtr& child) { if (child) { RemoveStepperItem(child); } } void DOMStepperItem::PrepareSpecializedComponent() { if (!stepperItemComponent_) { stepperItemComponent_ = AceType::MakeRefPtr(RefPtr()); } stepperItemComponent_->SetLabel(GetLabel()); stepperItemComponent_->SetTextStyle(textStyle_); ResetInitializedStyle(); if (flexComponent_) { flexComponent_->SetDirection(flexDirection_); flexComponent_->SetMainAxisAlign(flexMainAlign_); flexComponent_->SetCrossAxisAlign(flexCrossAlign_); } else { flexComponent_ = AceType::MakeRefPtr( flexDirection_, flexMainAlign_, flexCrossAlign_, std::list>()); flexComponent_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR); } if (displayComponent_) { displayComponent_->SetVisible(VisibleType::VISIBLE); displayComponent_->SetOpacity(DEFAULT_OPACITY); } else { displayComponent_ = AceType::MakeRefPtr(); } } RefPtr DOMStepperItem::CompositeSpecializedComponent(const std::vector>& components) { auto scroll = AceType::MakeRefPtr(flexComponent_); scroll->SetTakeBoundary(false); RefPtr component = scroll; if (!components.empty()) { const auto& parent = components.back(); if (parent) { parent->SetChild(component); } component = AceType::DynamicCast(components.front()); } if (stepperItemComponent_) { if (displayComponent_) { displayComponent_->SetChild(component); stepperItemComponent_->SetChild(displayComponent_); } else { stepperItemComponent_->SetChild(component); } } return stepperItemComponent_; } void DOMStepperItem::ResetInitializedStyle() { if (!stepperItemComponent_) { return; } auto theme = GetTheme(); if (theme) { stepperItemComponent_->SetFocusAnimationColor(theme->GetColor()); } } } // namespace OHOS::Ace::Framework