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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_PREVIEW_MOCK_MODEL_IMPL_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_PREVIEW_MOCK_MODEL_IMPL_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "bridge/declarative_frontend/jsview/canvas/js_canvas_renderer.h"
23 #include "bridge/declarative_frontend/jsview/canvas/js_rendering_context.h"
24 #include "bridge/declarative_frontend/view_stack_processor.h"
25 #include "core/components_ng/pattern/preview_mock/preview_mock_model.h"
26 
27 namespace OHOS::Ace::Framework {
28 namespace {
29 constexpr Dimension DEFAULT_FONT_SIZE = 30.0_px;
30 constexpr double DEFAULT_OFFSET = 25;
31 constexpr double DEFAULT_HEIGHT = 30;
32 }
33 
34 class ACE_EXPORT PreviewMockModelImpl : public PreviewMockModel {
35 public:
Create(const std::string & content)36     void Create(const std::string& content) {
37         RefPtr<OHOS::Ace::CustomPaintComponent> mockComponent = AceType::MakeRefPtr<OHOS::Ace::CustomPaintComponent>();
38         auto jsContext = Referenced::MakeRefPtr<JSRenderingContext>();
39         jsContext->SetAnti(true);
40         jsContext->SetCanvasPattern(mockComponent->GetTaskPool());
41         jsContext->SetAntiAlias();
42         mockComponent->GetTaskPool()->UpdateFontSize(DEFAULT_FONT_SIZE);
43         mockComponent->GetTaskPool()->FillText(
44             "This component is not supported on PC preview.", Offset(0, DEFAULT_OFFSET));
45         if (content == "Video") {
46             mockComponent->SetInspectorTag(content + "ComponentV2");
47         } else if (content == "PluginComponent" || content == "UIExtensionComponent") {
48             mockComponent->SetInspectorTag(content);
49         } else {
50             mockComponent->SetInspectorTag(content + "Component");
51         }
52         ViewStackProcessor::GetInstance()->Push(mockComponent);
53         RefPtr<BoxComponent> mountBox = ViewStackProcessor::GetInstance()->GetBoxComponent();
54         mountBox->SetColor(Color::FromString("#808080"));
55         mountBox->SetHeight(DEFAULT_HEIGHT);
56     }
57 };
58 } // namespace OHOS::Ace::Framework
59 
60 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_PREVIEW_MOCK_MODEL_IMPL_H
61