From 53a704786b44d158bc2fedee571ba1645029c3e9 Mon Sep 17 00:00:00 2001 From: yaoyuchi Date: Thu, 13 Jan 2022 16:41:55 +0800 Subject: [PATCH 1/3] add select inspector Signed-off-by: yaoyuchi --- .../core/components/select/render_select.h | 5 + .../core/components/select/select_component.h | 14 +- .../core/components_v2/inspector/BUILD.gn | 1 + .../inspector_composed_component.cpp | 8 +- .../inspector/inspector_constants.cpp | 4 + .../inspector/inspector_constants.h | 4 + .../inspector/select_composed_element.cpp | 294 ++++++++++++++++++ .../inspector/select_composed_element.h | 50 +++ 8 files changed, 376 insertions(+), 4 deletions(-) create mode 100644 frameworks/core/components_v2/inspector/select_composed_element.cpp create mode 100644 frameworks/core/components_v2/inspector/select_composed_element.h diff --git a/frameworks/core/components/select/render_select.h b/frameworks/core/components/select/render_select.h index 8d9cf841..3af55cdb 100644 --- a/frameworks/core/components/select/render_select.h +++ b/frameworks/core/components/select/render_select.h @@ -39,6 +39,11 @@ public: } } + RefPtr GetSelectComponent() const + { + return data_; + } + void SetFocusCallback(const std::function& value) { onFocusCallback_ = value; diff --git a/frameworks/core/components/select/select_component.h b/frameworks/core/components/select/select_component.h index 8dc5b61f..e3e146c8 100644 --- a/frameworks/core/components/select/select_component.h +++ b/frameworks/core/components/select/select_component.h @@ -244,10 +244,15 @@ public: if (!popup_) { return nullptr; } - + selected_ = index; return popup_->GetSelectOption(index); } + std::size_t GetSelected() const + { + return selected_; + } + void ClearAllOptions() { if (popup_) { @@ -346,6 +351,11 @@ public: } ACE_DEFINE_COMPONENT_EVENT(OnSelected, void(int32_t)); + RefPtr GetSelectPopupComponent() const + { + return popup_; + } + private: bool disabled_ { false }; bool clicked_ { false }; @@ -356,7 +366,7 @@ private: RefPtr theme_; std::function flushRefreshCallback_; Color backgroundColor_ = Color::TRANSPARENT; - + std::size_t selected_ = 0; // used for inspector node in PC preview int32_t nodeId_ = -1; diff --git a/frameworks/core/components_v2/inspector/BUILD.gn b/frameworks/core/components_v2/inspector/BUILD.gn index ce64e095..0a71b874 100644 --- a/frameworks/core/components_v2/inspector/BUILD.gn +++ b/frameworks/core/components_v2/inspector/BUILD.gn @@ -53,6 +53,7 @@ build_component("inspector_v2") { "scroll_bar_composed_element.cpp", "scroll_composed_element.cpp", "search_composed_element.cpp", + "select_composed_element.cpp", "shape_composed_element.cpp", "shape_container_composed_element.cpp", "sheet_composed_element.cpp", diff --git a/frameworks/core/components_v2/inspector/inspector_composed_component.cpp b/frameworks/core/components_v2/inspector/inspector_composed_component.cpp index d046b1b8..f228e0be 100644 --- a/frameworks/core/components_v2/inspector/inspector_composed_component.cpp +++ b/frameworks/core/components_v2/inspector/inspector_composed_component.cpp @@ -54,6 +54,7 @@ #include "core/components_v2/inspector/scroll_bar_composed_element.h" #include "core/components_v2/inspector/scroll_composed_element.h" #include "core/components_v2/inspector/search_composed_element.h" +#include "core/components_v2/inspector/select_composed_element.h" #include "core/components_v2/inspector/shape_composed_element.h" #include "core/components_v2/inspector/shape_container_composed_element.h" #include "core/components_v2/inspector/sheet_composed_element.h" @@ -187,7 +188,9 @@ const std::unordered_map CREATE_ELEMENT_MAP { { TEXTAREA_COMPONENT_TAG, [](const std::string& id) {return AceType::MakeRefPtr(id); } }, { TEXTINPUT_COMPONENT_TAG, - [](const std::string& id) {return AceType::MakeRefPtr(id); } } + [](const std::string& id) {return AceType::MakeRefPtr(id); } }, + { SELECT_COMPONENT_TAG, + [](const std::string& id) {return AceType::MakeRefPtr(id); } } }; } // namespace @@ -248,7 +251,8 @@ const std::unordered_map COMPONENT_TAG_TO_ETS_TAG_MAP { MENU_COMPONENT_TAG, MENU_ETS_TAG }, { MENU_TAG, MENU_ETS_TAG }, { TEXTAREA_COMPONENT_TAG, TEXTAREA_ETS_TAG }, - { TEXTINPUT_COMPONENT_TAG, TEXTINPUT_ETS_TAG } + { TEXTINPUT_COMPONENT_TAG, TEXTINPUT_ETS_TAG }, + { SELECT_COMPONENT_TAG, SELECT_ETS_TAG } }; RefPtr InspectorComposedComponent::CreateElement() diff --git a/frameworks/core/components_v2/inspector/inspector_constants.cpp b/frameworks/core/components_v2/inspector/inspector_constants.cpp index 6ed1b0bc..a4642f13 100644 --- a/frameworks/core/components_v2/inspector/inspector_constants.cpp +++ b/frameworks/core/components_v2/inspector/inspector_constants.cpp @@ -264,4 +264,8 @@ ACE_EXPORT extern const char TEXTAREA_ETS_TAG[] = "TextArea"; ACE_EXPORT extern const char TEXTINPUT_COMPONENT_TAG[] = "TextInput"; ACE_EXPORT extern const char TEXTINPUT_ETS_TAG[] = "TextInput"; +// select +ACE_EXPORT extern const char SELECT_COMPONENT_TAG[] = "SelectComponent"; +ACE_EXPORT extern const char SELECT_ETS_TAG[] = "Select"; + } // namespace OHOS::Ace::V2 diff --git a/frameworks/core/components_v2/inspector/inspector_constants.h b/frameworks/core/components_v2/inspector/inspector_constants.h index e525925e..ab58f836 100644 --- a/frameworks/core/components_v2/inspector/inspector_constants.h +++ b/frameworks/core/components_v2/inspector/inspector_constants.h @@ -275,6 +275,10 @@ ACE_EXPORT extern const char TEXTAREA_ETS_TAG[]; ACE_EXPORT extern const char TEXTINPUT_COMPONENT_TAG[]; ACE_EXPORT extern const char TEXTINPUT_ETS_TAG[]; +// select +ACE_EXPORT extern const char SELECT_COMPONENT_TAG[]; +ACE_EXPORT extern const char SELECT_ETS_TAG[]; + } // namespace OHOS::Ace::V2 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_INSPECTOR_CONSTANTS_H diff --git a/frameworks/core/components_v2/inspector/select_composed_element.cpp b/frameworks/core/components_v2/inspector/select_composed_element.cpp new file mode 100644 index 00000000..36e57554 --- /dev/null +++ b/frameworks/core/components_v2/inspector/select_composed_element.cpp @@ -0,0 +1,294 @@ +/* + * Copyright (c) 2021 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 "core/components_v2/inspector/select_composed_element.h" + +#include + +#include "core/components/select/select_element.h" +#include "core/components_v2/inspector/utils.h" + +#include "base/log/dump_log.h" + +namespace OHOS::Ace::V2 { + +const std::unordered_map> CREATE_JSON_MAP { + { "options", [](const SelectComposedElement& inspector) { return inspector.GetOptions(); } }, + { "selected", [](const SelectComposedElement& inspector) { return inspector.GetSelected(); } }, + { "value", [](const SelectComposedElement& inspector) { return inspector.GetSelectValue(); } }, + { "font", [](const SelectComposedElement& inspector) { return inspector.GetFont(); } }, + { "fontColor", [](const SelectComposedElement& inspector) { return inspector.GetFontColor(); } }, + { "selectedOptionBgColor", [](const SelectComposedElement& inspector) { return inspector.GetSelectBgColor(); } }, + { "selectedOptionFont", [](const SelectComposedElement& inspector) { return inspector.GetSelectFont(); } }, + { "selectedOptionFontColor", [](const SelectComposedElement& inspector) + { return inspector.GetSelectFontColor(); } }, + { "optionBgColor", [](const SelectComposedElement& inspector) { return inspector.GetOptionBgColor(); } }, + { "optionFont", [](const SelectComposedElement& inspector) { return inspector.GetOptionFont(); } }, + { "optionFontColor", [](const SelectComposedElement& inspector) { return inspector.GetOptionFontColor(); } } +}; + +void SelectComposedElement::Dump(void) +{ + InspectorComposedElement::Dump(); + DumpLog::GetInstance().AddDesc(std::string("select_composed_element")); + DumpLog::GetInstance().AddDesc(std::string("options: ").append(GetOptions())); + DumpLog::GetInstance().AddDesc(std::string("selected: ").append(GetSelected())); + DumpLog::GetInstance().AddDesc(std::string("value: ").append(GetSelectValue())); + DumpLog::GetInstance().AddDesc(std::string("font: ").append(GetFont())); + DumpLog::GetInstance().AddDesc(std::string("fontColor: ").append(GetFontColor())); + DumpLog::GetInstance().AddDesc(std::string("selectedOptionBgColor: ").append(GetSelectBgColor())); + DumpLog::GetInstance().AddDesc(std::string("selectedOptionFont: ").append(GetSelectFont())); + DumpLog::GetInstance().AddDesc(std::string("selectedOptionFontColor: ").append(GetSelectFontColor())); + DumpLog::GetInstance().AddDesc(std::string("optionBgColor: ").append(GetOptionBgColor())); + DumpLog::GetInstance().AddDesc(std::string("optionFont: ").append(GetOptionFont())); + DumpLog::GetInstance().AddDesc(std::string("optionFontColor: ").append(GetOptionFontColor())); +} + +std::unique_ptr SelectComposedElement::ToJsonObject() const +{ + auto resultJson = InspectorComposedElement::ToJsonObject(); + for (const auto& value : CREATE_JSON_MAP) { + resultJson->Put(value.first.c_str(), value.second(*this).c_str()); + } + return resultJson; +} + +std::string SelectComposedElement::GetOptions() const +{ + auto render = GetRenderSelect(); + auto jsonValue = JsonUtil::Create(true); + if (!render) { + return ""; + } + auto selectComponent = render->GetSelectComponent(); + if (!selectComponent) { + return ""; + } + auto popup = selectComponent->GetSelectPopupComponent(); + if (popup) { + auto vectorValue = popup->GetSelectOptions(); + auto jsonOptions = JsonUtil::CreateArray(false); + for (size_t i = 0 ; i < vectorValue.size() ; i++) { + if (vectorValue[i] && vectorValue[i]->GetIcon()) { + auto temp = JsonUtil::Create(false); + temp->Put("value", vectorValue[i]->GetValue().c_str()); + temp->Put("icon", vectorValue[i]->GetIcon()->GetSrc().c_str()); + auto index = std::to_string(i); + jsonOptions->Put(index.c_str(), temp); + } + } + jsonValue->Put("options", jsonOptions); + return jsonValue->ToString(); + } + return ""; +} + +std::string SelectComposedElement::GetSelected() const +{ + auto render = GetRenderSelect(); + if (render) { + if (render->GetSelectComponent()) { + return std::to_string(render->GetSelectComponent()->GetSelected()); + } + } + return ""; +} + +std::string SelectComposedElement::GetSelectValue() const +{ + auto render = GetRenderSelect(); + if (render) { + return render->GetSelectComponent()->GetTipText()->GetData(); + } + return ""; +} + +std::string SelectComposedElement::GetFont() const +{ + auto render = GetRenderSelect(); + if (render) { + return GetTextStyle(render->GetSelectComponent()->GetSelectStyle()); + } + return ""; +} + +std::string SelectComposedElement::GetFontColor() const +{ + auto render = GetRenderSelect(); + if (render) { + return ConvertColorToString(render->GetSelectComponent()->GetSelectStyle().GetTextColor()); + } + return ""; +} + +std::string SelectComposedElement::GetSelectBgColor() const +{ + auto render = GetRenderSelect(); + if (render) { + auto popup = render->GetSelectComponent()->GetPopup(); + if (!popup) { + LOGE("popup is null"); + return ""; + } + auto option = popup->GetSelectOptions(); + for (auto& optionItem : option) { + if (optionItem) { + return ConvertColorToString(optionItem->GetSelectedBackgroundColor()); + } + } + } + return ""; +} + +std::string SelectComposedElement::GetSelectFont() const +{ + auto render = GetRenderSelect(); + if (render) { + auto popup = render->GetSelectComponent()->GetPopup(); + if (!popup) { + LOGE("popup is null"); + return ""; + } + auto option = popup->GetSelectOptions(); + for (auto& optionItem : option) { + if (optionItem) { + return GetTextStyle(optionItem->GetSelectedTextStyle()); + } + } + } + return ""; +} + +std::string SelectComposedElement::GetSelectFontColor() const +{ + auto render = GetRenderSelect(); + if (render) { + auto popup = render->GetSelectComponent()->GetPopup(); + if (!popup) { + LOGE("popup is null"); + return ""; + } + auto option = popup->GetSelectOptions(); + for (auto& optionItem : option) { + if (optionItem) { + return ConvertColorToString(optionItem->GetSelectedTextStyle().GetTextColor()); + } + } + } + return ""; +} + +std::string SelectComposedElement::GetOptionBgColor() const +{ + auto render = GetRenderSelect(); + if (render) { + auto popup = render->GetSelectComponent()->GetPopup(); + if (!popup) { + LOGE("popup is null"); + return ""; + } + auto option = popup->GetSelectOptions(); + for (auto& optionItem : option) { + if (optionItem) { + return ConvertColorToString(optionItem->GetBackgroundColor()); + } + } + } + return ""; +} + +std::string SelectComposedElement::GetOptionFont() const +{ + auto render = GetRenderSelect(); + if (render) { + auto popup = render->GetSelectComponent()->GetPopup(); + if (!popup) { + LOGE("popup is null"); + return ""; + } + auto option = popup->GetSelectOptions(); + for (auto& optionItem : option) { + if (optionItem) { + return GetTextStyle(optionItem->GetTextStyle()); + } + } + } + return ""; +} + +std::string SelectComposedElement::GetOptionFontColor() const +{ + auto render = GetRenderSelect(); + if (render) { + return ConvertColorToString(render->GetSelectComponent()->GetSelectStyle().GetTextColor()); + } + return ""; +} + +OHOS::Ace::RefPtr SelectComposedElement::GetRenderSelect() const +{ + auto node = GetInspectorNode(SelectElement::TypeId()); + if (node) { + return AceType::DynamicCast(node); + } + return nullptr; +} + +std::string SelectComposedElement::GetTextStyle(TextStyle textStyle) const +{ + auto jsonValue = JsonUtil::Create(true); + jsonValue->Put("size", textStyle.GetFontSize().ToString().c_str()); + auto weight = textStyle.GetFontWeight(); + if (weight == FontWeight::W100) { + jsonValue->Put("weight", "100"); + } else if (weight == FontWeight::W200) { + jsonValue->Put("weight", "200"); + } else if (weight == FontWeight::W300) { + jsonValue->Put("weight", "300"); + } else if (weight == FontWeight::W400) { + jsonValue->Put("weight", "400"); + } else if (weight == FontWeight::W500) { + jsonValue->Put("weight", "500"); + } else if (weight == FontWeight::W600) { + jsonValue->Put("weight", "600"); + } else if (weight == FontWeight::W700) { + jsonValue->Put("weight", "700"); + } else if (weight == FontWeight::W800) { + jsonValue->Put("weight", "800"); + } else if (weight == FontWeight::W900) { + jsonValue->Put("weight", "900"); + } else { + jsonValue->Put("weight", ConvertWrapFontWeightToStirng(weight).c_str()); + } + auto family = textStyle.GetFontFamilies(); + std::string jsonFamily = ConvertFontFamily(family); + jsonValue->Put("family", jsonFamily.c_str()); + auto fontStyle = textStyle.GetFontStyle(); + jsonValue->Put("style", ConvertWrapFontStyleToStirng(fontStyle).c_str()); + return jsonValue->ToString(); +} + +std::string SelectComposedElement::ConvertFontFamily(const std::vector& fontFamily) const +{ + std::string result = ""; + for (const auto& item : fontFamily) { + result += item; + result += ","; + } + result = result.substr(0, result.size() - 1); + return result; +} + +} // namespace OHOS::Ace::V2 \ No newline at end of file diff --git a/frameworks/core/components_v2/inspector/select_composed_element.h b/frameworks/core/components_v2/inspector/select_composed_element.h new file mode 100644 index 00000000..85d40af5 --- /dev/null +++ b/frameworks/core/components_v2/inspector/select_composed_element.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 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 "core/components/select/render_select.h" +#include "core/components_v2/inspector/inspector_composed_element.h" +#include "core/pipeline/base/composed_element.h" + +namespace OHOS::Ace::V2 { + +class ACE_EXPORT SelectComposedElement : public InspectorComposedElement { + DECLARE_ACE_TYPE(SelectComposedElement, InspectorComposedElement) + +public: + explicit SelectComposedElement(const ComposeId& id) : InspectorComposedElement(id) {} + ~SelectComposedElement() override = default; + + void Dump(void) override; + std::unique_ptr ToJsonObject() const override; + + std::string GetOptions(void) const; + std::string GetSelected(void) const; + std::string GetSelectValue(void) const; + std::string GetFont(void) const; + std::string GetFontColor(void) const; + std::string GetSelectBgColor(void) const; + std::string GetSelectFont(void) const; + std::string GetSelectFontColor(void) const; + std::string GetOptionBgColor(void) const; + std::string GetOptionFont(void) const; + std::string GetOptionFontColor(void) const; + +private: + OHOS::Ace::RefPtr GetRenderSelect() const; + std::string ConvertFontFamily(const std::vector& fontFamily) const; + std::string GetTextStyle(TextStyle textStyle) const; +}; + +} \ No newline at end of file From 29e1fef9bdbc1ab960de87626de44cb13d6c69e2 Mon Sep 17 00:00:00 2001 From: yaoyuchi Date: Sat, 15 Jan 2022 16:15:41 +0800 Subject: [PATCH 2/3] flex select inspector Signed-off-by: yaoyuchi --- .../core/components_v2/inspector/select_composed_element.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/core/components_v2/inspector/select_composed_element.cpp b/frameworks/core/components_v2/inspector/select_composed_element.cpp index 36e57554..1fcdb4b8 100644 --- a/frameworks/core/components_v2/inspector/select_composed_element.cpp +++ b/frameworks/core/components_v2/inspector/select_composed_element.cpp @@ -32,7 +32,7 @@ const std::unordered_mapGetSelectComponent()) { - return std::to_string(render->GetSelectComponent()->GetSelected()); + return std::to_string(render->GetSelectComponent()->GetSelected()); } } return ""; From c7912592e6da0c06aa411985f8813c9e10802171 Mon Sep 17 00:00:00 2001 From: yaoyuchi Date: Sat, 15 Jan 2022 22:40:28 +0800 Subject: [PATCH 3/3] flex select inspector Signed-off-by: yaoyuchi --- .../components_v2/inspector/select_composed_element.cpp | 2 -- .../core/components_v2/inspector/select_composed_element.h | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/frameworks/core/components_v2/inspector/select_composed_element.cpp b/frameworks/core/components_v2/inspector/select_composed_element.cpp index 1fcdb4b8..8ca924e7 100644 --- a/frameworks/core/components_v2/inspector/select_composed_element.cpp +++ b/frameworks/core/components_v2/inspector/select_composed_element.cpp @@ -15,8 +15,6 @@ #include "core/components_v2/inspector/select_composed_element.h" -#include - #include "core/components/select/select_element.h" #include "core/components_v2/inspector/utils.h" diff --git a/frameworks/core/components_v2/inspector/select_composed_element.h b/frameworks/core/components_v2/inspector/select_composed_element.h index 85d40af5..7997b765 100644 --- a/frameworks/core/components_v2/inspector/select_composed_element.h +++ b/frameworks/core/components_v2/inspector/select_composed_element.h @@ -13,6 +13,9 @@ * limitations under the License. */ +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_SELECT_COMPOSED_ELEMENT_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_SELECT_COMPOSED_ELEMENT_H + #include "core/components/select/render_select.h" #include "core/components_v2/inspector/inspector_composed_element.h" #include "core/pipeline/base/composed_element.h" @@ -47,4 +50,6 @@ private: std::string GetTextStyle(TextStyle textStyle) const; }; -} \ No newline at end of file +} // namespace OHOS::Ace::V2 + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_SELECT_COMPOSED_ELEMENT_H \ No newline at end of file