Skip to content

Commit 9018565

Browse files
committed
fix(split): make the split settings work on tablets
Large screens ignore the regular split keys, so on tablets the "Enable split keyboard" switches did nothing and the keyboard stayed split. - On large screens without a hinge, the regular switches and split distance now use the large screen values. Foldables keep their own. - The toolbar key and all split switches save through one function. - The toolbar key no longer writes the base key on large screens, which could change the folded screen of a foldable. - The landscape switch is no longer overridden by a value the toolbar key saved earlier, and "Split keyboard when unfolded" applies to both orientations again. - Settings map: SwitchPreference is generic, don't read a row name from it (it listed a bogus "here" row). How split settings are read is unchanged. Claude-Session: https://claude.ai/code/session_017YwBxUnnx3DqiiATw5XxUk
1 parent 1b41148 commit 9018565

6 files changed

Lines changed: 171 additions & 53 deletions

File tree

‎app/src/main/java/helium314/keyboard/latin/settings/Settings.kt‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,7 @@ class Settings private constructor() : SharedPreferences.OnSharedPreferenceChang
188188
fun writeSplitKeyboardEnabled(enabled: Boolean, isLandscape: Boolean) {
189189
val settingsValues = mSettingsValues ?: return
190190
val prefs = mPrefs ?: return
191-
val basePref = if (isLandscape) PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE else PREF_ENABLE_SPLIT_KEYBOARD
192-
val profilePref = getProfileAwarePrefKey(basePref, settingsValues.mScreenProfile)
193-
val editor = prefs.edit()
194-
.putBoolean(profilePref, enabled)
195-
.putBoolean(basePref, enabled)
196-
if (settingsValues.mScreenProfile.isLarge() && !isLandscape) {
197-
editor.putBoolean(PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED, enabled)
198-
}
199-
editor.apply()
191+
saveSplitKeyboardEnabled(prefs, enabled, isLandscape, settingsValues.mScreenProfile)
200192
}
201193

202194
fun readShowToolbarOnly(): Boolean {
@@ -790,6 +782,35 @@ class Settings private constructor() : SharedPreferences.OnSharedPreferenceChang
790782
return getProfileAwareBoolean(prefs, basePref, profile, defaultValue)
791783
}
792784

785+
/** Used by the toolbar key and the settings switches, so they can't override each other. */
786+
fun saveSplitKeyboardEnabled(prefs: SharedPreferences, enabled: Boolean, isLandscape: Boolean,
787+
profile: helium314.keyboard.latin.utils.ScreenProfile) {
788+
val basePref = if (isLandscape) PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE else PREF_ENABLE_SPLIT_KEYBOARD
789+
val editor = prefs.edit().putBoolean(getProfileAwarePrefKey(basePref, profile), enabled)
790+
if (!profile.isLarge()) {
791+
editor.putBoolean(basePref, enabled)
792+
} else if (!isLandscape) {
793+
// no base key here: the folded screen of a foldable falls back to it
794+
editor.putBoolean(PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED, enabled)
795+
}
796+
editor.apply()
797+
}
798+
799+
/** Saves the portrait split switch for large screens, without changing the landscape state. */
800+
fun pinLargeLandscapeSplit(prefs: SharedPreferences) {
801+
val large = helium314.keyboard.latin.utils.ScreenProfile.LARGE
802+
val landscapeKey = getProfileAwarePrefKey(PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, large)
803+
if (!prefs.contains(landscapeKey))
804+
prefs.edit().putBoolean(landscapeKey, readSplitKeyboardEnabled(prefs, true, large)).apply()
805+
}
806+
807+
/** What tablets use while they have no own split distance: older versions saved it in the regular keys. */
808+
fun readRegularSplitSpacerScales(prefs: SharedPreferences): Array<Float> =
809+
Array(Defaults.PREF_SPLIT_SPACER_SCALE.size) {
810+
prefs.getFloat(createPrefKeyForBooleanSettings(PREF_SPLIT_SPACER_SCALE_PREFIX, it, 1),
811+
Defaults.PREF_SPLIT_SPACER_SCALE[it])
812+
}
813+
793814
fun readSplitSpacerScale(prefs: SharedPreferences, landscape: Boolean): Float {
794815
return readSplitSpacerScale(prefs, landscape, helium314.keyboard.latin.utils.ScreenProfile.COMPACT)
795816
}

‎app/src/main/java/helium314/keyboard/latin/utils/ScreenProfile.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ object ScreenProfileProvider {
3737
return hasHingeSensor
3838
}
3939

