forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathV2AppBarActivity.kt
More file actions
476 lines (457 loc) · 24.1 KB
/
Copy pathV2AppBarActivity.kt
File metadata and controls
476 lines (457 loc) · 24.1 KB
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
package com.microsoft.fluentuidemo.demos
import android.os.Bundle
import android.widget.Toast
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Email
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.icons.ListItemIcons
import com.microsoft.fluentui.icons.SearchBarIcons
import com.microsoft.fluentui.icons.listitemicons.Chevron
import com.microsoft.fluentui.icons.searchbaricons.Arrowback
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.ThemeMode
import com.microsoft.fluentui.theme.token.FluentAliasTokens
import com.microsoft.fluentui.theme.token.FluentColor
import com.microsoft.fluentui.theme.token.FluentIcon
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.AppBarInfo
import com.microsoft.fluentui.theme.token.controlTokens.AppBarSize
import com.microsoft.fluentui.theme.token.controlTokens.AppBarTokens
import com.microsoft.fluentui.theme.token.controlTokens.AvatarSize
import com.microsoft.fluentui.theme.token.controlTokens.AvatarStatus
import com.microsoft.fluentui.theme.token.controlTokens.TooltipControls
import com.microsoft.fluentui.tokenized.AppBar
import com.microsoft.fluentui.tokenized.SearchBar
import com.microsoft.fluentui.tokenized.controls.ToggleSwitch
import com.microsoft.fluentui.tokenized.listitem.ChevronOrientation
import com.microsoft.fluentui.tokenized.listitem.ListItem
import com.microsoft.fluentui.tokenized.persona.Avatar
import com.microsoft.fluentui.tokenized.persona.Person
import com.microsoft.fluentui.tokenized.segmentedcontrols.PillBar
import com.microsoft.fluentui.tokenized.segmentedcontrols.PillMetaData
import com.microsoft.fluentuidemo.R
import com.microsoft.fluentuidemo.V2DemoActivity
import kotlin.math.max
// Tags used for testing
const val APP_BAR_MODIFIABLE_PARAMETER_SECTION = "App Bar Modifiable Parameters"
const val APP_BAR_SUBTITLE_PARAM = "App Bar Subtitle Param"
const val APP_BAR_STYLE_PARAM = "App Bar AppBar Style Param"
const val APP_BAR_BUTTONBAR_PARAM = "App Bar ButtonBar Param"
const val APP_BAR_SEARCHBAR_PARAM = "App Bar SearchBar Param"
const val APP_BAR_LOGO_PARAM = "App Bar Logo Param"
const val APP_BAR_CENTER_ALIGN_PARAM = "App Bar Center Align Param"
const val APP_BAR_ENABLE_TOOLTIPS_PARAM = "App Bar Enable Tooltips Param"
const val APP_BAR_NAVIGATION_ICON_PARAM = "App Bar Navigation Icon Param"
class V2AppBarActivity : V2DemoActivity() {
init {
setupActivity(this)
}
override val paramsUrl = "https://github.com/microsoft/fluentui-android/wiki/Controls#params"
override val controlTokensUrl =
"https://github.com/microsoft/fluentui-android/wiki/Controls#control-tokens"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = this
setActivityContent {
var style: FluentStyle by rememberSaveable { mutableStateOf(FluentStyle.Neutral) }
var appBarSize: AppBarSize by rememberSaveable { mutableStateOf(AppBarSize.Small) }
var searchMode: Boolean by rememberSaveable { mutableStateOf(false) }
var subtitle: String? by rememberSaveable { mutableStateOf("Subtitle") }
var enableSearchBar: Boolean by rememberSaveable { mutableStateOf(false) }
var enableButtonBar: Boolean by rememberSaveable { mutableStateOf(false) }
var enableBottomBorder: Boolean by rememberSaveable { mutableStateOf(true) }
var centerAlignAppBar: Boolean by rememberSaveable { mutableStateOf(false) }
var enableTooltips: Boolean by rememberSaveable { mutableStateOf(false) }
var showNavigationIcon: Boolean by rememberSaveable { mutableStateOf(true) }
var yAxisDelta: Float by rememberSaveable { mutableStateOf(1.0F) }
var enableLogo: Boolean by rememberSaveable { mutableStateOf(true) }
Column(modifier = Modifier.pointerInput(Unit) {
detectDragGestures { _, distance ->
if (searchMode)
yAxisDelta = 0F
else
yAxisDelta = max(0F, distance.y + 10F) / 20F
}
}) {
ListItem.SectionHeader(
title = LocalContext.current.resources.getString(R.string.app_modifiable_parameters),
modifier = Modifier.testTag(APP_BAR_MODIFIABLE_PARAMETER_SECTION),
enableChevron = true,
enableContentOpenCloseTransition = true,
chevronOrientation = ChevronOrientation(90f, 0f),
) {
Column {
ListItem.Header(LocalContext.current.resources.getString(R.string.app_bar_size))
PillBar(
mutableListOf(
PillMetaData(
text = LocalContext.current.resources.getString(R.string.fluentui_large),
onClick = { appBarSize = AppBarSize.Large },
selected = appBarSize == AppBarSize.Large
),
PillMetaData(
text = LocalContext.current.resources.getString(R.string.fluentui_medium),
onClick = { appBarSize = AppBarSize.Medium },
selected = appBarSize == AppBarSize.Medium
),
PillMetaData(
text = LocalContext.current.resources.getString(R.string.fluentui_small),
onClick = { appBarSize = AppBarSize.Small },
selected = appBarSize == AppBarSize.Small
),
PillMetaData(
text = LocalContext.current.resources.getString(R.string.fluentui_search),
onClick = { searchMode = !searchMode },
selected = searchMode
)
), style = style,
showBackground = true
)
val subtitleText =
LocalContext.current.resources.getString(R.string.app_bar_subtitle)
ListItem.Item(
text = subtitleText,
subText = if (subtitle.isNullOrBlank())
LocalContext.current.resources.getString(R.string.fluentui_disabled)
else
LocalContext.current.resources.getString(R.string.fluentui_enabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
subtitle =
if (subtitle.isNullOrBlank())
subtitleText
else
null
},
modifier = Modifier.testTag(APP_BAR_SUBTITLE_PARAM),
checkedState = !subtitle.isNullOrBlank()
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.app_bar_style),
subText = if (style == FluentStyle.Neutral)
LocalContext.current.resources.getString(R.string.fluentui_neutral)
else
LocalContext.current.resources.getString(R.string.fluentui_brand),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
style =
if (style == FluentStyle.Neutral)
FluentStyle.Brand
else
FluentStyle.Neutral
},
modifier = Modifier.testTag(APP_BAR_STYLE_PARAM),
checkedState = style == FluentStyle.Brand
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.buttonbar),
subText = if (enableButtonBar)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
enableButtonBar = !enableButtonBar
},
modifier = Modifier.testTag(APP_BAR_BUTTONBAR_PARAM),
checkedState = enableButtonBar
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.searchbar),
subText = if (enableSearchBar)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
enableSearchBar = enableSearchBar || searchMode
ToggleSwitch(
onValueChange = {
enableSearchBar = !enableSearchBar
},
modifier = Modifier.testTag(APP_BAR_SEARCHBAR_PARAM),
checkedState = enableSearchBar,
enabledSwitch = !searchMode
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.app_bar_bottom_border),
subText = if (enableBottomBorder)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
enableBottomBorder = !enableBottomBorder
},
checkedState = enableBottomBorder,
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.left_logo),
subText = if (enableLogo)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
enableLogo = !enableLogo
},
modifier = Modifier.testTag(APP_BAR_LOGO_PARAM),
checkedState = enableLogo
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.navigation_icon),
subText = if (showNavigationIcon)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
showNavigationIcon = !showNavigationIcon
},
modifier = Modifier.testTag(APP_BAR_NAVIGATION_ICON_PARAM),
checkedState = showNavigationIcon
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.center_title_alignment),
subText = if (centerAlignAppBar)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
centerAlignAppBar = !centerAlignAppBar
},
modifier = Modifier.testTag(APP_BAR_CENTER_ALIGN_PARAM),
checkedState = centerAlignAppBar
)
}
)
ListItem.Item(
text = LocalContext.current.resources.getString(R.string.enable_tooltips),
subText = if (enableTooltips)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled),
trailingAccessoryContent = {
ToggleSwitch(
onValueChange = {
enableTooltips = !enableTooltips
},
modifier = Modifier.testTag(APP_BAR_ENABLE_TOOLTIPS_PARAM),
checkedState = enableTooltips
)
}
)
}
}
val buttonBarList = mutableListOf<PillMetaData>()
for (idx in 1..6) {
buttonBarList.add(
PillMetaData(
"Button $idx",
{
Toast.makeText(
context,
"Button $idx pressed",
Toast.LENGTH_SHORT
).show()
}
)
)
}
val appTitleDelta: Float by animateFloatAsState(
if (searchMode) 0F else 1F,
animationSpec = tween(durationMillis = 150, easing = LinearEasing)
)
val yAxisDeltaCoerced = yAxisDelta.coerceIn(0F, 1F)
val accessoryDelta: Float by animateFloatAsState(yAxisDeltaCoerced)
val rightIconColor: Color = if (style == FluentStyle.Neutral)
FluentTheme.aliasTokens.neutralForegroundColor[FluentAliasTokens.NeutralForegroundColorTokens.Foreground2].value(
FluentTheme.themeMode
)
else
FluentColor(
light = FluentTheme.aliasTokens.neutralForegroundColor[FluentAliasTokens.NeutralForegroundColorTokens.ForegroundOnColor].value(
ThemeMode.Light
),
dark = FluentTheme.aliasTokens.neutralForegroundColor[FluentAliasTokens.NeutralForegroundColorTokens.Foreground2].value(
ThemeMode.Dark
)
).value(FluentTheme.themeMode)
val appBarTokens = object : AppBarTokens() {
@Composable
override fun tooltipVisibilityControls(info: AppBarInfo): TooltipControls {
return TooltipControls(
enableTitleTooltip = enableTooltips,
enableSubtitleTooltip = enableTooltips,
enableNavigationIconTooltip = enableTooltips,
)
}
}
AppBar(
title = "Fluent UI Demo",
navigationIcon = if (showNavigationIcon) {
FluentIcon(
SearchBarIcons.Arrowback,
contentDescription = "Navigate Back",
onLongClick = {
Toast.makeText(
context,
"Navigation Icon long pressed",
Toast.LENGTH_SHORT
).show()
},
onClick = {
Toast.makeText(
context,
"Navigation Icon pressed",
Toast.LENGTH_SHORT
).show()
},
flipOnRtl = true
)
} else null,
subTitle = subtitle,
centerAlignAppBar = centerAlignAppBar,
logo = if (enableLogo) {
{
Avatar(
Person(
"Allan",
"Munger",
status = AvatarStatus.DND,
isActive = true
),
enablePresence = true,
size = AvatarSize.Size32,
modifier = if (!showNavigationIcon) {
Modifier.padding(start = 16.dp)
} else Modifier
)
}
} else null,
postTitleIcon = FluentIcon(
ListItemIcons.Chevron,
contentDescription = LocalContext.current.resources.getString(R.string.fluentui_chevron),
flipOnRtl = true
) {
Toast.makeText(context, "Title Icon pressed", Toast.LENGTH_SHORT).show()
},
postSubtitleIcon = FluentIcon(
ListItemIcons.Chevron,
contentDescription = LocalContext.current.resources.getString(R.string.fluentui_chevron),
onClick = {
Toast.makeText(context, "Subtitle Icon pressed", Toast.LENGTH_SHORT)
.show()
},
flipOnRtl = true
),
appBarSize = appBarSize,
style = style,
searchMode = searchMode,
bottomBorder = enableBottomBorder,
searchBar = if (enableSearchBar) {
{
SearchBar(
onValueChange = { _, _ -> },
modifier = Modifier.onFocusChanged { focusState ->
when {
focusState.isFocused -> {
searchMode = true
}
}
},
style = style,
navigationIconCallback = { searchMode = false }
)
}
} else null,
bottomBar = if (enableButtonBar) {
{ PillBar(metadataList = buttonBarList, style = style) }
} else null,
appTitleDelta = appTitleDelta,
accessoryDelta = accessoryDelta,
appBarTokens = appBarTokens,
rightAccessoryView = {
Icon(
Icons.Filled.Add,
"Add",
modifier = Modifier
.padding(10.dp)
.requiredSize(24.dp),
tint = rightIconColor,
onClick = {
Toast
.makeText(
context,
"Navigation Icon 1 Pressed",
Toast.LENGTH_SHORT
)
.show()
}
)
Icon(
Icons.Filled.Email,
"E-mail",
modifier = Modifier
.padding(10.dp)
.requiredSize(24.dp),
tint = rightIconColor,
onClick = {
Toast
.makeText(
context,
"Navigation Icon 2 Pressed",
Toast.LENGTH_SHORT
)
.show()
}
)
}
)
}
}
}
}