forked from anthonysidesap/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTooltipActivity.kt
233 lines (200 loc) · 7.8 KB
/
TooltipActivity.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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.fluentuidemo.demos
import android.graphics.PorterDuff
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewTreeObserver
import androidx.core.content.ContextCompat
import com.microsoft.fluentui.calendar.CalendarView
import com.microsoft.fluentui.snackbar.Snackbar
import com.microsoft.fluentui.tooltip.Tooltip
import com.microsoft.fluentui.util.ThemeUtil
import com.microsoft.fluentui.widget.Button
import com.microsoft.fluentuidemo.DemoActivity
import com.microsoft.fluentuidemo.R
import com.microsoft.fluentuidemo.databinding.ActivityTooltipBinding
class TooltipActivity : DemoActivity(), Tooltip.OnDismissListener, View.OnKeyListener {
companion object {
const val BUTTON_ID = "buttonId"
}
enum class TooltipType(
val buttonId: Int,
val messageId: Int,
val offsetXId: Int,
val offsetYId: Int
) {
TOP_START(R.id.tooltip_anchor_top_start, R.string.tooltip_top_start_message, 0, 0),
TOP_END(
R.id.tooltip_anchor_top_end,
R.string.tooltip_top_end_message,
R.dimen.tooltip_example_offset_x,
0
),
BOTTOM_START(R.id.tooltip_anchor_bottom_start, R.string.tooltip_bottom_start_message, 0, 0),
BOTTOM_END(
R.id.tooltip_anchor_bottom_end,
R.string.tooltip_bottom_end_message,
0,
R.dimen.tooltip_example_offset_y
)
}
private var tooltip: Tooltip? = null
private var optionsMenu: Menu? = null
private var buttonId: Int = 0
override val contentNeedsScrollableContainer: Boolean
get() = false
private lateinit var tooltipBinding: ActivityTooltipBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tooltipBinding =
ActivityTooltipBinding.inflate(LayoutInflater.from(container.context), container, true)
TooltipType.values().forEach { type ->
findViewById<Button>(type.buttonId).setOnClickListener {
val config = Tooltip.Config(getDimen(type.offsetXId), getDimen(type.offsetYId))
tooltip = Tooltip(this)
if (it.id == R.id.tooltip_anchor_top_start) {
tooltip?.setCustomBackgroundColor(
ContextCompat.getColor(
baseContext,
R.color.tooltip_custom_color
)
)
}
tooltip?.show(it, resources.getString(type.messageId), config)
buttonId = it.id
}
findViewById<Button>(type.buttonId).setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ESCAPE && event?.action == KeyEvent.ACTION_UP) {
// Dismiss tooltip when Escape key is pressed
tooltip?.dismiss()
tooltip = null
}
false
}
}
tooltipBinding.tooltipAnchorCenter.setOnClickListener {
tooltip = Tooltip(this).show(
it,
resources.getString(R.string.tooltip_center_message),
Tooltip.Config(touchDismissLocation = Tooltip.TouchDismissLocation.INSIDE)
)
tooltip?.onDismissListener = this
buttonId = it.id
}
tooltipBinding.tooltipAnchorCenter.setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ESCAPE && event?.action == KeyEvent.ACTION_UP) {
// Dismiss tooltip when Escape key is pressed
tooltip?.dismiss()
tooltip = null
}
false
}
tooltipBinding.tooltipAnchorCustomView.setOnClickListener {
tooltip = Tooltip(this).setFocusable(true).show(
it,
LayoutInflater.from(this).inflate(R.layout.tooltip_custom_view, null)
)
tooltip?.onDismissListener = this
buttonId = it.id
}
tooltipBinding.tooltipAnchorCustomView.setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ESCAPE && event?.action == KeyEvent.ACTION_UP) {
// Dismiss tooltip when Escape key is pressed
tooltip?.dismiss()
tooltip = null
}
false
}
tooltipBinding.tooltipAnchorCalendarDemo.setOnClickListener {
tooltip = Tooltip(this).show(it, CalendarView(this), Tooltip.Config())
tooltip?.onDismissListener = this
buttonId = it.id
}
tooltipBinding.tooltipAnchorCalendarDemo.setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ESCAPE && event?.action == KeyEvent.ACTION_UP) {
// Dismiss tooltip when Escape key is pressed
tooltip?.dismiss()
tooltip = null
}
false
}
savedInstanceState?.let {
buttonId = it.getInt(BUTTON_ID)
}
}
override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_ESCAPE && event?.action == KeyEvent.ACTION_UP) {
// Dismiss tooltip when Escape key is pressed
tooltip?.dismiss()
tooltip = null
return true // Event handled
}
return false // Event not handled
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_app_bar_layout, menu)
optionsMenu = menu
for (index in 0 until menu.size()) {
val drawable = menu.getItem(index).icon
drawable?.setColorFilter(
ThemeUtil.getThemeAttrColor(this, R.attr.fluentuiToolbarIconColor),
PorterDuff.Mode.SRC_IN
)
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_flag -> {
Tooltip(baseContext).show(
findViewById(item.itemId),
"Flag Tooltip Clicked",
Tooltip.Config(touchDismissLocation = Tooltip.TouchDismissLocation.INSIDE)
).onDismissListener = this
}
}
return super.onOptionsItemSelected(item)
}
override fun onStart() {
super.onStart()
if (buttonId > 0) {
val button = findViewById<Button>(buttonId)
button.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Show tooltip on configuration change
button.performClick()
button.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(BUTTON_ID, buttonId)
}
override fun onPause() {
super.onPause()
tooltip?.let {
if (!it.isShowing)
buttonId = 0
it.dismiss()
}
}
override fun onDismiss() {
Snackbar.make(
demoBinding.rootView,
resources.getString(R.string.tooltip_dismiss_message),
Snackbar.LENGTH_SHORT
).show()
}
private fun getDimen(id: Int): Int =
if (id > 0) Math.round(resources.getDimension(id)) else 0
}