@glassly/miniapp 0.3.0-dev.2 → 0.3.0-dev.462

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 (255) hide show
  1. package/README.md +219 -74
  2. package/dist/background/index.d.ts +14 -8
  3. package/dist/background/index.d.ts.map +1 -1
  4. package/dist/background/index.js +4 -0
  5. package/dist/background/index.js.map +1 -1
  6. package/dist/background/register.d.ts +1 -1
  7. package/dist/background/register.js +1 -1
  8. package/dist/display/index.d.ts +23 -0
  9. package/dist/display/index.d.ts.map +1 -0
  10. package/dist/display/index.js +17 -0
  11. package/dist/display/index.js.map +1 -0
  12. package/dist/display/measurer/TextMeasurer.d.ts +160 -0
  13. package/dist/display/measurer/TextMeasurer.d.ts.map +1 -0
  14. package/dist/display/measurer/TextMeasurer.js +272 -0
  15. package/dist/display/measurer/TextMeasurer.js.map +1 -0
  16. package/dist/display/measurer/index.d.ts +10 -0
  17. package/dist/display/measurer/index.d.ts.map +1 -0
  18. package/dist/display/measurer/index.js +11 -0
  19. package/dist/display/measurer/index.js.map +1 -0
  20. package/dist/display/measurer/script-detection.d.ts +53 -0
  21. package/dist/display/measurer/script-detection.d.ts.map +1 -0
  22. package/dist/display/measurer/script-detection.js +184 -0
  23. package/dist/display/measurer/script-detection.js.map +1 -0
  24. package/dist/display/pixelArt.d.ts +22 -0
  25. package/dist/display/pixelArt.d.ts.map +1 -0
  26. package/dist/display/pixelArt.js +126 -0
  27. package/dist/display/pixelArt.js.map +1 -0
  28. package/dist/display/profiles/g1.d.ts +33 -0
  29. package/dist/display/profiles/g1.d.ts.map +1 -0
  30. package/dist/display/profiles/g1.js +214 -0
  31. package/dist/display/profiles/g1.js.map +1 -0
  32. package/dist/display/profiles/g2.d.ts +13 -0
  33. package/dist/display/profiles/g2.d.ts.map +1 -0
  34. package/dist/display/profiles/g2.js +34 -0
  35. package/dist/display/profiles/g2.js.map +1 -0
  36. package/dist/display/profiles/index.d.ts +12 -0
  37. package/dist/display/profiles/index.d.ts.map +1 -0
  38. package/dist/display/profiles/index.js +15 -0
  39. package/dist/display/profiles/index.js.map +1 -0
  40. package/dist/display/profiles/nex.d.ts +10 -0
  41. package/dist/display/profiles/nex.d.ts.map +1 -0
  42. package/dist/display/profiles/nex.js +150 -0
  43. package/dist/display/profiles/nex.js.map +1 -0
  44. package/dist/display/profiles/types.d.ts +103 -0
  45. package/dist/display/profiles/types.d.ts.map +1 -0
  46. package/dist/display/profiles/types.js +2 -0
  47. package/dist/display/profiles/types.js.map +1 -0
  48. package/dist/display/profiles/z100.d.ts +33 -0
  49. package/dist/display/profiles/z100.d.ts.map +1 -0
  50. package/dist/display/profiles/z100.js +182 -0
  51. package/dist/display/profiles/z100.js.map +1 -0
  52. package/dist/display/scene/types.d.ts +292 -0
  53. package/dist/display/scene/types.d.ts.map +1 -0
  54. package/dist/display/scene/types.js +0 -0
  55. package/dist/display/scene/types.js.map +1 -0
  56. package/dist/display/wrapper/TextWrapper.d.ts +102 -0
  57. package/dist/display/wrapper/TextWrapper.d.ts.map +1 -0
  58. package/dist/display/wrapper/TextWrapper.js +469 -0
  59. package/dist/display/wrapper/TextWrapper.js.map +1 -0
  60. package/dist/display/wrapper/index.d.ts +12 -0
  61. package/dist/display/wrapper/index.d.ts.map +1 -0
  62. package/dist/display/wrapper/index.js +12 -0
  63. package/dist/display/wrapper/index.js.map +1 -0
  64. package/dist/display/wrapper/types.d.ts +75 -0
  65. package/dist/display/wrapper/types.d.ts.map +1 -0
  66. package/dist/display/wrapper/types.js +11 -0
  67. package/dist/display/wrapper/types.js.map +1 -0
  68. package/dist/globals.d.ts +5 -5
  69. package/dist/globals.d.ts.map +1 -1
  70. package/dist/globals.js +1 -1
  71. package/dist/globals.js.map +1 -1
  72. package/dist/hardware.d.ts +4 -18
  73. package/dist/hardware.d.ts.map +1 -1
  74. package/dist/hardware.js +2 -13
  75. package/dist/hardware.js.map +1 -1
  76. package/dist/index.d.ts +16 -10
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +3 -1
  79. package/dist/index.js.map +1 -1
  80. package/dist/knownApps.d.ts +26 -0
  81. package/dist/knownApps.d.ts.map +1 -0
  82. package/dist/knownApps.js +140 -0
  83. package/dist/knownApps.js.map +1 -0
  84. package/dist/mirror/canvas.d.ts +22 -0
  85. package/dist/mirror/canvas.d.ts.map +1 -0
  86. package/dist/mirror/canvas.js +311 -0
  87. package/dist/mirror/canvas.js.map +1 -0
  88. package/dist/mirror/geometry.d.ts +69 -0
  89. package/dist/mirror/geometry.d.ts.map +1 -0
  90. package/dist/mirror/geometry.js +75 -0
  91. package/dist/mirror/geometry.js.map +1 -0
  92. package/dist/mirror/index.d.ts +3 -0
  93. package/dist/mirror/index.d.ts.map +1 -0
  94. package/dist/mirror/index.js +2 -0
  95. package/dist/mirror/index.js.map +1 -0
  96. package/dist/mirror/types.d.ts +40 -0
  97. package/dist/mirror/types.d.ts.map +1 -0
  98. package/dist/mirror/types.js +2 -0
  99. package/dist/mirror/types.js.map +1 -0
  100. package/dist/modules/auth.d.ts +1 -1
  101. package/dist/modules/auth.d.ts.map +1 -1
  102. package/dist/modules/auth.js +2 -2
  103. package/dist/modules/auth.js.map +1 -1
  104. package/dist/modules/compass.d.ts +47 -0
  105. package/dist/modules/compass.d.ts.map +1 -0
  106. package/dist/modules/compass.js +39 -0
  107. package/dist/modules/compass.js.map +1 -0
  108. package/dist/modules/display.d.ts +192 -8
  109. package/dist/modules/display.d.ts.map +1 -1
  110. package/dist/modules/display.js +40 -3
  111. package/dist/modules/display.js.map +1 -1
  112. package/dist/modules/events.d.ts +30 -2
  113. package/dist/modules/events.d.ts.map +1 -1
  114. package/dist/modules/events.js.map +1 -1
  115. package/dist/modules/input.d.ts +40 -1
  116. package/dist/modules/input.d.ts.map +1 -1
  117. package/dist/modules/input.js +45 -1
  118. package/dist/modules/input.js.map +1 -1
  119. package/dist/modules/latest-render-scheduler.d.ts +19 -0
  120. package/dist/modules/latest-render-scheduler.d.ts.map +1 -0
  121. package/dist/modules/latest-render-scheduler.js +54 -0
  122. package/dist/modules/latest-render-scheduler.js.map +1 -0
  123. package/dist/modules/lightSensor.d.ts +44 -0
  124. package/dist/modules/lightSensor.d.ts.map +1 -0
  125. package/dist/modules/lightSensor.js +33 -0
  126. package/dist/modules/lightSensor.js.map +1 -0
  127. package/dist/modules/mic.d.ts +3 -2
  128. package/dist/modules/mic.d.ts.map +1 -1
  129. package/dist/modules/mic.js +3 -2
  130. package/dist/modules/mic.js.map +1 -1
  131. package/dist/modules/miniapps.d.ts +2 -0
  132. package/dist/modules/miniapps.d.ts.map +1 -1
  133. package/dist/modules/miniapps.js.map +1 -1
  134. package/dist/modules/mirror.d.ts +16 -0
  135. package/dist/modules/mirror.d.ts.map +1 -0
  136. package/dist/modules/mirror.js +40 -0
  137. package/dist/modules/mirror.js.map +1 -0
  138. package/dist/modules/mono-text-rows.d.ts +20 -0
  139. package/dist/modules/mono-text-rows.d.ts.map +1 -0
  140. package/dist/modules/mono-text-rows.js +26 -0
  141. package/dist/modules/mono-text-rows.js.map +1 -0
  142. package/dist/modules/navigation.d.ts.map +1 -1
  143. package/dist/modules/navigation.js +21 -14
  144. package/dist/modules/navigation.js.map +1 -1
  145. package/dist/modules/phone.d.ts +26 -1
  146. package/dist/modules/phone.d.ts.map +1 -1
  147. package/dist/modules/phone.js +28 -0
  148. package/dist/modules/phone.js.map +1 -1
  149. package/dist/modules/settings.d.ts +45 -0
  150. package/dist/modules/settings.d.ts.map +1 -0
  151. package/dist/modules/settings.js +54 -0
  152. package/dist/modules/settings.js.map +1 -0
  153. package/dist/modules/system.d.ts +21 -0
  154. package/dist/modules/system.d.ts.map +1 -1
  155. package/dist/modules/system.js +12 -0
  156. package/dist/modules/system.js.map +1 -1
  157. package/dist/modules/transcription.d.ts +3 -2
  158. package/dist/modules/transcription.d.ts.map +1 -1
  159. package/dist/modules/transcription.js +3 -2
  160. package/dist/modules/transcription.js.map +1 -1
  161. package/dist/protocol.d.ts +65 -5
  162. package/dist/protocol.d.ts.map +1 -1
  163. package/dist/protocol.js +66 -5
  164. package/dist/protocol.js.map +1 -1
  165. package/dist/react/MirrorCanvas.d.ts +10 -0
  166. package/dist/react/MirrorCanvas.d.ts.map +1 -0
  167. package/dist/react/MirrorCanvas.js +23 -0
  168. package/dist/react/MirrorCanvas.js.map +1 -0
  169. package/dist/react/index.d.ts +2 -0
  170. package/dist/react/index.d.ts.map +1 -1
  171. package/dist/react/index.js +1 -0
  172. package/dist/react/index.js.map +1 -1
  173. package/dist/react/useColorScheme.d.ts +2 -3
  174. package/dist/react/useColorScheme.d.ts.map +1 -1
  175. package/dist/react/useColorScheme.js +15 -8
  176. package/dist/react/useColorScheme.js.map +1 -1
  177. package/dist/react/useRpc.js +2 -2
  178. package/dist/react/useRpc.js.map +1 -1
  179. package/dist/react/useSafeArea.d.ts.map +1 -1
  180. package/dist/react/useSafeArea.js +2 -2
  181. package/dist/react/useSafeArea.js.map +1 -1
  182. package/dist/session.d.ts +50 -4
  183. package/dist/session.d.ts.map +1 -1
  184. package/dist/session.js +35 -6
  185. package/dist/session.js.map +1 -1
  186. package/dist/transport/auto.d.ts +2 -2
  187. package/dist/transport/auto.js +2 -2
  188. package/dist/transport/dispatch.d.ts +1 -1
  189. package/dist/transport/dispatch.js +1 -1
  190. package/dist/transport/mock.d.ts +4 -4
  191. package/dist/transport/mock.d.ts.map +1 -1
  192. package/dist/transport/mock.js +31 -7
  193. package/dist/transport/mock.js.map +1 -1
  194. package/dist/ui/index.d.ts +2 -0
  195. package/dist/ui/index.d.ts.map +1 -1
  196. package/dist/ui/index.js +1 -0
  197. package/dist/ui/index.js.map +1 -1
  198. package/package.json +19 -3
  199. package/src/background/index.ts +26 -4
  200. package/src/background/register.ts +1 -1
  201. package/src/display/index.ts +83 -0
  202. package/src/display/measurer/TextMeasurer.ts +346 -0
  203. package/src/display/measurer/index.ts +21 -0
  204. package/src/display/measurer/script-detection.ts +218 -0
  205. package/src/display/pixelArt.ts +141 -0
  206. package/src/display/profiles/g1.ts +237 -0
  207. package/src/display/profiles/g2.ts +39 -0
  208. package/src/display/profiles/index.ts +28 -0
  209. package/src/display/profiles/nex.ts +166 -0
  210. package/src/display/profiles/types.ts +132 -0
  211. package/src/display/profiles/z100.ts +198 -0
  212. package/src/display/scene/types.ts +335 -0
  213. package/src/display/wrapper/TextWrapper.ts +546 -0
  214. package/src/display/wrapper/index.ts +15 -0
  215. package/src/display/wrapper/types.ts +102 -0
  216. package/src/globals.ts +5 -5
  217. package/src/hardware.ts +6 -21
  218. package/src/index.ts +34 -6
  219. package/src/knownApps.ts +170 -0
  220. package/src/mirror/canvas.ts +322 -0
  221. package/src/mirror/geometry.ts +128 -0
  222. package/src/mirror/index.ts +2 -0
  223. package/src/mirror/types.ts +38 -0
  224. package/src/modules/auth.ts +2 -2
  225. package/src/modules/compass.ts +69 -0
  226. package/src/modules/display.ts +222 -12
  227. package/src/modules/events.ts +27 -1
  228. package/src/modules/input.ts +55 -2
  229. package/src/modules/latest-render-scheduler.ts +55 -0
  230. package/src/modules/lightSensor.ts +61 -0
  231. package/src/modules/mic.ts +3 -2
  232. package/src/modules/miniapps.ts +2 -0
  233. package/src/modules/mirror.ts +42 -0
  234. package/src/modules/mono-text-rows.ts +30 -0
  235. package/src/modules/navigation.ts +20 -14
  236. package/src/modules/phone.ts +40 -1
  237. package/src/modules/settings.ts +64 -0
  238. package/src/modules/system.ts +30 -1
  239. package/src/modules/transcription.ts +3 -2
  240. package/src/protocol.ts +72 -5
  241. package/src/react/MirrorCanvas.tsx +35 -0
  242. package/src/react/index.ts +3 -0
  243. package/src/react/useColorScheme.ts +17 -8
  244. package/src/react/useRpc.ts +2 -2
  245. package/src/react/useSafeArea.ts +2 -2
  246. package/src/session.ts +78 -8
  247. package/src/transport/auto.ts +2 -2
  248. package/src/transport/dispatch.ts +1 -1
  249. package/src/transport/mock.ts +42 -11
  250. package/src/ui/index.ts +3 -0
  251. package/dist/modules/heading.d.ts +0 -16
  252. package/dist/modules/heading.d.ts.map +0 -1
  253. package/dist/modules/heading.js +0 -20
  254. package/dist/modules/heading.js.map +0 -1
  255. package/src/modules/heading.ts +0 -23
