1 /*
2  * Copyright 2022 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 androidx.glance.appwidget
18 
19 import androidx.compose.runtime.Composable
20 import androidx.glance.Emittable
21 import androidx.glance.EmittableWithChildren
22 import androidx.glance.GlanceComposable
23 import androidx.glance.GlanceModifier
24 import androidx.glance.GlanceNode
25 
26 /**
27  * This composable is a marker used by [AppWidgetSession] to indicate that the result of a
28  * composition should be ignored.
29  *
30  * If this composable is present in the [Emittable] tree, [AppWidgetSession] will not translate the
31  * tree and update the app widget.
32  */
33 @Composable
34 @GlanceComposable
IgnoreResultnull35 internal fun IgnoreResult() {
36     GlanceNode(::EmittableIgnoreResult) {}
37 }
38 
39 internal class EmittableIgnoreResult : Emittable {
40     override var modifier: GlanceModifier = GlanceModifier
41 
<lambda>null42     override fun copy() = EmittableIgnoreResult().also { it.modifier = modifier }
43 }
44 
45 /**
46  * Returns true if this [Emittable] is an [EmittableIgnoreResult] or contains an
47  * [EmittableIgnoreResult] in its children.
48  */
shouldIgnoreResultnull49 internal fun Emittable.shouldIgnoreResult(): Boolean {
50     if (this is EmittableIgnoreResult) {
51         return true
52     } else if (this is EmittableWithChildren) {
53         if (children.any { it.shouldIgnoreResult() }) return true
54     }
55     return false
56 }
57