aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/core/StyledComponent.kt
blob: ace11158116e9e32e21362bf3a6a3d1bb6a1629f (plain)
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * Copyright (c) 2017-present Robert Jaros
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package pl.treksoft.kvision.core

import pl.treksoft.kvision.utils.Cache
import pl.treksoft.kvision.utils.asString
import kotlin.reflect.KProperty

/**
 * Base class for components supporting CSS styling.
 */
@Suppress("LargeClass")
abstract class StyledComponent {
    private val propertyValues: MutableMap<String, Any?> = mutableMapOf()
    internal val customStyles: MutableMap<String, String> = mutableMapOf()

    /**
     * Width of the current component.
     */
    open var width: CssSize? by refreshOnUpdate()

    /**
     * Minimal width of the current component.
     */
    open var minWidth: CssSize? by refreshOnUpdate()

    /**
     * Maximal width of the current component.
     */
    open var maxWidth: CssSize? by refreshOnUpdate()

    /**
     * Height of the current component.
     */
    open var height: CssSize? by refreshOnUpdate()

    /**
     * Minimal height of the current component.
     */
    open var minHeight: CssSize? by refreshOnUpdate()

    /**
     * Maximal height of the current component.
     */
    open var maxHeight: CssSize? by refreshOnUpdate()

    /**
     * CSS display of the current component.
     */
    open var display: Display? by refreshOnUpdate()

    /**
     * CSS position of the current component.
     */
    open var position: Position? by refreshOnUpdate()

    /**
     * Top edge of the current component.
     */
    open var top: CssSize? by refreshOnUpdate()

    /**
     * Left edge of the current component.
     */
    open var left: CssSize? by refreshOnUpdate()

    /**
     * Right edge of the current component.
     */
    open var right: CssSize? by refreshOnUpdate()

    /**
     * Bottom edge of the current component.
     */
    open var bottom: CssSize? by refreshOnUpdate()

    /**
     * Z-index of the current component.
     */
    open var zIndex: Int? by refreshOnUpdate()

    /**
     * CSS overflow of the current component.
     */
    open var overflow: Overflow? by refreshOnUpdate()

    /**
     * CSS overflow-wrap of the current component.
     */
    open var overflowWrap: OverflowWrap? by refreshOnUpdate()

    /**
     * CSS resize of the current component.
     */
    open var resize: Resize? by refreshOnUpdate()

    /**
     * Border of the current component.
     */
    open var border: Border? by refreshOnUpdate()

    /**
     * Top border of the current component.
     */
    open var borderTop: Border? by refreshOnUpdate()

    /**
     * Right border of the current component.
     */
    open var borderRight: Border? by refreshOnUpdate()

    /**
     * Bottom border of the current component.
     */
    open var borderBottom: Border? by refreshOnUpdate()

    /**
     * Left border of the current component.
     */
    open var borderLeft: Border? by refreshOnUpdate()

    /**
     * Margin of the current component.
     */
    open var margin: CssSize? by refreshOnUpdate()

    /**
     * Top margin of the current component.
     */
    open var marginTop: CssSize? by refreshOnUpdate()

    /**
     * Right margin of the current component.
     */
    open var marginRight: CssSize? by refreshOnUpdate()

    /**
     * Bottom margin of the current component.
     */
    open var marginBottom: CssSize? by refreshOnUpdate()

    /**
     * Left margin of the current component.
     */
    open var marginLeft: CssSize? by refreshOnUpdate()

    /**
     * Padding of the current component.
     */
    open var padding: CssSize? by refreshOnUpdate()

    /**
     * Top padding of the current component.
     */
    open var paddingTop: CssSize? by refreshOnUpdate()

    /**
     * Right padding of the current component.
     */
    open var paddingRight: CssSize? by refreshOnUpdate()

    /**
     * Bottom padding of the current component.
     */
    open var paddingBottom: CssSize? by refreshOnUpdate()

