Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toolbar option to toggle Split Keyboard Setting - Enhancement #1218 #1263

Merged
merged 13 commits into from
Feb 9, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,20 @@ public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues setti
final int keyboardWidth = ResourceUtils.getKeyboardWidth(res, settingsValues);
final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues);
final boolean oneHandedModeEnabled = settingsValues.mOneHandedModeEnabled;
final int orientation = mThemeContext.getResources().getConfiguration().orientation;
boolean splitLayoutEnabled;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
splitLayoutEnabled = settingsValues.mIsSplitKeyboardLandscapeEnabled;
} else {
splitLayoutEnabled = settingsValues.mIsSplitKeyboardPortraitEnabled;
}
mKeyboardLayoutSet = builder.setKeyboardGeometry(keyboardWidth, keyboardHeight)
.setSubtype(mRichImm.getCurrentSubtype())
.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey)
.setNumberRowEnabled(settingsValues.mShowsNumberRow)
.setLanguageSwitchKeyEnabled(settingsValues.isLanguageSwitchKeyEnabled())
.setEmojiKeyEnabled(settingsValues.mShowsEmojiKey)
.setSplitLayoutEnabled(settingsValues.mIsSplitKeyboardEnabled)
.setSplitLayoutEnabled(splitLayoutEnabled)
.setOneHandedModeEnabled(oneHandedModeEnabled)
.build();
try {
Expand All @@ -159,7 +166,7 @@ public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues setti
.setNumberRowEnabled(settingsValues.mShowsNumberRow)
.setLanguageSwitchKeyEnabled(settingsValues.isLanguageSwitchKeyEnabled())
.setEmojiKeyEnabled(settingsValues.mShowsEmojiKey)
.setSplitLayoutEnabled(settingsValues.mIsSplitKeyboardEnabled)
.setSplitLayoutEnabled(splitLayoutEnabled)
.setOneHandedModeEnabled(oneHandedModeEnabled)
.build();
mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState, oneHandedModeEnabled);
Expand Down Expand Up @@ -502,6 +509,23 @@ public void switchOneHandedMode() {
Settings.getInstance().writeOneHandedModeGravity(mKeyboardViewWrapper.getOneHandedGravity());
}

public void toggleSplitKeyboardMode() {
final Settings settings = Settings.getInstance();
// Toggle the SplitKeyboardEnabled Setting and reload the Keyboard
switch (mCurrentOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
settings.writeSplitKeyboardLandscapeMode(!settings.getCurrent().mIsSplitKeyboardLandscapeEnabled);
break;
case Configuration.ORIENTATION_PORTRAIT:
settings.writeSplitKeyboardPortraitMode(!settings.getCurrent().mIsSplitKeyboardPortraitEnabled);
break;
default:
break;
}
loadKeyboard(mLatinIME.getCurrentInputEditorInfo(), settings.getCurrent(),
mLatinIME.getCurrentAutoCapsState(), mLatinIME.getCurrentRecapitalizeState());
}

