• 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 #include "bridge/declarative_frontend/jsview/models/span_model_impl.h"
17 
18 #include <utility>
19 
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/event/ace_event_handler.h"
23 
24 namespace OHOS::Ace::Framework {
Create(const std::string & content)25 void SpanModelImpl::Create(const std::string& content)
26 {
27     auto spanComponent = AceType::MakeRefPtr<TextSpanComponent>(content);
28     ViewStackProcessor::GetInstance()->ClaimElementId(spanComponent);
29     ViewStackProcessor::GetInstance()->Push(spanComponent);
30 
31     // Init text style, allowScale is not supported in declarative.
32     auto textStyle = spanComponent->GetTextStyle();
33     textStyle.SetAllowScale(false);
34     spanComponent->SetTextStyle(textStyle);
35 }
36 
SetFontSize(const Dimension & value)37 void SpanModelImpl::SetFontSize(const Dimension& value)
38 {
39     auto component = GetComponent();
40     CHECK_NULL_VOID(component);
41 
42     auto textStyle = component->GetTextStyle();
43     textStyle.SetFontSize(value);
44     component->SetTextStyle(textStyle);
45 
46     auto declaration = component->GetDeclaration();
47     if (declaration) {
48         declaration->SetHasSetFontSize(true);
49     }
50 }
51 
SetTextColor(const Color & value)52 void SpanModelImpl::SetTextColor(const Color& value)
53 {
54     auto component = GetComponent();
55     CHECK_NULL_VOID(component);
56     auto textStyle = component->GetTextStyle();
57     textStyle.SetTextColor(value);
58     component->SetTextStyle(textStyle);
59     auto declaration = component->GetDeclaration();
60     if (declaration) {
61         declaration->SetHasSetFontColor(true);
62     }
63 }
64 
SetItalicFontStyle(Ace::FontStyle value)65 void SpanModelImpl::SetItalicFontStyle(Ace::FontStyle value)
66 {
67     auto component = GetComponent();
68     CHECK_NULL_VOID(component);
69     auto textStyle = component->GetTextStyle();
70     textStyle.SetFontStyle(value);
71     component->SetTextStyle(textStyle);
72 
73     auto declaration = component->GetDeclaration();
74     if (declaration) {
75         declaration->SetHasSetFontStyle(true);
76     }
77 }
78 
SetFontWeight(Ace::FontWeight value)79 void SpanModelImpl::SetFontWeight(Ace::FontWeight value)
80 {
81     auto component = GetComponent();
82     CHECK_NULL_VOID(component);
83 
84     auto textStyle = component->GetTextStyle();
85     textStyle.SetFontWeight(value);
86     component->SetTextStyle(textStyle);
87 
88     auto declaration = component->GetDeclaration();
89     if (declaration) {
90         declaration->SetHasSetFontWeight(true);
91     }
92 }
93 
SetFontFamily(const std::vector<std::string> & value)94 void SpanModelImpl::SetFontFamily(const std::vector<std::string>& value)
95 {
96     auto component = GetComponent();
97     CHECK_NULL_VOID(component);
98 
99     auto textStyle = component->GetTextStyle();
100     textStyle.SetFontFamilies(value);
101     component->SetTextStyle(textStyle);
102 
103     auto declaration = component->GetDeclaration();
104     if (declaration) {
105         declaration->SetHasSetFontFamily(true);
106     }
107 }
108 
SetTextDecoration(Ace::TextDecoration value)109 void SpanModelImpl::SetTextDecoration(Ace::TextDecoration value)
110 {
111     auto component = GetComponent();
112     CHECK_NULL_VOID(component);
113     auto textStyle = component->GetTextStyle();
114     textStyle.SetTextDecoration(value);
115     component->SetTextStyle(textStyle);
116 }
117 
SetTextDecorationColor(const Color & value)118 void SpanModelImpl::SetTextDecorationColor(const Color& value)
119 {
120     auto component = GetComponent();
121     CHECK_NULL_VOID(component);
122     auto textStyle = component->GetTextStyle();
123     textStyle.SetTextDecorationColor(value);
124     component->SetTextStyle(textStyle);
125 }
126 
SetTextCase(Ace::TextCase value)127 void SpanModelImpl::SetTextCase(Ace::TextCase value)
128 {
129     auto component = GetComponent();
130     CHECK_NULL_VOID(component);
131     auto textStyle = component->GetTextStyle();
132     textStyle.SetTextCase(value);
133     component->SetTextStyle(textStyle);
134 
135     auto declaration = component->GetDeclaration();
136     if (declaration) {
137         declaration->SetHasSetTextCase(true);
138     }
139 }
140 
SetLetterSpacing(const Dimension & value)141 void SpanModelImpl::SetLetterSpacing(const Dimension& value)
142 {
143     auto component = GetComponent();
144     CHECK_NULL_VOID(component);
145 
146     auto textStyle = component->GetTextStyle();
147     textStyle.SetLetterSpacing(value);
148     component->SetTextStyle(textStyle);
149 
150     auto declaration = component->GetDeclaration();
151     if (declaration) {
152         declaration->SetHasSetLetterSpacing(true);
153     }
154 }
155 
GetComponent()156 RefPtr<TextSpanComponent> SpanModelImpl::GetComponent()
157 {
158     auto* stack = ViewStackProcessor::GetInstance();
159     return AceType::DynamicCast<TextSpanComponent>(stack->GetMainComponent());
160 }
161 
SetOnClick(std::function<void (const BaseEventInfo *)> && click)162 void SpanModelImpl::SetOnClick(std::function<void(const BaseEventInfo*)>&& click)
163 {
164     auto component = GetComponent();
165     if (component) {
166         component->SetOnClick(EventMarker(std::move(click)));
167     }
168 }
169 
170 } // namespace OHOS::Ace::Framework
171