• 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 #include "core/components_ng/pattern/button/toggle_button_model_ng.h"
16 
17 #include "base/memory/ace_type.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/base/view_abstract.h"
20 #include "core/components_ng/base/view_stack_processor.h"
21 #include "core/components_ng/pattern/button/toggle_button_paint_property.h"
22 #include "core/components_ng/pattern/button/toggle_button_pattern.h"
23 
24 namespace OHOS::Ace::NG {
Create(const std::string & tagName)25 void ToggleButtonModelNG::Create(const std::string& tagName)
26 {
27     auto* stack = ViewStackProcessor::GetInstance();
28     auto nodeId = stack->ClaimNodeId();
29     auto frameNode =
30         FrameNode::GetOrCreateFrameNode(tagName, nodeId, []() { return AceType::MakeRefPtr<ToggleButtonPattern>(); });
31     stack->Push(frameNode);
32 }
33 
SetSelectedColor(const Color & selectedColor)34 void ToggleButtonModelNG::SetSelectedColor(const Color& selectedColor)
35 {
36     ACE_UPDATE_PAINT_PROPERTY(ToggleButtonPaintProperty, SelectedColor, selectedColor);
37 }
38 
SetIsOn(bool isOn)39 void ToggleButtonModelNG::SetIsOn(bool isOn)
40 {
41     ACE_UPDATE_PAINT_PROPERTY(ToggleButtonPaintProperty, IsOn, isOn);
42 }
43 
SetBackgroundColor(const Color & backgroundColor)44 void ToggleButtonModelNG::SetBackgroundColor(const Color& backgroundColor)
45 {
46     auto* stack = ViewStackProcessor::GetInstance();
47     CHECK_NULL_VOID(stack);
48     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
49     CHECK_NULL_VOID(frameNode);
50     auto buttonPattern = stack->GetMainFrameNodePattern<ToggleButtonPattern>();
51     if (buttonPattern) {
52         ACE_UPDATE_PAINT_PROPERTY(ToggleButtonPaintProperty, BackgroundColor, backgroundColor);
53         return;
54     }
55     NG::ViewAbstract::SetBackgroundColor(backgroundColor);
56 }
57 
58 } // namespace OHOS::Ace::NG