2017-02-20 16:13:48 +01:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2017 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.twidere.extension
|
|
|
|
|
|
|
|
import android.graphics.Rect
|
|
|
|
import android.graphics.RectF
|
2017-02-21 08:42:07 +01:00
|
|
|
import android.support.annotation.UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
import android.view.View
|
2017-05-13 08:19:23 +02:00
|
|
|
import android.widget.TextView
|
|
|
|
import org.mariotaku.ktextension.empty
|
2017-02-20 16:13:48 +01:00
|
|
|
|
|
|
|
private val tempLocation = IntArray(2)
|
|
|
|
private val tempRect = Rect()
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
fun View.getBounds(rect: RectF) {
|
|
|
|
rect.set(x, y, x + width, y + height)
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
fun View.getFrame(rect: Rect) {
|
|
|
|
rect.set(left, top, right, bottom)
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
|
|
|
fun View.getFrameRelatedTo(rect: Rect, other: View? = null) {
|
2017-02-20 16:13:48 +01:00
|
|
|
this.getFrame(rect)
|
2017-02-21 08:42:07 +01:00
|
|
|
if (other == null) {
|
|
|
|
offsetToRoot(this, rect)
|
|
|
|
} else if (other === this) {
|
|
|
|
rect.offsetTo(0, 0)
|
|
|
|
} else if (other !== parent) {
|
|
|
|
offsetToRoot(this, rect)
|
|
|
|
other.getFrame(tempRect)
|
|
|
|
offsetToRoot(other, tempRect)
|
2017-02-20 16:13:48 +01:00
|
|
|
rect.offset(-tempRect.left, -tempRect.top)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun View.getLocationOnScreen(rect: Rect) {
|
|
|
|
getLocationOnScreen(tempLocation)
|
|
|
|
rect.set(tempLocation[0], tempLocation[1], tempLocation[0] + width, tempLocation[1] + height)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun View.getLocationInWindow(rect: Rect) {
|
|
|
|
getLocationInWindow(tempLocation)
|
|
|
|
rect.set(tempLocation[0], tempLocation[1], tempLocation[0] + width, tempLocation[1] + height)
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:10:29 +02:00
|
|
|
fun View.addSystemUiVisibility(systemUiVisibility: Int) {
|
|
|
|
this.systemUiVisibility = this.systemUiVisibility or systemUiVisibility
|
|
|
|
}
|
|
|
|
|
|
|
|
fun View.removeSystemUiVisibility(systemUiVisibility: Int) {
|
|
|
|
this.systemUiVisibility = this.systemUiVisibility and systemUiVisibility.inv()
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:19:23 +02:00
|
|
|
fun View.hideIfEmpty(dependency: TextView, hideVisibility: Int = View.GONE) {
|
|
|
|
visibility = if (dependency.empty) {
|
|
|
|
hideVisibility
|
|
|
|
} else {
|
|
|
|
View.VISIBLE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:20:45 +02:00
|
|
|
fun View.setVisible(visible: Boolean, hiddenVisibility: Int = View.GONE) {
|
|
|
|
visibility = if (visible) View.VISIBLE else hiddenVisibility
|
|
|
|
}
|
2017-02-20 16:13:48 +01:00
|
|
|
private fun offsetToRoot(view: View, rect: Rect) {
|
|
|
|
var parent = view.parent as? View
|
|
|
|
while (parent != null) {
|
|
|
|
rect.offset(parent.left, parent.top)
|
|
|
|
parent = parent.parent as? View
|
|
|
|
}
|
|
|
|
}
|