• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.qs.panels.ui.compose
18 
19 import androidx.compose.runtime.Composable
20 import androidx.compose.ui.Modifier
21 import com.android.compose.animation.scene.ContentScope
22 import com.android.systemui.qs.panels.shared.model.SizedTile
23 import com.android.systemui.qs.panels.shared.model.TileRow
24 import com.android.systemui.qs.panels.ui.viewmodel.EditTileViewModel
25 import com.android.systemui.qs.panels.ui.viewmodel.TileViewModel
26 import com.android.systemui.qs.pipeline.shared.TileSpec
27 
28 /** A layout of tiles, indicating how they should be composed when showing in QS or in edit mode. */
29 interface GridLayout {
30 
31     /**
32      * [listening] can be used to compose the grid but limit when tiles should be listening. It
33      * should be a function tracking a snapshot state.
34      */
35     @Composable
ContentScopenull36     fun ContentScope.TileGrid(
37         tiles: List<TileViewModel>,
38         modifier: Modifier,
39         listening: () -> Boolean,
40     )
41 
42     @Composable
43     fun EditTileGrid(
44         tiles: List<EditTileViewModel>,
45         modifier: Modifier,
46         onAddTile: (TileSpec, Int) -> Unit,
47         onRemoveTile: (TileSpec) -> Unit,
48         onSetTiles: (List<TileSpec>) -> Unit,
49         onStopEditing: () -> Unit,
50     )
51 }
52 
53 /**
54  * A type of [GridLayout] that can be paginated, to use together with [PaginatedGridLayout].
55  *
56  * [splitIntoPages] determines how to split a list of tiles based on the number of rows and columns
57  * available.
58  */
59 interface PaginatableGridLayout : GridLayout {
60     fun splitIntoPages(
61         tiles: List<TileViewModel>,
62         rows: Int,
63         columns: Int,
64     ): List<List<TileViewModel>>
65 
66     companion object {
67 
68         /**
69          * Splits a list of [SizedTile] into rows, each with at most [columns] occupied.
70          *
71          * It will leave gaps at the end of a row if the next [SizedTile] has [SizedTile.width] that
72          * is larger than the space remaining in the row.
73          */
74         fun splitInRows(
75             tiles: List<SizedTile<TileViewModel>>,
76             columns: Int,
77         ): List<List<SizedTile<TileViewModel>>> {
78             val row = TileRow<TileViewModel>(columns)
79 
80             return buildList {
81                 for (tile in tiles) {
82                     check(tile.width <= columns)
83                     if (!row.maybeAddTile(tile)) {
84                         // Couldn't add tile to previous row, create a row with the current tiles
85                         // and start a new one
86                         add(row.tiles)
87                         row.clear()
88                         row.maybeAddTile(tile)
89                     }
90                 }
91                 if (row.tiles.isNotEmpty()) {
92                     add(row.tiles)
93                 }
94             }
95         }
96     }
97 }
98