• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 wm from '@ohos.window';
17import { CollectorType } from '../../profiler/base/ProfilerConstant';
18import SPLogger from '../../utils/SPLogger';
19
20const TAG = 'FloatWindowFun';
21
22export class FloatWindowFun {
23  static floatingWindowOffsetX: number = 50;
24  static floatingWindowOffsetY: number = 300;
25  static titleWindowOffsetX: number = 300;
26  static titleWindowOffsetY: number = 200;
27  static lineChartWindowOffsetX: number = 700;
28  static lineChartWindowOffsetY: number = 200;
29  static windowWidth: number = 2560;
30  static windowHeight: number = 1600;
31  static atWidth: number = 95;
32  static initAllFun(): void {
33    globalThis.CreateFloatingWindow = (): void => {
34      //5.5SP2  2106 改成 8
35      wm.create(
36        globalThis.abilityContext,
37        'sp_floatingWindow',
38        wm.WindowType.TYPE_FLOAT
39      ).then((floatWin) => {
40        floatWin
41          .moveTo(this.floatingWindowOffsetX, this.floatingWindowOffsetY)
42          .then(() => {
43            floatWin
44              .resetSize(
45                this.atWidth * globalThis.coefficient,
46                this.atWidth * globalThis.coefficient
47              )
48              .then(() => {
49                floatWin.getProperties().then((property) => {
50                  property.isTransparent = false;
51                });
52                floatWin.loadContent('pages/FloatBall').then(() => {
53                  floatWin.setBackgroundColor('#00000000').then(() => {
54                    //透明
55                    floatWin.show().then(() => {
56                      globalThis.showFloatingWindow = true;
57                    });
58                  });
59                });
60              });
61          });
62      });
63    };
64    globalThis.MoveFloatingWindow = (offsetX: number, offsetY: number): void => {
65      let xx =
66        this.floatingWindowOffsetX + offsetX * 2 < 0
67          ? 0
68          : this.floatingWindowOffsetX + offsetX * 2 > this.windowWidth - 200
69          ? this.windowWidth - 200
70          : this.floatingWindowOffsetX + offsetX * 2;
71      let yy =
72        this.floatingWindowOffsetY + offsetY * 2 < 0
73          ? 0
74          : this.floatingWindowOffsetY + offsetY * 2 > this.windowHeight - 200
75          ? this.windowHeight - 200
76          : this.floatingWindowOffsetY + offsetY * 2;
77
78      wm.find('sp_floatingWindow').then((fltWin) => {
79        fltWin.moveTo(xx, yy);
80      });
81    };
82
83    globalThis.SetFloatingWindowPosition = (
84      offsetX: number,
85      offsetY: number
86    ): void => {
87      this.floatingWindowOffsetX =
88        this.floatingWindowOffsetX + offsetX * 2 < 0
89          ? 0
90          : this.floatingWindowOffsetX + offsetX * 2 > this.windowWidth - 200
91          ? this.windowWidth - 200
92          : this.floatingWindowOffsetX + offsetX * 2;
93      this.floatingWindowOffsetY =
94        this.floatingWindowOffsetY + offsetY * 2 < 0
95          ? 0
96          : this.floatingWindowOffsetY + offsetY * 2 > this.windowHeight - 200
97          ? this.windowHeight - 200
98          : this.floatingWindowOffsetY + offsetY * 2;
99    };
100
101    globalThis.DestroyFloatingWindow = (): void => {
102      wm.find('sp_floatingWindow').then((fltWin) => {
103        fltWin.destroy().then(() => {
104          globalThis.showFloatingWindow = false;
105        });
106      });
107    };
108
109    globalThis.CreateTitleWindow = (): void => {
110      wm.create(
111        globalThis.abilityContext,
112        'sp_TitleWindow',
113        wm.WindowType.TYPE_FLOAT
114      ).then((floatWin) => {
115        console.log(TAG, 'CreateTitleWindow Done1');
116        floatWin
117          .moveTo(this.titleWindowOffsetX, this.titleWindowOffsetY)
118          .then(() => {
119            console.log(TAG, 'CreateTitleWindow Done2');
120            floatWin
121              .resetSize(
122                350 * globalThis.coefficient,
123                480 * globalThis.coefficient
124              )
125              .then(() => {
126                console.log(TAG, 'CreateTitleWindow Done3');
127                floatWin.getProperties().then((property) => {
128                  console.log(TAG, 'CreateTitleWindow Done4');
129                  property.isTransparent = false;
130                });
131                floatWin.loadContent('pages/TitleWindowPage').then(() => {
132                  console.log(TAG, 'CreateTitleWindow Done5');
133                  floatWin.setBackgroundColor('#00000000');
134                  floatWin.hide();
135                  console.log(TAG, 'CreateTitleWindow Done');
136                });
137              });
138          });
139      });
140    };
141
142    globalThis.MoveTitleWindow = (offsetX: number, offsetY: number): void => {
143      let xx =
144        this.titleWindowOffsetX + offsetX * 2 < 0
145          ? 0
146          : this.titleWindowOffsetX + offsetX * 2 > this.windowWidth - 500
147          ? this.windowWidth - 500
148          : this.titleWindowOffsetX + offsetX * 2;
149      let yy =
150        this.titleWindowOffsetY + offsetY * 2 < 0
151          ? 0
152          : this.titleWindowOffsetY + offsetY * 2 > this.windowHeight - 330
153          ? this.windowHeight - 330
154          : this.titleWindowOffsetY + offsetY * 2;
155      wm.find('sp_TitleWindow').then((fltWin) => {
156        fltWin.moveTo(xx, yy);
157      });
158    };
159
160    globalThis.SetTitleWindowPosition = (offsetX: number, offsetY: number): void => {
161      this.titleWindowOffsetX =
162        this.titleWindowOffsetX + offsetX * 2 < 0
163          ? 0
164          : this.titleWindowOffsetX + offsetX * 2 > this.windowWidth - 500
165          ? this.windowWidth - 500
166          : this.titleWindowOffsetX + offsetX * 2;
167      this.titleWindowOffsetY =
168        this.titleWindowOffsetY + offsetY * 2 < 0
169          ? 0
170          : this.titleWindowOffsetY + offsetY * 2 > this.windowHeight - 330
171          ? this.windowHeight - 330
172          : this.titleWindowOffsetY + offsetY * 2;
173    };
174
175    globalThis.DestroyTitleWindow = (): void => {
176      console.log('cm-destroy-TitleWindow');
177      wm.find('sp_TitleWindow').then((fltWin) => {
178        console.log('cm-destroy-TitleWindow1');
179        fltWin.destroy().then(() => {});
180      });
181    };
182
183    globalThis.HideTitleWindow = (): void => {
184      console.log('cm-hide-TitleWindow');
185      wm.find('sp_TitleWindow').then((fltWin) => {
186        console.log('cm-hide-TitleWindow1');
187        fltWin.hide();
188        console.log('cm-hide-TitleWindow2');
189      });
190    };
191
192    globalThis.ShowTitleWindow = (): void => {
193      console.log('cm-show-TitleWindow');
194      wm.find('sp_TitleWindow').then((fltWin) => {
195        console.log('cm-show-TitleWindow1');
196        fltWin.show();
197        console.log('cm-show-TitleWindow2');
198      });
199    };
200  }
201}
202