    /**
     * Left padding of the current component.
     */
    open var paddingLeft: CssSize? by refreshOnUpdate()

    /**
     * Text color for the current component.
     */
    open var color: Color? by refreshOnUpdate()

    /**
     * Text color for the current component given in hex format (write only).
     *
     * This property gives a convenient way to set the value of [color] property e.g.:
     *
     * c.colorHex = 0x00ff00
     *
     * The value read from this property is always null.
     */
    open var colorHex: Int?
        get() = null
        set(value) {
            color = if (value != null) Color.hex(value) else null
        }

    /**
     * Text color for the current component given with named constant (write only).
     *
     * This property gives a convenient way to set the value of [color] property e.g.:
     *
     * c.colorName = Col.GREEN
     *
     * The value read from this property is always null.
     */
    open var colorName: Col?
        get() = null
        set(value) {
            color = if (value != null) Color.name(value) else null
        }

    /**
     * Opacity of the current component.
     */
    open var opacity: Double? by refreshOnUpdate()

    /**
     * Background of the current component.
     */
    open var background: Background? by refreshOnUpdate()

    /**
     * CSS Text direction of the current component.
     */
    open var textDirection: Direction? by refreshOnUpdate()

    /**
     * CSS Text letter spacing of the current component.
     */
    open var letterSpacing: CssSize? by refreshOnUpdate()

    /**
     * CSS Text line height of the current component.
     */
    open var lineHeight: CssSize? by refreshOnUpdate()

    /**
     * CSS Text align of the current component.
     */
    open var textAlign: TextAlign? by refreshOnUpdate()

    /**
     * CSS Text decoration of the current component.
     */
    open var textDecoration: TextDecoration? by refreshOnUpdate()

    /**
     * CSS Text indent of the current component.
     */
    open var textIndent: CssSize? by refreshOnUpdate()

    /**
     * CSS Text shadow of the current component.
     */
    open var textShadow: TextShadow? by refreshOnUpdate()

    /**
     * CSS Text transform of the current component.
     */
    open var textTransform: TextTransform? by refreshOnUpdate()

    /**
     * CSS Text overflow of the current component.
     */
    open var textOverflow: TextOverflow? by refreshOnUpdate()

    /**
     * CSS Text unicode-bidi of the current component.
     */
    open var unicodeBidi: UnicodeBidi? by refreshOnUpdate()

    /**
     * CSS Text vertical align of the current component.
     */
    open var verticalAlign: VerticalAlign? by refreshOnUpdate()

    /**
     * CSS Text white space of the current component.
     */
    open var whiteSpace: WhiteSpace? by refreshOnUpdate()

    /**
     * CSS Text word spacing of the current component.
     */
    open var wordSpacing: CssSize? by refreshOnUpdate()

    /**
     * CSS font family of the current component.
     */
    open var fontFamily: String? by refreshOnUpdate()

    /**
     * CSS font size of the current component.
     */
    open var fontSize: CssSize? by refreshOnUpdate()

    /**
     * CSS font style of the current component.
     */
    open var fontStyle: FontStyle? by refreshOnUpdate()

    /**
     * CSS font weight of the current component.
     */
    open var fontWeight: FontWeight? by refreshOnUpdate()

    /**
     * CSS font variant of the current component.
     */
    open var fontVariant: FontVariant? by refreshOnUpdate()

    /**
     * CSS position float of the current component.
     */
    open var float: PosFloat? by refreshOnUpdate()

    /**
     * CSS clear float of the current component.
     */
    open var clear: Clear? by refreshOnUpdate()

    /**
     * CSS word break of the current component.
     */
    open var wordBreak: WordBreak? by refreshOnUpdate()

    /**
     * CSS line break of the current component.
     */
    open var lineBreak: LineBreak? by refreshOnUpdate()

    /**
     * CSS cursor shape over the current component.
     */
    open var cursor: Cursor? by refreshOnUpdate()

