deveco-harness 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/README.en.md +71 -0
  2. package/README.md +71 -0
  3. package/deveco-harness.example.jsonc +32 -0
  4. package/dist/index.js +758 -0
  5. package/package.json +48 -0
  6. package/prompts/build.txt +164 -0
  7. package/prompts/plan.txt +118 -0
  8. package/skills/arkts-error-fixes/README.md +151 -0
  9. package/skills/arkts-error-fixes/SKILL.md +150 -0
  10. package/skills/arkts-error-fixes/assets/AnyTypeError.ets +269 -0
  11. package/skills/arkts-error-fixes/assets/AppStorageError.ets +22 -0
  12. package/skills/arkts-error-fixes/assets/ArrowFunctionConversionError.ets +42 -0
  13. package/skills/arkts-error-fixes/assets/AvoidAreaTypeError.ets +43 -0
  14. package/skills/arkts-error-fixes/assets/BreakpointTypeError.ets +71 -0
  15. package/skills/arkts-error-fixes/assets/CatchClauseTypeError.ets +54 -0
  16. package/skills/arkts-error-fixes/assets/ColorConsistencyError.ets +88 -0
  17. package/skills/arkts-error-fixes/assets/ColorPropertyError.ets +39 -0
  18. package/skills/arkts-error-fixes/assets/ContextTypeError.ets +105 -0
  19. package/skills/arkts-error-fixes/assets/DecoratorStateError.ets +64 -0
  20. package/skills/arkts-error-fixes/assets/DisplayListenerTypeError.ets +129 -0
  21. package/skills/arkts-error-fixes/assets/DuplicateEntryError.ets +35 -0
  22. package/skills/arkts-error-fixes/assets/ESObjectTypeError.ets +137 -0
  23. package/skills/arkts-error-fixes/assets/FontColorPropertyError.ets +42 -0
  24. package/skills/arkts-error-fixes/assets/FunctionReturnTypeError.ets +41 -0
  25. package/skills/arkts-error-fixes/assets/IDataSourceError.ets +79 -0
  26. package/skills/arkts-error-fixes/assets/ImplementationNotAllowedError.ets +38 -0
  27. package/skills/arkts-error-fixes/assets/InterfaceMethodSignatureError.ets +43 -0
  28. package/skills/arkts-error-fixes/assets/NotificationError.ets +34 -0
  29. package/skills/arkts-error-fixes/assets/ObjectLiteralInterfaceError.ets +49 -0
  30. package/skills/arkts-error-fixes/assets/ObjectLiteralTypeError.ets +54 -0
  31. package/skills/arkts-error-fixes/assets/ObjectSpreadError.ets +29 -0
  32. package/skills/arkts-error-fixes/assets/PossiblyNullError.ets +32 -0
  33. package/skills/arkts-error-fixes/assets/ResourceConversionError.ets +159 -0
  34. package/skills/arkts-error-fixes/assets/StandaloneFunctionContext.ets +116 -0
  35. package/skills/arkts-error-fixes/assets/StandaloneFunctionError.ets +85 -0
  36. package/skills/arkts-error-fixes/assets/StorageLinkDefaultError.ets +41 -0
  37. package/skills/arkts-error-fixes/assets/TitleButtonRectTypeError.ets +86 -0
  38. package/skills/arkts-error-fixes/assets/UnusedVariableWarning.ets +102 -0
  39. package/skills/arkts-error-fixes/assets/UtilityTypeError.ets +176 -0
  40. package/skills/arkts-error-fixes/assets/WindowRectSizeError.ets +65 -0
  41. package/skills/arkts-error-fixes/assets/WindowTypeError.ets +37 -0
  42. package/skills/arkts-error-fixes/reference/any_type_errors.md +433 -0
  43. package/skills/arkts-error-fixes/reference/appstorage_errors.md +95 -0
  44. package/skills/arkts-error-fixes/reference/arrow_function_conversion_errors.md +163 -0
  45. package/skills/arkts-error-fixes/reference/avoid_area_type_errors.md +59 -0
  46. package/skills/arkts-error-fixes/reference/breakpoint_type_errors.md +122 -0
  47. package/skills/arkts-error-fixes/reference/catch_clause_type_errors.md +89 -0
  48. package/skills/arkts-error-fixes/reference/color_consistency_errors.md +209 -0
  49. package/skills/arkts-error-fixes/reference/color_property_errors.md +136 -0
  50. package/skills/arkts-error-fixes/reference/context_type_errors.md +260 -0
  51. package/skills/arkts-error-fixes/reference/decorator_state_errors.md +109 -0
  52. package/skills/arkts-error-fixes/reference/display_listener_type_errors.md +149 -0
  53. package/skills/arkts-error-fixes/reference/duplicate_entry_errors.md +193 -0
  54. package/skills/arkts-error-fixes/reference/esobject_type_errors.md +110 -0
  55. package/skills/arkts-error-fixes/reference/fontcolor_property_errors.md +104 -0
  56. package/skills/arkts-error-fixes/reference/function_return_type_errors.md +194 -0
  57. package/skills/arkts-error-fixes/reference/idata_source_errors.md +77 -0
  58. package/skills/arkts-error-fixes/reference/implementation_not_allowed_errors.md +191 -0
  59. package/skills/arkts-error-fixes/reference/interface_method_signature_errors.md +143 -0
  60. package/skills/arkts-error-fixes/reference/notification_errors.md +110 -0
  61. package/skills/arkts-error-fixes/reference/object_literal_interface_errors.md +156 -0
  62. package/skills/arkts-error-fixes/reference/object_literal_type_errors.md +110 -0
  63. package/skills/arkts-error-fixes/reference/object_spread_errors.md +105 -0
  64. package/skills/arkts-error-fixes/reference/possibly_null_errors.md +156 -0
  65. package/skills/arkts-error-fixes/reference/resource_conversion_errors.md +154 -0
  66. package/skills/arkts-error-fixes/reference/standalone_function_errors.md +68 -0
  67. package/skills/arkts-error-fixes/reference/storage_link_default_errors.md +124 -0
  68. package/skills/arkts-error-fixes/reference/title_button_rect_type_errors.md +167 -0
  69. package/skills/arkts-error-fixes/reference/unused_variable_warnings.md +295 -0
  70. package/skills/arkts-error-fixes/reference/utility_type_errors.md +290 -0
  71. package/skills/arkts-error-fixes/reference/window_rect_size_errors.md +70 -0
  72. package/skills/arkts-error-fixes/reference/window_type_errors.md +95 -0
  73. package/skills/arkts-grammar-standards/SKILL.md +71 -0
  74. package/skills/arkts-grammar-standards/references/basic-syntax.md +103 -0
  75. package/skills/arkts-grammar-standards/references/restrictions.md +100 -0
  76. package/skills/arkts-grammar-standards/references/topic-aliases.json +93 -0
  77. package/skills/arkts-grammar-standards/references/ts-diff.md +108 -0
  78. package/skills/arkts-runtime-fix/SKILL.md +152 -0
  79. package/skills/arkts-runtime-fix/SKILL_CN.md +123 -0
  80. package/skills/arkts-runtime-fix/evals/evals.json +17 -0
  81. package/skills/arkts-runtime-fix/scripts/collect-hilog.mjs +116 -0
  82. package/skills/arkts-runtime-fix/scripts/collect-hilog.ts +115 -0
  83. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.mjs +117 -0
  84. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.ts +116 -0
  85. package/skills/arkts-runtime-fix/scripts/jscrash-report.mjs +149 -0
  86. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.mjs +126 -0
  87. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.ts +126 -0
  88. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.mjs +160 -0
  89. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.ts +160 -0
  90. package/skills/arkts-runtime-fix/scripts/shared/hdc.mjs +107 -0
  91. package/skills/arkts-runtime-fix/scripts/shared/hdc.ts +58 -0
  92. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.mjs +143 -0
  93. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.ts +148 -0
  94. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.mjs +389 -0
  95. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.ts +409 -0
  96. package/skills/arkts-runtime-fix/scripts/shared/utils.mjs +27 -0
  97. package/skills/arkts-runtime-fix/scripts/shared/utils.ts +27 -0
  98. package/skills/arkui-knowledge/SKILL.md +90 -0
  99. package/skills/arkui-knowledge/references/api-guardrails.md +60 -0
  100. package/skills/arkui-knowledge/references/common-mistakes.md +119 -0
  101. package/skills/arkui-knowledge/references/component-cookbook.md +135 -0
  102. package/skills/arkui-knowledge/references/ui-quality-checklist.md +36 -0
  103. package/skills/deveco-create-project/SKILL.md +144 -0
  104. package/skills/deveco-create-project/application/AppScope/app.json5 +10 -0
  105. package/skills/deveco-create-project/application/AppScope/resources/base/element/string.json +8 -0
  106. package/skills/deveco-create-project/application/AppScope/resources/base/media/background.png +0 -0
  107. package/skills/deveco-create-project/application/AppScope/resources/base/media/foreground.png +0 -0
  108. package/skills/deveco-create-project/application/AppScope/resources/base/media/layered_image.json +7 -0
  109. package/skills/deveco-create-project/application/build-profile.json5 +42 -0
  110. package/skills/deveco-create-project/application/code-linter.json5 +32 -0
  111. package/skills/deveco-create-project/application/entry/build-profile.json5 +33 -0
  112. package/skills/deveco-create-project/application/entry/hvigorfile.ts +7 -0
  113. package/skills/deveco-create-project/application/entry/obfuscation-rules.txt +23 -0
  114. package/skills/deveco-create-project/application/entry/oh-package.json5 +10 -0
  115. package/skills/deveco-create-project/application/entry/src/main/ets/entryability/EntryAbility.ets +63 -0
  116. package/skills/deveco-create-project/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +31 -0
  117. package/skills/deveco-create-project/application/entry/src/main/ets/pages/Index.ets +38 -0
  118. package/skills/deveco-create-project/application/entry/src/main/module.json5 +50 -0
  119. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/color.json +8 -0
  120. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/float.json +8 -0
  121. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/string.json +16 -0
  122. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/background.png +0 -0
  123. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/foreground.png +0 -0
  124. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/layered_image.json +7 -0
  125. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/startIcon.png +0 -0
  126. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/backup_config.json +3 -0
  127. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/main_pages.json +5 -0
  128. package/skills/deveco-create-project/application/entry/src/main/resources/dark/element/color.json +8 -0
  129. package/skills/deveco-create-project/application/hvigor/hvigor-config.json5 +23 -0
  130. package/skills/deveco-create-project/application/hvigorfile.ts +7 -0
  131. package/skills/deveco-create-project/application/oh-package.json5 +10 -0
  132. package/skills/deveco-create-project/scripts/copy-template.mjs +216 -0
  133. package/skills/deveco-create-project/scripts/copy-template.ts +214 -0
  134. package/skills/deveco-create-project/scripts/detect-sdk.mjs +82 -0
  135. package/skills/deveco-create-project/scripts/detect-sdk.ts +95 -0
