1 /*
2  * Copyright (C) 2020 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.util
18 
19 import android.graphics.Rect
20 import android.util.IndentingPrintWriter
21 import android.view.View
22 import android.view.ViewGroup
23 import java.io.PrintWriter
24 
25 /** [Sequence] that yields all of the direct children of this [ViewGroup] */
26 val ViewGroup.children
<lambda>null27     get() = sequence {
28         for (i in 0 until childCount) yield(getChildAt(i))
29     }
30 
31 /** Inclusive version of [Iterable.takeWhile] */
<lambda>null32 fun <T> Sequence<T>.takeUntil(pred: (T) -> Boolean): Sequence<T> = sequence {
33     for (x in this@takeUntil) {
34         yield(x)
35         if (pred(x)) {
36             break
37         }
38     }
39 }
40 
41 /**
42  * If `this` is an [IndentingPrintWriter], it will process block inside an indentation level.
43  *
44  * If not, this will just process block.
45  */
indentIfPossiblenull46 inline fun PrintWriter.indentIfPossible(block: PrintWriter.() -> Unit) {
47     if (this is IndentingPrintWriter) increaseIndent()
48     block()
49     if (this is IndentingPrintWriter) decreaseIndent()
50 }
51 
52 /** Convenience extension property for [View.getBoundsOnScreen]. */
53 val View.boundsOnScreen: Rect
54     get() {
55         val bounds = Rect()
56         getBoundsOnScreen(bounds)
57         return bounds
58     }
59