Warning, /network/kdeconnect-android/src/org/kde/kdeconnect/UserInterface/About/AutoGridLayout.kt is written in an unsupported language. File is not indexed.
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Maxim Leshchenko <cnmaks90@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.UserInterface.About 0008 0009 import android.content.Context 0010 import android.util.AttributeSet 0011 import androidx.gridlayout.widget.GridLayout 0012 import org.kde.kdeconnect_tp.R 0013 import kotlin.math.max 0014 0015 /** 0016 * GridLayout that adjusts the number of columns and rows to fill all screen space 0017 */ 0018 class AutoGridLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : GridLayout(context, attrs, defStyleAttr) { 0019 private var defaultColumnCount = 0 0020 private var columnWidth = 0 0021 private var changeColumnCountIfTheyHaveOnlyOneElement = false 0022 0023 init { 0024 var typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoGridLayout, 0, defStyleAttr) 0025 0026 try { 0027 columnWidth = typedArray.getDimensionPixelSize(R.styleable.AutoGridLayout_columnWidth, 0) 0028 changeColumnCountIfTheyHaveOnlyOneElement = typedArray.getBoolean(R.styleable.AutoGridLayout_changeColumnCountIfTheyHaveOnlyOneElement, false) 0029 typedArray = context.obtainStyledAttributes(attrs, intArrayOf(android.R.attr.columnCount), 0, defStyleAttr) 0030 defaultColumnCount = typedArray.getInt(0, 10) 0031 } finally { 0032 typedArray.recycle() 0033 } 0034 0035 columnCount = 1 0036 } 0037 0038 override fun onMeasure(widthSpec: Int, heightSpec: Int) { 0039 super.onMeasure(widthSpec, heightSpec) 0040 val width = MeasureSpec.getSize(widthSpec) 0041 0042 if (columnWidth > 0 && width > 0) { 0043 val totalSpace = width - paddingRight - paddingLeft 0044 var calculatedColumnCount = max(1, totalSpace / columnWidth) 0045 0046 if (calculatedColumnCount < childCount && changeColumnCountIfTheyHaveOnlyOneElement) { 0047 calculatedColumnCount = defaultColumnCount 0048 } 0049 0050 columnCount = calculatedColumnCount 0051 } else { 0052 columnCount = defaultColumnCount 0053 } 0054 } 0055 }