Added android 10 dark theme support
This commit is contained in:
parent
46cdedce0b
commit
153f1bd9d0
33 changed files with 372 additions and 352 deletions
|
@ -23,7 +23,7 @@ class ATESecondaryTextView @JvmOverloads constructor(
|
|||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : BaselineGridTextView(context, attrs, defStyleAttr) {
|
||||
) : MaterialTextView(context, attrs, defStyleAttr) {
|
||||
|
||||
init {
|
||||
setTextColor(ThemeStore.textColorSecondary(context))
|
||||
|
|
|
@ -1,209 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.appthemehelper.common.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Paint;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import androidx.annotation.FontRes;
|
||||
import androidx.appcompat.widget.AppCompatTextView;
|
||||
|
||||
import com.google.android.material.textview.MaterialTextView;
|
||||
|
||||
import code.name.monkey.appthemehelper.R;
|
||||
|
||||
|
||||
/**
|
||||
* An extension to {@link AppCompatTextView} which aligns text to a 4dp baseline grid.
|
||||
* <p>
|
||||
* To achieve this we expose a {@code lineHeightHint} allowing you to specify the desired line
|
||||
* height (alternatively a {@code lineHeightMultiplierHint} to use a multiplier of the text size).
|
||||
* This line height will be adjusted to be a multiple of 4dp to ensure that baselines sit on
|
||||
* the grid.
|
||||
* <p>
|
||||
* We also adjust spacing above and below the text to ensure that the first line's baseline sits on
|
||||
* the grid (relative to the view's top) & that this view's height is a multiple of 4dp so that
|
||||
* subsequent views start on the grid.
|
||||
*/
|
||||
public class BaselineGridTextView extends MaterialTextView {
|
||||
|
||||
private final float FOUR_DIP;
|
||||
|
||||
private float lineHeightMultiplierHint = 1f;
|
||||
private float lineHeightHint = 0f;
|
||||
private boolean maxLinesByHeight = false;
|
||||
private int extraTopPadding = 0;
|
||||
private int extraBottomPadding = 0;
|
||||
private @FontRes
|
||||
int fontResId = 0;
|
||||
|
||||
public BaselineGridTextView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public BaselineGridTextView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, android.R.attr.textViewStyle);
|
||||
}
|
||||
|
||||
public BaselineGridTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
|
||||
final TypedArray a = context.obtainStyledAttributes(
|
||||
attrs, R.styleable.BaselineGridTextView, defStyleAttr, 0);
|
||||
|
||||
// first check TextAppearance for line height & font attributes
|
||||
if (a.hasValue(R.styleable.BaselineGridTextView_android_textAppearance)) {
|
||||
int textAppearanceId =
|
||||
a.getResourceId(R.styleable.BaselineGridTextView_android_textAppearance,
|
||||
android.R.style.TextAppearance);
|
||||
TypedArray ta = context.obtainStyledAttributes(
|
||||
textAppearanceId, R.styleable.BaselineGridTextView);
|
||||
parseTextAttrs(ta);
|
||||
ta.recycle();
|
||||
}
|
||||
|
||||
// then check view attrs
|
||||
parseTextAttrs(a);
|
||||
maxLinesByHeight = a.getBoolean(R.styleable.BaselineGridTextView_maxLinesByHeight, false);
|
||||
a.recycle();
|
||||
|
||||
FOUR_DIP = TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
|
||||
computeLineHeight();
|
||||
}
|
||||
|
||||
public float getLineHeightMultiplierHint() {
|
||||
return lineHeightMultiplierHint;
|
||||
}
|
||||
|
||||
public void setLineHeightMultiplierHint(float lineHeightMultiplierHint) {
|
||||
this.lineHeightMultiplierHint = lineHeightMultiplierHint;
|
||||
computeLineHeight();
|
||||
}
|
||||
|
||||
public float getLineHeightHint() {
|
||||
return lineHeightHint;
|
||||
}
|
||||
|
||||
public void setLineHeightHint(float lineHeightHint) {
|
||||
this.lineHeightHint = lineHeightHint;
|
||||
computeLineHeight();
|
||||
}
|
||||
|
||||
public boolean getMaxLinesByHeight() {
|
||||
return maxLinesByHeight;
|
||||
}
|
||||
|
||||
public void setMaxLinesByHeight(boolean maxLinesByHeight) {
|
||||
this.maxLinesByHeight = maxLinesByHeight;
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
public @FontRes
|
||||
int getFontResId() {
|
||||
return fontResId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCompoundPaddingTop() {
|
||||
// include extra padding to place the first line's baseline on the grid
|
||||
return super.getCompoundPaddingTop() + extraTopPadding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCompoundPaddingBottom() {
|
||||
// include extra padding to make the height a multiple of 4dp
|
||||
return super.getCompoundPaddingBottom() + extraBottomPadding;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
extraTopPadding = 0;
|
||||
extraBottomPadding = 0;
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
int height = getMeasuredHeight();
|
||||
height += ensureBaselineOnGrid();
|
||||
height += ensureHeightGridAligned(height);
|
||||
setMeasuredDimension(getMeasuredWidth(), height);
|
||||
checkMaxLines(height, MeasureSpec.getMode(heightMeasureSpec));
|
||||
}
|
||||
|
||||
private void parseTextAttrs(TypedArray a) {
|
||||
if (a.hasValue(R.styleable.BaselineGridTextView_lineHeightMultiplierHint)) {
|
||||
lineHeightMultiplierHint =
|
||||
a.getFloat(R.styleable.BaselineGridTextView_lineHeightMultiplierHint, 1f);
|
||||
}
|
||||
if (a.hasValue(R.styleable.BaselineGridTextView_lineHeightHint)) {
|
||||
lineHeightHint = a.getDimensionPixelSize(
|
||||
R.styleable.BaselineGridTextView_lineHeightHint, 0);
|
||||
}
|
||||
if (a.hasValue(R.styleable.BaselineGridTextView_android_fontFamily)) {
|
||||
fontResId = a.getResourceId(R.styleable.BaselineGridTextView_android_fontFamily, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures line height is a multiple of 4dp.
|
||||
*/
|
||||
private void computeLineHeight() {
|
||||
final Paint.FontMetrics fm = getPaint().getFontMetrics();
|
||||
final float fontHeight = Math.abs(fm.ascent - fm.descent) + fm.leading;
|
||||
final float desiredLineHeight = (lineHeightHint > 0)
|
||||
? lineHeightHint
|
||||
: lineHeightMultiplierHint * fontHeight;
|
||||
|
||||
final int baselineAlignedLineHeight =
|
||||
(int) ((FOUR_DIP * (float) Math.ceil(desiredLineHeight / FOUR_DIP)) + 0.5f);
|
||||
setLineSpacing(baselineAlignedLineHeight - fontHeight, 1f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the first line of text sits on the 4dp grid.
|
||||
*/
|
||||
private int ensureBaselineOnGrid() {
|
||||
float baseline = getBaseline();
|
||||
float gridAlign = baseline % FOUR_DIP;
|
||||
if (gridAlign != 0) {
|
||||
extraTopPadding = (int) (FOUR_DIP - Math.ceil(gridAlign));
|
||||
}
|
||||
return extraTopPadding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that height is a multiple of 4dp.
|
||||
*/
|
||||
private int ensureHeightGridAligned(int height) {
|
||||
float gridOverhang = height % FOUR_DIP;
|
||||
if (gridOverhang != 0) {
|
||||
extraBottomPadding = (int) (FOUR_DIP - Math.ceil(gridOverhang));
|
||||
}
|
||||
return extraBottomPadding;
|
||||
}
|
||||
|
||||
/**
|
||||
* When measured with an exact height, text can be vertically clipped mid-line. Prevent
|
||||
* this by setting the {@code maxLines} property based on the available space.
|
||||
*/
|
||||
private void checkMaxLines(int height, int heightMode) {
|
||||
if (!maxLinesByHeight || heightMode != MeasureSpec.EXACTLY) return;
|
||||
|
||||
int textHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom();
|
||||
int completeLines = (int) Math.floor(textHeight / getLineHeight());
|
||||
setMaxLines(completeLines);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package code.name.monkey.appthemehelper.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.TypedArray
|
||||
import androidx.annotation.AttrRes
|
||||
|
||||
/**
|
||||
|
@ -10,7 +9,7 @@ import androidx.annotation.AttrRes
|
|||
object ATHUtil {
|
||||
|
||||
fun isWindowBackgroundDark(context: Context): Boolean {
|
||||
return !ColorUtil.isColorLight(ATHUtil.resolveColor(context, android.R.attr.windowBackground))
|
||||
return !ColorUtil.isColorLight(resolveColor(context, android.R.attr.windowBackground))
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
|
|
|
@ -36,14 +36,6 @@
|
|||
<attr name="ateKey_prefCategory_textColor" format="string" />
|
||||
</declare-styleable>
|
||||
|
||||
<attr name="lineHeightHint" format="dimension" />
|
||||
|
||||
<declare-styleable name="BaselineGridTextView">
|
||||
<attr name="lineHeightMultiplierHint" format="float" />
|
||||
<attr name="lineHeightHint" />
|
||||
<attr name="maxLinesByHeight" format="boolean" />
|
||||
<attr name="android:textAppearance" />
|
||||
<attr name="android:fontFamily" />
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue