|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Carmen Alvarez |
| 3 | + * |
| 4 | + * This file is part of Poet Assistant. |
| 5 | + * |
| 6 | + * Poet Assistant is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Poet Assistant is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package ca.rmen.android.poetassistant |
| 21 | + |
| 22 | +import android.view.View |
| 23 | +import android.view.ViewGroup |
| 24 | +import androidx.core.view.ViewCompat |
| 25 | +import androidx.core.view.WindowInsetsCompat |
| 26 | +import androidx.core.view.updateLayoutParams |
| 27 | + |
| 28 | +fun fixInsets(view: View) { |
| 29 | + // Issue #206: |
| 30 | + // https://developer.android.com/develop/ui/views/layout/edge-to-edge#system-bars-insets |
| 31 | + ViewCompat.setOnApplyWindowInsetsListener(view) { v, windowInsets -> |
| 32 | + val insets = windowInsets.getInsets( |
| 33 | + WindowInsetsCompat.Type.systemBars() |
| 34 | + or WindowInsetsCompat.Type.displayCutout() or WindowInsetsCompat.Type.statusBars() |
| 35 | + ) |
| 36 | + // To control the status bar color, we have to draw a view behind it. |
| 37 | + // https://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int) |
| 38 | + // If we have this view, then make it the height of the status bar. |
| 39 | + val statusBarView = v.findViewById<View>(R.id.status_bar_view) |
| 40 | + statusBarView?.updateLayoutParams<ViewGroup.LayoutParams> { |
| 41 | + height = insets.top |
| 42 | + } |
| 43 | + v.updateLayoutParams<ViewGroup.MarginLayoutParams> { |
| 44 | + // If we don't have a status bar view, we need to shift the content |
| 45 | + // of the root view down, so it's below the status bar. |
| 46 | + if (statusBarView == null) { |
| 47 | + topMargin = insets.top |
| 48 | + } |
| 49 | + leftMargin = insets.left |
| 50 | + bottomMargin = insets.bottom |
| 51 | + rightMargin = insets.right |
| 52 | + } |
| 53 | + WindowInsetsCompat.CONSUMED |
| 54 | + } |
| 55 | +} |
0 commit comments