1 /*
2  * Copyright (c) 2020-2021 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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_toggle_button.h
28  *
29  * @brief Defines the attributes and common functions of a toggle button.
30  *
31  * Each toggle button contains two images, one for pressing and the other for releasing.
32  *
33  * @since 1.0
34  * @version 1.0
35  */
36 
37 #ifndef GRAPHIC_LITE_UI_TOGGLE_BUTTON_H
38 #define GRAPHIC_LITE_UI_TOGGLE_BUTTON_H
39 
40 #include "components/ui_checkbox.h"
41 #include "components/ui_image_view.h"
42 
43 namespace OHOS {
44 /**
45  * @brief Represents a toggle button.
46  *
47  * Each toggle button contains two images, one for pressing and the other for releasing.
48  *
49  * @see UICheckBox
50  * @since 1.0
51  * @version 1.0
52  */
53 class UIToggleButton : public UICheckBox {
54 public:
55     /**
56      * @brief A constructor used to create a <b>UIToggleButton</b> instance.
57      *
58      * @since 1.0
59      * @version 1.0
60      */
61     UIToggleButton();
62 
63     /**
64      * @brief A destructor used to delete the <b>UIToggleButton</b> instance.
65      *
66      * @since 1.0
67      * @version 1.0
68      */
~UIToggleButton()69     virtual ~UIToggleButton() {}
70 
71     /**
72      * @brief Obtains the component type.
73      *
74      * @return Returns the component type, as defined in {@link UIViewType}.
75      * @since 1.0
76      * @version 1.0
77      */
GetViewType()78     UIViewType GetViewType() const override
79     {
80         return UI_TOGGLE_BUTTON;
81     }
82 
83     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
84 
85     /**
86      * @brief Sets the state for this toggle button.
87      *
88      * @param state Indicates the state of this toggle button. The value <b>true</b> indicates the image displayed for
89      * <b>On</b>, and <b>false</b> indicates the image displayed for <b>Off</b>. If this function is not called, the
90      * image is displayed for <b>Off</b>.
91      * @since 1.0
92      * @version 1.0
93      */
94     void SetState(bool state);
95 
96     /**
97      * @brief Obtains the state of this toggle button.
98      *
99      * @return Returns <b>true</b> if the image for <b>On</b> is displayed; returns <b>false</b> if the image is
100      * displayed for <b>Off</b>.
101      * @since 1.0
102      * @version 1.0 */
GetState()103     bool GetState() const
104     {
105         return (state_ != UNSELECTED);
106     }
107 
108     /**
109      * @brief Sets the layout dirention for this toggle button.
110      *
111      * @param isRtl Indicates the layout dirention of this toggle button. The value <b>true</b> indicates layout
112      * direntionis right-to-left, which means the white circle in button is at left side when the state is ON,
113      * at right side when the state is OFF. The value <b>false</b> indicates layout direntionis left-to-right.
114      */
115     void EnableRtl(bool isRtl);
116 
117     bool IsRtl();
118 protected:
119     void CalculateSize() override;
120 #if DEFAULT_ANIMATION
121     void Callback(UIView* view) override;
122     void OnStop(UIView& view) override;
123 #endif
124 
125 private:
126     uint16_t corner_ = 0;
127     uint16_t radius_ = 0;
128     int16_t rectWidth_ = 0;
129     Point leftCenter_ = {0, 0};
130     Point rightCenter_ = {0, 0};
131     Point currentCenter_ = {0, 0};
132     ColorType bgColor_ = Color::White();
133     Rect rectMid_ = {0, 0, 0, 0};
134     bool isRtl_ = false;
135 }; // class UIToggleButton
136 } // namespace OHOS
137 #endif // GRAPHIC_LITE_UI_TOGGLE_BUTTON_H
138