40+
/** A large screen without a hinge: it has no folded state, so the regular settings must apply to it. */
41+
fun isTablet(context: Context): Boolean =
42+
getScreenProfile(context).isLarge() && !isFoldableDevice(context)
43+
4044
@JvmOverloads
4145
fun getScreenProfile(
4246
context: Context,

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

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import androidx.core.content.edit
4949
import helium314.keyboard.latin.settings.Settings
5050
import helium314.keyboard.latin.settings.getProfileAwarePrefKey
5151
import helium314.keyboard.latin.utils.ScreenProfile
52+
import helium314.keyboard.latin.utils.ScreenProfileProvider
5253
import helium314.keyboard.latin.utils.WallpaperFetcher
5354
import kotlinx.coroutines.flow.MutableStateFlow
5455
import java.util.Locale
@@ -68,8 +69,10 @@ fun AppearanceScreen(
6869
val popupOnEnabled = prefs.getBoolean(Settings.PREF_POPUP_ON, Defaults.PREF_POPUP_ON)
6970
val showHintsEnabled = prefs.getBoolean(Settings.PREF_SHOW_HINTS, Defaults.PREF_SHOW_HINTS)
7071
val narrowGapsEnabled = prefs.getBoolean(Settings.PREF_NARROW_KEY_GAPS, Defaults.PREF_NARROW_KEY_GAPS)
71-
val splitEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, Defaults.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE)
72-
|| prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, Defaults.PREF_ENABLE_SPLIT_KEYBOARD)
72+
val isTablet = ScreenProfileProvider.isTablet(ctx)
73+
val splitProfile = if (isTablet) ScreenProfile.LARGE else ScreenProfile.COMPACT
74+
val splitEnabled = Settings.readSplitKeyboardEnabled(prefs, false, splitProfile)
75+
|| Settings.readSplitKeyboardEnabled(prefs, true, splitProfile)
7376

7477
val fineTuneThemeExpanded = fineTuneExpanded(prefs, Settings.PREF_EXPAND_FINE_TUNE_THEME, Defaults.PREF_EXPAND_FINE_TUNE_THEME)
7578
val fineTuneKeyPreviewsExpanded = fineTuneExpanded(prefs, Settings.PREF_EXPAND_FINE_TUNE_KEY_PREVIEWS, Defaults.PREF_EXPAND_FINE_TUNE_KEY_PREVIEWS)
@@ -114,8 +117,8 @@ fun AppearanceScreen(
114117
Settings.PREF_SPACE_BAR_TEXT,
115118
Settings.PREF_EXPAND_FINE_TUNE_FOLDABLE,
116119
if (fineTuneFoldableExpanded) Settings.PREF_UNFOLDED_KEYBOARD_HEIGHT_SCALE_PREFIX else null,
117-
if (fineTuneFoldableExpanded) Settings.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED else null,
118-
if (fineTuneFoldableExpanded) Settings.PREF_UNFOLDED_SPLIT_SPACER_SCALE_PREFIX else null,
120+
if (fineTuneFoldableExpanded && !isTablet) Settings.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED else null,
121+
if (fineTuneFoldableExpanded && !isTablet) Settings.PREF_UNFOLDED_SPLIT_SPACER_SCALE_PREFIX else null,
119122
if (fineTuneFoldableExpanded) Settings.PREF_UNFOLDED_BOTTOM_PADDING_SCALE_PREFIX else null,
120123
if (fineTuneFoldableExpanded) Settings.PREF_UNFOLDED_SIDE_PADDING_SCALE_PREFIX else null,
121124
Settings.PREF_EXPAND_FINE_TUNE_KEYBOARD_LAYOUT,
@@ -162,6 +165,47 @@ fun AppearanceScreen(
162165
)
163166
}
164167

