• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 { int32 } from "@koalaui/common"
17
18export enum ArkAlignment {
19    TopStart,
20    Top,
21    TopEnd,
22    Start,
23    Center,
24    End,
25    BottomStart,
26    Bottom,
27    BottomEnd,
28}
29
30export function parseLength(parentValue: number, value: number, unit: int32): number {
31    switch (unit) {
32        //PX
33        case 0: {
34            const scale = 1 // TODO: need getting current device scale
35            return value * scale
36        }
37        //PERCENTAGE
38        case 3: {
39            return parentValue / 100 * value
40        }
41        default:
42            // VP, FP, LPX, UndefinedDimensionUnit: TODO: parse properly this units
43            return value
44    }
45}
46
47export function align(
48    alignment: ArkAlignment,
49    width: number,
50    height: number,
51    childWidth: number,
52    childHeight: number,
53    args: Float32Array
54) {
55    switch (alignment) {
56        case ArkAlignment.TopStart: {
57            break
58        }
59        case ArkAlignment.Start: {
60            args[1] += (height - childHeight) / 2
61            break
62        }
63        case ArkAlignment.BottomStart: {
64            args[1] += height - childHeight
65            break
66        }
67        case ArkAlignment.Top: {
68            args[0] += (width - childWidth) / 2
69            break
70        }
71        case ArkAlignment.Center: {
72            args[0] += (width - childWidth) / 2
73            args[1] += (height - childHeight) / 2
74            break
75        }
76        case ArkAlignment.Bottom: {
77            args[0] += (width - childWidth) / 2
78            args[1] += height - childHeight
79            break
80        }
81        case ArkAlignment.TopEnd: {
82            args[0] += width - childWidth
83            break
84        }
85        case ArkAlignment.End: {
86            args[0] += width - childWidth
87            args[1] += (height - childHeight) / 2
88            break
89        }
90        case ArkAlignment.BottomEnd: {
91            args[0] += width - childWidth
92            args[1] += height - childHeight
93            break
94        }
95    }
96}