1 /*
2  * Copyright (c) 2022 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_COMMON_STATE_ATTRUBUTES_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_STATE_ATTRUBUTES_H
18 
19 #include "base/memory/ace_type.h"
20 
21 namespace OHOS::Ace {
22 
23 constexpr int VISUAL_STATE_COUNT = 5; // Count of valid enums below (without UNDEFINED)
24 
25 enum class VisualState {
26     NORMAL = 0,
27     FOCUSED,
28     PRESSED,
29     DISABLED,
30     HOVER,
31     SELECTED,
32     NOTSET,
33 };
34 
35 template<class AttributeID>
36 class StateAttributeBase : public virtual AceType {
37     DECLARE_ACE_TYPE(StateAttributeBase<AttributeID>, AceType);
38 public:
StateAttributeBase(AttributeID id)39     explicit StateAttributeBase(AttributeID id) : id_(id) {}
~StateAttributeBase()40     virtual ~StateAttributeBase() {}
41     AttributeID id_;
42 };
43 
44 template<class AttributeID, class Attribute>
45 class StateAttributeValue : public StateAttributeBase<AttributeID> {
46     DECLARE_ACE_TYPE(StateAttributeValue, StateAttributeBase<AttributeID>);
47 public:
StateAttributeValue(AttributeID id,Attribute value)48     StateAttributeValue(AttributeID id, Attribute value)
49         : StateAttributeBase<AttributeID>(id), value_(value)
50     {}
~StateAttributeValue()51     virtual ~StateAttributeValue() {}
52     Attribute value_;
53 };
54 
55 template<class AttributeID>
56 class StateAttributes : public Referenced {
57 public:
HasAttribute(AttributeID attribute,VisualState state)58     bool HasAttribute(AttributeID attribute, VisualState state)
59     {
60         int stateIdx = static_cast<int>(state);
61         for (auto attrItem : stateAttrs_[stateIdx]) {
62             if (attrItem->id_ == attribute) {
63                 return true;
64             }
65         }
66         return false;
67     }
68 
69     template<class AttributeType>
AddAttribute(AttributeID attribute,AttributeType value,VisualState state)70     void AddAttribute(AttributeID attribute, AttributeType value, VisualState state)
71     {
72         int stateIdx = static_cast<int>(state);
73         stateAttrs_[stateIdx].push_back(
74             MakeRefPtr<StateAttributeValue<AttributeID, AttributeType>>(attribute, value));
75     }
76 
GetAttributesForState(VisualState state)77     const std::vector<RefPtr<StateAttributeBase<AttributeID>>>& GetAttributesForState(VisualState state)
78     {
79         int stateIdx = static_cast<int>(state);
80         return stateAttrs_[stateIdx];
81     }
82 
83 private:
84     // stateAttrs_ is an array with every entry pointing to
85     // vector of Attributes for one of the states.
86     // array[0] -> vector of attributes for state "normal"
87     // array[1] -> vector of attributes for state "focuse"
88     // ...
89     std::array<std::vector<RefPtr<StateAttributeBase<AttributeID>>>, VISUAL_STATE_COUNT> stateAttrs_;
90 };
91 
92 } // namespace OHOS::Ace
93 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_STATE_ATTRUBUTES_H
94