|
| 1 | +package com.osfans.trime.common |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import android.view.Window |
| 5 | +import android.widget.FrameLayout |
| 6 | +import android.widget.LinearLayout |
| 7 | + |
| 8 | +/** (This Kotlin file has been taken from florisboard project) |
| 9 | + * This file has been taken from the Android LatinIME project. Following modifications were done by |
| 10 | + * florisboard to the original source code: |
| 11 | + * - Convert the code from Java to Kotlin |
| 12 | + * - Change package name to match the current project's one |
| 13 | + * - Remove method newLayoutParam() |
| 14 | + * - Remove method placeViewAt() |
| 15 | + * - Remove unnecessary variable params in updateLayoutGravityOf(), lp can directly be used due to |
| 16 | + * Kotlin's smart cast feature |
| 17 | + * - Remove unused imports |
| 18 | + * |
| 19 | + * The original source code can be found at the following location: |
| 20 | + * https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/refs/heads/master/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java |
| 21 | + */ |
| 22 | +object ViewUtils { |
| 23 | + fun updateLayoutHeightOf(window: Window, layoutHeight: Int) { |
| 24 | + val params = window.attributes |
| 25 | + if (params != null && params.height != layoutHeight) { |
| 26 | + params.height = layoutHeight |
| 27 | + window.attributes = params |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fun updateLayoutHeightOf(view: View, layoutHeight: Int) { |
| 32 | + val params = view.layoutParams |
| 33 | + if (params != null && params.height != layoutHeight) { |
| 34 | + params.height = layoutHeight |
| 35 | + view.layoutParams = params |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fun updateLayoutGravityOf(view: View, layoutGravity: Int) { |
| 40 | + val lp = view.layoutParams |
| 41 | + if (lp is LinearLayout.LayoutParams) { |
| 42 | + if (lp.gravity != layoutGravity) { |
| 43 | + lp.gravity = layoutGravity |
| 44 | + view.layoutParams = lp |
| 45 | + } |
| 46 | + } else if (lp is FrameLayout.LayoutParams) { |
| 47 | + if (lp.gravity != layoutGravity) { |
| 48 | + lp.gravity = layoutGravity |
| 49 | + view.layoutParams = lp |
| 50 | + } |
| 51 | + } else { |
| 52 | + throw IllegalArgumentException( |
| 53 | + "Layout parameter doesn't have gravity: " + |
| 54 | + lp.javaClass.name |
| 55 | + ) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fun getLocationOnScreen(view: View): IntArray { |
| 60 | + val outLocation: IntArray = intArrayOf(0, 0) |
| 61 | + view.getLocationOnScreen(outLocation) |
| 62 | + return outLocation |
| 63 | + } |
| 64 | +} |
0 commit comments