quickwin 3.0.0 → 3.0.1

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 (95) hide show
  1. package/lib/abort.d.ts +21 -0
  2. package/lib/assert.d.ts +2 -0
  3. package/lib/bitmap.d.ts +7 -0
  4. package/lib/cache-utils.d.ts +6 -0
  5. package/lib/certs.d.ts +1 -0
  6. package/lib/dpi.d.ts +9 -0
  7. package/lib/dumpRects.d.ts +2 -0
  8. package/lib/exec-log.d.ts +23 -0
  9. package/lib/fetch.d.ts +4 -0
  10. package/lib/ffi/bind.d.ts +70 -0
  11. package/lib/ffi/ctype.d.ts +117 -0
  12. package/lib/ffi/index.d.ts +2 -0
  13. package/lib/ffi/struct.d.ts +174 -0
  14. package/lib/http-common.d.ts +143 -0
  15. package/lib/http-server.d.ts +59 -0
  16. package/lib/polyfill.d.ts +22 -0
  17. package/lib/react-qw/components/Button.d.ts +9 -0
  18. package/lib/react-qw/components/CheckBox.d.ts +11 -0
  19. package/lib/react-qw/components/ComboBox.d.ts +11 -0
  20. package/lib/react-qw/components/DateTimePicker.d.ts +15 -0
  21. package/lib/react-qw/components/Input.d.ts +15 -0
  22. package/lib/react-qw/components/Link.d.ts +12 -0
  23. package/lib/react-qw/components/ListBox.d.ts +13 -0
  24. package/lib/react-qw/components/ListView.d.ts +37 -0
  25. package/lib/react-qw/components/PathPicker.d.ts +23 -0
  26. package/lib/react-qw/components/ProgressBar.d.ts +9 -0
  27. package/lib/react-qw/components/RadioButton.d.ts +11 -0
  28. package/lib/react-qw/components/ScrollView.d.ts +9 -0
  29. package/lib/react-qw/components/Slider.d.ts +12 -0
  30. package/lib/react-qw/components/Tab.d.ts +14 -0
  31. package/lib/react-qw/components/Tooltip.d.ts +7 -0
  32. package/lib/react-qw/components/TreeView.d.ts +18 -0
  33. package/lib/react-qw/components/index.d.ts +16 -0
  34. package/lib/react-qw/index.d.ts +24 -0
  35. package/lib/react-qw/layout.d.ts +29 -0
  36. package/lib/react-qw/nmhdr.d.ts +1 -0
  37. package/lib/react-qw/props.d.ts +2 -0
  38. package/lib/react-qw/reconciler.d.ts +21 -0
  39. package/lib/stream.d.ts +20 -0
  40. package/lib/text-codec.d.ts +39 -0
  41. package/lib/text-measure.d.ts +14 -0
  42. package/lib/url.d.ts +54 -0
  43. package/lib/websocket.d.ts +73 -0
  44. package/lib/windows/{advapi32.ts → advapi32.d.ts} +6 -21
  45. package/lib/windows/comctl32.d.ts +14 -0
  46. package/lib/windows/comdlg32.d.ts +4 -0
  47. package/lib/windows/{gdi32.ts → gdi32.d.ts} +161 -98
  48. package/lib/windows/{kernel32.ts → kernel32.d.ts} +138 -115
  49. package/lib/windows/{msimg32.ts → msimg32.d.ts} +11 -14
  50. package/lib/windows/ntdll.d.ts +5 -0
  51. package/lib/windows/ole32.d.ts +3 -0
  52. package/lib/windows/{psapi.ts → psapi.d.ts} +9 -18
  53. package/lib/windows/{shell32.ts → shell32.d.ts} +25 -24
  54. package/lib/windows/structs.d.ts +3141 -0
  55. package/lib/windows/{user32.ts → user32.d.ts} +196 -193
  56. package/lib/windows/version.d.ts +27 -0
  57. package/lib/windows/{winspool.ts → winspool.d.ts} +18 -32
  58. package/package.json +15 -6
  59. package/qwin-nowasm-x86.exe +0 -0
  60. package/qwin-nowasm.exe +0 -0
  61. package/qwin-x86.exe +0 -0
  62. package/qwin.exe +0 -0
  63. package/lib/abort.ts +0 -69
  64. package/lib/assert.ts +0 -7
  65. package/lib/bitmap.ts +0 -39
  66. package/lib/cache-utils.ts +0 -40
  67. package/lib/certs.ts +0 -258
  68. package/lib/dpi.ts +0 -31
  69. package/lib/dumpRects.ts +0 -34
  70. package/lib/exec-log.ts +0 -30
  71. package/lib/fetch.ts +0 -589
  72. package/lib/ffi/bind.ts +0 -540
  73. package/lib/ffi/ctype.ts +0 -371
  74. package/lib/ffi/index.ts +0 -6
  75. package/lib/ffi/struct.ts +0 -780
  76. package/lib/http-common.ts +0 -441
  77. package/lib/http-server.ts +0 -604
  78. package/lib/polyfill.ts +0 -171
  79. package/lib/react-qw/components/index.ts +0 -17
  80. package/lib/react-qw/index.ts +0 -96
  81. package/lib/react-qw/layout.ts +0 -111
  82. package/lib/react-qw/nmhdr.ts +0 -7
  83. package/lib/react-qw/props.ts +0 -41
  84. package/lib/react-qw/reconciler.ts +0 -379
  85. package/lib/stream.ts +0 -22
  86. package/lib/text-codec.ts +0 -171
  87. package/lib/text-measure.ts +0 -44
  88. package/lib/url.ts +0 -201
  89. package/lib/websocket.ts +0 -600
  90. package/lib/windows/comctl32.ts +0 -20
  91. package/lib/windows/comdlg32.ts +0 -10
  92. package/lib/windows/ntdll.ts +0 -11
  93. package/lib/windows/ole32.ts +0 -9
  94. package/lib/windows/structs.ts +0 -807
  95. package/lib/windows/version.ts +0 -35
