1 /*
<lambda>null2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.systemui.common.ui.view
18
19 import android.view.View
20 import kotlinx.coroutines.DisposableHandle
21
22 /**
23 * Set this view's [View#importantForAccessibility] to [View#IMPORTANT_FOR_ACCESSIBILITY_YES] or
24 * [View#IMPORTANT_FOR_ACCESSIBILITY_NO] based on [value].
25 */
26 fun View.setImportantForAccessibilityYesNo(value: Boolean) {
27 importantForAccessibility =
28 if (value) View.IMPORTANT_FOR_ACCESSIBILITY_YES else View.IMPORTANT_FOR_ACCESSIBILITY_NO
29 }
30
31 /**
32 * Can be used to find the nearest parent of a view of a particular type.
33 *
34 * Usage:
35 * ```
36 * val textView = view.getNearestParent<TextView>()
37 * ```
38 */
getNearestParentnull39 inline fun <reified T : View> View.getNearestParent(): T? {
40 var view: Any? = this
41 while (view is View) {
42 if (view is T) return view
43 view = view.parent
44 }
45 return null
46 }
47
48 /** Adds a [View.OnLayoutChangeListener] and provides a [DisposableHandle] for teardown. */
onLayoutChangednull49 fun View.onLayoutChanged(onLayoutChanged: (v: View) -> Unit): DisposableHandle =
50 onLayoutChanged { v, _, _, _, _, _, _, _, _ ->
51 onLayoutChanged(v)
52 }
53
54 /** Adds the [View.OnLayoutChangeListener] and provides a [DisposableHandle] for teardown. */
onLayoutChangednull55 fun View.onLayoutChanged(listener: View.OnLayoutChangeListener): DisposableHandle {
56 addOnLayoutChangeListener(listener)
57 return DisposableHandle { removeOnLayoutChangeListener(listener) }
58 }
59
60 /** Adds a [View.OnApplyWindowInsetsListener] and provides a [DisposableHandle] for teardown. */
onApplyWindowInsetsnull61 fun View.onApplyWindowInsets(listener: View.OnApplyWindowInsetsListener): DisposableHandle {
62 setOnApplyWindowInsetsListener(listener)
63 return DisposableHandle { setOnApplyWindowInsetsListener(null) }
64 }
65
66 /** Adds a [View.OnTouchListener] and provides a [DisposableHandle] for teardown. */
onTouchListenernull67 fun View.onTouchListener(listener: View.OnTouchListener): DisposableHandle {
68 setOnTouchListener(listener)
69 return DisposableHandle { setOnTouchListener(null) }
70 }
71