• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import { Log } from '../../utils/Log';
17import { Dispatch, getStore, OhCombinedState } from '../../redux/store';
18
19class StateStruct {
20  xComponentWidth: number = 0;
21  xComponentHeight: number = 0;
22}
23
24class AssistiveGridViewDispatcher {
25  public setDispatch(dispatch: Dispatch) {
26    this.mDispatch = dispatch;
27  }
28
29  private mDispatch: Dispatch = (data) => data;
30}
31
32@Component
33export struct AssistiveGridView {
34  @State state: StateStruct = new StateStruct();
35  private TAG: string = '[AssistiveGridView]:';
36  private mGlobalAlpha: number = 0.5;
37  private mLineWidth: number = 0.5;
38  private mShadowColor: string = 'argb(#7F000000)';
39  private mShadowOffsetX: number = 0;
40  private mShadowOffsetY: number = 0;
41  private mStrokeStyle: string = '#FFFFFF';
42  private scaleLen: number = 4;
43  private settings: RenderingContextSettings = new RenderingContextSettings(true);
44  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
45  private mAction: AssistiveGridViewDispatcher = new AssistiveGridViewDispatcher();
46
47  aboutToAppear(): void {
48    Log.info(`${this.TAG} aboutToAppear invoke E`);
49    getStore().subscribe((state: OhCombinedState) => {
50      this.state = {
51        xComponentWidth: state.previewReducer.xComponentWidth,
52        xComponentHeight: state.previewReducer.xComponentHeight
53      };
54    }, (dispatch: Dispatch) => {
55      this.mAction.setDispatch(dispatch);
56    });
57    Log.info(`${this.TAG} aboutToAppear invoke X`)
58  }
59
60  build() {
61    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
62      Canvas(this.context)
63        .width('100%')
64        .height('100%')
65        .onReady(() => {
66          this.context.clearRect(0, 0, this.state.xComponentWidth, this.state.xComponentHeight)
67          this.drawRuler()
68        })
69    }
70    .width(this.state.xComponentWidth)
71    .height(this.state.xComponentHeight)
72  }
73
74  drawRuler(): void {
75    let xCWidth = this.state.xComponentWidth
76    let xCHeight = this.state.xComponentHeight
77    let curX = xCWidth / 3
78    let curY = xCHeight / 3
79    this.context.beginPath()
80    this.context.globalAlpha = this.mGlobalAlpha
81    this.context.lineWidth = this.mLineWidth
82    this.context.strokeStyle = this.mStrokeStyle
83    this.context.shadowColor = this.mShadowColor
84    this.context.shadowOffsetX = this.mShadowOffsetX
85    this.context.shadowOffsetY = this.mShadowOffsetY
86    for (let i = 1; i <= this.scaleLen; i++) {
87      if (i <= this.scaleLen / 2) {
88        this.context.moveTo(curX * i, 0)
89        this.context.lineTo(curX * i, xCHeight)
90      } else {
91        let n = Math.floor(i / 2)
92        this.context.moveTo(0, curY * n)
93        this.context.lineTo(xCWidth * n, curY * n)
94      }
95    }
96    this.context.closePath()
97    this.context.stroke()
98  }
99}