1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//build:20240524
/**
 * Base class for UI, inherited by other components. Subclasses are not allowed to modify corresponding function behavior. This js file does not need to be directly referenced or used.
 */
import utils from "./uiUtils.js"
import logger from './dxLogger.js'
import * as os from "os"
const uibase = {}
/**
* Modify or get the width of the component
* @param {number} w Optional, if not provided, get the width; otherwise, modify the width
*/
uibase.width = function (w) {
     if (!utils.validateNumber(w)) {
          return this.obj.getWidth()
     }
     this.obj.lvObjSetWidth(w)
}
/**
* Modify or get the height of the component
* @param {number} h Optional, if not provided, get the height; otherwise, modify the height
*/
uibase.height = function (h) {
     if (!utils.validateNumber(h)) {
          return this.obj.getHeight()
     }
     this.obj.lvObjSetHeight(h)
}
/**
 * Get width excluding borders and padding
 * @returns
 */
uibase.contentWidth = function () {
     return this.obj.lvObjGetContentWidth()
}
/**
 * Get height excluding borders and padding
 * @returns
 */
uibase.contentHeight = function () {
     return this.obj.lvObjGetContentHeight()
}
/**
 * Get top scroll distance
 * @returns
 */
uibase.scrollTop = function () {
     return this.obj.getScrollTop()
}
/**
 * Get bottom scroll distance
 * @returns
 */
uibase.scrollBottom = function () {
     return this.obj.getScrollBottom()
}
/**
 * Get left scroll distance
 * @returns
 */
uibase.scrollLeft = function () {
     return this.obj.getScrollLeft()
}
/**
 * Get right scroll distance
 * @returns
 */
uibase.scrollRight = function () {
     return this.obj.getScrollRight()
}
/**
* Modify the width and height of the component
* @param {number} w Required
* @param {number} h Required
*/
uibase.setSize = function (w, h) {
     let err = 'dxui.setSize: width or height should not be empty'
     utils.validateNumber(w, err)
     utils.validateNumber(h, err)
     this.obj.lvObjSetSize(w, h)
}
/**
* Modify or get the x coordinate of the component relative to the parent object
* @param {number} x Optional, if not provided, get the x coordinate; otherwise, modify the x coordinate
*/
uibase.x = function (x) {
     if (!utils.validateNumber(x)) {
          return this.obj.getX()
     }
     this.obj.lvObjSetX(x)
}
/**
* Modify or get the y coordinate of the component relative to the parent object
* @param {number} y Optional, if not provided, get the y coordinate; otherwise, modify the y coordinate
*/
uibase.y = function (y) {
     if (!utils.validateNumber(y)) {
          return this.obj.getY()
     }
     this.obj.lvObjSetY(y)
}
/**
* Modify the x and y coordinates of the component relative to the parent object
* @param {number} x Required
* @param {number} y Required
*/
uibase.setPos = function (x, y) {
     let err = 'dxui.setPos: x or y should not be empty'
     utils.validateNumber(x, err)
     utils.validateNumber(y, err)
     this.obj.lvObjSetPos(x, y)
}
/**
 * Move the component to the top layer, equivalent to the last created child component of the parent object, will cover all other child components
 */
uibase.moveForeground = function () {
     this.obj.moveForeground()
}
/**
 * Move the component to the bottom layer, equivalent to the first created child component of the parent object, will be covered by all other child components
 */
uibase.moveBackground = function () {
     this.obj.moveBackground()
}
/**
 * Subscribe to events, supported event types refer to utils.EVENT
 * @param {number} type Enum utils.EVENT, such as click, long press, etc.
 * @param {function} cb Event trigger callback function (cannot be an anonymous function)
 * @param {object} ud User data
 */
uibase.on = function (type, cb, ud) {
     this.obj.addEventCb(() => {
          if (cb) {
               cb({ target: this, ud: ud })
          }
     }, type)
}
/**
 * Send event, for example, to simulate clicking a button, you can send a CLICK event to the button
 * @param {number} type Enum utils.EVENT, such as click, long press, etc.
 */
uibase.send = function (type) {
     NativeObject.APP.NativeComponents.NativeEvent.lvEventSend(this.obj, type)
}
/**
 * Hide UI object
 */
uibase.hide = function () {
     if (!this.obj.hasFlag(1)) {
          this.obj.lvObjAddFlag(1);
     }
}
/**
 * Check if hidden
 * @returns
 */
