• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components_ng/pattern/security_component/save_button/save_button_model_ng.h"
17 
18 #include "core/components_ng/pattern/security_component/security_component_pattern.h"
19 
20 namespace OHOS::Ace::NG {
21 std::unique_ptr<SaveButtonModelNG> SaveButtonModelNG::instance_ = nullptr;
22 std::mutex SaveButtonModelNG::mutex_;
23 
24 static const std::vector<uint32_t> ICON_RESOURCE_TABLE = {
25     static_cast<uint32_t>(InternalResource::ResourceId::SAVE_BUTTON_FILLED_SVG),
26     static_cast<uint32_t>(InternalResource::ResourceId::SAVE_BUTTON_LINE_SVG),
27     static_cast<uint32_t>(InternalResource::ResourceId::SAVE_BUTTON_PICTURE_SVG)
28 };
29 
GetInstance()30 SaveButtonModelNG* SaveButtonModelNG::GetInstance()
31 {
32     if (!instance_) {
33         std::lock_guard<std::mutex> lock(mutex_);
34         if (!instance_) {
35             instance_.reset(new SaveButtonModelNG());
36         }
37     }
38     return instance_.get();
39 }
40 
Create(int32_t text,int32_t icon,int32_t backgroundType,bool isArkuiComponent)41 void SaveButtonModelNG::Create(int32_t text, int32_t icon,
42     int32_t backgroundType, bool isArkuiComponent)
43 {
44     SecurityComponentModelNG::CreateCommon(V2::SAVE_BUTTON_ETS_TAG,
45         text, icon, backgroundType, []() { return AceType::MakeRefPtr<SecurityComponentPattern>(); }, isArkuiComponent);
46 }
47 
GetIconResource(int32_t iconStyle,InternalResource::ResourceId & id)48 bool SaveButtonModelNG::GetIconResource(int32_t iconStyle, InternalResource::ResourceId& id)
49 {
50     if ((iconStyle < 0) || (static_cast<uint32_t>(iconStyle) >= ICON_RESOURCE_TABLE.size())) {
51         return false;
52     }
53     id = static_cast<InternalResource::ResourceId>(ICON_RESOURCE_TABLE[iconStyle]);
54     return true;
55 }
56 
GetTextResource(int32_t textStyle,std::string & text)57 bool SaveButtonModelNG::GetTextResource(int32_t textStyle, std::string& text)
58 {
59     auto theme = GetTheme();
60     if (theme == nullptr) {
61         return false;
62     }
63     text = theme->GetSaveDescriptions(textStyle);
64     return true;
65 }
66 
GetIconResourceStatic(int32_t iconStyle,InternalResource::ResourceId & id)67 bool SaveButtonModelNG::GetIconResourceStatic(int32_t iconStyle, InternalResource::ResourceId& id)
68 {
69     if ((iconStyle < 0) || (static_cast<uint32_t>(iconStyle) >= ICON_RESOURCE_TABLE.size())) {
70         return false;
71     }
72     id = static_cast<InternalResource::ResourceId>(ICON_RESOURCE_TABLE[iconStyle]);
73     return true;
74 }
75 
GetTextResourceStatic(int32_t textStyle,std::string & text)76 bool SaveButtonModelNG::GetTextResourceStatic(int32_t textStyle, std::string& text)
77 {
78     auto theme = GetTheme();
79     if (theme == nullptr) {
80         return false;
81     }
82     text = theme->GetSaveDescriptions(textStyle);
83     return true;
84 }
85 
CreateFrameNode(int32_t nodeId)86 RefPtr<FrameNode> SaveButtonModelNG::CreateFrameNode(int32_t nodeId)
87 {
88     auto frameNode = FrameNode::GetOrCreateFrameNode(
89         V2::SAVE_BUTTON_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<SecurityComponentPattern>(); });
90 
91     return frameNode;
92 }
93 
InitSaveButton(FrameNode * frameNode,const SaveButtonStyle & style,bool isArkuiComponent)94 bool SaveButtonModelNG::InitSaveButton(FrameNode* frameNode,
95     const SaveButtonStyle& style, bool isArkuiComponent)
96 {
97     CHECK_NULL_RETURN(frameNode, false);
98 
99     auto text = style.text.value_or(SaveButtonSaveDescription::TEXT_NULL);
100     auto icon = style.icon.value_or(SaveButtonIconStyle::ICON_NULL);
101     auto backgroundType = style.backgroundType.value_or(ButtonType::CAPSULE);
102 
103     if ((text == SaveButtonSaveDescription::TEXT_NULL) && (icon == SaveButtonIconStyle::ICON_NULL)) {
104         // set default values
105         text = SaveButtonStyle::DEFAULT_TEXT;
106         icon = SaveButtonStyle::DEFAULT_ICON;
107         backgroundType = SaveButtonStyle::DEFAULT_BACKGROUND_TYPE;
108     }
109 
110     SecurityComponentElementStyle secCompStyle = {
111         .text = static_cast<int32_t>(text),
112         .icon = static_cast<int32_t>(icon),
113         .backgroundType = static_cast<int32_t>(backgroundType)
114     };
115     return SecurityComponentModelNG::InitSecurityComponent(frameNode, secCompStyle, isArkuiComponent,
116         SaveButtonModelNG::GetIconResourceStatic, SaveButtonModelNG::GetTextResourceStatic);
117 }
118 } // namespace OHOS::Ace::NG
119