1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SYMBOL_SYMBOL_EFFECT_OPTIONS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SYMBOL_SYMBOL_EFFECT_OPTIONS_H
18 
19 #include <optional>
20 #include <string>
21 
22 #include "base/json/json_util.h"
23 #include "core/components_ng/pattern/symbol/constants.h"
24 
25 namespace OHOS::Ace::NG {
26 class SymbolEffectOptions {
27 public:
28     explicit SymbolEffectOptions(SymbolEffectType effectType);
29 
SymbolEffectOptions(SymbolEffectType effectType,ScopeType scopeType)30     SymbolEffectOptions(SymbolEffectType effectType, ScopeType scopeType)
31         : effectType_(effectType), scopeType_(scopeType)
32     {}
33 
SymbolEffectOptions(SymbolEffectType effectType,ScopeType scopeType,CommonSubType commonSubType)34     SymbolEffectOptions(SymbolEffectType effectType, ScopeType scopeType, CommonSubType commonSubType)
35         : effectType_(effectType), scopeType_(scopeType), commonSubType_(commonSubType)
36     {}
37 
SymbolEffectOptions(SymbolEffectType effectType,FillStyle fillStyle)38     SymbolEffectOptions(SymbolEffectType effectType, FillStyle fillStyle)
39         : effectType_(effectType), fillStyle_(fillStyle)
40     {}
41 
42     SymbolEffectOptions() = default;
43     ~SymbolEffectOptions() = default;
44 
SetEffectType(SymbolEffectType effectType)45     void SetEffectType(SymbolEffectType effectType)
46     {
47         effectType_ = effectType;
48     }
49 
SetScopeType(ScopeType scopeType)50     void SetScopeType(ScopeType scopeType)
51     {
52         scopeType_ = scopeType;
53     }
54 
SetCommonSubType(CommonSubType commonSubType)55     void SetCommonSubType(CommonSubType commonSubType)
56     {
57         commonSubType_ = commonSubType;
58     }
59 
SetFillStyle(FillStyle fillStyle)60     void SetFillStyle(FillStyle fillStyle)
61     {
62         fillStyle_ = fillStyle;
63     }
64 
SetRepeatCount(int32_t repeatCount)65     void SetRepeatCount(int32_t repeatCount)
66     {
67         repeatCount_ = repeatCount;
68     }
69 
SetIsActive(std::optional<bool> isActive)70     void SetIsActive(std::optional<bool> isActive)
71     {
72         isActive_ = isActive;
73     }
74 
Reset()75     void Reset()
76     {
77         if (isTxtActiveSource_ == 1) {
78             isTxtActive_ = false;
79         }
80     }
81 
SetTriggerNum(int32_t triggerNum)82     void SetTriggerNum(int32_t triggerNum)
83     {
84         triggerNum_ = triggerNum;
85     }
86 
UpdateFlags(const SymbolEffectOptions & lastOptions)87     void UpdateFlags(const SymbolEffectOptions& lastOptions)
88     {
89         bool isCurTriggerSetted = triggerNum_.has_value();
90         bool isTriggerHasSetted = lastOptions.GetTriggerNum().has_value();
91         bool isCurActiveSetted = isActive_.has_value();
92 
93         if (isCurTriggerSetted) {
94             // 本次设置了triggerValue, 比较两次的TriggerNum值
95             isTxtActiveSource_ = 1;
96             if (!isTriggerHasSetted) {
97                 // 上次无值
98                 if (triggerNum_ == -1) {
99                     isTxtActive_ = false;
100                 } else {
101                     isTxtActive_ = true;
102                 }
103             } else {
104                 // 上次有值
105                 int32_t lastTriggerNum = lastOptions.GetTriggerNum().value();
106                 int32_t curTriggerNum = triggerNum_.value();
107                 isTxtActive_ = curTriggerNum != lastTriggerNum;
108             }
109         } else if (isTriggerHasSetted) {
110             // 历史设置过triggerValue,本次没设置triggerValue(两个接口混用,isActive写在下面场景)
111             isTxtActiveSource_ = 1;
112             isTxtActive_ = lastOptions.GetIsTxtActive();
113             triggerNum_ = lastOptions.GetTriggerNum().value();
114         } else if (isCurActiveSetted) {
115             // 只设isActive => isActive
116             isTxtActiveSource_ = 0;
117             isTxtActive_ = isActive_.value();
118         } else {
119             // isActive && triggerValue都未设置 => false
120             isTxtActiveSource_ = -1;
121             isTxtActive_ = false;
122         }
123     }
124 
SetIsTxtActive(bool isTxtActive)125     void SetIsTxtActive(bool isTxtActive)
126     {
127         isTxtActive_ = isTxtActive;
128     }
129 
SetIsTxtActiveSource(int16_t isTxtActiveSource)130     void SetIsTxtActiveSource(int16_t isTxtActiveSource)
131     {
132         isTxtActiveSource_ = isTxtActiveSource;
133     }
134 
GetEffectType()135     const SymbolEffectType& GetEffectType() const
136     {
137         return effectType_;
138     }
139 
GetScopeType()140     const std::optional<ScopeType>& GetScopeType() const
141     {
142         return scopeType_;
143     }
144 
GetCommonSubType()145     const std::optional<CommonSubType>& GetCommonSubType() const
146     {
147         return commonSubType_;
148     }
149 
GetFillStyle()150     const std::optional<FillStyle>& GetFillStyle() const
151     {
152         return fillStyle_;
153     }
154 
GetRepeatCount()155     int32_t GetRepeatCount() const
156     {
157         return repeatCount_;
158     }
159 
GetIsActive()160     const std::optional<bool>& GetIsActive() const
161     {
162         return isActive_;
163     }
164 
GetIsTxtActive()165     bool GetIsTxtActive() const
166     {
167         return isTxtActive_;
168     }
169 
IsTriggerChanged()170     const std::optional<bool>& IsTriggerChanged() const
171     {
172         return isTriggerNumChanged_;
173     }
174 
GetTriggerNum()175     const std::optional<int32_t>& GetTriggerNum() const
176     {
177         return triggerNum_;
178     }
179 
180     bool operator==(const SymbolEffectOptions& info) const;
181     bool operator!=(const SymbolEffectOptions& info) const;
182 
ToString()183     std::string ToString() const
184     {
185         auto json = JsonUtil::Create(true);
186         json->Put("effectType", static_cast<int32_t>(effectType_));
187         json->Put("scopeType", static_cast<int32_t>(scopeType_.value_or(ScopeType::LAYER)));
188         json->Put("commonSubType", static_cast<int32_t>(commonSubType_.value_or(CommonSubType::DOWN)));
189         json->Put("fillStyle", static_cast<int32_t>(fillStyle_.value_or(FillStyle::CUMULATIVE)));
190         json->Put("isTxtActive", isTxtActive_);
191         if (triggerNum_.has_value()) {
192             json->Put("triggerNum", triggerNum_.value());
193         }
194         if (isTriggerNumChanged_.has_value()) {
195             json->Put("isTriggerNumChanged", isTriggerNumChanged_.value());
196         }
197         if (isActive_.has_value()) {
198             json->Put("isActive", isActive_.value());
199         }
200         return json->ToString();
201     }
202 
203 private:
204     SymbolEffectType effectType_ = SymbolEffectType::NONE;
205     std::optional<ScopeType> scopeType_;
206     std::optional<CommonSubType> commonSubType_;
207     std::optional<FillStyle> fillStyle_;
208     std::optional<bool> isActive_;
209     std::optional<int32_t> triggerNum_;
210     std::optional<bool> isTriggerNumChanged_;
211     bool isTxtActive_ = false;
212     int16_t isTxtActiveSource_ = -1; // -1:未设置开关   0:isActive   1:用户js接口的Trigger
213     int32_t repeatCount_ = 1;
214 };
215 } // namespace OHOS::Ace::NG
216 
217 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SYMBOL_SYMBOL_SOURCE_INFO_H
218