• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 Base from '@ohos.base';
17import common from '@ohos.app.ability.common';
18import util from '@ohos.util';
19
20import {CapsuleUtil} from '../InjectNotice/CapsuleUtil'
21
22const TAG = 'InjectNotice';
23
24@Extend(Text) function textModify(width: Length,
25  fontSize: number | string | Resource,
26  fontColor: ResourceColor, align: Alignment) {
27  .width(width)
28  .fontSize(fontSize)
29  .fontColor(fontColor)
30  .align(align)
31}
32
33@Entry
34@Component
35struct PluginWindow {
36  private mContext ?: common.UIExtensionContext;
37  @State titleText: string = '';
38  @State bnDisconnect: string = '';
39  @State handlePopup: boolean = false;
40  @State hoverColor: ResourceColor = 'sys.color.comp_background_primary';
41  private bnHotPress: boolean = false;
42  aboutToAppear(): void {
43    console.debug('PluginWindow page aboutToAppear');
44    this.mContext = getContext(this) as common.UIExtensionContext;
45    let text = this.mContext.resourceManager.getStringSync($r('app.string.notice_text'));
46    this.titleText = util.format(text, '');
47    this.bnDisconnect = this.mContext.resourceManager.getStringSync($r('app.string.bn_notice_cancel'));
48    console.debug(`PluginWindow page aboutToAppear titleText:${this.titleText} ${this.bnDisconnect}`);
49  }
50
51  @Builder buildTitle() {
52    Text($r('app.string.notice_title'))
53      .textModify('272vp', $r('sys.float.Title_S'), $r('sys.color.font_primary'), Alignment.Start)
54      .fontWeight(FontWeight.Bold);
55  }
56
57  @Builder buildContent() {
58    Text(this.titleText)
59      .textModify('272vp', $r('sys.float.Subtitle_S'), $r('sys.color.font_secondary'), Alignment.Start)
60      .fontWeight(FontWeight.Regular)
61      .margin({ top: '2vp' });
62  }
63
64  @Builder buildSymbolGlyph() {
65    SymbolGlyph($r('sys.symbol.link_slash'))
66      .fontSize('24vp')
67      .fontColor([$r('sys.color.icon_primary')])
68      .onClick(() => {
69        this.handleBnClick();
70      });
71  }
72
73  build() {
74    Row() {
75      Column() {
76        this.buildTitle();
77        this.buildContent();
78      }
79
80      Column() {
81        this.buildSymbolGlyph();
82      }
83      .justifyContent(FlexAlign.Center)
84      .backgroundColor(this.hoverColor)
85      .width('40vp').height('40vp')
86      .bindPopup(this.handlePopup, {
87        message: this.bnDisconnect,
88      })
89      .onHover((isHover: boolean, event: HoverEvent) => {
90        this.onBnHover(isHover, event);
91      })
92      .onMouse((event: MouseEvent) => {
93        this.onBnMouse(event);
94      })
95    }
96    .margin({top: '8vp', bottom: '4vp', right: '16vp', left: '24vp'});
97  }
98
99  onBnHover(isHover: boolean, event: HoverEvent): void {
100    if (isHover) {
101      if (!this.bnHotPress) {
102        this.handlePopup = true;
103        this.hoverColor = $r('sys.color.interactive_hover');
104      }
105    } else {
106      this.handlePopup = false;
107    }
108    this.hoverColor = 'sys.color.comp_background_primary';
109  }
110
111  onBnMouse(event: MouseEvent): void {
112    if (event.action == MouseAction.Press) {
113      this.bnHotPress = true;
114      this.hoverColor = $r('sys.color.interactive_pressed');
115    } else {
116      this.bnHotPress = false;
117    }
118  }
119
120  handleBnClick() :void {
121    console.debug(TAG, 'handleBnClick entry');
122    let instance: CapsuleUtil = CapsuleUtil.getInstance();
123    try {
124      instance.processCapsule(false);
125      instance.closePanel();
126    } catch (error) {
127      let err = error as Base.BusinessError;
128      console.error(TAG, `handleBnClick capsule close err:${JSON.stringify(err)}`);
129    }
130    console.debug(TAG, `handleBnClick capsule close ok`);
131    instance.cancelAuthorization();
132    console.debug(TAG, 'handleBnClick exit');
133  }
134}