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 #include "core/components/text_overlay/text_overlay_theme.h"
16 #include "core/components_ng/pattern/rich_editor_drag/rich_editor_drag_info.h"
17 #include "core/components_ng/pattern/rich_editor_drag/rich_editor_drag_overlay_modifier.h"
18 #include "core/components_ng/pattern/rich_editor_drag/rich_editor_drag_paint_method.h"
19 #include "core/components_ng/pattern/text_drag/text_drag_pattern.h"
20 
21 namespace OHOS::Ace::NG {
22 constexpr int32_t CONSTANT_DOUBLE = 2;
RichEditorDragPaintMethod(const WeakPtr<Pattern> & pattern,const RefPtr<TextDragOverlayModifier> & overlayMod,const RefPtr<RichEditorDragContentModifier> & contentMod,const RichEditorDragInfo & info)23 RichEditorDragPaintMethod::RichEditorDragPaintMethod(const WeakPtr<Pattern>& pattern,
24     const RefPtr<TextDragOverlayModifier>& overlayMod, const RefPtr<RichEditorDragContentModifier>& contentMod,
25     const RichEditorDragInfo& info)
26     : TextDragPaintMethod(pattern, overlayMod), contentModifier_(contentMod), info_(info)
27 {}
28 
GetContentModifier(PaintWrapper * paintWrapper)29 RefPtr<Modifier> RichEditorDragPaintMethod::GetContentModifier(PaintWrapper* paintWrapper)
30 {
31     return contentModifier_;
32 }
33 
UpdateContentModifier(PaintWrapper * paintWrapper)34 void RichEditorDragPaintMethod::UpdateContentModifier(PaintWrapper* paintWrapper)
35 {
36     auto pipleline = PipelineContext::GetCurrentContext();
37     CHECK_NULL_VOID(pipleline);
38     auto textOverlayTheme = pipleline->GetTheme<TextOverlayTheme>();
39     CHECK_NULL_VOID(textOverlayTheme);
40     auto modifier = DynamicCast<RichEditorDragOverlayModifier>(overlayModifier_);
41     CHECK_NULL_VOID(modifier);
42     auto handleDiameter = textOverlayTheme->GetHandleDiameter().ConvertToPx();
43     modifier->SetHandleRadius(handleDiameter / 2.0f);
44     if (!AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
45         modifier->SetHandleColor(textOverlayTheme->GetHandleColor());
46     } else {
47         modifier->SetHandleColor(info_.handleColor.value_or(textOverlayTheme->GetHandleColor()));
48     }
49     modifier->SetInnerHandleRadius(textOverlayTheme->GetHandleDiameterInner().ConvertToPx() / 2.0f);
50     modifier->SetInnerHandleColor(textOverlayTheme->GetHandleColorInner());
51     auto pattern = DynamicCast<TextDragPattern>(pattern_.Upgrade());
52     CHECK_NULL_VOID(pattern);
53     auto screenWdith = SystemProperties::GetDevicePhysicalWidth();
54     auto screenHeight = SystemProperties::GetDevicePhysicalHeight();
55     RectF boundsRect(-handleDiameter - screenWdith, -handleDiameter - screenHeight,
56         pattern->GetFrameWidth() + (screenWdith + handleDiameter) * CONSTANT_DOUBLE,
57         pattern->GetFrameHeight() + (screenHeight + handleDiameter) * CONSTANT_DOUBLE);
58     modifier->SetBoundsRect(boundsRect);
59     CHECK_NULL_VOID(paintWrapper);
60     auto offset = paintWrapper->GetGeometryNode()->GetFrameOffset();
61     modifier->SetFirstHandle(info_.firstHandle - offset);
62     modifier->SetSecondHandle(info_.secondHandle - offset);
63     auto textTheme = pipleline->GetTheme<TextTheme>();
64     CHECK_NULL_VOID(textTheme);
65     auto selectorColor = info_.selectedBackgroundColor.value_or(textTheme->GetSelectedColor());
66     modifier->SetSelectedColor(selectorColor.GetValue());
67 }
68 } // namespace OHOS::Ace::NG
69