1 /*
2 * Copyright (c) 2023 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 "core/components_ng/pattern/rich_editor/rich_editor_overlay_modifier.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/pattern/progress/progress_modifier.h"
20 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
21 #include "core/components_ng/pattern/rich_editor/rich_editor_theme.h"
22 #include "core/components_ng/render/drawing.h"
23 #include "core/components_ng/render/drawing_prop_convertor.h"
24 #include "core/pipeline_ng/pipeline_context.h"
25 #include "base/log/ace_trace.h"
26
27 namespace OHOS::Ace::NG {
RichEditorOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern> & pattern,const WeakPtr<ScrollBarOverlayModifier> & scrollbarOverlayModifier,WeakPtr<ScrollEdgeEffect> && edgeEffect)28 RichEditorOverlayModifier::RichEditorOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern,
29 const WeakPtr<ScrollBarOverlayModifier>& scrollbarOverlayModifier, WeakPtr<ScrollEdgeEffect>&& edgeEffect)
30 : TextOverlayModifier(), pattern_(pattern), edgeEffect_(edgeEffect),
31 scrollBarOverlayModifier_(scrollbarOverlayModifier)
32 {
33 caretVisible_ = AceType::MakeRefPtr<PropertyBool>(false);
34 AttachProperty(caretVisible_);
35 caretOffset_ = AceType::MakeRefPtr<PropertyOffsetF>(OffsetF());
36 AttachProperty(caretOffset_);
37 caretHeight_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
38 AttachProperty(caretHeight_);
39 caretWidth_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
40 AttachProperty(caretWidth_);
41 selectedBackgroundColor_ = AceType::MakeRefPtr<PropertyInt>(0);
42 AttachProperty(selectedBackgroundColor_);
43 caretColor_ = AceType::MakeRefPtr<PropertyInt>(0);
44 AttachProperty(caretColor_);
45 scrollOffset_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
46 AttachProperty(scrollOffset_);
47 frameSize_ = AceType::MakeRefPtr<PropertySizeF>(SizeF());
48 AttachProperty(frameSize_);
49 scrollBarOpacityType_ = AceType::MakeRefPtr<PropertyInt>(-1);
50 AttachProperty(scrollBarOpacityType_);
51 textHeight_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
52 AttachProperty(textHeight_);
53 previewTextDecorationColor_ = AceType::MakeRefPtr<PropertyColor>(Color());
54 AttachProperty(previewTextDecorationColor_);
55 previewTextUnderlineWidth_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
56 AttachProperty(previewTextUnderlineWidth_);
57 showPreviewTextDecoration_ = AceType::MakeRefPtr<PropertyBool>(false);
58 AttachProperty(showPreviewTextDecoration_);
59 }
60
SetPreviewTextDecorationColor(const Color & value)61 void RichEditorOverlayModifier::SetPreviewTextDecorationColor(const Color& value)
62 {
63 previewTextDecorationColor_->Set(value);
64 }
65
SetPreviewTextUnderlineWidth(float value)66 void RichEditorOverlayModifier::SetPreviewTextUnderlineWidth(float value)
67 {
68 previewTextUnderlineWidth_->Set(value);
69 }
70
SetShowPreviewTextDecoration(bool value)71 void RichEditorOverlayModifier::SetShowPreviewTextDecoration(bool value)
72 {
73 showPreviewTextDecoration_->Set(value);
74 }
75
SetPreviewTextStyle(const PreviewTextStyle & value)76 void RichEditorOverlayModifier::SetPreviewTextStyle(const PreviewTextStyle& value)
77 {
78 previewTextStyle_ = value;
79 }
80
SetCaretOffsetAndHeight(const OffsetF & cursorOffset,float height)81 void RichEditorOverlayModifier::SetCaretOffsetAndHeight(const OffsetF& cursorOffset, float height)
82 {
83 caretOffset_->Set(cursorOffset);
84 if (height > 0.0f) {
85 caretHeight_->Set(height);
86 }
87 }
88
SetCaretColor(uint32_t caretColor)89 void RichEditorOverlayModifier::SetCaretColor(uint32_t caretColor)
90 {
91 CHECK_NULL_VOID(caretColor_);
92 caretColor_->Set(static_cast<int32_t>(caretColor));
93 }
94
SetSelectedBackgroundColor(uint32_t selectedBackgroundColor)95 void RichEditorOverlayModifier::SetSelectedBackgroundColor(uint32_t selectedBackgroundColor)
96 {
97 CHECK_NULL_VOID(selectedBackgroundColor_);
98 selectedBackgroundColor_->Set(static_cast<int32_t>(selectedBackgroundColor));
99 }
100
SetCaretWidth(float width)101 void RichEditorOverlayModifier::SetCaretWidth(float width)
102 {
103 if (width <= 0.0f) {
104 return;
105 }
106 caretWidth_->Set(width);
107 }
108
GetCaretWidth() const109 float RichEditorOverlayModifier::GetCaretWidth() const
110 {
111 return caretWidth_->Get();
112 }
113
SetCaretVisible(bool value)114 void RichEditorOverlayModifier::SetCaretVisible(bool value)
115 {
116 caretVisible_->Set(value);
117 }
118
SetScrollOffset(float value)119 void RichEditorOverlayModifier::SetScrollOffset(float value)
120 {
121 scrollOffset_->Set(value);
122 }
123
SetScrollBarOpacityType(int32_t value)124 void RichEditorOverlayModifier::SetScrollBarOpacityType(int32_t value)
125 {
126 scrollBarOpacityType_->Set(value);
127 }
128
SetTextHeight(float value)129 void RichEditorOverlayModifier::SetTextHeight(float value)
130 {
131 textHeight_->Set(value);
132 }
133
SetFrameSize(const SizeF & value)134 void RichEditorOverlayModifier::SetFrameSize(const SizeF& value)
135 {
136 frameSize_->Set(value);
137 }
138
GetCaretHeight() const139 float RichEditorOverlayModifier::GetCaretHeight() const
140 {
141 return caretHeight_->Get();
142 }
143
GetCaretOffset() const144 OffsetF RichEditorOverlayModifier::GetCaretOffset() const
145 {
146 return caretOffset_->Get();
147 }
148
PaintPreviewTextDecoration(DrawingContext & drawingContext) const149 void RichEditorOverlayModifier::PaintPreviewTextDecoration(DrawingContext& drawingContext) const
150 {
151 CHECK_NULL_VOID(showPreviewTextDecoration_->Get());
152 if (previewTextStyle_ != PreviewTextStyle::UNDERLINE) {
153 TAG_LOGW(AceLogTag::ACE_RICH_TEXT, "is not UNDERLINE style");
154 return;
155 }
156 auto pattern = AceType::DynamicCast<RichEditorPattern>(pattern_.Upgrade());
157 CHECK_NULL_VOID(pattern);
158
159 auto previewTextDecorationColor = caretColor_->Get();
160 auto previewTextUnderlineWidth = previewTextUnderlineWidth_->Get();
161 auto roundRectRadius = previewTextUnderlineWidth / 2;
162 auto previewTextRects = pattern->GetPreviewTextRects();
163 drawingContext.canvas.Save();
164 RSBrush brush;
165 brush.SetAntiAlias(true);
166 brush.SetColor(previewTextDecorationColor);
167 drawingContext.canvas.AttachBrush(brush);
168 for (const auto& previewTextRect : previewTextRects) {
169 RSRect rect(previewTextRect.Left(), previewTextRect.Bottom() - previewTextUnderlineWidth,
170 previewTextRect.Right(), previewTextRect.Bottom());
171 drawingContext.canvas.DrawRoundRect(RSRoundRect(rect, roundRectRadius, roundRectRadius));
172 }
173 drawingContext.canvas.DetachBrush();
174 drawingContext.canvas.Restore();
175 }
176
PaintCaret(DrawingContext & drawingContext) const177 void RichEditorOverlayModifier::PaintCaret(DrawingContext& drawingContext) const
178 {
179 if (!caretVisible_->Get()) {
180 return;
181 }
182 auto& canvas = drawingContext.canvas;
183 auto offset = caretOffset_->Get();
184 canvas.Save();
185 RSPen pen;
186 pen.SetAntiAlias(true);
187 float caretWidth = static_cast<float>(caretWidth_->Get());
188 float caretHeight = static_cast<float>(caretHeight_->Get());
189 pen.SetWidth(caretWidth);
190 pen.SetCapStyle(RSPen::CapStyle::ROUND_CAP);
191 pen.SetColor(caretColor_->Get());
192 canvas.AttachPen(pen);
193 float midPosX = offset.GetX() + caretWidth / 2;
194 float startPosY = offset.GetY();
195 float endPosY = startPosY + caretHeight;
196 float roundCapRadius = caretWidth / 2;
197 canvas.DrawLine(RSPoint(midPosX, startPosY + roundCapRadius), RSPoint(midPosX, endPosY - roundCapRadius));
198 canvas.DetachPen();
199 canvas.Restore();
200 }
201
PaintScrollBar(DrawingContext & context)202 void RichEditorOverlayModifier::PaintScrollBar(DrawingContext& context)
203 {
204 auto scrollBarOverlayModifier = scrollBarOverlayModifier_.Upgrade();
205 CHECK_NULL_VOID(scrollBarOverlayModifier);
206 auto pattern = DynamicCast<RichEditorPattern>(pattern_.Upgrade());
207 CHECK_NULL_VOID(!pattern || pattern->GetBarDisplayMode() != DisplayMode::OFF);
208 scrollBarOverlayModifier->onDraw(context);
209 }
210
PaintEdgeEffect(const SizeF & frameSize,RSCanvas & canvas)211 void RichEditorOverlayModifier::PaintEdgeEffect(const SizeF& frameSize, RSCanvas& canvas)
212 {
213 auto edgeEffect = edgeEffect_.Upgrade();
214 CHECK_NULL_VOID(edgeEffect);
215 edgeEffect->Paint(canvas, frameSize, { 0.0f, 0.0f });
216 }
217
onDraw(DrawingContext & drawingContext)218 void RichEditorOverlayModifier::onDraw(DrawingContext& drawingContext)
219 {
220 ACE_SCOPED_TRACE("RichEditorOverlayOnDraw");
221 drawingContext.canvas.Save();
222 auto richEditorPattern = AceType::DynamicCast<RichEditorPattern>(pattern_.Upgrade());
223 CHECK_NULL_VOID(richEditorPattern);
224 auto contentRect = richEditorPattern->GetTextContentRect();
225
226 drawingContext.canvas.ClipRect(ToRSRect(contentRect), RSClipOp::INTERSECT);
227 PaintCaret(drawingContext);
228 PaintPreviewTextDecoration(drawingContext);
229 SetSelectedColor(selectedBackgroundColor_->Get());
230 TextOverlayModifier::onDraw(drawingContext);
231 drawingContext.canvas.Restore();
232
233 PaintScrollBar(drawingContext);
234 PaintEdgeEffect(frameSize_->Get(), drawingContext.canvas);
235 }
236
UpdateScrollBar(PaintWrapper * paintWrapper)237 void RichEditorOverlayModifier::UpdateScrollBar(PaintWrapper* paintWrapper)
238 {
239 auto richEditorPattern = AceType::DynamicCast<RichEditorPattern>(pattern_.Upgrade());
240 CHECK_NULL_VOID(richEditorPattern);
241 auto scrollBar = richEditorPattern->GetScrollControllerBar();
242 if (!scrollBar || !scrollBar->NeedPaint()) {
243 return;
244 }
245 auto scrollBarOverlayModifier = scrollBarOverlayModifier_.Upgrade();
246 CHECK_NULL_VOID(scrollBarOverlayModifier);
247 if (scrollBar->GetPositionModeUpdate()) {
248 scrollBarOverlayModifier->SetPositionMode(scrollBar->GetPositionMode());
249 }
250 SetScrollBarOpacityType(static_cast<int32_t>(scrollBar->GetOpacityAnimationType()));
251 scrollBarOverlayModifier->StartBarAnimation(scrollBar->GetHoverAnimationType(),
252 scrollBar->GetOpacityAnimationType(), scrollBar->GetNeedAdaptAnimation(), scrollBar->GetActiveRect());
253 scrollBar->SetHoverAnimationType(HoverAnimationType::NONE);
254 scrollBarOverlayModifier->SetBarColor(scrollBar->GetForegroundColor());
255 scrollBar->SetOpacityAnimationType(OpacityAnimationType::NONE);
256 }
257 } // namespace OHOS::Ace::NG
258