uibase.isHide = function () {
     return this.obj.hasFlag(1);
}
/**
 * Show hidden UI object
 */
uibase.show = function () {
     if (this.obj.hasFlag(1)) {
          this.obj.lvObjClearFlag(1);
     }
}
/**
 * Disable/enable object
 * @param {*} en false/true, true is disabled, false is enabled
 */
uibase.disable = function (en) {
     if (en) {
          this.obj.addState(utils.STATE.DISABLED)
     } else {
          this.obj.clearState(utils.STATE.DISABLED)
     }
}
/**
 * Whether object is clickable
 * @param {*} en false/true, true is clickable, false is not clickable
 */
uibase.clickable = function (en) {
     if (en) {
          this.obj.lvObjAddFlag(utils.OBJ_FLAG.CLICKABLE)
     } else {
          this.obj.lvObjClearFlag(utils.OBJ_FLAG.CLICKABLE)
     }
}
/**
 * Check if disabled
 * @returns true is disabled, false is enabled
 */
uibase.isDisable = function () {
     return this.obj.hasState(utils.STATE.DISABLED)
}
/**
 * Focus object
 * @param {*} en false/true, true is focus, false is unfocus
 */
uibase.focus = function (en) {
     if (en) {
          this.obj.addState(utils.STATE.FOCUSED)
     } else {
          this.obj.clearState(utils.STATE.FOCUSED)
     }
}
/**
 * Check if focused
 * @returns true is focused, false is not focused
 */
uibase.isFocus = function () {
     return this.obj.hasState(utils.STATE.FOCUSED)
}
 
/**
 * Set UI style, can be set individually for each style, or define a style object first and then bind it to the UI object
 * Bind UI object and style object, can be bound to different parts or different states
 * @param {object} style Object returned by style.js build function
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.addStyle = function (style, type) {
     if (!style || !style.obj) {
          throw new Error('dxui.addStyle: style should not be null')
     }
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjAddStyle(style.obj, type);
}
/**
* Set all padding (left, right, top, bottom) to one value
* @param {number} pad Padding value
* @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
*/
uibase.padAll = function (pad, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStylePadAll(pad, type)
}
/**
 * Set/get right padding to one value
 * @param {number} pad Padding value
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.padRight = function (pad, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!utils.validateNumber(pad)) {
          return this.obj.getStylePadRight(type)
     }
     this.obj.setStylePadRight(pad, type)
}
/**
  * Set/get left padding to one value
  * @param {number} pad Padding value
  * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
  */
uibase.padLeft = function (pad, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!utils.validateNumber(pad)) {
          return this.obj.getStylePadLeft(type)
     }
     this.obj.setStylePadLeft(pad, type)
}
/**
  * Set/get top padding to one value
  * @param {number} pad Padding value
  * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
  */
uibase.padTop = function (pad, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!utils.validateNumber(pad)) {
          return this.obj.getStylePadTop(type)
     }
     this.obj.setStylePadTop(pad, type)
}
/**
  * Set/get bottom padding to one value
  * @param {number} pad Padding value
  * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
  */