@@ -0,0 +1,546 @@
1
+ import {TextMeasurer} from "../measurer/TextMeasurer"
2
+ import {isCJKCharacter, needsHyphenForBreak} from "../measurer/script-detection"
3
+ import type {WrapOptions, WrapResult, LineMetrics, BreakMode} from "./types"
4
+ import {DEFAULT_WRAP_OPTIONS} from "./types"
5
+
6
+ /**
7
+ * Wraps text to fit display constraints.
8
+ *
9
+ * Supports multiple break modes:
10
+ * - 'character': Break mid-word with hyphen for 100% line utilization
11
+ * - 'word': Break at word boundaries, hyphenate only if word > line width
12
+ * - 'strict-word': Break at word boundaries only, no hyphenation
13
+ *
14
+ * Key features:
15
+ * - Pixel-accurate wrapping (no abstract units)
16
+ * - Hyphen-aware breaking (accounts for hyphen width)
17
+ * - CJK support (breaks anywhere without hyphen)
18
+ * - Preserves explicit newlines
19
+ * - Respects byte limits for BLE transmission
20
+ */
21
+ export class TextWrapper {
22
+ private readonly measurer: TextMeasurer
23
+ private readonly defaultOptions: Required<WrapOptions>
24
+
25
+ constructor(measurer: TextMeasurer, defaultOptions?: WrapOptions) {
26
+ this.measurer = measurer
27
+
28
+ const profile = measurer.getProfile()
29
+ this.defaultOptions = {
30
+ maxWidthPx: profile.displayWidthPx,
31
+ maxLines: profile.maxLines,
32
+ maxBytes: profile.maxPayloadBytes,
33
+ ...DEFAULT_WRAP_OPTIONS,
34
+ ...defaultOptions,
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Wrap text to fit within constraints.
40
+ *
41
+ * @param text - Text to wrap (may contain \n for explicit breaks)
42
+ * @param options - Override default options
43
+ * @returns Wrap result with lines and metadata
44
+ */
45
+ wrap(text: string, options?: WrapOptions): WrapResult {
46
+ const opts = this.mergeOptions(options)
47
+ const {maxWidthPx, maxLines, maxBytes, breakMode, preserveNewlines} = opts
48
+
49
+ if (!text || text.length === 0) {
50
+ return this.createEmptyResult(opts)
51
+ }
52
+
53
+ // Split by explicit newlines if preserving them
54
+ const paragraphs = preserveNewlines ? text.split("\n") : [text]
55
+
56
+ const allLines: string[] = []
57
+ const allMetrics: LineMetrics[] = []
58
+ let totalBytes = 0
59
+ let truncated = false
60
+
61
+ for (let pIndex = 0; pIndex < paragraphs.length; pIndex++) {
62
+ const paragraph = paragraphs[pIndex]
63
+ const isFromNewline = pIndex > 0
64
+
65
+ // Wrap this paragraph
66
+ const paragraphLines = this.wrapParagraph(paragraph, maxWidthPx, breakMode, opts)
67
+
68
+ for (let lIndex = 0; lIndex < paragraphLines.length; lIndex++) {
69
+ const line = paragraphLines[lIndex]
70
+ const lineBytes = this.measurer.getByteSize(line)
71
+
72
+ // Check if we've exceeded limits
73
+ if (allLines.length >= maxLines) {
74
+ truncated = true
75
+ break
76
+ }
77
+
78
+ if (totalBytes + lineBytes > maxBytes) {
79
+ truncated = true
80
+ break
81
+ }
82
+
83
+ const widthPx = this.measurer.measureText(line)
84
+ const metrics: LineMetrics = {
85
+ text: line,
86
+ widthPx,
87
+ bytes: lineBytes,
88
+ utilizationPercent: Math.round((widthPx / maxWidthPx) * 100),
89
+ endsWithHyphen: line.endsWith(opts.hyphenChar) && lIndex < paragraphLines.length - 1,
90
+ fromExplicitNewline: isFromNewline && lIndex === 0,
91
+ }
92
+
93
+ allLines.push(line)
94
+ allMetrics.push(metrics)
95
+ totalBytes += lineBytes
96
+
97
+ // Add newline byte between lines
98
+ if (allLines.length < maxLines) {
99
+ totalBytes += 1 // \n is 1 byte
100
+ }
101
+ }
102
+
103
+ if (truncated) break
104
+ }
105
+
106
+ // Find max line width
107
+ const maxLineWidthPx = allMetrics.reduce((max, m) => Math.max(max, m.widthPx), 0)
108
+
109
+ return {
110
+ lines: allLines,
111
+ truncated,
112
+ maxLineWidthPx,
113
+ totalBytes,
114
+ lineMetrics: allMetrics,
115
+ originalText: text,
116
+ breakMode,
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Simple wrap returning just lines (convenience method).
122
+ *
123
+ * @param text - Text to wrap
124
+ * @param options - Override default options
125
+ * @returns Array of wrapped lines
126
+ */
127
+ wrapToLines(text: string, options?: WrapOptions): string[] {
128
+ return this.wrap(text, options).lines
129
+ }
130
+
131
+ /**
132
+ * Check if text needs wrapping.
133
+ *
134
+ * @param text - Text to check
135
+ * @param maxWidthPx - Optional width override
136
+ * @returns true if text exceeds single line
137
+ */
138
+ needsWrap(text: string, maxWidthPx?: number): boolean {
139
+ const width = maxWidthPx ?? this.defaultOptions.maxWidthPx
140
+ return this.measurer.measureText(text) > width || text.includes("\n")
141
+ }
142
+
143
+ /**
144
+ * Get current default options.
145
+ */
146
+ getOptions(): Required<WrapOptions> {
147
+ return {...this.defaultOptions}
148
+ }
149
+
150
+ /**
151
+ * Get the measurer instance.
152
+ */
153
+ getMeasurer(): TextMeasurer {
154
+ return this.measurer
155
+ }
156
+
157
+ /**
158
+ * Wrap a single paragraph (no newlines) according to break mode.
159
+ */
160
+ private wrapParagraph(
161
+ paragraph: string,
162
+ maxWidthPx: number,
163
+ breakMode: BreakMode,
164
+ opts: Required<WrapOptions>,
165
+ ): string[] {
166
+ const trimmed = opts.trimLines ? paragraph.trim() : paragraph
167
+
168
+ if (!trimmed) {
169
+ return [""]
170
+ }
171
+
172
+ // Check if it fits on one line
173
+ if (this.measurer.fitsInWidth(trimmed, maxWidthPx)) {
174
+ return [trimmed]
175
+ }
176
+
177
+ switch (breakMode) {
178
+ case "character":
179
+ return this.wrapCharacterMode(trimmed, maxWidthPx, opts)
180
+ case "character-no-hyphen":
181
+ return this.wrapCharacterNoHyphenMode(trimmed, maxWidthPx, opts)
182
+ case "word":
183
+ return this.wrapWordMode(trimmed, maxWidthPx, opts)
184
+ case "strict-word":
185
+ return this.wrapStrictWordMode(trimmed, maxWidthPx, opts)
186
+ default:
187
+ return this.wrapCharacterMode(trimmed, maxWidthPx, opts)
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Character break mode: Break mid-word with hyphen for 100% line utilization.
193
+ */
194
+ private wrapCharacterMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
195
+ return this.wrapCharacterModeInternal(text, maxWidthPx, opts, true)
196
+ }
197
+
198
+ /**
199
+ * Character break mode without hyphen: Break mid-word cleanly without adding hyphen.
200
+ */
201
+ private wrapCharacterNoHyphenMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
202
+ return this.wrapCharacterModeInternal(text, maxWidthPx, opts, false)
203
+ }
204
+
205
+ /**
206
+ * Internal character break implementation with configurable hyphenation.
207
+ */
208
+ private wrapCharacterModeInternal(
209
+ text: string,
210
+ maxWidthPx: number,
211
+ opts: Required<WrapOptions>,
212
+ useHyphen: boolean,
213
+ ): string[] {
214
+ const lines: string[] = []
215
+ const hyphenWidth = this.measurer.measureChar(opts.hyphenChar)
216
+
217
+ let currentLine = ""
218
+ let currentWidth = 0
219
+
220
+ const chars = Array.from(text)
221
+
222
+ for (let i = 0; i < chars.length; i++) {
223
+ const char = chars[i]
224
+ const charWidth = this.measurer.measureChar(char)
225
+
226
+ // Would this character fit?
227
+ if (currentWidth + charWidth <= maxWidthPx) {
228
+ currentLine += char
229
+ currentWidth += charWidth
230
+ } else {
231
+ // Character doesn't fit - need to break
232
+
233
+ // Determine if we need a hyphen
234
+ const prevChar = currentLine.length > 0 ? currentLine[currentLine.length - 1] : ""
235
+ const needsHyphen =
236
+ useHyphen && currentLine.length >= opts.minCharsBeforeHyphen && needsHyphenForBreak(prevChar, char)
237
+
238
+ if (needsHyphen) {
239
+ // Back off characters until hyphen fits
240
+ const result = this.backoffForHyphen(currentLine, currentWidth, maxWidthPx, hyphenWidth, opts)
241
+
242
+ if (result.skipHyphen) {
243
+ // Found a natural word boundary while backing off - no hyphen needed
244
+ lines.push(result.line)
245
+ } else {
246
+ lines.push(result.line + opts.hyphenChar)
247
+ }
248
+
249
+ // Start new line with backed-off chars + current char
250
+ currentLine = result.remainder + char
251
+ currentWidth = this.measurer.measureText(currentLine)
252
+ } else if (useHyphen === false && !isCJKCharacter(char) && char !== " ") {
253
+ // No-hyphen mode: just break cleanly without hyphen
254
+ const trimmedLine = opts.trimLines ? currentLine.trimEnd() : currentLine
255
+ if (trimmedLine) {
256
+ lines.push(trimmedLine)
257
+ }
258
+ currentLine = char
259
+ currentWidth = charWidth
260
+ } else {
261
+ // No hyphen needed (CJK, space, punctuation)
262
+ const trimmedLine = opts.trimLines ? currentLine.trimEnd() : currentLine
263
+ if (trimmedLine) {
264
+ lines.push(trimmedLine)
265
+ }
266
+ currentLine = char === " " ? "" : char
267
+ currentWidth = char === " " ? 0 : charWidth
268
+ }
269
+ }
270
+ }
271
+
272
+ // Add remaining text
273
+ if (currentLine) {
274
+ const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine
275
+ if (trimmedLine) {
276
+ lines.push(trimmedLine)
277
+ }
278
+ }
279
+
280
+ return lines.length > 0 ? lines : [""]
281
+ }
282
+
283
+ /**
284
+ * Word break mode: Break at word boundaries, hyphenate only if word > line width.
285
+ */
286
+ private wrapWordMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
287
+ const lines: string[] = []
288
+ const words = this.splitIntoWords(text)
289
+ const spaceWidth = this.measurer.getSpaceWidth()
290
+
291
+ let currentLine = ""
292
+ let currentWidth = 0
293
+
294
+ for (const word of words) {
295
+ const wordWidth = this.measurer.measureText(word)
296
+ const needsSpace = currentLine.length > 0
297
+ const totalWidth = currentWidth + (needsSpace ? spaceWidth : 0) + wordWidth
298
+
299
+ if (totalWidth <= maxWidthPx) {
300
+ // Word fits on current line
301
+ if (needsSpace) {
302
+ currentLine += " "
303
+ currentWidth += spaceWidth
304
+ }
305
+ currentLine += word
306
+ currentWidth += wordWidth
307
+ } else {
308
+ // Word doesn't fit
309
+ if (currentLine.length > 0) {
310
+ // Push current line and start new one
311
+ lines.push(opts.trimLines ? currentLine.trim() : currentLine)
312
+ currentLine = ""
313
+ currentWidth = 0
314
+ }
315
+
316
+ // Check if word itself exceeds line width
317
+ if (wordWidth > maxWidthPx) {
318
+ // Word is too long - hyphenate it
319
+ const hyphenatedLines = this.hyphenateLongWord(word, maxWidthPx, opts)
320
+ for (let i = 0; i < hyphenatedLines.length - 1; i++) {
321
+ lines.push(hyphenatedLines[i])
322
+ }
323
+ // Last piece becomes start of current line
324
+ currentLine = hyphenatedLines[hyphenatedLines.length - 1]
325
+ currentWidth = this.measurer.measureText(currentLine)
326
+ } else {
327
+ // Word fits on new line
328
+ currentLine = word
329
+ currentWidth = wordWidth
330
+ }
331
+ }
332
+ }
333
+
334
+ // Add remaining text
335
+ if (currentLine) {
336
+ const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine
337
+ if (trimmedLine) {
338
+ lines.push(trimmedLine)
339
+ }
340
+ }
341
+
342
+ return lines.length > 0 ? lines : [""]
343
+ }
344
+
345
+ /**
346
+ * Strict word break mode: Break at word boundaries only, no hyphenation.
347
+ * Long words will overflow the line.
348
+ */
349
+ private wrapStrictWordMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
350
+ const lines: string[] = []
351
+ const words = this.splitIntoWords(text)
352
+ const spaceWidth = this.measurer.getSpaceWidth()
353
+
354
+ let currentLine = ""
355
+ let currentWidth = 0
356
+
357
+ for (const word of words) {
358
+ const wordWidth = this.measurer.measureText(word)
359
+ const needsSpace = currentLine.length > 0
360
+ const totalWidth = currentWidth + (needsSpace ? spaceWidth : 0) + wordWidth
361
+
362
+ if (totalWidth <= maxWidthPx) {
363
+ // Word fits on current line
364
+ if (needsSpace) {
365
+ currentLine += " "
366
+ currentWidth += spaceWidth
367
+ }
368
+ currentLine += word
369
+ currentWidth += wordWidth
370
+ } else {
371
+ // Word doesn't fit
372
+ if (currentLine.length > 0) {
373
+ // Push current line
374
+ lines.push(opts.trimLines ? currentLine.trim() : currentLine)
375
+ }
376
+ // Start new line with word (even if it overflows)
377
+ currentLine = word
378
+ currentWidth = wordWidth
379
+ }
380
+ }
381
+
382
+ // Add remaining text
383
+ if (currentLine) {
384
+ const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine
385
+ if (trimmedLine) {
386
+ lines.push(trimmedLine)
387
+ }
388
+ }
389
+
390
+ return lines.length > 0 ? lines : [""]
391
+ }
392
+
393
+ /**
394
+ * Split text into words, handling CJK characters specially.
395
+ * CJK characters are treated as individual "words" since they can break anywhere.
396
+ */
397
+ private splitIntoWords(text: string): string[] {
398
+ const words: string[] = []
399
+ let currentWord = ""
400
+
401
+ for (const char of text) {
402
+ if (char === " " || char === "\t") {
403
+ if (currentWord) {
404
+ words.push(currentWord)
405
+ currentWord = ""
406
+ }
407
+ } else if (isCJKCharacter(char)) {
408
+ // CJK characters are individual words
409
+ if (currentWord) {
410
+ words.push(currentWord)
411
+ currentWord = ""
412
+ }
413
+ words.push(char)
414
+ } else {
415
+ currentWord += char
416
+ }
417
+ }
418
+
419
+ if (currentWord) {
420
+ words.push(currentWord)
421
+ }
422
+
423
+ return words
424
+ }
425
+
426
+ /**
427
+ * Hyphenate a word that's too long to fit on a single line.
428
+ */
429
+ private hyphenateLongWord(word: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
430
+ const lines: string[] = []
431
+ const hyphenWidth = this.measurer.measureChar(opts.hyphenChar)
432
+ const chars = Array.from(word)
433
+
434
+ let currentLine = ""
435
+ let currentWidth = 0
436
+
437
+ for (let i = 0; i < chars.length; i++) {
438
+ const char = chars[i]
439
+ const charWidth = this.measurer.measureChar(char)
440
+ const isLastChar = i === chars.length - 1
441
+
442
+ // Check if we need to break (leaving room for hyphen if not last char)
443
+ const widthNeeded = isLastChar ? charWidth : charWidth + hyphenWidth
444
+
445
+ if (currentWidth + widthNeeded <= maxWidthPx) {
446
+ currentLine += char
447
+ currentWidth += charWidth
448
+ } else {
449
+ // Need to break - back off if needed to fit hyphen
450
+ if (currentLine.length >= opts.minCharsBeforeHyphen) {
451
+ const result = this.backoffForHyphen(currentLine, currentWidth, maxWidthPx, hyphenWidth, opts)
452
+ if (result.skipHyphen) {
453
+ lines.push(result.line)
454
+ } else {
455
+ lines.push(result.line + opts.hyphenChar)
456
+ }
457
+ currentLine = result.remainder + char
458
+ currentWidth = this.measurer.measureText(currentLine)
459
+ } else {
460
+ // Not enough chars for hyphen, just break
461
+ if (currentLine) {
462
+ lines.push(currentLine)
463
+ }
464
+ currentLine = char
465
+ currentWidth = charWidth
466
+ }
467
+ }
468
+ }
469
+
470
+ if (currentLine) {
471
+ lines.push(currentLine)
472
+ }
473
+
474
+ return lines.length > 0 ? lines : [word]
475
+ }
476
+
477
+ /**
478
+ * Back off characters from line end until hyphen fits.
479
+ * Returns null if we back off to a space (natural break point - no hyphen needed).
480
+ */
481
+ private backoffForHyphen(
482
+ line: string,
483
+ lineWidth: number,
484
+ maxWidthPx: number,
485
+ hyphenWidth: number,
486
+ opts: Required<WrapOptions>,
487
+ ): {line: string; remainder: string; skipHyphen: boolean} {
488
+ let adjustedLine = line
489
+ let adjustedWidth = lineWidth
490
+ let remainder = ""
491
+
492
+ // Remove characters until hyphen fits
493
+ while (adjustedWidth + hyphenWidth > maxWidthPx && adjustedLine.length > opts.minCharsBeforeHyphen) {
494
+ const lastChar = adjustedLine[adjustedLine.length - 1]
495
+ const lastCharWidth = this.measurer.measureChar(lastChar)
496
+
497
+ adjustedLine = adjustedLine.slice(0, -1)
498
+ adjustedWidth -= lastCharWidth
499
+ remainder = lastChar + remainder
500
+
501
+ // If we've backed off to a space, we've found a natural word boundary
502
+ // No hyphen is needed - just trim the space and break there
503
+ if (adjustedLine.length > 0 && adjustedLine[adjustedLine.length - 1] === " ") {
504
+ // Trim trailing space from the line
505
+ adjustedLine = adjustedLine.trimEnd()
506
+ return {line: adjustedLine, remainder, skipHyphen: true}
507
+ }
508
+ }
509
+
510
+ return {line: adjustedLine, remainder, skipHyphen: false}
511
+ }
512
+
513
+ /**
514
+ * Merge user options with defaults.
515
+ */
516
+ private mergeOptions(options?: WrapOptions): Required<WrapOptions> {
517
+ return {
518
+ ...this.defaultOptions,
519
+ ...options,
520
+ }
521
+ }
522
+
523
+ /**
524
+ * Create an empty result for empty input.
525
+ */
526
+ private createEmptyResult(opts: Required<WrapOptions>): WrapResult {
527
+ return {
528
+ lines: [""],
529
+ truncated: false,
530
+ maxLineWidthPx: 0,
531
+ totalBytes: 0,
532
+ lineMetrics: [
533
+ {
534
+ text: "",
535
+ widthPx: 0,
536
+ bytes: 0,
537
+ utilizationPercent: 0,
538
+ endsWithHyphen: false,
539
+ fromExplicitNewline: false,
540
+ },
541
+ ],
542
+ originalText: "",
543
+ breakMode: opts.breakMode,
544
+ }
545
+ }
546
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Text Wrapper Module
3
+ *
4
+ * Provides text wrapping with multiple break modes:
5
+ * - 'character': Break mid-word with hyphen for 100% line utilization
6
+ * - 'word': Break at word boundaries, hyphenate only if word > line width
7
+ * - 'strict-word': Break at word boundaries only, no hyphenation
8
+ */
9
+
10
+ // Main class
11
+ export {TextWrapper} from "./TextWrapper"
12
+
13
+ // Types
14
+ export type {WrapOptions, WrapResult, LineMetrics, BreakMode} from "./types"
15
+ export {DEFAULT_WRAP_OPTIONS} from "./types"
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Options for text wrapping.
3
+ */
4
+ export interface WrapOptions {
5
+ /** Maximum width in pixels (defaults to profile's displayWidthPx) */
6
+ maxWidthPx?: number
7
+
8
+ /** Maximum number of lines (defaults to profile's maxLines) */
9
+ maxLines?: number
10
+
11
+ /** Maximum total bytes (defaults to profile's maxPayloadBytes) */
12
+ maxBytes?: number
13
+
14
+ /**
15
+ * Break mode:
16
+ * - 'character': Break mid-word with hyphen for 100% utilization
17
+ * - 'word': Break at word boundaries, hyphenate only if word > line
18
+ * - 'strict-word': Break at word boundaries only, truncate long words
19
+ */
20
+ breakMode?: BreakMode
21
+
22
+ /** Character to use for hyphenation (default: '-') */
23
+ hyphenChar?: string
24
+
25
+ /** Minimum characters before allowing hyphen break (default: 3) */
26
+ minCharsBeforeHyphen?: number
27
+
28
+ /** Whether to trim whitespace from line ends (default: true) */
29
+ trimLines?: boolean
30
+
31
+ /** Whether to preserve explicit newlines in input (default: true) */
32
+ preserveNewlines?: boolean
33
+ }
34
+
35
+ /**
36
+ * Break mode for text wrapping.
37
+ * - 'character': Break mid-word with hyphen for 100% utilization
38
+ * - 'character-no-hyphen': Break mid-word without hyphen (clean break)
39
+ * - 'word': Break at word boundaries, hyphenate only if word > line
40
+ * - 'strict-word': Break at word boundaries only, truncate long words
41
+ */
42
+ export type BreakMode = "character" | "character-no-hyphen" | "word" | "strict-word"
43
+
44
+ /**
45
+ * Result of wrapping operation.
46
+ */
47
+ export interface WrapResult {
48
+ /** Wrapped lines */
49
+ lines: string[]
50
+
51
+ /** Whether content was truncated to fit constraints */
52
+ truncated: boolean
53
+
54
+ /** Total pixel width of widest line */
55
+ maxLineWidthPx: number
56
+
57
+ /** Total byte size of all lines */
58
+ totalBytes: number
59
+
60
+ /** Per-line metadata */
61
+ lineMetrics: LineMetrics[]
62
+
63
+ /** Original input text */
64
+ originalText: string
65
+
66
+ /** Break mode used */
67
+ breakMode: BreakMode
68
+ }
69
+
70
+ /**
71
+ * Per-line metrics from wrapping.
72
+ */
73
+ export interface LineMetrics {
74
+ /** The line text */
75
+ text: string
76
+
77
+ /** Width in pixels */
78
+ widthPx: number
79
+
80
+ /** Byte size of this line */
81
+ bytes: number
82
+
83
+ /** Utilization percentage (widthPx / maxWidthPx * 100) */
84
+ utilizationPercent: number
85
+
86
+ /** Whether this line ends with a hyphen from breaking */
87
+ endsWithHyphen: boolean
88
+
89
+ /** Whether this line was created from an explicit newline */
90
+ fromExplicitNewline: boolean
91
+ }
92
+
93
+ /**
94
+ * Default wrap options.
95
+ */
96
+ export const DEFAULT_WRAP_OPTIONS: Required<Omit<WrapOptions, "maxWidthPx" | "maxLines" | "maxBytes">> = {
97
+ breakMode: "character-no-hyphen",
98
+ hyphenChar: "-",
99
+ minCharsBeforeHyphen: 3,
100
+ trimLines: true,
101
+ preserveNewlines: true,
102
+ }
package/src/globals.ts CHANGED
@@ -22,7 +22,7 @@ export interface MiniappCapsuleMenuRect {
22
22
 
23
23
  export type MiniappColorScheme = "light" | "dark"
24
24
 
25
- export interface GlasslyGlobals {
25
+ export interface HostGlobals {
26
26
  /** Package id the host assigned to this miniapp (e.g. "com.glassly.example"). */
27
27
  packageName?: string
28
28
  /** "ios" | "android" | other. */
@@ -43,20 +43,20 @@ export interface GlasslyGlobals {
43
43
  capsuleMenu?: MiniappCapsuleMenuRect
44
44
  /**
45
45
  * The host's current color scheme. Miniapps can follow this to match the
46
- * phone's appearance. Updates at runtime arrive via the session's
47
- * `colorScheme` event — see `useColorScheme()` in @glassly/miniapp/react.
46
+ * phone's appearance. The host updates this value and dispatches
47
+ * `glassly:color-scheme-change` — see `useColorScheme()` in @glassly/miniapp/ui.
48
48
  */
49
49
  colorScheme?: MiniappColorScheme
50
50
  }
51
51
 
52
52
  declare global {
53
53
  interface Window {
54
- Glassly?: GlasslyGlobals
54
+ Glassly?: HostGlobals
55
55
  }
56
56
  }
57
57
 
58
58
  /** Reads window.Glassly safely — returns an empty object if not set. */
59
- export function getGlasslyGlobals(): GlasslyGlobals {
59
+ export function getHostGlobals(): HostGlobals {
60
60
  if (typeof window === "undefined") return {}
61
61
  return window.Glassly ?? {}
62
62
  }