forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FloatingActionButton.kt
167 lines (159 loc) · 6.5 KB
/
FloatingActionButton.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
package com.microsoft.fluentui.tokenized.controls
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens.ControlType
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.FABInfo
import com.microsoft.fluentui.theme.token.controlTokens.FABSize
import com.microsoft.fluentui.theme.token.controlTokens.FABState
import com.microsoft.fluentui.theme.token.controlTokens.FABTokens
/**
* API to create a Floating Action Button. This button has elevation and can be expanded and collapsed.
* In expanded state, Icon + Text are displayed. In collapsed state, only icon is displayed.
*
* @param onClick OnClick behaviour for the button
* @param modifier Optional Modifier for FAB
* @param state State the FAB is supposed to be in. Default: [FABState.Expanded]
* @param size Size of the FAB. Default: [FABSize.Large]
* @param enabled Boolean for enabling/disabling FAB. Default: [true]
* @param interactionSource Interaction Source for user interactions.
* @param icon ImageVector for Icon to be displayed. Default: [null]
* @param text String to be displayed. Default: [null]
* @param fabTokens Tokens to customize design of FAB. Default: [null]
*/
@Composable
fun FloatingActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
state: FABState = FABState.Expanded,
size: FABSize = FABSize.Large,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
icon: ImageVector? = null,
text: String? = null,
fabTokens: FABTokens? = null
) {
if (icon == null && (text == null || text == ""))
return
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = fabTokens
?: FluentTheme.controlTokens.tokens[ControlType.FloatingActionButtonControlType] as FABTokens
val fabInfo = FABInfo(state, size)
val clickAndSemanticsModifier = Modifier.clickable(
interactionSource = interactionSource,
indication = LocalIndication.current,
enabled = enabled,
onClickLabel = null,
role = Role.Button,
onClick = onClick
)
val isFabExpanded: Boolean =
(text != null && text != "" && fabInfo.state == FABState.Expanded)
val backgroundColor = token.backgroundBrush(fabInfo = fabInfo).getBrushByState(
enabled = enabled,
selected = false,
interactionSource = interactionSource
)
val contentPadding = if (isFabExpanded) token.textPadding(fabInfo)
else token.iconPadding(fabInfo)
val iconSpacing = if (isFabExpanded) token.spacing(fabInfo) else 0.dp
val shape = CircleShape
val borders: List<BorderStroke> =
token.borderStroke(fabInfo = fabInfo).getBorderStrokeByState(
enabled = enabled,
selected = false,
interactionSource = interactionSource
)
var borderModifier: Modifier = Modifier
var borderWidth = 0.dp
for (border in borders) {
borderWidth += border.width
borderModifier = borderModifier.border(borderWidth, border.brush, shape)
}
Box(
modifier
.heightIn(min = token.fixedHeight(fabInfo))
.defaultMinSize(minWidth = token.minWidth(fabInfo))
.shadow(
elevation = token
.elevation(fabInfo = fabInfo)
.getElevationByState(
enabled = enabled,
selected = false,
interactionSource = interactionSource
),
shape = CircleShape
)
.background(
brush = backgroundColor,
shape = shape
)
.clip(shape)
.semantics(mergeDescendants = true) { contentDescription = text ?: "" }
.then(clickAndSemanticsModifier)
.then(borderModifier),
propagateMinConstraints = true
) {
Row(
Modifier.padding(contentPadding),
horizontalArrangement = Arrangement.spacedBy(
iconSpacing,
Alignment.CenterHorizontally
),
verticalAlignment = Alignment.CenterVertically
) {
if (icon != null)
Icon(
imageVector = icon,
contentDescription = text,
modifier = Modifier
.size(
token.iconSize(fabInfo)
)
.clearAndSetSemantics { },
tint = token.iconColor(fabInfo = fabInfo).getColorByState(
enabled = enabled,
selected = false,
interactionSource = interactionSource
)
)
AnimatedVisibility(isFabExpanded) {
BasicText(
text = text!!,
modifier = Modifier.clearAndSetSemantics { },
style = token.typography(fabInfo).merge(
TextStyle(
color = token.textColor(fabInfo = fabInfo).getColorByState(
enabled = enabled,
selected = false,
interactionSource = interactionSource
)
)
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}