uibase.padBottom = function (pad, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!utils.validateNumber(pad)) {
          return this.obj.getStylePadBottom(type)
     }
     this.obj.setStylePadBottom(pad, type)
}
/**
 * Set/get border width
 * @param {number} w
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.borderWidth = function (w, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!utils.validateNumber(w)) {
          return this.obj.lvObjGetStyleBorderWidth(type)
     }
     this.obj.lvObjSetStyleBorderWidth(w, type)
}
// Deprecated
uibase.setBorderColor = function (color, type) {
     this.borderColor(color, type)
}
/**
 * Set border color
 * @param {number} color Supports number type: e.g. 0x34ffaa; string type (starting with #), e.g.: '#34ffaa'
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.borderColor = function (color, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.setStyleBorderColor(utils.colorParse(color), type)
}
/**
 * Set border radius
 * @param {number} r
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.radius = function (r, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleRadius(r, type)
}
/**
 * Set background opacity, value range is 0-100, smaller value is more transparent
 * @param {number} opa Must be 0-100
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.bgOpa = function (opa, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleBgOpa(utils.OPA_MAPPING(opa), type)
}
/**
 * Set background color
 * @param {any} color Supports number type: e.g. 0x34ffaa; string type (starting with #), e.g.: '#34ffaa'
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.bgColor = function (color, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleBgColor(utils.colorParse(color), type)
}
/**
 * Set shadow
 * @param {number} width Shadow width
 * @param {number} x Horizontal offset
 * @param {number} y Vertical offset
 * @param {number} spread Spread distance
 * @param {number} color Color
 * @param {number} opa Opacity, must be 0-100
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.shadow = function (width, x, y, spread, color, opa, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleShadowWidth(width, type)
     this.obj.lvObjSetStyleShadowOfsX(x, type)
     this.obj.lvObjSetStyleShadowOfsY(y, type)
     this.obj.lvObjSetStyleShadowSpread(spread, type)
     this.obj.setStyleShadowColor(color, type)
     this.obj.setStyleShadowOpa(utils.OPA_MAPPING(opa), type)
}
/**
 * Set text color
 * @param {any} color Supports number type: e.g. 0x34ffaa; string type (starting with #), e.g.: '#34ffaa'
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.textColor = function (color, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleTextColor(utils.colorParse(color), type)
}
/**
 * Set text alignment
 * @param {number} align Refer to utils.TEXT_ALIGN
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.textAlign = function (align, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleTextAlign(align, type)
}
/**
 * Set text font
 * @param {object} font Object returned by font.js build function
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.textFont = function (font, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     if (!font || !font.obj) {
          throw new Error("dxui.textFont: 'font' parameter should not be null")
     }
     this.obj.lvObjSetStyleTextFont(font.obj, type)
}
/**
 * Set line object (line) color
 * @param {any} color Supports number type: e.g. 0x34ffaa; string type (starting with #), e.g.: '#34ffaa'
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.lineColor = function (color, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleLineColor(utils.colorParse(color), type)
}
/**
 * Set line object (line) width
 * @param {number} w
 * @param {number} type Refer to utils.STYLE, optional, default is bound to the object itself
 */
uibase.lineWidth = function (w, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.lvObjSetStyleLineWidth(w, type)
}
/**
 * Set line object (line) rounded corners
 * @param {boolean} enable true/false
 */
uibase.lineRound = function (enable) {
     this.obj.lvObjSetStyleLineRounded(enable)
}
/**
 * Set the scrollbar display mode for the UI object
 * @param {boolean} state true/false
 */
uibase.scrollbarMode = function (state) {
     this.obj.lvObjSetScrollbarMode(state)
}
/**
 * Set whether the UI object supports scrolling
 * @param {boolean} state
 */
uibase.scroll = function (state) {
     if (state) {
          this.obj.lvObjAddFlag(16)
     } else {
          this.obj.lvObjClearFlag(16)
     }
}
/**
 * Align the object with another reference object
 * @param {object} ref Reference object
 * @param {number} type Alignment direction, refer to dxui.Utils.ALIGN enum
 * @param {number} x X offset
 * @param {number} y Y offset
 */
uibase.alignTo = function (ref, type, x, y) {
     if (!ref || !ref.obj) {
          throw new Error("dxui.alignto: 'ref' parameter should not be null")
     }
     this.obj.lvObjAlignTo(ref.obj, type, x, y)
}
/**
 * Align the object with its parent object
 * @param {number} type Alignment direction, refer to dxui.Utils.ALIGN enum
 * @param {number} x X offset
 * @param {number} y Y offset
 */
uibase.align = function (type, x, y) {
     this.obj.lvObjAlign(type, x, y)
}
/**
 * Flexbox layout allows for more flexible positioning, arrangement, and distribution of elements, making it easier to create responsive and scalable layouts.
 * It is based on a container with flexible items inside. Here are some concepts for using this layout:
 * 1. Container: The container holds the flexible items inside and can arrange them from left to right or right to left, etc.
 * 2. Main axis and cross axis: The main axis is the primary arrangement direction of items in the container, usually horizontal or vertical, allowing items to be arranged horizontally or vertically.
 *   The cross axis is perpendicular to the main axis and can specify how items are arranged on the cross axis.
 *   The main and cross axes are set by flexFlow(), mainly ROW (horizontal) and COLUMN (vertical). Those with WRAP suffix automatically wrap when items exceed the container, and those with REVERSE suffix arrange in the opposite direction (right to left for horizontal, bottom to top for vertical).
 * 3. Main axis alignment: START (default main axis order), END (opposite of default main axis order), CENTER (centered on main axis), SPACE_EVENLY (evenly distributed on main axis with equal spacing), SPACE_AROUND (evenly distributed on main axis, each item gets equal space), SPACE_BETWEEN (flush at both ends, evenly spaced in between), set by flexAlign().
 * 4. Cross axis alignment: Treat each row or column as an item, align on the cross axis direction, alignment methods same as main axis, set by flexAlign().
 * 5. Overall alignment: Treat all items in the container as a whole, align within the container, alignment methods same as main axis, set by flexAlign().
 * @param {number} type Main axis and cross axis settings
 */
