-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathProjectActionsToolbar.kt
More file actions
105 lines (93 loc) · 3.61 KB
/
ProjectActionsToolbar.kt
File metadata and controls
105 lines (93 loc) · 3.61 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
package com.itsaky.androidide.ui
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.InputDevice
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageButton
import android.widget.LinearLayout
import com.google.android.material.appbar.MaterialToolbar
import com.itsaky.androidide.common.R
import com.itsaky.androidide.common.databinding.ProjectActionsToolbarBinding
class ProjectActionsToolbar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
var onNavIconLongClick: (() -> Unit)? = null
) : MaterialToolbar(context, attrs) {
init {
// Navigation icon is no longer used in ProjectActionsToolbar
// It's now handled by the title toolbar
// Remove any navigation icon that might be set
navigationIcon = null
}
private val binding: ProjectActionsToolbarBinding =
ProjectActionsToolbarBinding.inflate(LayoutInflater.from(context), this, true)
@Deprecated("Title is now displayed separately. Use the title_text TextView in content_editor.xml instead.")
fun setTitleText(title: String) = Unit
fun addMenuItem(
icon: Drawable?,
hint: String,
onClick: () -> Unit,
onLongClick: () -> Unit,
onHover: ((View) -> Unit)? = null,
onHoverExit: (() -> Unit)? = null,
shouldAddMargin: Boolean
) {
val item = ImageButton(context).apply {
if (onHover == null) {
tooltipText = hint
}
contentDescription = hint
setImageDrawable(icon)
addCircleRipple()
// Set layout params for width and height
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
// Apply uniform spacing to all buttons for consistent appearance
// Use a smaller spacing value for tighter button layout
marginEnd = resources.getDimensionPixelSize(R.dimen.toolbar_item_spacing) / 2
}
setOnClickListener { onClick() }
setOnLongClickListener {
onLongClick()
true
}
var hoverRunnable: Runnable? = null
setOnHoverListener { view, event ->
if (!event.isFromSource(InputDevice.SOURCE_MOUSE)) return@setOnHoverListener false
when (event.actionMasked) {
MotionEvent.ACTION_HOVER_ENTER -> {
hoverRunnable?.let { view.removeCallbacks(it) }
hoverRunnable = Runnable { onHover?.invoke(view) }
view.postDelayed(hoverRunnable, 600L)
}
MotionEvent.ACTION_HOVER_EXIT -> {
hoverRunnable?.let { view.removeCallbacks(it) }
onHoverExit?.invoke()
}
}
false
}
}
binding.menuContainer.addView(item)
}
private fun View.addCircleRipple() = with(TypedValue()) {
context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
this,
true
)
setBackgroundResource(resourceId)
}
fun clearMenu() {
binding.menuContainer.removeAllViews()
}
fun setOnNavIconLongClickListener(listener: (() -> Unit)?) {
this.onNavIconLongClick = listener
}
}