• 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 { BusinessError } from '@ohos.base';
17import pasteboard from '@ohos.pasteboard';
18import promptAction from '@ohos.promptAction';
19import Logger from '../../utils/Logger';
20
21@Component
22export struct SecPasteButton {
23  @Link pasteContent: string;
24  @StorageLink('copyButtonFocused') copyButtonFocused: boolean = true;
25  @State imageFocused: Resource = $r('app.media.paste1');
26  @State backColorFocused: Resource = $r('app.color.button_default_bg_color1');
27  @State textColorFocused: Resource = $r('app.color.button_default_bg_color');
28  @State imageNormal: Resource = $r('app.media.paste');
29  @State backColorNormal: Resource = $r('app.color.button_default_bg_color1');
30  @State textColorNormal: Resource = $r('app.color.button_default_text_color');
31
32  pastToMessage() {
33    let systemPasteboard = pasteboard.getSystemPasteboard();
34    systemPasteboard.getData().then((pasteData) => {
35      let primaryText = pasteData.getPrimaryText();
36      this.pasteContent = primaryText;
37      if (!primaryText) {
38        this.promptAction('Empty');
39      }
40      this.promptAction($r('app.string.Paste_succeed'));
41      Logger.error('Succeed to get PasteData. primaryText' + primaryText);
42    }).catch ((error: string) => {
43      this.promptAction(error);
44      Logger.info('promise, getCurrentLocation: error=' + JSON.stringify(error));
45    });
46  }
47
48  promptAction(message: string | Resource) {
49    try {
50      promptAction.showToast({
51        message: message,
52        duration: 3000,
53      });
54    } catch (error) {
55      let e: BusinessError = error as BusinessError;
56      Logger.error(`showToast args error code is ${e.code}, message is ${e.message}`);
57    }
58  }
59
60  build() {
61    Column() {
62      Row() {
63        if (!this.copyButtonFocused) {
64          PasteButton({
65            icon:PasteIconStyle.LINES, text:PasteDescription.PASTE, buttonType:ButtonType.Capsule
66          })
67            .onClick(() => {
68              this.copyButtonFocused = false;
69              this.pastToMessage();
70            })
71            .backgroundColor(this.backColorFocused)
72            .fontColor(this.textColorFocused)
73        } else {
74          PasteButton({
75            icon:PasteIconStyle.LINES, text:PasteDescription.PASTE, buttonType:ButtonType.Capsule
76          })
77            .onClick(() => {
78              this.copyButtonFocused = false;
79              this.pastToMessage();
80            })
81            .backgroundColor(this.backColorNormal)
82            .fontColor(this.textColorNormal)
83        }
84      }
85    }
86  }
87}
88