uibase.flexFlow = function (type) {
     this.obj.lvObjSetFlexFlow(type)
}
/**
 *
 * @param {number} main Child element alignment along the main axis
 * @param {number} cross Child element alignment along the cross axis
 * @param {number} track All child elements alignment relative to the container
 */
uibase.flexAlign = function (main, cross, track) {
     this.obj.lvObjSetFlexAlign(main, cross, track)
}
/**
 * Update the size of a control. When getting a control's size returns 0, call this first, equivalent to updating the display cache.
 */
uibase.update = function () {
     this.obj.lvObjUpdateLayout()
}
/**
 * Add a state to a control
 * @param {number} state State enum
 */
uibase.addState = function (state) {
     this.obj.addState(state)
}
/**
 * Remove a state from a control. To unfocus a focused input box, call this method to remove the FOCUSED state
 * @param {number} state State enum
 */
uibase.clearState = function (state) {
     this.obj.clearState(state)
}
/**
 * Check if a control has a state. To check if an input box is focused, use this method with the FOCUSED parameter
 * @param {number} state State enum
 * @returns true/false
 */
uibase.hasState = function (state) {
     return this.obj.hasState(state)
}
/**
 * Redraw a control, force refresh the control's cache. Can forcefully solve screen artifacts, but calling in a loop will reduce performance
 */
uibase.invalidate = function () {
     this.obj.invalidate()
}
/**
 * Scroll a child control until it's visible. If an item has been scrolled outside the container and is not visible, call this method to scroll it into view.
 * @param {boolean} en Whether to enable animation, enabled will scroll slowly, disabled will jump directly
 * @param {boolean} notRecursive Default recursive, suitable for general scrolling and nested scrolling controls
 */
uibase.scrollToView = function (en, isRecursive) {
     if (isRecursive) {
          this.obj.scrollToView(en)
     } else {
          this.obj.scrollToViewRecursive(en)
     }
}
/**
 * Scroll a control in the x direction
 * @param {number} x Scroll distance on x-axis
 * @param {boolean} en Whether to enable animation
 */
uibase.scrollToX = function (x, en) {
     this.obj.scrollToX(x, en)
}
/**
 * Scroll a control in the y direction
 * @param {number} y Scroll distance on y-axis
 * @param {boolean} en Whether to enable animation
 */
uibase.scrollToY = function (y, en) {
     this.obj.scrollToY(y, en)
}
/**
 * Element snapshot (essentially a screenshot. To save a full screen screenshot, use this method on the screen object)
 * @param {string} fileName Required, filename to save the snapshot (note: extension should match the format)
 * @param {number} type Optional, defaults to png, snapshot format 0:bmp/1:png/2:jpg(jpeg)
 * @param {number} cf Optional, an RGB color storage format
 */
uibase.snapshot = function (fileName, type = 1, cf = NativeObject.APP.NativeComponents.NativeEnum.LV_IMG_CF_TRUE_COLOR_ALPHA) {
     if (!fileName) {
          return
     }
     // Default storage location is /app/data/snapshot
     os.mkdir("/app/data/snapshot/")
     this.obj.lvSnapshotTake(cf, "/app/data/snapshot/" + fileName, type)
}
/**
 * Set the base layout direction of the object
 * @param {number} dir Refer to utils.STYLE Required, base layout direction of the object
 * @param {number} type Refer to utils.STYLE Optional, defaults to binding with the object itself
 */
uibase.setStyleBaseDir = function (dir, type) {
     if (!utils.validateNumber(type)) {
          type = 0
     }
     this.obj.setStyleBaseDir(dir, type)
}
export default uibase;