2016-07-04 03:31:17 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.ktextension
|
|
|
|
|
2017-05-17 09:36:59 +02:00
|
|
|
import android.content.Context
|
2016-12-09 13:22:17 +01:00
|
|
|
import android.graphics.drawable.Drawable
|
2017-05-17 09:36:59 +02:00
|
|
|
import android.support.annotation.DrawableRes
|
|
|
|
import android.support.v4.content.ContextCompat
|
2016-07-04 03:31:17 +02:00
|
|
|
import android.support.v4.view.MenuItemCompat
|
|
|
|
import android.view.Menu
|
2017-05-17 09:36:59 +02:00
|
|
|
import android.view.MenuItem
|
2017-04-11 16:22:50 +02:00
|
|
|
import android.view.SubMenu
|
2017-05-17 09:36:59 +02:00
|
|
|
import org.mariotaku.twidere.graphic.ActionIconDrawable
|
2016-07-04 03:31:17 +02:00
|
|
|
|
|
|
|
fun Menu.setItemAvailability(id: Int, available: Boolean) {
|
|
|
|
val item = findItem(id) ?: return
|
|
|
|
item.isVisible = available
|
|
|
|
item.isEnabled = available
|
|
|
|
}
|
|
|
|
|
2016-12-28 04:17:11 +01:00
|
|
|
fun Menu.setGroupAvailability(groupId: Int, available: Boolean) {
|
2016-07-04 03:31:17 +02:00
|
|
|
setGroupEnabled(groupId, available)
|
|
|
|
setGroupVisible(groupId, available)
|
|
|
|
}
|
|
|
|
|
2017-04-08 16:06:04 +02:00
|
|
|
fun Menu.isItemChecked(id: Int): Boolean {
|
|
|
|
return findItem(id)?.isChecked ?: false
|
|
|
|
}
|
|
|
|
|
2016-07-04 03:31:17 +02:00
|
|
|
fun Menu.setItemChecked(id: Int, checked: Boolean) {
|
|
|
|
findItem(id)?.isChecked = checked
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Menu.setMenuItemIcon(id: Int, icon: Int) {
|
|
|
|
findItem(id)?.setIcon(icon)
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:22:17 +01:00
|
|
|
fun Menu.setMenuItemIcon(id: Int, icon: Drawable) {
|
2017-02-04 08:39:08 +01:00
|
|
|
findItem(id)?.icon = icon
|
2016-12-09 13:22:17 +01:00
|
|
|
}
|
|
|
|
|
2016-07-04 03:31:17 +02:00
|
|
|
fun Menu.setMenuItemTitle(id: Int, title: Int) {
|
|
|
|
findItem(id)?.setTitle(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Menu.setMenuItemShowAsActionFlags(id: Int, flags: Int) {
|
|
|
|
val item = findItem(id) ?: return
|
|
|
|
item.setShowAsActionFlags(flags)
|
|
|
|
MenuItemCompat.setShowAsAction(item, flags)
|
|
|
|
}
|
2017-04-11 16:22:50 +02:00
|
|
|
|
|
|
|
fun Menu.findSubmenu(id: Int): SubMenu? {
|
|
|
|
return findItem(id)?.subMenu
|
|
|
|
}
|
2017-05-17 09:36:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
fun MenuItem.setActionIcon(context: Context, @DrawableRes iconRes: Int) {
|
|
|
|
val oldIcon = this.icon
|
|
|
|
if (oldIcon is ActionIconDrawable) {
|
|
|
|
val starIcon = ContextCompat.getDrawable(context, iconRes)
|
|
|
|
this.icon = ActionIconDrawable(starIcon, oldIcon.defaultColor)
|
|
|
|
} else {
|
|
|
|
setIcon(iconRes)
|
|
|
|
}
|
|
|
|
}
|