-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow the user to choose a different font
Android's choices for font customisation can be limited, depending on the vendor. Allow users to choose from a small collection of embedded fonts, chosen by asking users for recommendations. The font choice is implemented as a preference. Provide a custom dialog that shows the fonts (in that font) so the user can see what they're choosing between. Ensure the font's license information is displayed in the "About" section.
- Loading branch information
1 parent
dfc16c0
commit 6539146
Showing
66 changed files
with
616 additions
and
68 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/keylesspalace/tusky/util/EmbeddedFontFamily.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2023 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program 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. | ||
* | ||
* Pachli 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. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package com.keylesspalace.tusky.util | ||
|
||
import androidx.annotation.FontRes | ||
import androidx.annotation.StyleRes | ||
import com.keylesspalace.tusky.R | ||
|
||
enum class EmbeddedFontFamily(@FontRes val font: Int, @StyleRes val style: Int) { | ||
DEFAULT(-1, -1), | ||
ATKINSON_HYPERLEGIBLE(R.font.atkinson_hyperlegible, R.style.FontAtkinsonHyperlegible), | ||
COMICNEUE(R.font.comicneue, R.style.FontComicNeue), | ||
ESTEDAD(R.font.estedad, R.style.FontEstedad), | ||
LEXEND(R.font.lexend, R.style.FontLexend), | ||
LUCIOLE(R.font.luciole, R.style.FontLuciole), | ||
OPENDYSLEXIC(R.font.opendyslexic, R.style.FontOpenDyslexic); | ||
|
||
companion object { | ||
fun from(s: String?): EmbeddedFontFamily { | ||
s ?: return DEFAULT | ||
|
||
return try { | ||
valueOf(s.uppercase()) | ||
} catch (_: Throwable) { | ||
DEFAULT | ||
} | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
app/src/main/java/com/keylesspalace/tusky/view/FontFamilyDialogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2023 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program 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. | ||
* | ||
* Pachli 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. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package com.keylesspalace.tusky.view | ||
|
||
import android.content.DialogInterface.BUTTON_POSITIVE | ||
import android.graphics.Typeface | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import android.widget.TextView | ||
import androidx.appcompat.R | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.preference.ListPreference | ||
import androidx.preference.ListPreferenceDialogFragmentCompat | ||
import com.keylesspalace.tusky.util.EmbeddedFontFamily | ||
|
||
/** | ||
* Dialog fragment for choosing a font family. Displays the list of font families with each | ||
* entry in its font. | ||
*/ | ||
class FontFamilyDialogFragment : ListPreferenceDialogFragmentCompat() { | ||
private var clickedDialogEntryIndex = 0 | ||
private lateinit var entries: Array<CharSequence> | ||
private lateinit var entryValues: Array<CharSequence> | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
if (savedInstanceState == null) { | ||
val preference = preference as ListPreference | ||
check(!(preference.entries == null || preference.entryValues == null)) { | ||
"ListPreference requires an entries array and an entryValues array." | ||
} | ||
clickedDialogEntryIndex = preference.findIndexOfValue(preference.value) | ||
entries = preference.entries | ||
entryValues = preference.entryValues | ||
} else { | ||
clickedDialogEntryIndex = savedInstanceState.getInt(SAVE_STATE_INDEX, 0) | ||
entries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES) as Array<CharSequence> | ||
entryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES) as Array<CharSequence> | ||
} | ||
} | ||
|
||
override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) { | ||
super.onPrepareDialogBuilder(builder) | ||
|
||
val context = requireContext() | ||
|
||
// Use the same layout AlertDialog uses, as android.R.layout.simple_list_item_single_choice | ||
// puts the radio button at the end of the line, but the default dialog style puts it at | ||
// the start. | ||
val a = context.obtainStyledAttributes( | ||
null, | ||
R.styleable.AlertDialog, | ||
R.attr.alertDialogStyle, | ||
0 | ||
) | ||
val layout = a.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0) | ||
a.recycle() | ||
|
||
val adapter = object : ArrayAdapter<CharSequence>(context, layout, entries) { | ||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
val view = super.getView(position, convertView, parent) | ||
|
||
val fontFamily = EmbeddedFontFamily.from(entryValues[position].toString()) | ||
if (fontFamily == EmbeddedFontFamily.DEFAULT) { | ||
(view as TextView).typeface = Typeface.DEFAULT | ||
} else { | ||
(view as TextView).setTextAppearance(fontFamily.style) | ||
} | ||
return view | ||
} | ||
} | ||
|
||
builder.setSingleChoiceItems(adapter, clickedDialogEntryIndex) { dialog, which -> | ||
clickedDialogEntryIndex = which | ||
this@FontFamilyDialogFragment.onClick(dialog, BUTTON_POSITIVE) | ||
dialog.dismiss() | ||
} | ||
|
||
// The typical interaction for list-based dialogs is to have click-on-an-item dismiss the | ||
// dialog instead of the user having to press 'Ok'. | ||
builder.setPositiveButton(null, null) | ||
} | ||
|
||
override fun onDialogClosed(positiveResult: Boolean) { | ||
if (positiveResult && clickedDialogEntryIndex >= 0) { | ||
val value = entryValues[clickedDialogEntryIndex].toString() | ||
val preference = preference as ListPreference | ||
if (preference.callChangeListener(value)) { | ||
preference.value = value | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val TXN_TAG = "com.keylesspalace.tusky.view.FontFamilyDialogFragment" | ||
const val SAVE_STATE_INDEX = "FontFamilyDialogFragment.index" | ||
const val SAVE_STATE_ENTRIES = "FontFamilyDialogFragment.entries" | ||
const val SAVE_STATE_ENTRY_VALUES = "FontFamilyDialogFragment.entryValues" | ||
|
||
fun newInstance(key: String): FontFamilyDialogFragment { | ||
val fragment = FontFamilyDialogFragment() | ||
val b = Bundle(1) | ||
b.putString(ARG_KEY, key) | ||
fragment.arguments = b | ||
return fragment | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
<font | ||
app:fontStyle="normal" | ||
app:fontWeight="400" | ||
app:font="@font/atkinson_hyperlegible_regular_102" /> | ||
<font | ||
app:fontStyle="italic" | ||
app:fontWeight="400" | ||
app:font="@font/atkinson_hyperlegible_italic_102" /> | ||
<font | ||
app:fontStyle="normal" | ||
app:fontWeight="700" | ||
app:font="@font/atkinson_hyperlegible_bold_102" /> | ||
<font | ||
app:fontStyle="italic" | ||
app:fontWeight="700" | ||
app:font="@font/atkinson_hyperlegible_bolditalic_102" /> | ||
|
||
</font-family> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
<font | ||
app:fontStyle="normal" | ||
app:fontWeight="300" | ||
app:font="@font/comicneue_light" /> | ||
<font | ||
app:fontStyle="italic" | ||
app:fontWeight="300" | ||
app:font="@font/comicneue_lightitalic" /> | ||
<font | ||
app:fontStyle="normal" | ||
app:fontWeight="400" | ||
app:font="@font/comicneue_regular" /> | ||
<font | ||
app:fontStyle="italic" | ||
app:fontWeight="400" | ||
app:font="@font/comicneue_light" /> | ||
<font | ||
app:fontStyle="normal" | ||
app:fontWeight="700" | ||
app:font="@font/comicneue_bold" /> | ||
<font | ||
app:fontStyle="italic" | ||
app:fontWeight="700" | ||
app:font="@font/comicneue_bolditalic" /> | ||
</font-family> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.