Skip to content

Commit b084b33

Browse files
committed
fix(ui): make dialogs consistent and usable with the keyboard open
Keyboard dialogs follow the keyboard theme's colours and corners. The five text expander dialogs use the shared dialog, with a warning colour for delete and discard. Shared settings dialogs use the screen width and stay above the keyboard, so their buttons remain visible. Lighter placeholder and section chips in the text expander. Menus use a three dot button; the bulk editor's load, save and restore actions are named entries in it, replacing two arrow icons.
1 parent 23533ae commit b084b33

13 files changed

Lines changed: 247 additions & 135 deletions

File tree

‎app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesView.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package helium314.keyboard.keyboard.emoji
99

10+
import helium314.keyboard.latin.utils.applyKeyboardStyle
1011
import helium314.keyboard.latin.utils.keyboardSearchBarBackground
1112
import androidx.core.graphics.ColorUtils
1213
import android.view.Gravity
@@ -422,6 +423,7 @@ class EmojiPalettesView @JvmOverloads constructor(
422423
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
423424
}
424425
dialog.show()
426+
dialog.applyKeyboardStyle()
425427
true
426428
}
427429
}

‎app/src/main/java/helium314/keyboard/latin/LatinIME.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package helium314.keyboard.latin
22

3+
import helium314.keyboard.latin.utils.applyKeyboardStyle
34
import android.Manifest
45
import android.annotation.SuppressLint
56
import android.app.AlertDialog
@@ -1540,6 +1541,7 @@ class LatinIME : InputMethodService(),
15401541
optionsDialog = dialog
15411542
try {
15421543
dialog.show()
1544+
dialog.applyKeyboardStyle()
15431545
return true
15441546
} catch (e: Exception) {
15451547
Log.e(TAG, "Failed to show optionsDialog", e)

‎app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package helium314.keyboard.latin.suggestions
77

8+
import helium314.keyboard.latin.utils.applyKeyboardStyle
89
import helium314.keyboard.latin.utils.keyboardCornerRadiusPx
910
import android.animation.ValueAnimator
1011
import android.annotation.SuppressLint
@@ -1374,6 +1375,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int)
13741375
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
13751376
}
13761377
dialog.show()
1378+
dialog.applyKeyboardStyle()
13771379
}
13781380