168+
@Composable
169+
private fun SplitSwitch(
170+
setting: Setting,
171+
landscape: Boolean,
172+
profile: ScreenProfile,
173+
beforeChange: () -> Unit = {},
174+
afterSave: () -> Unit = {},
175+
) {
176+
val prefs = LocalContext.current.prefs()
177+
val baseKey = if (landscape) Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE else Settings.PREF_ENABLE_SPLIT_KEYBOARD
178+
SwitchPreference(
179+
name = setting.title,
180+
description = setting.description,
181+
// show and save the value the keyboard actually reads, which the toolbar key may have set
182+
key = getProfileAwarePrefKey(baseKey, profile),
183+
default = Settings.readSplitKeyboardEnabled(prefs, landscape, profile),
184+
allowCheckedChange = {
185+
beforeChange()
186+
true
187+
},
188+
) {
189+
Settings.saveSplitKeyboardEnabled(prefs, it, landscape, profile)
190+
afterSave()
191+
KeyboardSwitcher.getInstance().reloadKeyboard()
192+
}
193+
}
194+
195+
/** The regular split switches control the large screen values only on tablets, which can't be folded. */
196+
@Composable
197+
private fun RegularSplitSwitch(setting: Setting, landscape: Boolean) {
198+
val ctx = LocalContext.current
199+
val isTablet = ScreenProfileProvider.isTablet(ctx)
200+
SplitSwitch(
201+
setting = setting,
202+
landscape = landscape,
203+
profile = if (isTablet) ScreenProfile.LARGE else ScreenProfile.COMPACT,
204+
// runs before the new value is saved, while the landscape state can still be read
205+
beforeChange = { if (isTablet && !landscape) Settings.pinLargeLandscapeSplit(ctx.prefs()) },
206+
)
207+
}
208+
165209
fun createAppearanceSettings(context: Context) = listOf(
166210
Setting(context, Settings.PREF_THEME_STYLE, R.string.theme_style) { setting ->
167211
val ctx = LocalContext.current
@@ -371,13 +415,7 @@ fun createAppearanceSettings(context: Context) = listOf(
371415
}
372416
},
373417
Setting(context, Settings.PREF_ENABLE_SPLIT_KEYBOARD, R.string.enable_split_keyboard) {
374-
SwitchPreference(it, Defaults.PREF_ENABLE_SPLIT_KEYBOARD) {
375-
val prefs = context.prefs()
376-
val enabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, Defaults.PREF_ENABLE_SPLIT_KEYBOARD)
377-
val profilePref = getProfileAwarePrefKey(Settings.PREF_ENABLE_SPLIT_KEYBOARD, ScreenProfile.COMPACT)
378-
prefs.edit().putBoolean(profilePref, enabled).apply()
379-
KeyboardSwitcher.getInstance().reloadKeyboard()
380-
}
418+
RegularSplitSwitch(it, landscape = false)
381419
},
382420
Setting(context, Settings.PREF_EXPAND_FINE_TUNE_FOLDABLE,
383421
R.string.fine_tune_foldable_title, R.string.fine_tune_foldable_summary
@@ -395,15 +433,16 @@ fun createAppearanceSettings(context: Context) = listOf(
395433
) { KeyboardSwitcher.getInstance().setThemeNeedsReload() }
396434
},
397435
Setting(context, Settings.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED, R.string.unfolded_split_keyboard) {
398-
SwitchPreference(it, Defaults.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED) {
399-
val prefs = context.prefs()
400-
val enabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED, Defaults.PREF_ENABLE_SPLIT_KEYBOARD_UNFOLDED)
401-
val profilePref = getProfileAwarePrefKey(Settings.PREF_ENABLE_SPLIT_KEYBOARD, ScreenProfile.LARGE)
402-
prefs.edit().putBoolean(profilePref, enabled).apply()
403-
KeyboardSwitcher.getInstance().reloadKeyboard()
404-
}
436+
// returning early also hides it from settings search
437+
if (ScreenProfileProvider.isTablet(LocalContext.current)) return@Setting
438+
val prefs = LocalContext.current.prefs()
439+
SplitSwitch(it, landscape = false, profile = ScreenProfile.LARGE, afterSave = {
440+
// a landscape value saved by the toolbar key would otherwise keep overriding this switch
441+
prefs.edit { remove(getProfileAwarePrefKey(Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, ScreenProfile.LARGE)) }
442+
})
405443
},
406444
Setting(context, Settings.PREF_UNFOLDED_SPLIT_SPACER_SCALE_PREFIX, R.string.unfolded_split_spacer_scale) { setting ->
445+
if (ScreenProfileProvider.isTablet(LocalContext.current)) return@Setting
407446
MultiSliderPreference(
408447
name = setting.title,
409448
baseKey = setting.key,
@@ -516,17 +555,20 @@ fun createAppearanceSettings(context: Context) = listOf(
516555
) { KeyboardSwitcher.getInstance().reloadKeyboard() }
517556
},
518557
Setting(context, Settings.PREF_SPLIT_SPACER_SCALE_PREFIX, R.string.split_spacer_scale) { setting ->
558+
val ctx = LocalContext.current
559+
val isTablet = ScreenProfileProvider.isTablet(ctx)
519560
MultiSliderPreference(
520561
name = setting.title,
521-
baseKey = setting.key,
562+
// large screens prefer these keys over the regular ones
563+
baseKey = if (isTablet) Settings.PREF_UNFOLDED_SPLIT_SPACER_SCALE_PREFIX else setting.key,
522564
dimensions = listOf(stringResource(R.string.landscape)),
523-
defaults = Defaults.PREF_SPLIT_SPACER_SCALE,
565+
defaults = if (isTablet) Settings.readRegularSplitSpacerScales(ctx.prefs()) else Defaults.PREF_SPLIT_SPACER_SCALE,
524566
range = 0.5f..2f,
525567
description = { "${(100 * it).roundToInt()}%" }
526568
) { KeyboardSwitcher.getInstance().setThemeNeedsReload() }
527569
},
528570
Setting(context, Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, R.string.enable_split_keyboard_landscape) {
529-
SwitchPreference(it, Defaults.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE) { KeyboardSwitcher.getInstance().reloadKeyboard() }
571+
RegularSplitSwitch(it, landscape = true)
530572
},
531573
Setting(context, Settings.PREF_NARROW_KEY_GAPS, R.string.prefs_narrow_key_gaps) {
532574
SwitchPreference(it, Defaults.PREF_NARROW_KEY_GAPS) { KeyboardSwitcher.getInstance().setThemeNeedsReload() }

‎app/src/test/java/helium314/keyboard/latin/utils/ScreenProfileTest.kt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,57 @@ class ScreenProfileTest {
180180
assertEquals(false, unfoldedSplitDisabled)
181181
}
182182

183+
@Test
184+
fun testPinLargeLandscapeSplitKeepsLandscapeState() {
185+
val prefs = context.prefs()
186+
Settings.pinLargeLandscapeSplit(prefs)
187+
// what the tablet portrait switch saves afterwards
188+
prefs.edit().putBoolean("split_keyboard_large", false).apply()
189+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, false, ScreenProfile.LARGE))
190+
assertEquals(true, Settings.readSplitKeyboardEnabled(prefs, true, ScreenProfile.LARGE))
191+
}
192+
193+
@Test
194+
fun testPinLargeLandscapeSplitKeepsSavedValueAndCompact() {
195+
val prefs = context.prefs()
196+
prefs.edit().putBoolean("split_keyboard_landscape_large", false).apply()
197+
Settings.pinLargeLandscapeSplit(prefs)
198+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, true, ScreenProfile.LARGE))
199+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, true, ScreenProfile.COMPACT))
200+
}
201+
202+
@Test
203+
fun testSavingLargeSplitDoesNotChangeFoldedScreen() {
204+
val prefs = context.prefs()
205+
for (landscape in listOf(false, true)) {
206+
Settings.saveSplitKeyboardEnabled(prefs, true, landscape, ScreenProfile.LARGE)
207+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, landscape, ScreenProfile.COMPACT))
208+
Settings.saveSplitKeyboardEnabled(prefs, false, landscape, ScreenProfile.LARGE)
209+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, landscape, ScreenProfile.LARGE))
210+
}
211+
}
212+
213+
@Test
214+
fun testSavingCompactSplitOverridesEarlierValueAndKeepsLarge() {
215+
val prefs = context.prefs()
216+
for (landscape in listOf(false, true)) {
217+
Settings.saveSplitKeyboardEnabled(prefs, true, landscape, ScreenProfile.COMPACT)
218+
Settings.saveSplitKeyboardEnabled(prefs, false, landscape, ScreenProfile.COMPACT)
219+
assertEquals(false, Settings.readSplitKeyboardEnabled(prefs, landscape, ScreenProfile.COMPACT))
220+
assertEquals(true, Settings.readSplitKeyboardEnabled(prefs, landscape, ScreenProfile.LARGE))
221+
}
222+
}
223+
224+
@Test
225+
fun testRegularSplitSpacerScalesMatchLargeFallback() {
226+
val prefs = context.prefs()
227+
prefs.edit().putFloat("split_spacer_scale_true", 0.6f).apply()
228+
val shown = Settings.readRegularSplitSpacerScales(prefs)
229+
for (landscape in listOf(false, true))
230+
assertEquals(Settings.readSplitSpacerScale(prefs, landscape, ScreenProfile.LARGE),
231+
shown[if (landscape) 1 else 0], 0.001f)
232+
}
233+
183234
@Test
184235
fun testUnfoldedBottomPaddingScale() {
185236
val prefs = context.prefs()

0 commit comments

Comments
 (0)