1 /*
2  * Copyright 2021 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.compose.ui.platform
18 
19 import androidx.compose.runtime.Composable
20 import androidx.compose.runtime.LaunchedEffect
21 import androidx.compose.runtime.Stable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.compose.runtime.rememberUpdatedState
25 import androidx.compose.runtime.setValue
26 import androidx.compose.runtime.snapshotFlow
27 import androidx.compose.ui.input.pointer.EmptyPointerKeyboardModifiers
28 import androidx.compose.ui.input.pointer.PointerKeyboardModifiers
29 import androidx.compose.ui.unit.IntSize
30 
31 /** Provides information about the Window that is hosting this compose hierarchy. */
32 @Stable
33 interface WindowInfo {
34     /**
35      * Indicates whether the window hosting this compose hierarchy is in focus.
36      *
37      * When there are multiple windows visible, either in a multi-window environment or if a popup
38      * or dialog is visible, this property can be used to determine if the current window is in
39      * focus.
40      */
41     val isWindowFocused: Boolean
42 
43     /** Indicates the state of keyboard modifiers (pressed or not). */
44     @Suppress("OPT_IN_MARKER_ON_WRONG_TARGET")
45     val keyboardModifiers: PointerKeyboardModifiers
46         get() = WindowInfoImpl.GlobalKeyboardModifiers.value
47 
48     /**
49      * Size of the window. This size excludes insets, such as any system bars, so it is not safe to
50      * assume that this size matches the available space of the compose hierarchy hosted inside this
51      * window. Instead this size should be used as a breakpoint when changing between UI
52      * configurations, or similar window-dependent configuration.
53      */
54     val containerSize: IntSize
55         get() = IntSize(Int.MIN_VALUE, Int.MIN_VALUE)
56 }
57 
58 @Composable
WindowFocusObservernull59 internal fun WindowFocusObserver(onWindowFocusChanged: (isWindowFocused: Boolean) -> Unit) {
60     val windowInfo = LocalWindowInfo.current
61     val callback = rememberUpdatedState(onWindowFocusChanged)
62     LaunchedEffect(windowInfo) {
63         snapshotFlow { windowInfo.isWindowFocused }.collect { callback.value(it) }
64     }
65 }
66 
67 internal class WindowInfoImpl : WindowInfo {
68     private val _containerSize = mutableStateOf(IntSize.Zero)
69 
70     override var isWindowFocused: Boolean by mutableStateOf(false)
71 
72     override var keyboardModifiers: PointerKeyboardModifiers
73         get() = GlobalKeyboardModifiers.value
74         set(value) {
75             GlobalKeyboardModifiers.value = value
76         }
77 
78     override var containerSize: IntSize
79         get() = _containerSize.value
80         set(value) {
81             _containerSize.value = value
82         }
83 
84     companion object {
85         // One instance across all windows makes sense, since the state of KeyboardModifiers is
86         // common for all windows.
87         internal val GlobalKeyboardModifiers = mutableStateOf(EmptyPointerKeyboardModifiers())
88     }
89 }
90