• 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
16/// <reference path='./import.ts' />
17class MenuFontColorModifier extends ModifierWithKey<ResourceColor> {
18  constructor(value: ResourceColor) {
19    super(value);
20  }
21  static identity: Symbol = Symbol('fontColor');
22  applyPeer(node: KNode, reset: boolean): void {
23    if (reset) {
24      getUINativeModule().menu.resetMenuFontColor(node);
25    } else {
26      getUINativeModule().menu.setMenuFontColor(node, this.value);
27    }
28  }
29
30  checkObjectDiff(): boolean {
31    return !isBaseOrResourceEqual(this.stageValue, this.value);
32  }
33}
34
35class MenuFontModifier extends ModifierWithKey<Font> {
36  constructor(value: Font) {
37    super(value);
38  }
39  static identity: Symbol = Symbol('font');
40  applyPeer(node: KNode, reset: boolean): void {
41    if (reset || !this.value) {
42      getUINativeModule().menu.resetFont(node);
43    } else {
44      getUINativeModule().menu.setFont(node,
45        this.value.size,
46        this.value.weight,
47        this.value.family,
48        this.value.style);
49    }
50  }
51
52  checkObjectDiff(): boolean {
53    let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size);
54    let weightEQ = this.stageValue.weight === this.value.weight;
55    let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family);
56    let styleEQ = this.stageValue.style === this.value.style;
57    return !sizeEQ || !weightEQ || !familyEQ || !styleEQ;
58  }
59}
60
61class RadiusModifier extends ModifierWithKey<Dimension | BorderRadiuses> {
62  constructor(value: Dimension | BorderRadiuses) {
63    super(value);
64  }
65  static identity: Symbol = Symbol('radius');
66  applyPeer(node: KNode, reset: boolean): void {
67    if (reset) {
68      getUINativeModule().menu.resetRadius(node);
69    } else {
70      if (isNumber(this.value) || isString(this.value) || isResource(this.value)) {
71        getUINativeModule().menu.setRadius(node, this.value, this.value, this.value, this.value);
72      } else {
73        getUINativeModule().menu.setRadius(node,
74          (this.value as BorderRadiuses).topLeft,
75          (this.value as BorderRadiuses).topRight,
76          (this.value as BorderRadiuses).bottomLeft,
77          (this.value as BorderRadiuses).bottomRight);
78      }
79    }
80  }
81
82  checkObjectDiff(): boolean {
83    if (isResource(this.stageValue) && isResource(this.value)) {
84      return !isResourceEqual(this.stageValue, this.value);
85    } else if (!isResource(this.stageValue) && !isResource(this.value)) {
86      return !((this.stageValue as BorderRadiuses).topLeft === (this.value as BorderRadiuses).topLeft &&
87        (this.stageValue as BorderRadiuses).topRight === (this.value as BorderRadiuses).topRight &&
88        (this.stageValue as BorderRadiuses).bottomLeft === (this.value as BorderRadiuses).bottomLeft &&
89        (this.stageValue as BorderRadiuses).bottomRight === (this.value as BorderRadiuses).bottomRight);
90    } else {
91      return true;
92    }
93  }
94}
95
96class MenuWidthModifier extends ModifierWithKey<Length> {
97  constructor(value: Length) {
98    super(value);
99  }
100  static identity: Symbol = Symbol('menuWidth');
101  applyPeer(node: KNode, reset: boolean): void {
102    if (reset) {
103      getUINativeModule().menu.resetWidth(node);
104    } else {
105      getUINativeModule().menu.setWidth(node, this.value);
106    }
107  }
108
109  checkObjectDiff(): boolean {
110    return !isBaseOrResourceEqual(this.stageValue, this.value);
111  }
112}
113
114class ArkMenuComponent extends ArkComponent implements MenuAttribute {
115  constructor(nativePtr: KNode) {
116    super(nativePtr);
117  }
118  width(value: Length): this {
119    modifierWithKey(this._modifiersWithKeys, MenuWidthModifier.identity, MenuWidthModifier, value);
120    return this;
121  }
122  fontSize(value: any): this {
123    throw new Error('Method not implemented.');
124  }
125  font(value: Font): this {
126    modifierWithKey(this._modifiersWithKeys, MenuFontModifier.identity, MenuFontModifier, value);
127    return this;
128  }
129  fontColor(value: ResourceColor): this {
130    modifierWithKey(this._modifiersWithKeys, MenuFontColorModifier.identity, MenuFontColorModifier, value);
131    return this;
132  }
133  radius(value: any): this {
134    modifierWithKey(this._modifiersWithKeys, RadiusModifier.identity, RadiusModifier, value);
135    return this;
136  }
137}
138
139// @ts-ignore
140globalThis.Menu.attributeModifier = function (modifier) {
141  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
142  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
143  let component = this.createOrGetNode(elmtId, () => {
144    return new ArkMenuComponent(nativeNode);
145  });
146  applyUIAttributes(modifier, nativeNode, component);
147  component.applyModifierPatch();
148};
149