@@ -0,0 +1,86 @@
1
+ import { window } from '@kit.ArkUI';
2
+ import { common } from '@kit.AbilityKit';
3
+
4
+ // ❌ 错误示例:返回类型声明为 window.Rect
5
+ // async function getTitleButtonRect(context: common.UIAbilityContext): Promise<window.Rect> {
6
+ // return new Promise((resolve, reject) => {
7
+ // window.getLastWindow(context, (err, win) => {
8
+ // if (err.code !== 0) {
9
+ // reject(new Error(err.message));
10
+ // return;
11
+ // }
12
+ // const titleButtonRect = win.getTitleButtonRect();
13
+ // resolve(titleButtonRect); // 类型错误:TitleButtonRect 不能赋值给 Rect
14
+ // });
15
+ // });
16
+ // }
17
+
18
+ // ✅ 正确示例:返回类型声明为 window.TitleButtonRect
19
+ async function getTitleButtonRect(context: common.UIAbilityContext): Promise<window.TitleButtonRect> {
20
+ return new Promise((resolve, reject) => {
21
+ window.getLastWindow(context, (err, win) => {
22
+ if (err.code !== 0) {
23
+ reject(new Error(err.message));
24
+ return;
25
+ }
26
+ const titleButtonRect = win.getTitleButtonRect();
27
+ resolve(titleButtonRect);
28
+ });
29
+ });
30
+ }
31
+
32
+ // ✅ 如果需要 Rect 类型,进行类型转换
33
+ // 注意:TitleButtonRect 类型可能不包含 left 和 top 属性
34
+ // 如果需要完整的 Rect 信息,需要从其他 API 获取
35
+ async function getTitleButtonRectAsRect(context: common.UIAbilityContext): Promise<window.Rect> {
36
+ return new Promise((resolve, reject) => {
37
+ window.getLastWindow(context, (err, win) => {
38
+ if (err.code !== 0) {
39
+ reject(new Error(err.message));
40
+ return;
41
+ }
42
+ const titleButtonRect = win.getTitleButtonRect();
43
+ // 创建新的 Rect 对象
44
+ // TitleButtonRect 只包含 width 和 height 属性
45
+ resolve({
46
+ left: 0,
47
+ top: 0,
48
+ width: titleButtonRect.width,
49
+ height: titleButtonRect.height
50
+ });
51
+ });
52
+ });
53
+ }
54
+
55
+ @Entry
56
+ @Component
57
+ struct TitleButtonRectTypeError {
58
+ @State titleBarHeight: number = 0;
59
+ @State titleBarWidth: number = 0;
60
+
61
+ async aboutToAppear() {
62
+ const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
63
+ try {
64
+ const titleButtonRect = await getTitleButtonRect(context);
65
+ // TitleButtonRect 只包含 width 和 height 属性
66
+ this.titleBarHeight = titleButtonRect.height;
67
+ this.titleBarWidth = titleButtonRect.width;
68
+ } catch (err) {
69
+ console.error('获取标题栏按钮区域失败:', err instanceof Error ? err.message : String(err));
70
+ }
71
+ }
72
+
73
+ build() {
74
+ Column() {
75
+ Text('TitleButtonRect 类型错误示例')
76
+ .fontSize(20)
77
+ .fontWeight(FontWeight.Bold)
78
+ .margin({ bottom: 20 })
79
+
80
+ Text(`标题栏高度: ${this.titleBarHeight}`)
81
+ .margin({ bottom: 8 })
82
+ Text(`标题栏宽度: ${this.titleBarWidth}`)
83
+ }
84
+ .padding(20)
85
+ }
86
+ }
@@ -0,0 +1,102 @@
1
+ @Entry
2
+ @Component
3
+ struct UnusedVariableWarningExample {
4
+ @State scrollOffset: number = 0;
5
+ private scroller: Scroller = new Scroller();
6
+
7
+ build() {
8
+ Column() {
9
+ Text('未使用变量警告示例')
10
+ .fontSize(20)
11
+ .fontWeight(FontWeight.Bold)
12
+ .margin({ bottom: 20 })
13
+
14
+ Column({ space: 12 }) {
15
+ Text('❌ 错误示例: scrollState 未使用')
16
+ .fontSize(14)
17
+ .fontColor('#666666')
18
+
19
+ Text('.onDidScroll((scrollOffset: number, scrollState: ScrollState) => {')
20
+ .fontSize(12)
21
+ .fontColor('#FF5722')
22
+ .fontFamily('monospace')
23
+ .padding(8)
24
+ .backgroundColor('#FFF3E0')
25
+ .borderRadius(4)
26
+
27
+ Text(' this.scrollOffset = scrollOffset;')
28
+ .fontSize(12)
29
+ .fontColor('#FF5722')
30
+ .fontFamily('monospace')
31
+ .padding(8)
32
+ .backgroundColor('#FFF3E0')
33
+ .borderRadius(4)
34
+
35
+ Text('})')
36
+ .fontSize(12)
37
+ .fontColor('#FF5722')
38
+ .fontFamily('monospace')
39
+ .padding(8)
40
+ .backgroundColor('#FFF3E0')
41
+ .borderRadius(4)
42
+
43
+ Text('✅ 正确示例 1: 使用下划线前缀标记未使用参数')
44
+ .fontSize(14)
45
+ .fontColor('#666666')
46
+
47
+ Text('.onDidScroll((scrollOffset: number, _scrollState: ScrollState) => {')
48
+ .fontSize(12)
49
+ .fontColor('#4CAF50')
50
+ .fontFamily('monospace')
51
+ .padding(8)
52
+ .backgroundColor('#E8F5E9')
53
+ .borderRadius(4)
54
+
55
+ Text(' this.scrollOffset = scrollOffset;')
56
+ .fontSize(12)
57
+ .fontColor('#4CAF50')
58
+ .fontFamily('monospace')
59
+ .padding(8)
60
+ .backgroundColor('#E8F5E9')
61
+ .borderRadius(4)
62
+
63
+ Text('})')
64
+ .fontSize(12)
65
+ .fontColor('#4CAF50')
66
+ .fontFamily('monospace')
67
+ .padding(8)
68
+ .backgroundColor('#E8F5E9')
69
+ .borderRadius(4)
70
+ }
71
+ .alignItems(HorizontalAlign.Start)
72
+ .margin({ bottom: 20 })
73
+
74
+ Text('实际使用示例:')
75
+ .fontSize(14)
76
+ .fontColor('#666666')
77
+ .margin({ bottom: 8 })
78
+
79
+ Scroll(this.scroller) {
80
+ Column() {
81
+ ForEach(Array.from({ length: 50 }), (_: Object, index: number) => {
82
+ Text(`Item ${index + 1}`)
83
+ .fontSize(16)
84
+ .padding(12)
85
+ .margin({ bottom: 8 })
86
+ .backgroundColor('#F5F5F5')
87
+ .borderRadius(4)
88
+ }, (_: Object, index: number) => `${index}`)
89
+ }
90
+ .width('100%')
91
+ }
92
+ .scrollable(ScrollDirection.Vertical)
93
+ .scrollBar(BarState.Auto)
94
+ .onDidScroll((scrollOffset: number, _scrollState: ScrollState) => {
95
+ this.scrollOffset = scrollOffset;
96
+ })
97
+ }
98
+ .width('100%')
99
+ .height('100%')
100
+ .padding(20)
101
+ }
102
+ }
@@ -0,0 +1,176 @@
1
+ /**
2
+ * ❌ 错误示例:使用 Parameters 工具类型
3
+ *
4
+ * 错误原因:ArkTS 不支持 TypeScript 的 Parameters 工具类型
5
+ * 错误信息:Some of utility types are not supported (arkts-no-utility-types)
6
+ */
7
+ @Entry
8
+ @Component
9
+ struct UtilityTypeError {
10
+ @State count: number = 0;
11
+
12
+ // ❌ 错误:使用 Parameters 工具类型
13
+ private handleClick = (event: ClickEvent) => {
14
+ this.count++;
15
+ };
16
+
17
+ // ❌ 错误:使用 Parameters 工具类型
18
+ private debounce<T extends (...args: any[]) => void>(func: T, delay: number): T {
19
+ return ((...args: Parameters<T>) => {
20
+ setTimeout(() => {
21
+ func(...args);
22
+ }, delay);
23
+ }) as T;
24
+ }
25
+
26
+ build() {
27
+ Column() {
28
+ Text(`Count: ${this.count}`)
29
+ .fontSize(20)
30
+ .margin({ bottom: 20 })
31
+
32
+ Button('Click Me')
33
+ .onClick(this.handleClick)
34
+ }
35
+ }
36
+ }
37
+
38
+ /**
39
+ * ✅ 正确示例:使用 Object[] 替代 Parameters 工具类型
40
+ */
41
+ @Entry
42
+ @Component
43
+ struct UtilityTypeCorrect {
44
+ @State count: number = 0;
45
+
46
+ private handleClick = (event: ClickEvent) => {
47
+ this.count++;
48
+ };
49
+
50
+ // ✅ 正确:使用 Object[] 替代 Parameters<T>
51
+ private debounce(func: Function, delay: number): Function {
52
+ return (...args: Object[]): void => {
53
+ setTimeout(() => {
54
+ func(...args);
55
+ }, delay);
56
+ };
57
+ }
58
+
59
+ build() {
60
+ Column() {
61
+ Text(`Count: ${this.count}`)
62
+ .fontSize(20)
63
+ .margin({ bottom: 20 })
64
+
65
+ Button('Click Me')
66
+ .onClick(this.handleClick)
67
+ }
68
+ }
69
+ }
70
+
71
+ /**
72
+ * ✅ 正确示例:使用明确的类型定义
73
+ */
74
+ @Entry
75
+ @Component
76
+ struct UtilityTypeExplicit {
77
+ @State message: string = 'Hello';
78
+
79
+ // ✅ 正确:使用明确的类型定义
80
+ private handleClick = (event: ClickEvent): void => {
81
+ this.message = 'Clicked!';
82
+ };
83
+
84
+ // ✅ 正确:使用明确的类型定义
85
+ private handleInput = (value: string): void => {
86
+ this.message = value;
87
+ };
88
+
89
+ // ✅ 正确:使用明确的类型定义
90
+ private debounce(func: (event: ClickEvent) => void, delay: number): (event: ClickEvent) => void {
91
+ let timeoutId: number = -1;
92
+ return (event: ClickEvent): void => {
93
+ clearTimeout(timeoutId);
94
+ timeoutId = setTimeout(() => {
95
+ func(event);
96
+ }, delay);
97
+ };
98
+ }
99
+
100
+ build() {
101
+ Column() {
102
+ Text(this.message)
103
+ .fontSize(20)
104
+ .margin({ bottom: 20 })
105
+
106
+ Button('Click Me')
107
+ .onClick(this.debounce(this.handleClick, 300))
108
+ }
109
+ }
110
+ }
111
+
112
+ /**
113
+ * ✅ 正确示例:使用类型别名定义函数类型
114
+ */
115
+ type EventHandler = (event: ClickEvent) => void;
116
+ type InputHandler = (value: string) => void;
117
+
118
+ @Entry
119
+ @Component
120
+ struct UtilityTypeInterface {
121
+ @State message: string = 'Hello';
122
+
123
+ // ✅ 正确:使用类型别名定义函数类型
124
+ private handleClick: EventHandler = (event: ClickEvent): void => {
125
+ this.message = 'Clicked!';
126
+ };
127
+
128
+ // ✅ 正确:使用类型别名定义函数类型
129
+ private handleInput: InputHandler = (value: string): void => {
130
+ this.message = value;
131
+ };
132
+
133
+ build() {
134
+ Column() {
135
+ Text(this.message)
136
+ .fontSize(20)
137
+ .margin({ bottom: 20 })
138
+
139
+ Button('Click Me')
140
+ .onClick(this.handleClick)
141
+ }
142
+ }
143
+ }
144
+
145
+ /**
146
+ * ✅ 正确示例:使用类型别名
147
+ */
148
+ type ClickHandler = (event: ClickEvent) => void;
149
+ type ValueHandler = (value: string) => void;
150
+
151
+ @Entry
152
+ @Component
153
+ struct UtilityTypeAlias {
154
+ @State message: string = 'Hello';
155
+
156
+ // ✅ 正确:使用类型别名
157
+ private handleClick: ClickHandler = (event: ClickEvent): void => {
158
+ this.message = 'Clicked!';
159
+ };
160
+
161
+ // ✅ 正确:使用类型别名
162
+ private handleInput: ValueHandler = (value: string): void => {
163
+ this.message = value;
164
+ };
165
+
166
+ build() {
167
+ Column() {
168
+ Text(this.message)
169
+ .fontSize(20)
170
+ .margin({ bottom: 20 })
171
+
172
+ Button('Click Me')
173
+ .onClick(this.handleClick)
174
+ }
175
+ }
176
+ }
@@ -0,0 +1,65 @@
1
+ // window.Rect 和 window.Size 类型使用示例
2
+ import { window } from '@kit.ArkUI';
3
+
4
+ @Entry
5
+ @Component
6
+ struct WindowRectSizeExample {
7
+ @State windowRect: window.Rect = { left: 0, top: 0, width: 0, height: 0 };
8
+ @State windowSize: window.Size = { width: 0, height: 0 };
9
+ @State breakpoint: string = 'sm';
10
+
11
+ aboutToAppear() {
12
+ try {
13
+ window.getLastWindow(this.getUIContext().getHostContext(), (err, win) => {
14
+ if (err.code !== 0) {
15
+ console.error('Failed to get window:', err);
16
+ return;
17
+ }
18
+ const properties = win.getWindowProperties();
19
+ this.windowRect = properties.windowRect;
20
+ this.windowSize = { width: properties.windowRect.width, height: properties.windowRect.height };
21
+ this.breakpoint = this.calculateBreakpoint(properties.windowRect.width);
22
+
23
+ win.on('windowSizeChange', (size: window.Size) => {
24
+ this.windowSize = size;
25
+ this.windowRect = {
26
+ left: this.windowRect.left,
27
+ top: this.windowRect.top,
28
+ width: size.width,
29
+ height: size.height
30
+ };
31
+ this.breakpoint = this.calculateBreakpoint(size.width);
32
+ });
33
+ });
34
+ } catch (e) {
35
+ console.error('Error:', e);
36
+ }
37
+ }
38
+
39
+ private calculateBreakpoint(width: number): string {
40
+ if (width < 600) return 'sm';
41
+ if (width < 840) return 'md';
42
+ if (width < 1440) return 'lg';
43
+ return 'xl';
44
+ }
45
+
46
+ build() {
47
+ Column({ space: 20 }) {
48
+ Text('窗口信息')
49
+ .fontSize(24)
50
+ .fontWeight(FontWeight.Bold)
51
+
52
+ Text(`窗口位置: x=${this.windowRect.left}, y=${this.windowRect.top}`)
53
+ .fontSize(16)
54
+
55
+ Text(`窗口尺寸: w=${this.windowRect.width}, h=${this.windowRect.height}`)
56
+ .fontSize(16)
57
+
58
+ Text(`当前断点: ${this.breakpoint}`)
59
+ .fontSize(16)
60
+ }
61
+ .width('100%')
62
+ .height('100%')
63
+ .padding(20)
64
+ }
65
+ }
@@ -0,0 +1,37 @@
1
+ import { window } from '@kit.ArkUI';
2
+ import { common } from '@kit.AbilityKit';
3
+
4
+ @Entry
5
+ @Component
6
+ struct WindowTypeError {
7
+ @State windowWidth: number = 0;
8
+
9
+ aboutToAppear() {
10
+ try {
11
+ const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
12
+
13
+ window.getLastWindow(context, (err, win) => {
14
+ if (err.code !== 0) {
15
+ console.error('Failed to get window:', err);
16
+ return;
17
+ }
18
+
19
+ const properties = win.getWindowProperties();
20
+ this.windowWidth = properties.windowRect.width;
21
+
22
+ win.on('windowSizeChange', (size: window.Size) => {
23
+ this.windowWidth = size.width;
24
+ });
25
+ });
26
+ } catch (err) {
27
+ console.error('Failed to get context:', err);
28
+ }
29
+ }
30
+
31
+ build() {
32
+ Column() {
33
+ Text(`Window Width: ${this.windowWidth}`)
34
+ .fontSize(20)
35
+ }
36
+ }
37
+ }