| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import android.annotation.SuppressLint |
| 4 | import android.view.View |
| 5 | import org.telegram.ui.ActionBar.ActionBarMenuItem |
| 6 | import org.telegram.ui.ActionBar.Theme |
| 7 | |
| 8 | @SuppressLint("ViewConstructor") |
| 9 | class PopupBuilder @JvmOverloads constructor(anchor: View, dialog: Boolean = false) : ActionBarMenuItem(anchor.context, null, Theme.ACTION_BAR_WHITE_SELECTOR_COLOR, -0x4c4c4d) { |
| 10 | |
| 11 | init { |
| 12 | |
| 13 | setAnchor(anchor) |
| 14 | |
| 15 | isShowOnTop = dialog |
| 16 | |
| 17 | isVerticalScrollBarEnabled = true |
| 18 | |
| 19 | } |
| 20 | |
| 21 | fun setItems(items: Array<CharSequence?>, listener: (Int, CharSequence) -> Unit) { |
| 22 | |
| 23 | removeAllSubItems() |
| 24 | |
| 25 | for (item in items) { |
| 26 | if (item == null) continue |
| 27 | addSubItem(items.indexOf(item), item) |
| 28 | } |
| 29 | |
| 30 | setDelegate { |
| 31 | |
| 32 | listener(it, items[it]!!) |
| 33 | |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | |
| 38 | fun setItems(items: List<CharSequence?>, listener: (Int, CharSequence) -> Unit) { |
| 39 | |
| 40 | removeAllSubItems() |
| 41 | |
| 42 | for (item in items) { |
| 43 | if (item == null) continue |
| 44 | addSubItem(items.indexOf(item), item) |
| 45 | } |
| 46 | |
| 47 | setDelegate { |
| 48 | |
| 49 | listener(it, items[it]!!) |
| 50 | |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | fun show() { |
| 56 | |
| 57 | toggleSubMenu() |
| 58 | |
| 59 | } |
| 60 | |
| 61 | } |