    private var snStyleCache: List<StringPair>? = null

    /**
     * @suppress
     * Internal function
     * Re-renders the current component.
     * @return current component
     */
    open fun refresh(): StyledComponent {
        snStyleCache = null
        return this
    }

    internal fun getSnStyleInternal(): List<StringPair> {
        return snStyleCache ?: {
            val s = getSnStyle()
            snStyleCache = s
            s
        }()
    }

    /**
     * Returns the list of String pairs defining CSS style attributes and their values.
     * @return the list of attributes and their values
     */
    @Suppress("ComplexMethod", "LongMethod")
    open fun getSnStyle(): List<StringPair> {
        val cacheKey = getCacheKey()
        return globalStyleCache[cacheKey] ?: run {
            val snstyle = mutableListOf<StringPair>()
            width?.let {
                snstyle.add("width" to it.asString())
            }
            minWidth?.let {
                snstyle.add("min-width" to it.asString())
            }
            maxWidth?.let {
                snstyle.add("max-width" to it.asString())
            }
            height?.let {
                snstyle.add("height" to it.asString())
            }
            minHeight?.let {
                snstyle.add("min-height" to it.asString())
            }
            maxHeight?.let {
                snstyle.add("max-height" to it.asString())
            }
            display?.let {
                snstyle.add("display" to it.display)
            }
            position?.let {
                snstyle.add("position" to it.position)
            }
            top?.let {
                snstyle.add("top" to it.asString())
            }
            left?.let {
                snstyle.add("left" to it.asString())
            }
            right?.let {
                snstyle.add("right" to it.asString())
            }
            bottom?.let {
                snstyle.add("bottom" to it.asString())
            }
            zIndex?.let {
                snstyle.add("z-index" to it.toString())
            }
            overflow?.let {
                snstyle.add("overflow" to it.overflow)
            }
            overflowWrap?.let {
                snstyle.add("overflow-wrap" to it.overflowWrap)
            }
            resize?.let {
                snstyle.add("resize" to it.resize)
            }
            border?.let {
                snstyle.add("border" to it.asString())
            }
            borderTop?.let {
                snstyle.add("border-top" to it.asString())
            }
            borderRight?.let {
                snstyle.add("border-right" to it.asString())
            }
            borderBottom?.let {
                snstyle.add("border-bottom" to it.asString())
            }
            borderLeft?.let {
                snstyle.add("border-left" to it.asString())
            }
            margin?.let {
                snstyle.add("margin" to it.asString())
            }
            marginTop?.let {
                snstyle.add("margin-top" to it.asString())
            }
            marginRight?.let {
                snstyle.add("margin-right" to it.asString())
            }
            marginBottom?.let {
                snstyle.add("margin-bottom" to it.asString())
            }
            marginLeft?.let {
                snstyle.add("margin-left" to it.asString())
            }
            padding?.let {
                snstyle.add("padding" to it.asString())
            }
            paddingTop?.let {
                snstyle.add("padding-top" to it.asString())
            }
            paddingRight?.let {
                snstyle.add("padding-right" to it.asString())
            }
            paddingBottom?.let {
                snstyle.add("padding-bottom" to it.asString())
            }
            paddingLeft?.let {
                snstyle.add("padding-left" to it.asString())
            }
            color?.let {
                snstyle.add("color" to it.asString())
            }
            opacity?.let {
                snstyle.add("opacity" to it.toString())
            }
            background?.let {
                snstyle.add("background" to it.asString())
            }
            textDirection?.let {
                snstyle.add("direction" to it.direction)
            }
            letterSpacing?.let {
                snstyle.add("letter-spacing" to it.asString())
            }
            lineHeight?.let {
                snstyle.add("line-height" to it.asString())
            }
            textAlign?.let {
                snstyle.add("text-align" to it.textAlign)
            }
            textDecoration?.let {
                snstyle.add("text-decoration" to it.asString())
            }
            textIndent?.let {
                snstyle.add("text-indent" to it.asString())
            }
            textShadow?.let {
                snstyle.add("text-shadow" to it.asString())
            }
            textTransform?.let {
                snstyle.add("text-transform" to it.textTransform)
            }
            textOverflow?.let {
                snstyle.add("text-overflow" to it.textOverflow)
            }
            unicodeBidi?.let {
                snstyle.add("unicode-bidi" to it.unicodeBidi)
            }
            verticalAlign?.let {
                snstyle.add("vertical-align" to it.verticalAlign)
            }
            whiteSpace?.let {
                snstyle.add("white-space" to it.whiteSpace)
            }
            wordSpacing?.let {
                snstyle.add("word-spacing" to it.asString())
            }
            fontFamily?.let {
                snstyle.add("font-family" to it)
            }
            fontSize?.let {
                snstyle.add("font-size" to it.asString())
            }
            fontStyle?.let {
                snstyle.add("font-style" to it.fontStyle)
            }
            fontWeight?.let {
                snstyle.add("font-weight" to it.fontWeight)
            }
            fontVariant?.let {
                snstyle.add("font-variant" to it.fontVariant)
            }
            float?.let {
                snstyle.add("float" to it.posFloat)
            }
            clear?.let {
                snstyle.add("clear" to it.clear)
            }
            wordBreak?.let {
                snstyle.add("word-break" to it.wordBreak)
            }
            lineBreak?.let {
                snstyle.add("line-break" to it.lineBreak)
            }
            cursor?.let {
                snstyle.add("cursor" to it.cursor)
            }
            if (customStyles.isNotEmpty()) {
                snstyle += customStyles.toList()
            }
            globalStyleCache[cacheKey] = snstyle
            return snstyle
        }
    }

