forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathV2MenuActivity.kt
More file actions
157 lines (147 loc) · 7.14 KB
/
Copy pathV2MenuActivity.kt
File metadata and controls
157 lines (147 loc) · 7.14 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
package com.microsoft.fluentuidemo.demos
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.ThemeMode
import com.microsoft.fluentui.theme.token.FluentAliasTokens
import com.microsoft.fluentui.tokenized.listitem.ListItem
import com.microsoft.fluentui.tokenized.menu.Menu
import com.microsoft.fluentuidemo.R
import com.microsoft.fluentuidemo.V2DemoActivity
val DefaultMenuInputWidthFraction = 0.35f
class V2MenuActivity : V2DemoActivity() {
init {
setupActivity(this)
}
override val paramsUrl = "https://github.com/microsoft/fluentui-android/wiki/Controls#params-21"
override val controlTokensUrl = "https://github.com/microsoft/fluentui-android/wiki/Controls#control-tokens-21"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = this
setActivityContent {
CreateMenuActivityUI(context)
}
}
}
@Composable
fun CreateMenuActivityUI(context: Context) {
val xOffsetState = rememberSaveable { mutableStateOf("0") }
val yOffsetState = rememberSaveable { mutableStateOf("0") }
val repeatContentTextCountState = rememberSaveable { mutableStateOf("4") }
val contentTextState =
rememberSaveable { mutableStateOf(context.getString(R.string.menu_content_text_input)) }
Column {
Column {
ListItem.Header(title = context.getString(R.string.menu_xOffset),
titleMaxLines = 2,
trailingAccessoryContent = {
BasicTextField(value = xOffsetState.value,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = { xOffsetState.value = it.trim() },
modifier = Modifier.background(Color.White).fillMaxWidth(fraction = DefaultMenuInputWidthFraction))
}
)
ListItem.Header(title = context.getString(R.string.menu_yOffset),
titleMaxLines = 2,
trailingAccessoryContent = {
BasicTextField(value = yOffsetState.value,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = { yOffsetState.value = it.trim() },
modifier = Modifier.background(Color.White).fillMaxWidth(fraction = DefaultMenuInputWidthFraction))
})
ListItem.Header(title = context.getString(R.string.menu_content_text),
titleMaxLines = 2,
trailingAccessoryContent = {
BasicTextField(
value = contentTextState.value,
onValueChange = { contentTextState.value = it },
modifier = Modifier.background(Color.White).fillMaxWidth(fraction = DefaultMenuInputWidthFraction))
})
ListItem.Header(title = context.getString(R.string.menu_repeat_content_text),
titleMaxLines = 2,
trailingAccessoryContent = {
BasicTextField(value = repeatContentTextCountState.value,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = { repeatContentTextCountState.value = it.trim() },
modifier = Modifier.background(Color.White).fillMaxWidth(fraction = DefaultMenuInputWidthFraction))
})
ListItem.SectionDescription(description = context.getString(R.string.menu_description))
}
val xOffset =
if (xOffsetState.value.toFloatOrNull() == null) 0.dp else xOffsetState.value.toFloat().dp
val yOffset =
if (yOffsetState.value.toFloatOrNull() == null) 0.dp else yOffsetState.value.toFloat().dp
val repeat =
if (repeatContentTextCountState.value == "" || repeatContentTextCountState.value.toIntOrNull() == null) 0 else repeatContentTextCountState.value.toInt()
Column(verticalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxSize()) {
MenuRow(context, xOffset, yOffset, contentTextState.value, repeat)
if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT) {
MenuRow(context, xOffset, yOffset, contentTextState.value, repeat)
MenuRow(context, xOffset, yOffset, contentTextState.value, repeat)
}
}
}
}
@Composable
fun MenuRow(context: Context, xOffset: Dp, yOffset: Dp, contentText: String, repeat: Int) {
Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()) {
Menu(context, xOffset, yOffset, contentText, repeat)
Menu(context, xOffset, yOffset, contentText, repeat)
Menu(context, xOffset, yOffset, contentText, repeat)
}
}
@Composable
fun Menu(context: Context, xOffset: Dp, yOffset: Dp, contentText: String, count: Int) {
var expanded by remember { mutableStateOf(false) }
Box {
com.microsoft.fluentui.tokenized.controls.Button(
onClick = { expanded = true },
text = context.getString(R.string.menu_open_menu_button)
)
Menu(
opened = expanded,
offset = DpOffset(xOffset, yOffset),
onDismissRequest = { expanded = false }
) {
Column(Modifier.verticalScroll(rememberScrollState())) {
repeat(count) {
BasicText(
text = "$contentText ${it + 1}",
style = TextStyle(
color = FluentTheme.aliasTokens.neutralForegroundColor[FluentAliasTokens.NeutralForegroundColorTokens.Foreground1].value(
themeMode = FluentTheme.themeMode
)
)
)
}
}
}
}
}