/**
* Displays a toast message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static helium314.keyboard.keyboard.internal.keyboard_parser.EmojiParserKt.EMOJI_HINT_LABEL;

import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.text.TextUtils;
import helium314.keyboard.latin.utils.Log;

Expand Down Expand Up @@ -57,7 +58,9 @@ public DynamicGridKeyboard(final SharedPreferences prefs, final Keyboard templat
final int paddingWidth = mOccupiedWidth - mBaseWidth;
mBaseWidth = width - paddingWidth;
mOccupiedWidth = width;
final float spacerWidth = Settings.getInstance().getCurrent().mSplitKeyboardSpacerRelativeWidth * mBaseWidth;
final float spacerWidth = ( Settings.getInstance().getCurrent().mDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE ) ?
Settings.getInstance().getCurrent().mSplitKeyboardLandscapeSpacerRelativeWidth * mBaseWidth :
Settings.getInstance().getCurrent().mSplitKeyboardPortraitSpacerRelativeWidth * mBaseWidth;
final Key key0 = getTemplateKey(TEMPLATE_KEY_CODE_0);
final Key key1 = getTemplateKey(TEMPLATE_KEY_CODE_1);
final int horizontalGap = Math.abs(key1.getX() - key0.getX()) - key0.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package helium314.keyboard.keyboard.internal

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.util.Xml
import androidx.annotation.XmlRes
Expand Down Expand Up @@ -126,7 +127,12 @@ open class KeyboardBuilder<KP : KeyboardParams>(protected val mContext: Context,
}

private fun addSplit() {
val spacerRelativeWidth = Settings.getInstance().current.mSplitKeyboardSpacerRelativeWidth
val spacerRelativeWidth: Float
if ( Settings.getInstance().current.mDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE ) {
spacerRelativeWidth = Settings.getInstance().current.mSplitKeyboardLandscapeSpacerRelativeWidth;
} else {
spacerRelativeWidth = Settings.getInstance().current.mSplitKeyboardPortraitSpacerRelativeWidth;
}
// adjust gaps for the whole keyboard, so it's the same for all rows
mParams.mRelativeHorizontalGap *= 1f / (1f + spacerRelativeWidth)
mParams.mHorizontalGap = (mParams.mRelativeHorizontalGap * mParams.mId.mWidth).toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.FULL_RIGHT -> R.drawable.ic_to_end
ToolbarKey.PAGE_START -> R.drawable.ic_page_start
ToolbarKey.PAGE_END -> R.drawable.ic_page_end
ToolbarKey.SPLIT -> R.drawable.ic_ime_switcher
})
}
} }
Expand Down Expand Up @@ -211,6 +212,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.FULL_RIGHT -> R.drawable.ic_to_end
ToolbarKey.PAGE_START -> R.drawable.ic_page_start
ToolbarKey.PAGE_END -> R.drawable.ic_page_end
ToolbarKey.SPLIT -> R.drawable.ic_ime_switcher
})
}
} }
Expand Down Expand Up @@ -271,6 +273,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.FULL_RIGHT -> R.drawable.ic_to_end_rounded
ToolbarKey.PAGE_START -> R.drawable.ic_page_start_rounded
ToolbarKey.PAGE_END -> R.drawable.ic_page_end_rounded
ToolbarKey.SPLIT -> R.drawable.ic_ime_switcher
})
}
} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ object KeyCode {
FN, CLIPBOARD_CLEAR_HISTORY, NUMPAD,

// heliboard only
SYMBOL_ALPHA, START_ONE_HANDED_MODE, STOP_ONE_HANDED_MODE, SWITCH_ONE_HANDED_MODE, SHIFT_ENTER,
SYMBOL_ALPHA, START_ONE_HANDED_MODE, STOP_ONE_HANDED_MODE, SWITCH_ONE_HANDED_MODE, SPLIT_LAYOUT, SHIFT_ENTER,
ACTION_NEXT, ACTION_PREVIOUS, NOT_SPECIFIED, CLIPBOARD_COPY_ALL, WORD_LEFT, WORD_RIGHT, PAGE_UP,
PAGE_DOWN, META, TAB, ESCAPE, INSERT, SLEEP, MEDIA_PLAY, MEDIA_PAUSE, MEDIA_PLAY_PAUSE, MEDIA_NEXT,
MEDIA_PREVIOUS, VOL_UP, VOL_DOWN, MUTE, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, BACK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public static String printableCode(final int code) {
case KeyCode.START_ONE_HANDED_MODE: return "startOneHandedMode";
case KeyCode.STOP_ONE_HANDED_MODE: return "stopOneHandedMode";
case KeyCode.SWITCH_ONE_HANDED_MODE: return "switchOneHandedMode";
case KeyCode.SPLIT_LAYOUT: return "splitLayout";
case KeyCode.NUMPAD: return "numpad";
default:
if (code < CODE_SPACE) return String.format("\\u%02X", code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ private void handleFunctionalEvent(final Event event, final InputTransaction inp
case KeyCode.REDO:
sendDownUpKeyEventWithMetaState(KeyEvent.KEYCODE_Z, KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON);
break;
case KeyCode.SPLIT_LAYOUT:
KeyboardSwitcher.getInstance().toggleSplitKeyboardMode();
break;
case KeyCode.VOICE_INPUT:
// switching to shortcut IME, shift state, keyboard,... is handled by LatinIME,
// {@link KeyboardSwitcher#onEvent(Event)}, or {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class AppearanceSettingsFragment : SubScreenFragment() {
private val dayNightPref: TwoStatePreference? by lazy { preferenceScreen.findPreference(Settings.PREF_THEME_DAY_NIGHT) }
private val userColorsPref: Preference by lazy { preferenceScreen.findPreference("theme_select_colors")!! }
private val userColorsPrefNight: Preference? by lazy { preferenceScreen.findPreference("theme_select_colors_night") }
private val splitPref: TwoStatePreference? by lazy { preferenceScreen.findPreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD) }
private val splitLandscapePref: TwoStatePreference? by lazy { preferenceScreen.findPreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE) }
private val splitPortraitPref: TwoStatePreference? by lazy { preferenceScreen.findPreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD_PORTRAIT) }
private val splitScalePref: Preference? by lazy { preferenceScreen.findPreference(Settings.PREF_SPLIT_SPACER_SCALE) }

private val dayImageFilePicker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
Expand All @@ -85,8 +86,8 @@ class AppearanceSettingsFragment : SubScreenFragment() {
setupScalePrefs(Settings.PREF_BOTTOM_PADDING_SCALE, SettingsValues.DEFAULT_SIZE_SCALE)
if (splitScalePref != null) {
setupScalePrefs(Settings.PREF_SPLIT_SPACER_SCALE, SettingsValues.DEFAULT_SIZE_SCALE)
splitScalePref?.isVisible = splitPref?.isChecked == true
splitPref?.setOnPreferenceChangeListener { _, value ->
splitScalePref?.isVisible = splitPortraitPref?.isChecked == true
splitPortraitPref?.setOnPreferenceChangeListener { _, value ->
splitScalePref?.isVisible = value as Boolean
true
}
Expand Down Expand Up @@ -122,13 +123,6 @@ class AppearanceSettingsFragment : SubScreenFragment() {
removePreference("theme_select_colors_night")
}
}
val metrics = requireContext().resources.displayMetrics
val widthDp = TypedValueCompat.pxToDp(metrics.widthPixels.toFloat(), metrics)
val heightDp = TypedValueCompat.pxToDp(metrics.heightPixels.toFloat(), metrics)
if ((min(widthDp, heightDp) < 600 && max(widthDp, heightDp) < 720)) {
removePreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD)
removePreference(Settings.PREF_SPLIT_SPACER_SCALE)
}
}

private fun setColorPrefs(style: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_SHOW_EMOJI_KEY = "show_emoji_key";
public static final String PREF_VARIABLE_TOOLBAR_DIRECTION = "var_toolbar_direction";
public static final String PREF_ADDITIONAL_SUBTYPES = "additional_subtypes";
public static final String PREF_ENABLE_SPLIT_KEYBOARD = "split_keyboard";
public static final String PREF_ENABLE_SPLIT_KEYBOARD_PORTRAIT = "split_keyboard_portrait";
public static final String PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE = "split_keyboard_landscape";
public static final String PREF_SPLIT_SPACER_SCALE = "split_spacer_scale";
public static final String PREF_KEYBOARD_HEIGHT_SCALE = "keyboard_height_scale";
public static final String PREF_BOTTOM_PADDING_SCALE = "bottom_padding_scale";
Expand Down Expand Up @@ -493,6 +494,15 @@ public void writeOneHandedModeGravity(final int gravity) {
(getCurrent().mDisplayOrientation == Configuration.ORIENTATION_PORTRAIT), gravity).apply();
}

public void writeSplitKeyboardPortraitMode(final boolean enabled) {

mPrefs.edit().putBoolean(PREF_ENABLE_SPLIT_KEYBOARD_PORTRAIT, enabled).apply();
}

public void writeSplitKeyboardLandscapeMode(final boolean enabled) {
mPrefs.edit().putBoolean(PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, enabled).apply();
}

public static boolean readHasHardwareKeyboard(final Configuration conf) {
// The standard way of finding out whether we have a hardware keyboard. This code is taken
// from InputMethodService#onEvaluateInputShown, which canonically determines this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ public class SettingsValues {
public final boolean mSlidingKeyInputPreviewEnabled;
public final int mKeyLongpressTimeout;
public final boolean mEnableEmojiAltPhysicalKey;
public final boolean mIsSplitKeyboardEnabled;
public final float mSplitKeyboardSpacerRelativeWidth;
public final boolean mIsSplitKeyboardPortraitEnabled;
public final boolean mIsSplitKeyboardLandscapeEnabled;
public final float mSplitKeyboardLandscapeSpacerRelativeWidth;
public final float mSplitKeyboardPortraitSpacerRelativeWidth;
public final boolean mQuickPinToolbarKeys;
public final int mScreenMetrics;
public final boolean mAddToPersonalDictionary;
Expand Down Expand Up @@ -191,10 +193,14 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout);
mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration());
final float displayWidthDp = TypedValueCompat.pxToDp(res.getDisplayMetrics().widthPixels, res.getDisplayMetrics());
mIsSplitKeyboardEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, false) && displayWidthDp > 600; // require display width of 600 dp for split
mIsSplitKeyboardPortraitEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD_PORTRAIT, false) && displayWidthDp > 0;
mIsSplitKeyboardLandscapeEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD_LANDSCAPE, false) && displayWidthDp > 0;
// determine spacerWidth from display width and scale setting
mSplitKeyboardSpacerRelativeWidth = mIsSplitKeyboardEnabled
? Math.min(Math.max((displayWidthDp - 600) / 600f + 0.15f, 0.15f), 0.35f) * prefs.getFloat(Settings.PREF_SPLIT_SPACER_SCALE, DEFAULT_SIZE_SCALE)
mSplitKeyboardPortraitSpacerRelativeWidth = mIsSplitKeyboardPortraitEnabled
? Math.min(Math.max((displayWidthDp) / 0.15f, 0.15f), 0.35f) * prefs.getFloat(Settings.PREF_SPLIT_SPACER_SCALE, DEFAULT_SIZE_SCALE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.min(Math.max((displayWidthDp) / 0.15f, 0.15f), 0.35f) is always 0.35 if your display is more than 1 dp wide.
I think the old method should still be used, so users don't have to re-adjust the split width setting.

: 0f;
mSplitKeyboardLandscapeSpacerRelativeWidth = mIsSplitKeyboardLandscapeEnabled
? Math.min(Math.max((displayWidthDp) / 0.15f, 0.15f), 0.35f) * prefs.getFloat(Settings.PREF_SPLIT_SPACER_SCALE, DEFAULT_SIZE_SCALE)
: 0f;
mQuickPinToolbarKeys = prefs.getBoolean(Settings.PREF_QUICK_PIN_TOOLBAR_KEYS, false);
mScreenMetrics = Settings.readScreenMetrics(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.content.SharedPreferences
import android.content.res.Configuration
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.ImageButton
Expand Down Expand Up @@ -41,6 +42,11 @@ fun createToolbarKey(context: Context, iconsSet: KeyboardIconsSet, key: ToolbarK
button.isActivated = !when (key) {
INCOGNITO -> Settings.readAlwaysIncognitoMode(DeviceProtectedUtils.getSharedPreferences(context))
ONE_HANDED -> Settings.getInstance().current.mOneHandedModeEnabled
SPLIT -> if (context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Settings.getInstance().current.mIsSplitKeyboardLandscapeEnabled
} else {
Settings.getInstance().current.mIsSplitKeyboardPortraitEnabled
}
AUTOCORRECT -> Settings.getInstance().current.mAutoCorrectionEnabledPerUserSettings
else -> true
}
Expand Down Expand Up @@ -78,6 +84,7 @@ fun getCodeForToolbarKey(key: ToolbarKey) = Settings.getInstance().getCustomTool
FULL_RIGHT -> KeyCode.MOVE_END_OF_LINE
PAGE_START -> KeyCode.MOVE_START_OF_PAGE
PAGE_END -> KeyCode.MOVE_END_OF_PAGE
SPLIT -> KeyCode.SPLIT_LAYOUT
}

fun getCodeForToolbarKeyLongClick(key: ToolbarKey) = Settings.getInstance().getCustomToolbarLongpressCode(key) ?: when (key) {
Expand All @@ -101,7 +108,7 @@ fun getCodeForToolbarKeyLongClick(key: ToolbarKey) = Settings.getInstance().getC

// names need to be aligned with resources strings (using lowercase of key.name)
enum class ToolbarKey {
VOICE, CLIPBOARD, NUMPAD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, CUT, PASTE, ONE_HANDED,
VOICE, CLIPBOARD, NUMPAD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, CUT, PASTE, ONE_HANDED, SPLIT,
INCOGNITO, AUTOCORRECT, CLEAR_CLIPBOARD, CLOSE_HISTORY, EMOJI, LEFT, RIGHT, UP, DOWN, WORD_LEFT, WORD_RIGHT,
PAGE_UP, PAGE_DOWN, FULL_LEFT, FULL_RIGHT, PAGE_START, PAGE_END
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
<string name="settings_category_experimental">Experimental</string>
<!-- Settings category title for Appearance & Layout/Misc [CHAR LIMIT=33] -->
<string name="settings_category_miscellaneous">Miscellaneous</string>
<!-- Option for enabling or disabling the split keyboard layout. [CHAR LIMIT=65]-->
<string name="enable_split_keyboard">Enable split keyboard</string>
<!-- Option for enabling or disabling the split keyboard layout in Landscape. [CHAR LIMIT=65]-->
<string name="enable_split_keyboard_landscape">Enable split keyboard (landscape mode)</string>
<!-- Option for enabling or disabling the split keyboard layout in Portrait. [CHAR LIMIT=65]-->
<string name="enable_split_keyboard_portrait">Enable split keyboard (portrait mode)</string>
<!-- Option for setting distance for split keyboard -->
<string name="split_spacer_scale">Split distance</string>
<!-- Option name for including other IMEs in the language key switch list [CHAR LIMIT=30] -->
Expand Down Expand Up @@ -261,6 +263,7 @@
<string name="select_all" tools:keep="@string/select_all" translatable="false">@android:string/selectAll</string>
<string name="select_word" tools:keep="@string/select_word">Select word</string>
<string name="one_handed" tools:keep="@string/one_handed">One-handed mode</string>
<string name="split" tools:keep="@string/split">Split keyboard toggle</string>
<string name="full_left" tools:keep="@string/full_left">Full left</string>
<string name="full_right" tools:keep="@string/full_right">Full right</string>
<string name="page_start" tools:keep="@string/page_start">Page start</string>
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/xml/prefs_screen_appearance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@
android:title="@string/settings_category_miscellaneous">

<SwitchPreference
android:key="split_keyboard"
android:title="@string/enable_split_keyboard"
android:key="split_keyboard_landscape"
android:title="@string/enable_split_keyboard_landscape"
android:persistent="true"
android:defaultValue="false" />

<SwitchPreference
android:key="split_keyboard_portrait"
android:title="@string/enable_split_keyboard_portrait"
android:persistent="true"
android:defaultValue="false" />

Expand Down