forked from OfficeDev/ui-fabric-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TooltipActivity.kt
103 lines (83 loc) · 3.47 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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.officeuifabricdemo.demos
import android.os.Bundle
import android.view.ViewTreeObserver
import android.widget.Button
import com.microsoft.officeuifabric.snackbar.Snackbar
import com.microsoft.officeuifabric.tooltip.Tooltip
import com.microsoft.officeuifabricdemo.DemoActivity
import com.microsoft.officeuifabricdemo.R
import kotlinx.android.synthetic.main.activity_demo_detail.*
import kotlinx.android.synthetic.main.activity_tooltip.*
class TooltipActivity : DemoActivity(), Tooltip.OnDismissListener {
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 buttonId: Int = 0
override val contentNeedsScrollableContainer: Boolean
get() = false
override val contentLayoutId: Int
get() = R.layout.activity_tooltip
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
TooltipType.values().forEach { type ->
findViewById<Button>(type.buttonId).setOnClickListener {
val config = Tooltip.Config(getDimen(type.offsetXId), getDimen(type.offsetYId))
tooltip = Tooltip(this).show(it, resources.getString(type.messageId), config)
buttonId = it.id
}
}
tooltip_anchor_center.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
}
savedInstanceState?.let {
buttonId = it.getInt(BUTTON_ID)
}
}
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(root_view, 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
}