• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     NOTSET,
32 };
33 
34 template<class AttributeID>
35 class StateAttributeBase : public virtual AceType {
36     DECLARE_ACE_TYPE(StateAttributeBase<AttributeID>, AceType);
37 public:
StateAttributeBase(AttributeID id)38     explicit StateAttributeBase(AttributeID id) : id_(id) {}
~StateAttributeBase()39     virtual ~StateAttributeBase() {}
40     AttributeID id_;
41 };
42 
43 template<class AttributeID, class Attribute>
44 class StateAttributeValue : public StateAttributeBase<AttributeID> {
45     DECLARE_ACE_TYPE(StateAttributeValue, StateAttributeBase<AttributeID>);
46 public:
StateAttributeValue(AttributeID id,Attribute value)47     StateAttributeValue(AttributeID id, Attribute value)
48         : StateAttributeBase<AttributeID>(id), value_(value)
49     {}
~StateAttributeValue()50     virtual ~StateAttributeValue() {}
51     Attribute value_;
52 };
53 
54 template<class AttributeID>
55 class StateAttributes : public Referenced {
56 public:
HasAttribute(AttributeID attribute,VisualState state)57     bool HasAttribute(AttributeID attribute, VisualState state)
58     {
59         int stateIdx = static_cast<int>(state);
60         for (auto attrItem : stateAttrs_[stateIdx]) {
61             if (attrItem->id_ == attribute) {
62                 return true;
63             }
64         }
65         return false;
66     }
67 
68     template<class AttributeType>
AddAttribute(AttributeID attribute,AttributeType value,VisualState state)69     void AddAttribute(AttributeID attribute, AttributeType value, VisualState state)
70     {
71         int stateIdx = static_cast<int>(state);
72         stateAttrs_[stateIdx].push_back(
73             MakeRefPtr<StateAttributeValue<AttributeID, AttributeType>>(attribute, value));
74     }
75 
GetAttributesForState(VisualState state)76     const std::vector<RefPtr<StateAttributeBase<AttributeID>>>& GetAttributesForState(VisualState state)
77     {
78         int stateIdx = static_cast<int>(state);
79         return stateAttrs_[stateIdx];
80     }
81 
82 private:
83     // stateAttrs_ is an array with every entry pointing to
84     // vector of Attributes for one of the states.
85     // array[0] -> vector of attributes for state "normal"
86     // array[1] -> vector of attributes for state "focuse"
87     // ...
88     std::array<std::vector<RefPtr<StateAttributeBase<AttributeID>>>, VISUAL_STATE_COUNT> stateAttrs_;
89 };
90 
91 } // namespace OHOS::Ace
92 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_STATE_ATTRUBUTES_H
93