13791381
fun showTranslateLanguageSelector() {
Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
11
package helium314.keyboard.latin.utils
22

3+
import android.app.AlertDialog
34
import android.content.Context
5+
import android.content.res.ColorStateList
6+
import android.graphics.Color
7+
import android.graphics.drawable.GradientDrawable
8+
import android.graphics.drawable.InsetDrawable
49
import android.view.ContextThemeWrapper
5-
import helium314.keyboard.latin.R
10+
import android.view.View
11+
import android.view.ViewGroup
12+
import android.widget.CheckedTextView
13+
import android.widget.TextView
14+
import androidx.core.graphics.ColorUtils
15+
import helium314.keyboard.latin.common.ColorType
16+
import helium314.keyboard.latin.settings.Settings
17+
18+
private const val MAX_CORNER_RADIUS_DP = 28
19+
private const val WINDOW_INSET_DP = 16
20+
private const val MIN_ACCENT_CONTRAST = 4.5
621

722
// todo: ideally the custom InputMethodPicker would be removed / replaced with compose dialog, then this can be removed
823
fun getPlatformDialogThemeContext(context: Context): Context {
9-
// Because {@link AlertDialog.Builder.create()} doesn't honor the specified theme with
10-
// createThemeContextWrapper=false, the result dialog box has unneeded paddings around it.
11-
return ContextThemeWrapper(context, R.style.platformActivityTheme)
24+
// light or dark like the keyboard, not like the system, so everything the dialog draws itself stays readable
25+
// An activity theme, because {@link AlertDialog.Builder.create()} doesn't honor a specified dialog theme with
26+
// createThemeContextWrapper=false, the result dialog box would have unneeded paddings around it.
27+
val isLight = ColorUtils.calculateLuminance(keyboardDialogBackgroundColor()) > 0.4
28+
return ContextThemeWrapper(context,
29+
if (isLight) android.R.style.Theme_DeviceDefault_Light else android.R.style.Theme_DeviceDefault)
30+
}
31+
32+
private fun keyboardDialogBackgroundColor() =
33+
ColorUtils.setAlphaComponent(Settings.getValues().mColors.get(ColorType.MAIN_BACKGROUND), 0xFF)
34+
35+
/** Gives a dialog opened from the keyboard the keyboard's colours and corners. Call after [AlertDialog.show]. */
36+
fun AlertDialog.applyKeyboardStyle() {
37+
val window = window ?: return
38+
val colors = Settings.getValues().mColors
39+
val density = context.resources.displayMetrics.density
40+
val background = keyboardDialogBackgroundColor()
41+
val textColor = ColorUtils.setAlphaComponent(colors.get(ColorType.KEY_TEXT), 0xFF)
42+
val accent = ColorUtils.setAlphaComponent(colors.get(ColorType.ACTION_KEY_BACKGROUND), 0xFF)
43+
val accentIsReadable = ColorUtils.calculateContrast(accent, background) >= MIN_ACCENT_CONTRAST
44+
val shape = GradientDrawable().apply {
45+
// the largest key radius would cut into the text
46+
cornerRadius = minOf(keyboardCornerRadiusPx(context.resources), MAX_CORNER_RADIUS_DP * density)
47+
setColor(background)
48+
}
49+
window.setBackgroundDrawable(InsetDrawable(shape, (WINDOW_INSET_DP * density).toInt()))
50+
51+
val titleId = context.resources.getIdentifier("alertTitle", "id", "android")
52+
if (titleId != 0) findViewById<TextView>(titleId)?.setTextColor(textColor)
53+
findViewById<TextView>(android.R.id.message)?.setTextColor(textColor)
54+
for (which in intArrayOf(AlertDialog.BUTTON_POSITIVE, AlertDialog.BUTTON_NEGATIVE, AlertDialog.BUTTON_NEUTRAL)) {
55+
getButton(which)?.setTextColor(if (accentIsReadable) accent else textColor)
56+
}
57+
58+
val list = listView ?: return
59+
fun styleRow(row: View) {
60+
if (row !is TextView) return
61+
row.setTextColor(textColor)
62+
if (row is CheckedTextView) row.checkMarkTintList = ColorStateList.valueOf(textColor)
63+
}
64+
list.setBackgroundColor(Color.TRANSPARENT)
65+
// rows are created while the list is laid out, which may be after this
66+
list.setOnHierarchyChangeListener(object : ViewGroup.OnHierarchyChangeListener {
67+
override fun onChildViewAdded(parent: View?, child: View) = styleRow(child)
68+
override fun onChildViewRemoved(parent: View?, child: View?) = Unit
69+
})
70+
for (i in 0 until list.childCount) styleRow(list.getChildAt(i))
1271
}

‎app/src/main/java/helium314/keyboard/settings/SearchScreen.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import androidx.compose.runtime.remember
4242
import androidx.compose.runtime.setValue
4343
import androidx.compose.ui.Modifier
4444
import androidx.compose.ui.draw.clip
45-
import androidx.compose.ui.draw.rotate
4645
import androidx.compose.ui.focus.FocusRequester
4746
import androidx.compose.ui.focus.focusRequester
4847
import androidx.compose.ui.res.painterResource
@@ -334,7 +333,7 @@ fun <T: Any?> SearchScreen(
334333
var showMenu by remember { mutableStateOf(false) }
335334
IconButton(
336335
onClick = { showMenu = true }
337-
) { Icon(painterResource(R.drawable.ic_arrow_left), "menu", Modifier.rotate(-90f)) }
336+
) { Icon(painterResource(R.drawable.ic_more_vert), "menu") }
338337
DropdownMenu(
339338
expanded = showMenu,
340339
onDismissRequest = { showMenu = false }
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
package helium314.keyboard.settings.dialogs
3+
4+
import android.os.Build
5+
import android.view.WindowManager
6+
import androidx.compose.foundation.gestures.detectTapGestures
7+
import androidx.compose.foundation.layout.Box
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.fillMaxWidth
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.layout.safeDrawingPadding
12+
import androidx.compose.foundation.layout.widthIn
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.SideEffect
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.rememberUpdatedState
17+
import androidx.compose.ui.Alignment
18+
import androidx.compose.ui.Modifier
19+
import androidx.compose.ui.input.pointer.pointerInput
20+
import androidx.compose.ui.platform.LocalView
21+
import androidx.compose.ui.unit.dp
22+
import androidx.compose.ui.window.Dialog
23+
import androidx.compose.ui.window.DialogProperties
24+
import androidx.compose.ui.window.DialogWindowProvider
25+
26+
/**
27+
* The window of the settings dialogs: as wide as the screen allows, and above the keyboard, so a dialog with a text
28+
* field keeps its buttons visible. The dialog content must be able to shrink, by scrolling.
29+
* Of [properties], the dismiss and security settings are used.
30+
*/
31+
@Composable
32+
fun DialogWindow(onDismissRequest: () -> Unit, properties: DialogProperties, content: @Composable () -> Unit) {
33+
Dialog(
34+
onDismissRequest = onDismissRequest,
35+
properties = DialogProperties(
36+
dismissOnBackPress = properties.dismissOnBackPress,
37+
dismissOnClickOutside = properties.dismissOnClickOutside,
38+
securePolicy = properties.securePolicy,
39+
usePlatformDefaultWidth = false,
40+
decorFitsSystemWindows = false,
41+
)
42+
) {
43+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
44+
// before Android 11 the keyboard insets are only reported to windows that resize for it
45+
val view = LocalView.current
46+
SideEffect {
47+
@Suppress("DEPRECATION")
48+
(view.parent as? DialogWindowProvider)?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
49+
}
50+
}
51+
val dismiss by rememberUpdatedState(onDismissRequest)
52+
val dismissOnClickOutside by rememberUpdatedState(properties.dismissOnClickOutside)
53+
// the window covers the screen, so taps around the dialog don't count as outside of it by themselves
54+
Box(
55+
modifier = Modifier
56+
.fillMaxSize()
57+
.pointerInput(Unit) { detectTapGestures { if (dismissOnClickOutside) dismiss() } }
58+
.safeDrawingPadding()
59+
.padding(16.dp),
60+
contentAlignment = Alignment.Center
61+
) {
62+
Box(
63+
modifier = Modifier
64+
.widthIn(max = 560.dp)
65+
.fillMaxWidth()
66+
.pointerInput(Unit) { detectTapGestures { } }, // taps on the dialog itself stay in it
67+
propagateMinConstraints = true
68+
) {
69+
content()
70+
}
71+
}
72+
}
73+
}

‎app/src/main/java/helium314/keyboard/settings/dialogs/PreferenceDialog.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import androidx.compose.ui.platform.LocalFocusManager
3434
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
3535
import androidx.compose.ui.res.painterResource
3636
import androidx.compose.ui.unit.dp
37-
import androidx.compose.ui.window.Dialog
3837
import androidx.compose.ui.window.DialogProperties
3938
import helium314.keyboard.latin.R
4039

@@ -170,7 +169,7 @@ fun PreferenceDialog(
170169
buttons: @Composable (() -> Unit)? = null,
171170
content: @Composable (() -> Unit)? = null,
172171
) {
173-
Dialog(
172+
DialogWindow(
174173
onDismissRequest = onDismissRequest,
175174
properties = properties
176175
) {

‎app/src/main/java/helium314/keyboard/settings/dialogs/ThreeButtonAlertDialog.kt‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import androidx.compose.material3.contentColorFor
2424
import androidx.compose.runtime.Composable
2525
import androidx.compose.runtime.CompositionLocalProvider
2626
import androidx.compose.ui.Modifier
27+
import androidx.compose.ui.graphics.Color
2728
import androidx.compose.ui.res.stringResource
2829
import androidx.compose.ui.tooling.preview.Preview
2930
import androidx.compose.ui.unit.dp
30-
import androidx.compose.ui.window.Dialog
3131
import androidx.compose.ui.window.DialogProperties
3232
import helium314.keyboard.settings.Theme
3333
import helium314.keyboard.settings.previewDark
@@ -46,6 +46,7 @@ fun ThreeButtonAlertDialogContent(
4646
confirmButtonText: String? = stringResource(android.R.string.ok),
4747
cancelButtonText: String? = null,
4848
neutralButtonText: String? = null,
49+
confirmButtonColor: Color = Color.Unspecified, // for destructive actions
4950
) {
5051
PreferenceDialogContent(
5152
onDismissRequest = onDismissRequest,
@@ -72,7 +73,7 @@ fun ThreeButtonAlertDialogContent(
7273
enabled = checkOk(),
7374
onClick = { onConfirmed(); onDismissRequest() },
7475
) {
75-
Text(confirmButtonText, maxLines = 1, softWrap = false)
76+
Text(confirmButtonText, color = confirmButtonColor, maxLines = 1, softWrap = false)
7677
}
7778
}
7879
} else {
@@ -97,7 +98,7 @@ fun ThreeButtonAlertDialogContent(
9798
enabled = checkOk(),
9899
onClick = { onConfirmed(); onDismissRequest() },
99100
) {
100-
Text(confirmButtonText, maxLines = 1, softWrap = false)
101+
Text(confirmButtonText, color = confirmButtonColor, maxLines = 1, softWrap = false)
101102
}
102103
}
103104
}
@@ -121,10 +122,11 @@ fun ThreeButtonAlertDialog(
121122
confirmButtonText: String? = stringResource(android.R.string.ok),
122123
cancelButtonText: String? = null,
123124
neutralButtonText: String? = null,
125+
confirmButtonColor: Color = Color.Unspecified, // for destructive actions
124126
reducePadding: Boolean = false,
125127
properties: DialogProperties = DialogProperties()
126128
) {
127-
Dialog(
129+
DialogWindow(
128130
onDismissRequest = onDismissRequest,
129131
properties = properties
130132
) {
@@ -140,7 +142,8 @@ fun ThreeButtonAlertDialog(
140142
checkOk = checkOk,
141143
confirmButtonText = confirmButtonText,
142144
cancelButtonText = cancelButtonText,
143-
neutralButtonText = neutralButtonText
145+
neutralButtonText = neutralButtonText,
146+
confirmButtonColor = confirmButtonColor
144147
)
145148
}
146149
}

‎app/src/main/java/helium314/keyboard/settings/screens/TextExpanderScreen.kt‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import androidx.compose.foundation.layout.size
3333
import androidx.compose.foundation.rememberScrollState
3434
import androidx.compose.foundation.shape.RoundedCornerShape
3535
import androidx.compose.foundation.verticalScroll
36-
import androidx.compose.material3.AlertDialog
3736
import androidx.compose.material3.Button
3837
import androidx.compose.material3.Card
3938
import androidx.compose.material3.CardDefaults
@@ -474,35 +473,22 @@ fun TextExpanderScreen(onClickBack: () -> Unit) {
474473
val cleanName = if (shortcutKey.startsWith(TextExpanderUtils.REGEX_PREFIX)) {
475474
shortcutKey.substring(TextExpanderUtils.REGEX_PREFIX.length)
476475
} else shortcutKey
477-
AlertDialog(
476+
ThreeButtonAlertDialog(
478477
onDismissRequest = { shortcutToDelete = null },
479-
confirmButton = {
480-
TextButton(
481-
onClick = {
482-
val updated = shortcutsMap.toMutableMap()
483-
updated.remove(shortcutKey)
484-
shortcutsMap = updated
485-
TextExpanderUtils.saveShortcuts(context, updated)
486-
if (entry.imageUri != null) {
487-
TextExpanderImageUtils.deleteImageIfUnreferenced(context, entry.imageUri, updated)
488-
}
489-
shortcutToDelete = null
490-
}
491-
) {
492-
Text(
493-
text = stringResource(R.string.delete),
494-
color = MaterialTheme.colorScheme.error,
495-
fontWeight = FontWeight.Bold
496-
)
497-
}
498-
},
499-
dismissButton = {
500-
TextButton(onClick = { shortcutToDelete = null }) {
501-
Text(stringResource(android.R.string.cancel))
478+
onConfirmed = {
479+
val updated = shortcutsMap.toMutableMap()
480+
updated.remove(shortcutKey)
481+
shortcutsMap = updated
482+
TextExpanderUtils.saveShortcuts(context, updated)
483+
if (entry.imageUri != null) {
484+
TextExpanderImageUtils.deleteImageIfUnreferenced(context, entry.imageUri, updated)
502485
}
503486
},
487+
confirmButtonText = stringResource(R.string.delete),
488+
confirmButtonColor = MaterialTheme.colorScheme.error,
489+
cancelButtonText = stringResource(android.R.string.cancel),
504490
title = { Text(stringResource(R.string.text_expander_delete_confirm_title)) },
505-
text = { Text(stringResource(R.string.text_expander_delete_confirm_message, cleanName)) }
491+
content = { Text(stringResource(R.string.text_expander_delete_confirm_message, cleanName)) },
506492
)
507493
}
508494

@@ -831,6 +817,10 @@ internal fun <V> inStoredOrder(stored: Map<String, V>, edited: Map<String, V>):
831817
}
832818

833819
/** Placeholder tags, a tap inserts the tag at the cursor */
820+
// a light tint of the text color: container colors can be dark in a light theme, for example monochrome system colors
821+
@Composable
822+
internal fun textExpanderChipColor() = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.07f)
823+
834824
@Composable
835825
internal fun PlaceholderTagsRow(
836826
value: TextFieldValue,
@@ -858,7 +848,7 @@ internal fun PlaceholderTagsRow(
858848
Box(
859849
modifier = Modifier
860850
.clip(RoundedCornerShape(8.dp))
861-
.background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f))
851+
.background(textExpanderChipColor())
862852
.clickable {
863853
val text = editingTemplate.text
864854
val selection = editingTemplate.selection

0 commit comments

Comments
 (0)