forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListItem.kt
1059 lines (1030 loc) · 49.7 KB
/
ListItem.kt
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.microsoft.fluentui.tokenized.listitem
import androidx.compose.animation.*
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.*
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.*
import androidx.compose.ui.text.*
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.*
import com.microsoft.fluentui.listitem.R
import com.microsoft.fluentui.icons.ListItemIcons
import com.microsoft.fluentui.icons.listitemicons.Chevron
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens.ControlType
import com.microsoft.fluentui.theme.token.FluentGlobalTokens
import com.microsoft.fluentui.theme.token.controlTokens.*
import com.microsoft.fluentui.theme.token.controlTokens.BorderInset.None
import com.microsoft.fluentui.theme.token.controlTokens.BorderType.NoBorder
import com.microsoft.fluentui.theme.token.controlTokens.ListItemType.*
import com.microsoft.fluentui.theme.token.controlTokens.TextPlacement.Bottom
import com.microsoft.fluentui.theme.token.controlTokens.TextPlacement.Top
import com.microsoft.fluentui.util.dpToPx
object ListItem {
private fun clearSemantics(properties: (SemanticsPropertyReceiver.() -> Unit)?): Modifier {
return if (properties != null) {
Modifier.clearAndSetSemantics(properties)
} else {
Modifier
}
}
private fun Modifier.clickAndSemanticsModifier(
interactionSource: MutableInteractionSource,
onClick: () -> Unit,
enabled: Boolean,
rippleColor: Color
): Modifier = composed {
Modifier.clickable(
interactionSource = interactionSource,
indication = rememberRipple(color = rippleColor),
onClickLabel = null,
enabled = enabled,
onClick = onClick
)
}
private fun Modifier.borderModifier(
border: BorderType, borderColor: Color, borderSize: Float, borderInset: Float
): Modifier = drawBehind {
when (border) {
BorderType.Top -> drawLine(
borderColor, Offset(0f, 0f), Offset(size.width, 0f), borderSize * density
)
BorderType.Bottom -> drawLine(
borderColor,
Offset(borderInset, size.height),
Offset(size.width, size.height),
borderSize * density
)
BorderType.TopBottom -> {
drawLine(
borderColor, Offset(0f, 0f), Offset(size.width, 0f), borderSize * density
)
drawLine(
borderColor,
Offset(borderInset, size.height),
Offset(size.width, size.height),
borderSize * density
)
}
NoBorder -> {
}
}
}
/*
This function calculates the placeholder width for action text
*/
@Composable
fun PlaceholderForActionText(
actionTextComposable: @Composable () -> Unit,
content: @Composable (width: Dp) -> Unit,
) {
SubcomposeLayout { constraints ->
val calculatedWidth = subcompose(
"textToCalculate",
actionTextComposable
)[0].measure(Constraints()).width.toDp()
val contentPlaceable = subcompose("inlineText") {
content(calculatedWidth)
}[0].measure(constraints)
layout(contentPlaceable.width, contentPlaceable.height) {
contentPlaceable.place(0, 0)
}
}
}
@Composable
private fun InlineText(
description: String,
actionText: String,
onClick: () -> Unit,
descriptionTextColor: Color,
actionTextColor: Color,
descriptionTextTypography: TextStyle,
actionTextTypography: TextStyle,
backgroundColor: Brush
) {
PlaceholderForActionText(actionTextComposable = {
BasicText(
text = actionText,
style = actionTextTypography,
)
}) { measuredWidth ->
val text = buildAnnotatedString {
if (description.isNotEmpty()) {
withStyle(
style = SpanStyle(
color = descriptionTextColor,
fontSize = descriptionTextTypography.fontSize,
fontWeight = descriptionTextTypography.fontWeight
)
) {
append("$description ")
}
}
//below alternate text will be replaced by composable
appendInlineContent("key", actionText)
}
val widthInDp: TextUnit = with(LocalDensity.current) {
measuredWidth.toSp()
}
val inlineContent = mapOf(
Pair("key", InlineTextContent(
Placeholder(
width = widthInDp,
height = 16.sp,
placeholderVerticalAlign = PlaceholderVerticalAlign.TextCenter
)
) {
Box(
Modifier
.background(backgroundColor)
.clickable(
onClick = onClick
)
) {
BasicText(
text = actionText,
style = actionTextTypography.merge(TextStyle(color = actionTextColor))
)
}
})
)
BasicText(
text = text, inlineContent = inlineContent
)
}
}
@Composable
internal fun InternalItem(
text: String,
modifier: Modifier = Modifier,
subText: String? = null,
secondarySubText: String? = null,
secondarySubTextAnnotated: AnnotatedString? = null,
secondarySubTextInlineContent: Map<String, InlineTextContent> = mapOf(),
textAlignment: ListItemTextAlignment = ListItemTextAlignment.Regular,
unreadDot: Boolean = false,
enabled: Boolean = true,
textMaxLines: Int = 1,
subTextMaxLines: Int = 1,
secondarySubTextMaxLines: Int = 1,
onClick: (() -> Unit)? = null,
primaryTextLeadingContent: (@Composable () -> Unit)? = null,
primaryTextTrailingContent: (@Composable () -> Unit)? = null,
secondarySubTextLeadingContent: (@Composable () -> Unit)? = null,
secondarySubTextTrailingContent: (@Composable () -> Unit)? = null,
border: BorderType = NoBorder,
borderInset: BorderInset = None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
bottomContent: (@Composable () -> Unit)? = null,
leadingAccessoryContent: (@Composable () -> Unit)? = null,
trailingAccessoryContent: (@Composable () -> Unit)? = null,
leadingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
trailingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
textAccessibilityProperties: (SemanticsPropertyReceiver.() -> Unit)? = null,
listItemTokens: ListItemTokens? = null
) {
val listItemType = if (subText == null && secondarySubText == null) {
OneLine
} else if ((secondarySubText == null && subText != null) || (secondarySubText != null && subText == null)) {
TwoLine
} else {
ThreeLine
}
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = listItemTokens
?: FluentTheme.controlTokens.tokens[ControlType.ListItemControlType] as ListItemTokens
val listItemInfo = ListItemInfo(
listItemType = listItemType,
borderInset = borderInset,
horizontalSpacing = FluentGlobalTokens.SizeTokens.Size160,
verticalSpacing = FluentGlobalTokens.SizeTokens.Size120,
unreadDot = unreadDot
)
val backgroundColor =
token.backgroundBrush(listItemInfo).getBrushByState(
enabled = true, selected = false, interactionSource = interactionSource
)
val primaryTextTypography = token.primaryTextTypography(listItemInfo)
val subTextTypography = token.subTextTypography(listItemInfo)
val secondarySubTextTypography =
token.secondarySubTextTypography(listItemInfo)
val primaryTextColor = token.primaryTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val subTextColor = token.subTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val secondarySubTextColor = token.secondarySubTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val rippleColor = token.rippleColor(listItemInfo)
val unreadDotColor = token.unreadDotColor(listItemInfo)
val padding = token.padding(listItemInfo)
val borderSize = token.borderSize(listItemInfo).value
val borderInsetToPx = with(LocalDensity.current) {
token.borderInset(listItemInfo).toPx()
}
val borderColor = token.borderColor(listItemInfo).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val leadingAccessoryAlignment = when (leadingAccessoryContentAlignment) {
Alignment.Top -> Alignment.TopCenter
Alignment.Bottom -> Alignment.BottomCenter
else -> Alignment.Center
}
val trailingAccessoryAlignment = when (trailingAccessoryContentAlignment) {
Alignment.Top -> Alignment.TopEnd
Alignment.Bottom -> Alignment.BottomEnd
else -> Alignment.CenterEnd
}
Row(
modifier
.background(backgroundColor)
.fillMaxWidth()
.height(IntrinsicSize.Max)
.borderModifier(border, borderColor, borderSize, borderInsetToPx)
.then(
if(onClick != null){
Modifier.clickAndSemanticsModifier(
interactionSource,
onClick = onClick,
enabled,
rippleColor
)
}
else Modifier
)
) {
if (leadingAccessoryContent != null && textAlignment == ListItemTextAlignment.Regular) {
Box(
modifier = Modifier
.padding(
start = if (unreadDot) 4.dp else padding.calculateStartPadding(
LocalLayoutDirection.current
),
top = if (leadingAccessoryContentAlignment == Alignment.Top) padding.calculateTopPadding() else 0.dp,
bottom = if (leadingAccessoryContentAlignment == Alignment.Bottom) padding.calculateBottomPadding() else 0.dp
)
.fillMaxHeight(), contentAlignment = leadingAccessoryAlignment
) {
Row(verticalAlignment = Alignment.CenterVertically) {
if (unreadDot) {
Canvas(
modifier = Modifier
.sizeIn(minWidth = 8.dp, minHeight = 8.dp)
) {
drawCircle(
color = unreadDotColor, style = Fill, radius = dpToPx(4.dp)
)
}
Spacer(modifier = Modifier.width(4.dp))
}
leadingAccessoryContent()
}
}
}
val contentAlignment =
if (textAlignment == ListItemTextAlignment.Regular) Alignment.CenterStart else Alignment.Center
Box(
Modifier
.padding(horizontal = padding.calculateStartPadding(LocalLayoutDirection.current))
.weight(1f)
.then(clearSemantics(textAccessibilityProperties)),
contentAlignment = contentAlignment
) {
Column(Modifier.padding(vertical = padding.calculateTopPadding())) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
if (primaryTextLeadingContent != null) {
primaryTextLeadingContent()
}
BasicText(
modifier = Modifier.weight(1f, false),
text = text,
style = primaryTextTypography.merge(TextStyle(color = primaryTextColor)),
maxLines = textMaxLines,
overflow = TextOverflow.Ellipsis
)
if (primaryTextTrailingContent != null) {
primaryTextTrailingContent()
}
}
Row(verticalAlignment = Alignment.CenterVertically) {
if (subText != null && textAlignment == ListItemTextAlignment.Regular) {
BasicText(
text = subText,
style = subTextTypography.merge(TextStyle(color = subTextColor)),
maxLines = subTextMaxLines,
overflow = TextOverflow.Ellipsis
)
}
}
if (textAlignment == ListItemTextAlignment.Regular) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
if (bottomContent != null) {
Row(modifier.padding(top = 7.dp, bottom = 7.dp)) {
bottomContent()
}
} else {
if (secondarySubTextLeadingContent != null) {
secondarySubTextLeadingContent()
}
if (secondarySubText != null) {
BasicText(
modifier = Modifier.weight(1f, false),
text = secondarySubText,
style = secondarySubTextTypography.merge(TextStyle(color = secondarySubTextColor)),
maxLines = secondarySubTextMaxLines,
overflow = TextOverflow.Ellipsis
)
} else if (secondarySubTextAnnotated != null) {
BasicText(
modifier = Modifier.weight(1f, false),
text = secondarySubTextAnnotated,
inlineContent = secondarySubTextInlineContent,
maxLines = secondarySubTextMaxLines,
overflow = TextOverflow.Ellipsis
)
}
if (secondarySubTextTrailingContent != null) {
secondarySubTextTrailingContent()
}
}
}
}
}
}
if (trailingAccessoryContent != null && textAlignment == ListItemTextAlignment.Regular) {
Box(
Modifier
.padding(
top = if (trailingAccessoryContentAlignment == Alignment.Top) padding.calculateTopPadding() else 0.dp,
bottom = if (trailingAccessoryContentAlignment == Alignment.Bottom) padding.calculateBottomPadding() else 0.dp,
end = padding.calculateEndPadding(LocalLayoutDirection.current)
)
.fillMaxHeight(),
contentAlignment = trailingAccessoryAlignment
) {
trailingAccessoryContent()
}
}
}
}
/**
* Create a Single line or a multi line List item. A multi line list can be formed by providing either a secondary text or a tertiary text. Use this if secondarySubText is a String
*
* @param text Primary text.
* @param modifier Optional modifier for List item.
* @param subText Optional secondaryText or a subtitle.
* @param secondarySubText Optional tertiary text or a footer.
* @param textAlignment Optional [ListItemTextAlignment] to align text in the center or start at the lead.
* @param unreadDot Option boolean value that display a dot on leading edge of the accessory Content and makes the primary text bold on true
* @param enabled Optional enable/disable List item
* @param textMaxLines Optional max visible lines for primary text.
* @param subTextMaxLines Optional max visible lines for secondary text.
* @param secondarySubTextMaxLines Optional max visible lines for tertiary text.
* @param onClick Optional onClick action for list item.
* @param primaryTextLeadingContent Optional primary text leading Content.
* @param primaryTextTrailingContent Optional primary text trailing Content.
* @param secondarySubTextLeadingContent Optional secondary text leading Content.
* @param secondarySubTextTrailingContent Optional secondary text trailing Content.
* @param border [BorderType] Optional border for the list item.
* @param borderInset [BorderInset]Optional borderInset for list item.
* @param bottomContent Optional bottom Content under Text field.
* @param leadingAccessoryContent Optional composable leading accessory Content.
* @param trailingAccessoryContent Optional composable trailing accessory Content.
* @param leadingAccessoryContentAlignment Alignment for leading accessory Content to align Top, Bottom or Center
* @param trailingAccessoryContentAlignment Alignment for trailing accessory Content to align Top, Bottom or Center
* @param textAccessibilityProperties Accessibility properties for the text in list item.
* @param listItemTokens Optional list item tokens for list item appearance.If not provided then list item tokens will be picked from [AppThemeController]
*
*/
@Composable
fun Item(
text: String,
modifier: Modifier = Modifier,
subText: String? = null,
secondarySubText: String? = null,
textAlignment: ListItemTextAlignment = ListItemTextAlignment.Regular,
unreadDot: Boolean = false,
enabled: Boolean = true,
textMaxLines: Int = 1,
subTextMaxLines: Int = 1,
secondarySubTextMaxLines: Int = 1,
onClick: (() -> Unit)? = null,
primaryTextLeadingContent: (@Composable () -> Unit)? = null,
primaryTextTrailingContent: (@Composable () -> Unit)? = null,
secondarySubTextLeadingContent: (@Composable () -> Unit)? = null,
secondarySubTextTrailingContent: (@Composable () -> Unit)? = null,
border: BorderType = NoBorder,
borderInset: BorderInset = None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
bottomContent: (@Composable () -> Unit)? = null,
leadingAccessoryContent: (@Composable () -> Unit)? = null,
trailingAccessoryContent: (@Composable () -> Unit)? = null,
leadingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
trailingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
textAccessibilityProperties: (SemanticsPropertyReceiver.() -> Unit)? = null,
listItemTokens: ListItemTokens? = null
) {
InternalItem(
text = text,
modifier = modifier,
subText = subText,
secondarySubText = secondarySubText,
textAlignment = textAlignment,
unreadDot = unreadDot,
enabled = enabled,
textMaxLines = textMaxLines,
subTextMaxLines = subTextMaxLines,
secondarySubTextMaxLines = secondarySubTextMaxLines,
onClick = onClick,
primaryTextLeadingContent = primaryTextLeadingContent,
primaryTextTrailingContent = primaryTextTrailingContent,
secondarySubTextLeadingContent = secondarySubTextLeadingContent,
secondarySubTextTrailingContent = secondarySubTextTrailingContent,
border = border,
borderInset = borderInset,
interactionSource = interactionSource,
bottomContent = bottomContent,
leadingAccessoryContent = leadingAccessoryContent,
trailingAccessoryContent = trailingAccessoryContent,
leadingAccessoryContentAlignment = leadingAccessoryContentAlignment,
trailingAccessoryContentAlignment = trailingAccessoryContentAlignment,
textAccessibilityProperties = textAccessibilityProperties,
listItemTokens = listItemTokens
)
}
/**
* Create a Single line or a multi line List item. A multi line list can be formed by providing either a secondary text or a tertiary text. Use this if the secondarySubtext is an Annotated String
*
* @param text Primary text.
* @param modifier Optional modifier for List item.
* @param subText Optional secondaryText or a subtitle.
* @param secondarySubTextAnnotated Optional tertiary text (Annotated) or a footer.
* @param secondarySubTextInlineContent Map of composables to replace certain ranges of text in [secondarySubTextAnnotated].
* @param textAlignment Optional [ListItemTextAlignment] to align text in the center or start at the lead.
* @param unreadDot Option boolean value that display a dot on leading edge of the accessory Content and makes the primary text bold on true
* @param enabled Optional enable/disable List item
* @param textMaxLines Optional max visible lines for primary text.
* @param subTextMaxLines Optional max visible lines for secondary text.
* @param secondarySubTextMaxLines Optional max visible lines for tertiary text.
* @param onClick Optional onClick action for list item.
* @param primaryTextLeadingContent Optional primary text leading Content.
* @param primaryTextTrailingContent Optional primary text trailing Content.
* @param secondarySubTextLeadingContent Optional secondary text leading Content.
* @param secondarySubTextTrailingContent Optional secondary text trailing Content.
* @param border [BorderType] Optional border for the list item.
* @param borderInset [BorderInset]Optional borderInset for list item.
* @param bottomContent Optional bottom Content under Text field.
* @param leadingAccessoryContent Optional composable leading accessory Content.
* @param trailingAccessoryContent Optional composable trailing accessory Content.
* @param leadingAccessoryContentAlignment Alignment for leading accessory Content to align Top, Bottom or Center
* @param trailingAccessoryContentAlignment Alignment for trailing accessory Content to align Top, Bottom or Center
* @param textAccessibilityProperties Accessibility properties for the text in list item.
* @param listItemTokens Optional list item tokens for list item appearance.If not provided then list item tokens will be picked from [AppThemeController]
*
*/
@Composable
fun Item(
text: String,
modifier: Modifier = Modifier,
subText: String? = null,
secondarySubTextAnnotated: AnnotatedString? = null,
secondarySubTextInlineContent: Map<String, InlineTextContent> = mapOf(),
textAlignment: ListItemTextAlignment = ListItemTextAlignment.Regular,
unreadDot: Boolean = false,
enabled: Boolean = true,
textMaxLines: Int = 1,
subTextMaxLines: Int = 1,
secondarySubTextMaxLines: Int = 1,
onClick: (() -> Unit)? = null,
primaryTextLeadingContent: (@Composable () -> Unit)? = null,
primaryTextTrailingContent: (@Composable () -> Unit)? = null,
secondarySubTextLeadingContent: (@Composable () -> Unit)? = null,
secondarySubTextTrailingContent: (@Composable () -> Unit)? = null,
border: BorderType = NoBorder,
borderInset: BorderInset = None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
bottomContent: (@Composable () -> Unit)? = null,
leadingAccessoryContent: (@Composable () -> Unit)? = null,
trailingAccessoryContent: (@Composable () -> Unit)? = null,
leadingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
trailingAccessoryContentAlignment: Alignment.Vertical = Alignment.CenterVertically,
textAccessibilityProperties: (SemanticsPropertyReceiver.() -> Unit)? = null,
listItemTokens: ListItemTokens? = null
) {
InternalItem(
text = text,
modifier = modifier,
subText = subText,
secondarySubTextAnnotated = secondarySubTextAnnotated,
secondarySubTextInlineContent = secondarySubTextInlineContent,
textAlignment = textAlignment,
unreadDot = unreadDot,
enabled = enabled,
textMaxLines = textMaxLines,
subTextMaxLines = subTextMaxLines,
secondarySubTextMaxLines = secondarySubTextMaxLines,
onClick = onClick,
primaryTextLeadingContent = primaryTextLeadingContent,
primaryTextTrailingContent = primaryTextTrailingContent,
secondarySubTextLeadingContent = secondarySubTextLeadingContent,
secondarySubTextTrailingContent = secondarySubTextTrailingContent,
border = border,
borderInset = borderInset,
interactionSource = interactionSource,
bottomContent = bottomContent,
leadingAccessoryContent = leadingAccessoryContent,
trailingAccessoryContent = trailingAccessoryContent,
leadingAccessoryContentAlignment = leadingAccessoryContentAlignment,
trailingAccessoryContentAlignment = trailingAccessoryContentAlignment,
textAccessibilityProperties = textAccessibilityProperties,
listItemTokens = listItemTokens
)
}
/**
* Create a Section header. Section headers are list tiles that delineates sections of a list or grid list
*
* @param modifier Optional modifier for List item.
* @param title Section header title.
* @param titleMaxLines Optional max visible lines for title.
* @param accessoryTextTitle Optional accessory text.
* @param accessoryTextOnClick Optional onClick action for accessory text.
* @param enabled Optional enable/disable List item
* @param style [SectionHeaderStyle] Section header style.
* @param enableChevron Adds a chevron icon before text
* @param chevronOrientation Pass [ChevronOrientation] to apply chevron icon transition when clicked on the list item. Defaults to static (enter and exit transition are same).
* @param border [BorderType] Optional border for the list item.
* @param borderInset [BorderInset] Optional borderInset for list item.
* @param listItemTokens Optional list item tokens for list item appearance.If not provided then list tokens will be picked from [AppThemeController]
* @param enter [EnterTransition] used for content appearing transition
* @param exit [ExitTransition] used for content disappearing transition
* @param trailingAccessoryContent Optional composable trailing accessory Content.
* @param content Composable content to appear or disappear on clicking the list item
*
*/
@Composable
fun SectionHeader(
title: String,
modifier: Modifier = Modifier,
titleMaxLines: Int = 1,
accessoryTextTitle: String? = null,
accessoryTextOnClick: (() -> Unit)? = null,
enabled: Boolean = true,
style: SectionHeaderStyle = SectionHeaderStyle.Bold,
enableChevron: Boolean = true,
chevronOrientation: ChevronOrientation = ChevronOrientation(0f, 0f),
border: BorderType = NoBorder,
borderInset: BorderInset = None,
enableContentOpenCloseTransition: Boolean = false,
enter: EnterTransition = expandVertically(),
exit: ExitTransition = shrinkVertically(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
trailingAccessoryContent: (@Composable () -> Unit)? = null,
listItemTokens: ListItemTokens? = null,
content: (@Composable () -> Unit)? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = listItemTokens
?: FluentTheme.controlTokens.tokens[ControlType.ListItemControlType] as ListItemTokens
val listItemInfo = ListItemInfo(
listItemType = SectionHeader,
borderInset = borderInset,
horizontalSpacing = FluentGlobalTokens.SizeTokens.Size160,
verticalSpacing = FluentGlobalTokens.SizeTokens.Size120,
style = style
)
val backgroundColor =
token.backgroundBrush(listItemInfo).getBrushByState(
enabled = true, selected = false, interactionSource = interactionSource
)
val cellHeight = token.cellHeight(listItemInfo)
val primaryTextTypography =
token.sectionHeaderPrimaryTextTypography(listItemInfo)
val actionTextTypography =
token.sectionHeaderActionTextTypography(listItemInfo)
val primaryTextColor = token.primaryTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val actionTextColor = token.actionTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val rippleColor = token.rippleColor(listItemInfo)
val padding = token.padding(listItemInfo)
val borderSize = token.borderSize(listItemInfo).value
val borderInsetToPx = with(LocalDensity.current) {
token.borderInset(listItemInfo).toPx()
}
val borderColor = token.borderColor(listItemInfo).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val chevronTint = token.chevronTint(listItemInfo)
var expandedState by rememberSaveable { mutableStateOf(false) }
val rotationState by animateFloatAsState(
targetValue = if (!enableContentOpenCloseTransition || expandedState) chevronOrientation.enterTransition else chevronOrientation.exitTransition
)
val expandedString = LocalContext.current.resources.getString(R.string.fluentui_expanded)
val collapsedString = LocalContext.current.resources.getString(R.string.fluentui_collapsed)
Box(
modifier = modifier
.fillMaxWidth()
.heightIn(min = cellHeight)
.background(backgroundColor)
.then(
if(enableContentOpenCloseTransition && content != null){
Modifier.clickAndSemanticsModifier(
interactionSource,
onClick = {
expandedState = !expandedState
},
enabled,
rippleColor
)
}
else Modifier
)
.semantics(mergeDescendants = true) {
contentDescription =
"{$title}." + if (enableContentOpenCloseTransition) {
if (expandedState) {
expandedString
} else {
collapsedString
}
} else {""}
}
.borderModifier(border, borderColor, borderSize, borderInsetToPx)
) {
Column {
Row(
Modifier
.background(backgroundColor)
.fillMaxWidth()
.heightIn(min = cellHeight)
.padding(bottom = padding.calculateBottomPadding()),
verticalAlignment = Alignment.Bottom
) {
Box(
Modifier
.padding(
horizontal = padding.calculateStartPadding(
LocalLayoutDirection.current
)
)
.weight(1f), contentAlignment = Alignment.BottomStart
) {
Row(verticalAlignment = Alignment.CenterVertically) {
if (enableChevron) {
Icon(
painter = rememberVectorPainter(image = ListItemIcons.Chevron),
contentDescription = null,
Modifier
.rotate(rotationState)
.testTag("Chevron"),
tint = chevronTint
)
}
BasicText(
modifier = Modifier.clearAndSetSemantics { },
text = title,
style = primaryTextTypography.merge(TextStyle(color = primaryTextColor)),
maxLines = titleMaxLines,
overflow = TextOverflow.Ellipsis
)
}
}
Row(Modifier.padding(end = padding.calculateEndPadding(LocalLayoutDirection.current))) {
if (accessoryTextTitle != null) {
BasicText(
text = accessoryTextTitle,
Modifier.clickable(role = Role.Button,
onClick = accessoryTextOnClick ?: {}),
style = actionTextTypography.merge(TextStyle(color = actionTextColor))
)
}
}
if (trailingAccessoryContent != null) {
Box(
Modifier.padding(
end = padding.calculateEndPadding(
LocalLayoutDirection.current
)
), contentAlignment = Alignment.BottomStart
) {
trailingAccessoryContent()
}
}
}
Row {
if (content != null) {
AnimatedVisibility(
visible = !enableContentOpenCloseTransition || expandedState,
enter = enter,
exit = exit
) {
content()
}
}
}
}
}
}
/**
* Create a Section description. Section description are lists that provide added context to a list.
*
* @param modifier Optional modifier for List item.
* @param description description text.
* @param enabled Optional enable/disable List item
* @param listItemTokens Optional list item tokens for list item appearance.If not provided then list tokens will be picked from [AppThemeController]
* @param actionText Option boolean to append "Action" text button to the description text.
* @param descriptionPlacement [TextPlacement] Enum value for placing the description text in the list item.
* @param onClick Optional onClick action for list item.
* @param onActionClick Optional onClick action for actionText.
* @param border [BorderType] Optional border for the list item.
* @param borderInset [BorderInset] Optional borderInset for list item.
* @param leadingAccessoryContent Optional composable leading accessory Content.
* @param trailingAccessoryContent Optional composable trailing accessory Content.
*
*/
@Composable
fun SectionDescription(
description: String,
modifier: Modifier = Modifier,
enabled: Boolean = true,
actionText: String? = null,
descriptionPlacement: TextPlacement = Top,
border: BorderType = NoBorder,
borderInset: BorderInset = None,
onClick: (() -> Unit)? = null,
onActionClick: (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
leadingAccessoryContent: (@Composable () -> Unit)? = null,
trailingAccessoryContent: (@Composable () -> Unit)? = null,
listItemTokens: ListItemTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = listItemTokens
?: FluentTheme.controlTokens.tokens[ControlType.ListItemControlType] as ListItemTokens
val listItemInfo = ListItemInfo(
listItemType = SectionDescription,
horizontalSpacing = FluentGlobalTokens.SizeTokens.Size160,
verticalSpacing = FluentGlobalTokens.SizeTokens.Size80,
borderInset = borderInset,
placement = descriptionPlacement
)
val backgroundColor =
token.backgroundBrush(listItemInfo).getBrushByState(
enabled = true, selected = false, interactionSource = interactionSource
)
val cellHeight = token.cellHeight(listItemInfo)
val descriptionTextTypography =
token.descriptionTextTypography(listItemInfo)
val actionTextTypography = token.actionTextTypography(listItemInfo)
val descriptionTextColor = token.descriptionTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val actionTextColor = token.actionTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val rippleColor = token.rippleColor(listItemInfo)
val borderSize = token.borderSize(listItemInfo).value
val borderInsetToPx = with(LocalDensity.current) {
token.borderInset(listItemInfo).toPx()
}
val borderColor = token.borderColor(listItemInfo).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val descriptionAlignment = token.descriptionPlacement(listItemInfo)
val padding = token.padding(listItemInfo)
Row(
modifier
.fillMaxWidth()
.heightIn(min = cellHeight)
.background(backgroundColor)
.borderModifier(border, borderColor, borderSize, borderInsetToPx)
.clickAndSemanticsModifier(
interactionSource, onClick = onClick ?: {}, enabled, rippleColor
), verticalAlignment = descriptionAlignment
) {
if (leadingAccessoryContent != null && descriptionPlacement == Top) {
Box(
Modifier.padding(padding.calculateStartPadding(LocalLayoutDirection.current)),
contentAlignment = Alignment.Center
) {
leadingAccessoryContent()
}
}
Box(
Modifier
.padding(
start = if (leadingAccessoryContent == null) padding.calculateStartPadding(
LocalLayoutDirection.current
) else 0.dp,
end = padding.calculateEndPadding(LocalLayoutDirection.current),
top = if (descriptionPlacement == Top) padding.calculateTopPadding() else 0.dp,
bottom = if (descriptionPlacement == Bottom) padding.calculateTopPadding() else 0.dp
)
.weight(1f)
) {
if (!actionText.isNullOrBlank()) {
InlineText(
description = description,
actionText = actionText,
onClick = onActionClick ?: {},
actionTextTypography = actionTextTypography,
actionTextColor = actionTextColor,
descriptionTextColor = descriptionTextColor,
descriptionTextTypography = descriptionTextTypography,
backgroundColor = backgroundColor
)
} else {
BasicText(
text = description,
style = descriptionTextTypography.merge(TextStyle(color = descriptionTextColor))
)
}
}
if (trailingAccessoryContent != null) {
Box(
Modifier.padding(end = padding.calculateEndPadding(LocalLayoutDirection.current)),
contentAlignment = Alignment.Center
) {
trailingAccessoryContent()
}
}
}
}
/**
* Create a Header. Headers are list tiles that delineates heading of a list or grid list
*
* @param modifier Optional modifier for List item.
* @param title Section header title.
* @param titleMaxLines Optional max visible lines for title.
* @param accessoryTextTitle Optional accessory text.
* @param accessoryTextOnClick Optional onClick action for accessory text.
* @param enabled Optional enable/disable List item
* @param style [SectionHeaderStyle] Section header style.
* @param border [BorderType] Optional border for the list item.
* @param borderInset [BorderInset] Optional borderInset for list item.
* @param listItemTokens Optional list item tokens for list item appearance.If not provided then list tokens will be picked from [AppThemeController]
* @param trailingAccessoryContent Optional composable trailing accessory Content.
*
*/
@Composable
fun Header(
title: String,
modifier: Modifier = Modifier,
titleMaxLines: Int = 1,
accessoryTextTitle: String? = null,
accessoryTextOnClick: (() -> Unit)? = null,
enabled: Boolean = true,
style: SectionHeaderStyle = SectionHeaderStyle.Bold,
border: BorderType = NoBorder,
borderInset: BorderInset = None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
trailingAccessoryContent: (@Composable () -> Unit)? = null,
listItemTokens: ListItemTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = listItemTokens
?: FluentTheme.controlTokens.tokens[ControlType.ListItemControlType] as ListItemTokens
val listItemInfo = ListItemInfo(
listItemType = OneLine,
style = style,
horizontalSpacing = FluentGlobalTokens.SizeTokens.Size160,
verticalSpacing = FluentGlobalTokens.SizeTokens.Size80,
borderInset = borderInset
)
val backgroundColor =
token.backgroundBrush(listItemInfo).getBrushByState(
enabled = true, selected = false, interactionSource = interactionSource
)
val cellHeight = token.cellHeight(listItemInfo)
val primaryTextTypography =
token.sectionHeaderPrimaryTextTypography(listItemInfo)
val actionTextTypography =
token.sectionHeaderActionTextTypography(listItemInfo)
val primaryTextColor = token.primaryTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val actionTextColor = token.actionTextColor(
listItemInfo
).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource
)
val padding = token.padding(listItemInfo)
val borderSize = token.borderSize(listItemInfo).value
val borderInsetToPx = with(LocalDensity.current) {
token.borderInset(listItemInfo).toPx()
}
val borderColor = token.borderColor(listItemInfo).getColorByState(
enabled = enabled, selected = false, interactionSource = interactionSource