• 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 MenuItemSelectedModifier extends ModifierWithKey<boolean> {
18  constructor(value: boolean) {
19    super(value);
20  }
21  static identity: Symbol = Symbol('menuItemSelected');
22  applyPeer(node: KNode, reset: boolean) {
23    if (reset) {
24      getUINativeModule().menuitem.resetMenuItemSelected(node);
25    } else {
26      getUINativeModule().menuitem.setMenuItemSelected(node, this.value);
27    }
28  }
29
30  checkObjectDiff(): boolean {
31    return !isBaseOrResourceEqual(this.stageValue, this.value);
32  }
33}
34
35class LabelFontColorModifier extends ModifierWithKey<ResourceColor> {
36  constructor(value: ResourceColor) {
37    super(value);
38  }
39  static identity: Symbol = Symbol('labelfontColor');
40  applyPeer(node: KNode, reset: boolean): void {
41    if (reset) {
42      getUINativeModule().menuitem.resetLabelFontColor(node);
43    } else {
44      getUINativeModule().menuitem.setLabelFontColor(node, this.value);
45    }
46  }
47
48  checkObjectDiff(): boolean {
49    return !isBaseOrResourceEqual(this.stageValue, this.value);
50  }
51}
52
53class ContentFontColorModifier extends ModifierWithKey<ResourceColor> {
54  constructor(value: ResourceColor) {
55    super(value);
56  }
57  static identity: Symbol = Symbol('contentfontColor');
58  applyPeer(node: KNode, reset: boolean): void {
59    if (reset) {
60      getUINativeModule().menuitem.resetContentFontColor(node);
61    } else {
62      getUINativeModule().menuitem.setContentFontColor(node, this.value);
63    }
64  }
65
66  checkObjectDiff(): boolean {
67    return !isBaseOrResourceEqual(this.stageValue, this.value);
68  }
69}
70
71class LabelFontModifier extends ModifierWithKey<Font> {
72  constructor(value: Font) {
73    super(value);
74  }
75  static identity: Symbol = Symbol('labelFont');
76  applyPeer(node: KNode, reset: boolean): void {
77    if (reset || !this.value) {
78      getUINativeModule().menuitem.resetLabelFont(node);
79    } else {
80      getUINativeModule().menuitem.setLabelFont(node, (this.value as Font).size,
81        (this.value as Font).weight, (this.value as Font).family,
82        (this.value as Font).style);
83    }
84  }
85
86  checkObjectDiff(): boolean {
87    let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size);
88    let weightEQ = this.stageValue.weight === this.value.weight;
89    let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family);
90    let styleEQ = this.stageValue.style === this.value.style;
91    return !sizeEQ || !weightEQ || !familyEQ || !styleEQ;
92  }
93}
94
95class ContentFontModifier extends ModifierWithKey<Font> {
96  constructor(value: Font) {
97    super(value);
98  }
99  static identity: Symbol = Symbol('contentFont');
100  applyPeer(node: KNode, reset: boolean): void {
101    if (reset || !this.value) {
102      getUINativeModule().menuitem.resetContentFont(node);
103    } else {
104      getUINativeModule().menuitem.setContentFont(node, (this.value as Font).size,
105        (this.value as Font).weight, (this.value as Font).family,
106        (this.value as Font).style);
107    }
108  }
109
110  checkObjectDiff(): boolean {
111    let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size);
112    let weightEQ = this.stageValue.weight === this.value.weight;
113    let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family);
114    let styleEQ = this.stageValue.style === this.value.style;
115    return !sizeEQ || !weightEQ || !familyEQ || !styleEQ;
116  }
117}
118
119class MenuItemSelectIconModifier extends ModifierWithKey<boolean | ResourceStr> {
120  constructor(value: boolean | ResourceStr) {
121    super(value);
122  }
123  static identity: Symbol = Symbol('selectIcon');
124  applyPeer(node: KNode, reset: boolean): void {
125    if (reset || !this.value) {
126      getUINativeModule().menuitem.resetSelectIcon(node);
127    } else {
128      getUINativeModule().menuitem.setSelectIcon(node, this.value);
129    }
130  }
131  checkObjectDiff(): boolean {
132    return !isBaseOrResourceEqual(this.stageValue, this.value);
133  }
134}
135
136class ArkMenuItemComponent extends ArkComponent implements MenuItemAttribute {
137  constructor(nativePtr: KNode) {
138    super(nativePtr);
139  }
140  selected(value: boolean): this {
141    modifierWithKey(this._modifiersWithKeys, MenuItemSelectedModifier.identity, MenuItemSelectedModifier, value);
142    return this;
143  }
144  selectIcon(value: boolean | ResourceStr): this {
145    modifierWithKey(this._modifiersWithKeys, MenuItemSelectIconModifier.identity, MenuItemSelectIconModifier, value);
146    return this;
147  }
148  onChange(callback: (selected: boolean) => void): this {
149    throw new Error('Method not implemented.');
150  }
151  contentFont(value: Font): this {
152    modifierWithKey(this._modifiersWithKeys, ContentFontModifier.identity, ContentFontModifier, value);
153    return this;
154  }
155  contentFontColor(value: ResourceColor): this {
156    modifierWithKey(this._modifiersWithKeys, ContentFontColorModifier.identity, ContentFontColorModifier, value);
157    return this;
158  }
159  labelFont(value: Font): this {
160    modifierWithKey(this._modifiersWithKeys, LabelFontModifier.identity, LabelFontModifier, value);
161    return this;
162  }
163  labelFontColor(value: ResourceColor): this {
164    modifierWithKey(this._modifiersWithKeys, LabelFontColorModifier.identity, LabelFontColorModifier, value);
165    return this;
166  }
167}
168// @ts-ignore
169globalThis.MenuItem.attributeModifier = function (modifier) {
170  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
171  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
172  let component = this.createOrGetNode(elmtId, () => {
173    return new ArkMenuItemComponent(nativeNode);
174  });
175  applyUIAttributes(modifier, nativeNode, component);
176  component.applyModifierPatch();
177};
178