    /**
     * Returns the value of a custom CSS style.
     * @param name the name of the style
     * @return the value of the style
     */
    fun getStyle(name: String): String? {
        return this.customStyles[name]
    }

    /**
     * Sets the value of a custom CSS style.
     * @param name the name of the style
     * @param value the value of the style
     */
    fun setStyle(name: String, value: String): StyledComponent {
        this.customStyles[name] = value
        refresh()
        return this
    }

    /**
     * Removes the value of a custom CSS style.
     * @param name the name of the style
     */
    fun removeStyle(name: String): StyledComponent {
        this.customStyles.remove(name)
        refresh()
        return this
    }

    protected open fun getCacheKey(): String {
        return ((propertyValues.map {
            it.toString()
        }) + (customStyles.map {
            it.toString()
        })).joinToString("###KvSep###")
    }

    private fun <T> refreshOnUpdate(refreshFunction: ((T) -> Unit) = { this.refresh() }) =
        RefreshDelegateProvider<T>(null, refreshFunction)

    private fun <T> refreshOnUpdate(initialValue: T, refreshFunction: ((T) -> Unit) = { this.refresh() }) =
        RefreshDelegateProvider(initialValue, refreshFunction)

    private inner class RefreshDelegateProvider<T>(
        private val initialValue: T?, private val refreshFunction: (T) -> Unit
    ) {
        operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): RefreshDelegate<T> {
            if (initialValue != null) propertyValues[prop.name] = initialValue
            return RefreshDelegate(refreshFunction)
        }
    }

    private inner class RefreshDelegate<T>(private val refreshFunction: ((T) -> Unit)) {
        @Suppress("UNCHECKED_CAST")
        operator fun getValue(thisRef: StyledComponent, property: KProperty<*>): T {
            val value = propertyValues[property.name]
            return if (value != null) {
                value as T
            } else {
                null as T
            }
        }

        operator fun setValue(thisRef: StyledComponent, property: KProperty<*>, value: T) {
            propertyValues[property.name] = value
            refreshFunction(value)
        }
    }

    companion object {
        internal val globalStyleCache = Cache<String, List<StringPair>>()
    }
}