@@ -1,379 +0,0 @@
1
- import ReactReconciler from 'react-reconciler'
2
- import { createContext } from 'react'
3
- import { DefaultEventPriority, NoEventPriority } from 'react-reconciler/constants'
4
- import * as gui from 'gui'
5
- import * as os from 'os'
6
- import { GetClientRect, GetWindowLongPtr, CallWindowProc, SetWindowPos, ShowWindow, InvalidateRect, SetParent, SetWindowText } from '../windows/user32.js'
7
- import { createSystemDpiFont, getScaleFactor } from '../dpi.js'
8
- import { RECT } from '../windows/structs.js'
9
- import { NULL } from '../ffi/ctype.js'
10
- import { applyProps } from './props.js'
11
- import { calculateFlexLayout, type FlexStyle } from './layout.js'
12
- import { getButtonIdealSize, measureTextForHwnd } from '../text-measure.js'
13
- import type { WIntrinsicProps } from './jsx.js'
14
- import { Gwlp, SetWindowPosFlag, SetWindowPosHwnd, ShowWindowCmd, WindowStyle, WmMsg } from '../../constants'
15
-
16
- // DEBUG 由 esbuild --define 在 bundle 时替换(见 build.ts),生产环境为 false
17
- declare const DEBUG: boolean
18
-
19
- const dpiFont = createSystemDpiFont()
20
- export const scaleFactor = getScaleFactor()
21
-
22
- type Container = gui.HWND
23
- export type Props = WIntrinsicProps
24
-
25
- export interface Instance {
26
- hwnd: gui.HWND | null
27
- type: string
28
- props: Props
29
- children: Instance[]
30
- lastRect?: { x: number; y: number; w: number; h: number }
31
- }
32
-
33
- const DELAYED_CONTROLS = new Set(['SysListView32', 'SysTreeView32', 'LISTBOX', 'COMBOBOX', 'msctls_trackbar32', 'SysDateTimePick32', 'SysLink'])
34
-
35
- function isDelayedControl(winClass: string): boolean {
36
- return DELAYED_CONTROLS.has(winClass)
37
- }
38
-
39
- function setupWindowProc(instance: Instance, hwnd: gui.HWND) {
40
- instance.hwnd = hwnd
41
- const oldProc = GetWindowLongPtr(hwnd, Gwlp.WNDPROC)
42
- instancesByHwnd.set(hwnd, instance)
43
- gui.SetWindowProc(hwnd, (hwnd: gui.HWND, msg: number, wParam: number, lParam: number) => {
44
- const ev = instance.props.onEvent
45
- if (!ev) return CallWindowProc(oldProc, hwnd, msg, wParam, lParam)
46
- if (typeof ev === 'object') {
47
- return ev.fn({ hwnd, msg, wParam, lParam, callOldWndProc: () => CallWindowProc(oldProc, hwnd, msg, wParam, lParam) })
48
- }
49
- const result = CallWindowProc(oldProc, hwnd, msg, wParam, lParam)
50
- const override = ev({ hwnd, msg, wParam, lParam })
51
- return typeof override === 'number' && Number.isInteger(override) ? override : result
52
- })
53
- }
54
-
55
- function ensureChildWindow(child: Instance, parentHwnd: gui.HWND): gui.HWND {
56
- if (child.hwnd !== null) return child.hwnd
57
- const sty = child.props.style || {}
58
- const ws = ((child.props.ws ?? 0) | WindowStyle.CHILD) & ~WindowStyle.VISIBLE
59
- const hwnd = gui.CreateWindow(
60
- child.type, child.props.text || '', ws,
61
- 0, 0,
62
- (typeof sty.width === 'number' ? sty.width : 100) * scaleFactor, (typeof sty.height === 'number' ? sty.height : 30) * scaleFactor,
63
- parentHwnd, null
64
- )!
65
- if (dpiFont) gui.SendMessage(hwnd, WmMsg.SETFONT, dpiFont, 1)
66
- setupWindowProc(child, hwnd)
67
- return hwnd
68
- }
69
-
70
- type HostContext = Record<string, never>
71
-
72
- export const instancesByHwnd = new Map<gui.HWND, Instance>()
73
-
74
- function runFlexLayout(inst: Instance) {
75
- const children = inst.children
76
- if (children.length === 0) return
77
- const s = inst.props.style
78
- const flex: FlexStyle = s || {}
79
- const visible = children.filter(c => !c.props.hidden)
80
- if (visible.length === 0) return
81
- const rcBuf = RECT.alloc()
82
- if (!GetClientRect(inst.hwnd!, rcBuf.ptr)) { console.log('flex: no rect for', inst.hwnd, inst.type); return }
83
- const rect = RECT.decode(rcBuf)
84
- const pw = (rect.right - rect.left) / scaleFactor
85
- const ph = (rect.bottom - rect.top) / scaleFactor
86
- if (pw <= 0 || ph <= 0) { console.log('flex: zero size for', inst.hwnd, inst.type, pw, ph); return }
87
- const autoSizes = new Map<Instance, { w: number; h: number }>()
88
- for (const child of visible) {
89
- const sty = child.props.style || {}
90
- if (!child.hwnd) continue
91
- if (child.type === 'BUTTON' && (sty.width === 'auto' || sty.height === 'auto')) {
92
- const ideal = getButtonIdealSize(child.hwnd)
93
- autoSizes.set(child, { w: ideal.width / scaleFactor, h: ideal.height / scaleFactor })
94
- } else if (child.type === 'STATIC' && (sty.width === 'auto' || sty.height === 'auto') && child.props.text) {
95
- const measured = measureTextForHwnd(child.hwnd, child.props.text!)
96
- autoSizes.set(child, { w: measured.width / scaleFactor, h: measured.height / scaleFactor })
97
- }
98
- }
99
- const results = calculateFlexLayout(flex, pw, ph, visible.map(c => {
100
- const sty = c.props.style || {}
101
- const auto = autoSizes.get(c)
102
- return {
103
- style: {
104
- ...sty,
105
- width: auto ? (sty.width === 'auto' ? auto.w : sty.width) : sty.width,
106
- height: auto ? (sty.height === 'auto' ? auto.h : sty.height) : sty.height,
107
- }
108
- }
109
- }))
110
- const pl = flex.paddingLeft ?? flex.padding ?? 0
111
- const pt = flex.paddingTop ?? flex.padding ?? 0
112
- for (let i = 0; i < results.length; i++) {
113
- const r = results[i]!
114
- const child = visible[i]!
115
- const lr = child.lastRect
116
- if (lr && lr.x === r.x && lr.y === r.y && lr.w === r.width && lr.h === r.height) continue
117
- console.log('flex: set', child.type, child.hwnd, 'to', r.x, r.y, r.width, r.height)
118
- SetWindowPos(child.hwnd!, SetWindowPosHwnd.TOP, (r.x + pl) * scaleFactor, (r.y + pt) * scaleFactor, r.width * scaleFactor, r.height * scaleFactor, SetWindowPosFlag.SWP_SHOWWINDOW)
119
- InvalidateRect(child.hwnd!, NULL, 1)
120
- child.lastRect = { x: r.x, y: r.y, w: r.width, h: r.height }
121
- }
122
- for (const c of children) runFlexLayout(c)
123
- }
124
-
125
- export function forceFlexLayout(hwnd: gui.HWND): void {
126
- const inst = instancesByHwnd.get(hwnd)
127
- if (inst) runFlexLayout(inst)
128
- }
129
-
130
- /**
131
- * 扩展 HostConfig 类型定义
132
- *
133
- * @types/react-reconciler 的 HostConfig 存在以下问题:
134
- * 1. 缺少 rendererPackageName/rendererVersion 属性
135
- *
136
- * 解决方案:扩展补充缺失定义
137
- */
138
- type QuickWinHostConfig = ReactReconciler.HostConfig<
139
- string, Props, Container, Instance, Instance,
140
- never, never, never, gui.HWND,
141
- HostContext, never, number, -1, null
142
- > & {
143
- // rendererPackageName/rendererVersion(原类型定义中缺失)
144
- rendererPackageName: string
145
- rendererVersion: string
146
- }
147
-
148
- let currentUpdatePriority = NoEventPriority
149
-
150
- const hostConfig: QuickWinHostConfig = {
151
- // Core methods
152
- createInstance(type: string, props: Props, rootContainer: Container) {
153
- if (DEBUG) console.log('[reconciler] createInstance called:', type, 'class=' + props.type)
154
- const winClass = props.type
155
- if (isDelayedControl(winClass)) {
156
- if (DEBUG) console.log('[reconciler] delayed control, skipping CreateWindow:', winClass)
157
- return { hwnd: null, type: winClass, props, children: [] }
158
- }
159
- if (winClass === 'STATIC' && props.text !== undefined && props.children !== undefined && props.children !== null) {
160
- throw new Error('[STATIC] text and children are mutually exclusive')
161
- }
162
- const sty = props.style || {}
163
- // reconciler 创建的都是子窗口,确保 WS_CHILD 避免定位异常;创建时不带 WS_VISIBLE 防止 (0,0) 闪烁
164
- const ws = ((props.ws ?? 0) | WindowStyle.CHILD) & ~WindowStyle.VISIBLE
165
- if (DEBUG) console.log('[reconciler] CreateWindow args:', winClass, props.text ?? '', ws, 0, 0, sty.width ?? 100, sty.height ?? 30, rootContainer)
166
- const hwnd = gui.CreateWindow(
167
- winClass, props.text ?? '', ws,
168
- 0, 0,
169
- (typeof sty.width === 'number' ? sty.width : 100) * scaleFactor, (typeof sty.height === 'number' ? sty.height : 30) * scaleFactor,
170
- rootContainer, null
171
- )!
172
- if (dpiFont) gui.SendMessage(hwnd, WmMsg.SETFONT, dpiFont, 1)
173
- if (DEBUG) console.log('[reconciler] createInstance hwnd result:', hwnd, 'null?', hwnd === null)
174
- const instance: Instance = { hwnd, type: winClass, props, children: [] }
175
- setupWindowProc(instance, hwnd)
176
- return instance
177
- },
178
-
179
- createTextInstance(_text: string, _rootContainer: Container) {
180
- throw new Error('should never be called (shouldSetTextContent=true)')
181
- },
182
-
183
- appendInitialChild(parent: Instance, child: Instance) {
184
- if (child.hwnd === null) {
185
- ensureChildWindow(child, parent.hwnd!)
186
- } else {
187
- SetParent(child.hwnd, parent.hwnd ?? NULL)
188
- }
189
- if (DEBUG) console.log('[reconciler] appendInitialChild parent:', parent.hwnd, 'child:', child.hwnd)
190
- parent.children.push(child)
191
- },
192
-
193
- appendChild(parent: Instance, child: Instance) {
194
- if (child.hwnd === null) {
195
- ensureChildWindow(child, parent.hwnd!)
196
- } else {
197
- SetParent(child.hwnd, parent.hwnd ?? NULL)
198
- }
199
- if (DEBUG) console.log('[reconciler] appendChild parent:', parent.hwnd, 'child:', child.hwnd)
200
- parent.children.push(child)
201
- },
202
-
203
- appendChildToContainer(container: Container, child: Instance) {
204
- if (child.hwnd === null) {
205
- ensureChildWindow(child, container)
206
- } else {
207
- SetParent(child.hwnd, container)
208
- }
209
- const rootInst = instancesByHwnd.get(container)
210
- if (rootInst) rootInst.children.push(child)
211
- if (DEBUG) console.log('[reconciler] appendChildToContainer container:', container, 'child hwnd:', child.hwnd)
212
- },
213
-
214
- insertBefore(parent: Instance, child: Instance, beforeChild: Instance) {
215
- if (child.hwnd === null) {
216
- ensureChildWindow(child, parent.hwnd!)
217
- } else {
218
- SetParent(child.hwnd, parent.hwnd ?? NULL)
219
- }
220
- if (DEBUG) console.log('[reconciler] insertBefore parent:', parent.hwnd, 'child:', child.hwnd, 'before:', beforeChild.hwnd)
221
- const idx = parent.children.indexOf(beforeChild)
222
- if (idx >= 0) parent.children.splice(idx, 0, child)
223
- },
224
-
225
- insertInContainerBefore(container: Container, child: Instance, _before: Instance) {
226
- if (child.hwnd === null) {
227
- ensureChildWindow(child, container)
228
- } else {
229
- SetParent(child.hwnd, container)
230
- }
231
- const rootInst = instancesByHwnd.get(container)
232
- if (!rootInst) throw new Error('insertInContainerBefore: no root instance for container')
233
- const idx = rootInst.children.indexOf(_before)
234
- if (idx >= 0) rootInst.children.splice(idx, 0, child)
235
- else rootInst.children.push(child)
236
- if (DEBUG) console.log('[reconciler] insertInContainerBefore container:', container, 'child hwnd:', child.hwnd)
237
- },
238
-
239
- removeChild(parent: Instance, child: Instance) {
240
- if (DEBUG) console.log('[reconciler] removeChild parent:', parent.hwnd, 'child:', child.hwnd, 'type:', child.type)
241
- if (child.hwnd !== null) gui.DestroyWindow(child.hwnd)
242
- const idx = parent.children.indexOf(child)
243
- if (idx >= 0) parent.children.splice(idx, 1)
244
- },
245
-
246
- removeChildFromContainer(container: Container, child: Instance) {
247
- if (child.hwnd === null) return
248
- if (DEBUG) console.log('[reconciler] removeChildFromContainer container:', container, 'child hwnd:', child.hwnd)
249
- gui.DestroyWindow(child.hwnd)
250
- const rootInst = instancesByHwnd.get(container)
251
- if (rootInst) {
252
- const idx = rootInst.children.indexOf(child)
253
- if (idx >= 0) rootInst.children.splice(idx, 1)
254
- }
255
- },
256
-
257
- commitUpdate(instance: Instance, _type: string, oldProps: Props, newProps: Props, _internalHandle: unknown) {
258
- applyProps(instance, newProps, oldProps)
259
- },
260
-
261
- commitMount(_instance: Instance, _type: string, _props: Props, _internal: unknown) { },
262
-
263
- finalizeInitialChildren(instance: Instance, type: string, props: Props) {
264
- if (DEBUG) console.log('[reconciler] finalizeInitialChildren instance:', instance.hwnd, 'props:', JSON.stringify(props))
265
- applyProps(instance, props, { type })
266
- return false
267
- },
268
-
269
- resetAfterCommit(containerInfo: Container) {
270
- if (DEBUG) console.log('[reconciler] resetAfterCommit')
271
- const rootInst = instancesByHwnd.get(containerInfo)
272
- if (!rootInst) throw new Error('resetAfterCommit: no root instance for container')
273
- runFlexLayout(rootInst)
274
- },
275
-
276
- resetTextContent(_instance: Instance) { },
277
-
278
- commitTextUpdate(instance: Instance, _oldText: string, newText: string) {
279
- SetWindowText(instance.hwnd!, newText)
280
- },
281
-
282
- hideInstance(instance: Instance) {
283
- if (instance.hwnd !== null) ShowWindow(instance.hwnd, ShowWindowCmd.HIDE)
284
- },
285
-
286
- hideTextInstance(instance: Instance) {
287
- if (instance.hwnd !== null) ShowWindow(instance.hwnd, ShowWindowCmd.HIDE)
288
- },
289
-
290
- unhideInstance(instance: Instance, _props: Props) {
291
- if (instance.hwnd !== null) ShowWindow(instance.hwnd, ShowWindowCmd.SHOW)
292
- },
293
-
294
- unhideTextInstance(instance: Instance, _text: string) {
295
- if (instance.hwnd !== null) ShowWindow(instance.hwnd, ShowWindowCmd.SHOW)
296
- },
297
-
298
- shouldSetTextContent(_type: string, props: Props) {
299
- return typeof props.children === 'string' || typeof props.children === 'number'
300
- },
301
-
302
- getPublicInstance(instance: Instance) {
303
- return instance.hwnd!
304
- },
305
-
306
- getRootHostContext(_rootContainer: Container) {
307
- return {}
308
- },
309
-
310
- getChildHostContext(_parentContext: HostContext, _type: string) {
311
- return {}
312
- },
313
-
314
- prepareForCommit(_containerInfo: Container) {
315
- return null
316
- },
317
-
318
- preparePortalMount(_containerInfo: Container) { },
319
-
320
- clearContainer(_container: Container) {
321
- // 不需要清理,React reconciler 会通过 removeChildFromContainer 来移除子节点
322
- },
323
-
324
- scheduleTimeout(fn: () => void, delay: number) {
325
- return os.setTimeout(fn, delay)
326
- },
327
-
328
- cancelTimeout(id: os.TimerId) {
329
- os.clearTimeout?.(id)
330
- },
331
-
332
- noTimeout: -1,
333
-
334
- supportsMutation: true,
335
- supportsPersistence: false,
336
- supportsHydration: false,
337
- isPrimaryRenderer: true,
338
- supportsMicrotasks: false,
339
-
340
- setCurrentUpdatePriority(p: number) { currentUpdatePriority = p },
341
- getCurrentUpdatePriority() { return currentUpdatePriority },
342
- resolveUpdatePriority() { return DefaultEventPriority },
343
-
344
- resolveEventType() { return null },
345
- resolveEventTimeStamp() { return Date.now() },
346
- trackSchedulerEvent() { },
347
-
348
- shouldAttemptEagerTransition() { return false },
349
- detachDeletedInstance(instance: Instance) {
350
- if (DEBUG) console.log('[reconciler] detachDeletedInstance hwnd:', instance.hwnd, 'type:', instance.type)
351
- if (instance.hwnd !== null) instancesByHwnd.delete(instance.hwnd)
352
- },
353
-
354
- // 添加必需方法
355
- getInstanceFromNode(_node: unknown) { return null },
356
- beforeActiveInstanceBlur() { },
357
- afterActiveInstanceBlur() { },
358
- prepareScopeUpdate(_scopeInstance: unknown, _instance: unknown) { },
359
- getInstanceFromScope(_scopeInstance: unknown) { return null },
360
-
361
- // Suspense/Concurrent Mode 支持
362
- maySuspendCommit() { return false },
363
- requestPostPaintCallback() { },
364
- preloadInstance() { return true },
365
- startSuspendingCommit() { },
366
- suspendInstance() { },
367
- waitForCommitToBeReady() { return null },
368
-
369
- rendererVersion: '0.1.0',
370
- rendererPackageName: 'react-qw',
371
- NotPendingTransition: null,
372
- // @types/react.Context<T> 与 @types/react-reconciler.ReactContext<T> 是同一运行时对象
373
- // 的两个不兼容类型定义(ReactContext 多 _currentValue 等字段),此 cast 不可避免
374
- HostTransitionContext: createContext(null) as unknown as ReactReconciler.ReactContext<null>,
375
- resetFormInstance(_form: never) { },
376
- }
377
-
378
- const reconciler = ReactReconciler(hostConfig)
379
- export default reconciler
package/lib/stream.ts DELETED
@@ -1,22 +0,0 @@
1
- export {}
2
-
3
- import {
4
- ReadableStream as ReadableStreamImpl,
5
- ReadableStreamDefaultController as ReadableStreamDefaultControllerImpl,
6
- ReadableStreamDefaultReader as ReadableStreamDefaultReaderImpl,
7
- } from './vendor/web-streams/ponyfill.mjs'
8
-
9
- declare global {
10
- type ReadableStreamReadResult<R = any> =
11
- | { done: false; value: R }
12
- | { done: true; value?: undefined }
13
-
14
- interface ReadableStreamReader<R = any> extends ReadableStreamDefaultReaderImpl<R> {}
15
- interface ReadableStreamDefaultReader<R = any> extends ReadableStreamDefaultReaderImpl<R> {}
16
- interface ReadableStream<R = any> extends ReadableStreamImpl<R> {}
17
- interface ReadableStreamDefaultController<R = any> extends ReadableStreamDefaultControllerImpl<R> {}
18
-
19
- var ReadableStream: typeof ReadableStreamImpl
20
- }
21
-
22
- globalThis.ReadableStream = ReadableStreamImpl
package/lib/text-codec.ts DELETED
@@ -1,171 +0,0 @@
1
- export {} // make this a module so declare global works
2
-
3
- declare global {
4
- interface TextDecoder<T extends _Label | undefined = _Label | undefined> extends TextDecoderImpl<T> {}
5
- var TextDecoder: typeof TextDecoderImpl
6
-
7
- interface TextEncoder<T extends _Label | undefined = _Label | undefined> extends TextEncoderImpl<T> {}
8
- var TextEncoder: typeof TextEncoderImpl
9
- }
10
-
11
- const _labelMap = {
12
- 'utf-8': 'utf-8',
13
- 'utf8': 'utf-8',
14
- 'unicode-1-1-utf-8': 'utf-8',
15
- 'utf-16': 'utf-16le',
16
- 'utf-16le': 'utf-16le',
17
- 'utf-16be': 'utf-16be',
18
- } as const
19
-
20
- type _Label = keyof typeof _labelMap
21
- type _Encoding = typeof _labelMap[_Label]
22
- type _Result<T> = T extends _Label ? typeof _labelMap[T] : 'utf-8'
23
-
24
- function _normalizeLabel<T extends _Label | undefined>(label: T): _Result<T> {
25
- if (!label) return 'utf-8' as _Result<T>
26
- const result = _labelMap[label.trim().toLowerCase() as _Label]
27
- if (result) return result as _Result<T>
28
- throw new RangeError(`The encoding label "${label}" is not supported`)
29
- }
30
-
31
- function _decodeUTF8(buf: Uint8Array, fatal: boolean): string {
32
- let out = ''
33
- for (let i = 0; i < buf.length; ) {
34
- const b = buf[i++]!
35
- if (b < 0x80) { out += String.fromCharCode(b); continue }
36
- if (b < 0xC0) { out += fatal ? (() => { throw new TypeError('unexpected continuation byte') })() : '\uFFFD'; continue }
37
- let c: number, min: number
38
- if (b < 0xE0) {
39
- if (i >= buf.length) { out += fatal ? (() => { throw new TypeError('incomplete utf-8') })() : '\uFFFD'; break }
40
- const b2 = buf[i++]!
41
- if ((b2 & 0xC0) !== 0x80) { out += '\uFFFD'; continue }
42
- c = ((b & 0x1F) << 6) | (b2 & 0x3F)
43
- min = 0x80
44
- } else if (b < 0xF0) {
45
- if (i + 1 >= buf.length) { out += fatal ? (() => { throw new TypeError('incomplete utf-8') })() : '\uFFFD'; break }
46
- const b2 = buf[i++]!; const b3 = buf[i++]!
47
- if (((b2 & 0xC0) !== 0x80) || ((b3 & 0xC0) !== 0x80)) { out += '\uFFFD'; continue }
48
- c = ((b & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F)
49
- min = 0x800
50
- } else if (b < 0xF8) {
51
- if (i + 2 >= buf.length) { out += fatal ? (() => { throw new TypeError('incomplete utf-8') })() : '\uFFFD'; break }
52
- const b2 = buf[i++]!; const b3 = buf[i++]!; const b4 = buf[i++]!
53
- if (((b2 & 0xC0) !== 0x80) || ((b3 & 0xC0) !== 0x80) || ((b4 & 0xC0) !== 0x80)) { out += '\uFFFD'; continue }
54
- c = ((b & 0x07) << 18) | ((b2 & 0x3F) << 12) | ((b3 & 0x3F) << 6) | (b4 & 0x3F)
55
- min = 0x10000
56
- } else { out += fatal ? (() => { throw new TypeError('invalid utf-8 start') })() : '\uFFFD'; continue }
57
- if (c < min || (c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) {
58
- out += fatal ? (() => { throw new TypeError('invalid utf-8 codepoint') })() : '\uFFFD'; continue
59
- }
60
- if (c <= 0xFFFF) out += String.fromCharCode(c)
61
- else { c -= 0x10000; out += String.fromCharCode(0xD800 | (c >> 10), 0xDC00 | (c & 0x3FF)) }
62
- }
63
- return out
64
- }
65
-
66
- function _decodeUTF16(buf: Uint8Array, le: boolean, fatal: boolean): string {
67
- const dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
68
- let out = ''
69
- for (let i = 0; i + 1 < buf.length; i += 2) {
70
- let c = dv.getUint16(i, le)
71
- if (c >= 0xD800 && c <= 0xDBFF) {
72
- i += 2
73
- if (i + 1 >= buf.length) {
74
- out += fatal ? (() => { throw new TypeError('truncated surrogate pair') })() : '\uFFFD'; break
75
- }
76
- const c2 = dv.getUint16(i, le)
77
- if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
78
- out += String.fromCharCode(c, c2)
79
- } else {
80
- out += fatal ? (() => { throw new TypeError('invalid surrogate pair') })() : '\uFFFD'
81
- i -= 2
82
- }
83
- } else if (c >= 0xDC00 && c <= 0xDFFF) {
84
- out += fatal ? (() => { throw new TypeError('unexpected trailing surrogate') })() : '\uFFFD'
85
- } else {
86
- out += String.fromCharCode(c)
87
- }
88
- }
89
- return out
90
- }
91
-
92
- class TextDecoderImpl<T extends _Label | undefined = _Label | undefined> {
93
- _encoding: _Encoding
94
- _fatal: boolean
95
- _ignoreBOM: boolean
96
-
97
- constructor(label?: T, options?: { fatal?: boolean, ignoreBOM?: boolean }) {
98
- this._encoding = _normalizeLabel(label)
99
- this._fatal = options?.fatal ?? false
100
- this._ignoreBOM = options?.ignoreBOM ?? false
101
- }
102
-
103
- get encoding(): _Result<T> { return this._encoding as _Result<T> }
104
- get fatal(): boolean { return this._fatal }
105
- get ignoreBOM(): boolean { return this._ignoreBOM }
106
-
107
- decode(buffer?: ArrayBufferView | ArrayBuffer | null): string {
108
- if (!buffer) return ''
109
- const arr = buffer instanceof Uint8Array ? buffer
110
- : buffer instanceof ArrayBuffer ? new Uint8Array(buffer)
111
- : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
112
-
113
- if (this._encoding === 'utf-8') {
114
- return _decodeUTF8(arr, this._fatal)
115
- }
116
-
117
- const le = this._encoding === 'utf-16le'
118
- let offset = 0
119
-
120
- if (arr.length >= 2 && !this._ignoreBOM) {
121
- const bomLo = le ? arr[0]! : arr[1]!
122
- const bomHi = le ? arr[1]! : arr[0]!
123
- const bom = (bomHi << 8) | bomLo
124
- if (bom === 0xFEFF || bom === 0xFFFE) {
125
- offset = 2
126
- }
127
- }
128
-
129
- return _decodeUTF16(arr.slice(offset), le, this._fatal)
130
- }
131
- }
132
-
133
- class TextEncoderImpl<T extends _Label | undefined = _Label | undefined> {
134
- _encoding: _Encoding
135
-
136
- constructor(label?: T) {
137
- this._encoding = _normalizeLabel(label)
138
- }
139
-
140
- get encoding(): _Result<T> { return this._encoding as _Result<T> }
141
-
142
- encode(input?: string): Uint8Array<ArrayBuffer> {
143
- if (!input) return new Uint8Array(0)
144
-
145
- if (this._encoding === 'utf-8') {
146
- const bytes: number[] = []
147
- for (let i = 0; i < input.length; i++) {
148
- let c = input.charCodeAt(i)
149
- if (c < 0x80) { bytes.push(c) }
150
- else if (c < 0x800) { bytes.push(0xC0 | (c >> 6), 0x80 | (c & 0x3F)) }
151
- else if (c < 0xD800 || c >= 0xE000) { bytes.push(0xE0 | (c >> 12), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)) }
152
- else { i++; const c2 = input.charCodeAt(i); c = 0x10000 + ((c & 0x3FF) << 10) | (c2 & 0x3FF); bytes.push(0xF0 | (c >> 18), 0x80 | ((c >> 12) & 0x3F), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)) }
153
- }
154
- return new Uint8Array(bytes)
155
- }
156
-
157
- const le = this._encoding === 'utf-16le'
158
- const buf = new ArrayBuffer(input.length * 2)
159
- const dv = new DataView(buf)
160
- for (let i = 0; i < input.length; i++) dv.setUint16(i * 2, input.charCodeAt(i), le)
161
- return new Uint8Array(buf)
162
- }
163
- }
164
-
165
- if (typeof globalThis.TextDecoder === 'undefined') {
166
- globalThis.TextDecoder = TextDecoderImpl
167
- }
168
-
169
- if (typeof globalThis.TextEncoder === 'undefined') {
170
- globalThis.TextEncoder = TextEncoderImpl
171
- }
@@ -1,44 +0,0 @@
1
- import { NULL, type Ptr } from './ffi/ctype.js'
2
- import * as gui from 'gui'
3
- import { DrawText, GetDC, GetClientRect, GetWindowRect, ReleaseDC } from './windows/user32.js'
4
- import { SelectObject } from './windows/gdi32.js'
5
- import { RECT, SIZE } from './windows/structs.js'
6
- import { ButtonExtMsg, DrawTextFlag, WmMsg } from '../constants'
7
-
8
- export function measureText(hdc: Ptr<'HDC'>, text: string, maxWidth: number): { width: number; height: number } {
9
- // rect 双向位不注册 encoder(CALCRECT 就地写回):encode(初值) 新建 + DeepPartial 初值一步到位
10
- //(left/top/bottom 缺省跳过保持 0)→ 传 .ptr → decode 读回
11
- const r = RECT.encode({ right: maxWidth })
12
- DrawText(hdc, text, -1, r.ptr, DrawTextFlag.CALCRECT)
13
- const { right, bottom } = RECT.decode(r)
14
- return { width: right, height: bottom }
15
- }
16
-
17
- export function getButtonIdealSize(hwnd: gui.HWND): { width: number; height: number } {
18
- // SIZE 纯出参位(GETIDEALSIZE 就地写回):alloc → 传 .ptr → decode 读回
19
- const size = SIZE.alloc()
20
- gui.SendMessage(hwnd, ButtonExtMsg.GETIDEALSIZE, 0, size.ptr)
21
- const { cx, cy } = SIZE.decode(size)
22
- return { width: cx, height: cy }
23
- }
24
-
25
- export function measureTextForHwnd(hwnd: gui.HWND, text: string): { width: number; height: number } {
26
- const hdc = GetDC(hwnd)
27
- if (!hdc) return { width: 0, height: 0 }
28
- // SendMessage 返回 LRESULT 裸数;GETFONT 语义为 HFONT——断言进 GDI 对象品牌链
29
- const hFont = gui.SendMessage(hwnd, WmMsg.GETFONT, 0, 0) as Ptr<'HGDIOBJ'>
30
- const oldFont = hFont ? SelectObject(hdc, hFont) : NULL
31
- const result = measureText(hdc, text, 0)
32
- if (hFont) SelectObject(hdc, oldFont)
33
- ReleaseDC(hwnd, hdc)
34
- const wr = RECT.alloc()
35
- const cr = RECT.alloc()
36
- const okWr = GetWindowRect(hwnd, wr.ptr)
37
- const okCr = GetClientRect(hwnd, cr.ptr)
38
- if (okWr && okCr) {
39
- const w = RECT.decode(wr), c = RECT.decode(cr)
40
- result.width += (w.right - w.left) - (c.right - c.left)
41
- result.height += (w.bottom - w.top) - (c.bottom - c.top)
42
- }
43
- return result
44
- }