Skip to content

Commit

Permalink
Implement InputMethodModeSwitch and so many other things
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jul 2, 2024
1 parent 1efba66 commit e36ee33
Show file tree
Hide file tree
Showing 29 changed files with 340 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import org.jyutping.jyutping.extensions.keyboardLightBackground
import org.jyutping.jyutping.extensions.space
import org.jyutping.jyutping.keyboard.Candidate
import org.jyutping.jyutping.keyboard.InputMethodMode
import org.jyutping.jyutping.keyboard.KeyboardCase
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.utilities.DatabaseHelper
Expand Down Expand Up @@ -54,6 +55,15 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),

override val savedStateRegistry: SavedStateRegistry get() = savedStateRegistryController.savedStateRegistry

val inputMethodMode: MutableState<InputMethodMode> = mutableStateOf(InputMethodMode.Cantonese)
fun toggleInputMethodMode() {
val newMode: InputMethodMode = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> InputMethodMode.ABC
InputMethodMode.ABC -> InputMethodMode.Cantonese
}
inputMethodMode.value = newMode
}

val keyboardForm: MutableState<KeyboardForm> = mutableStateOf(KeyboardForm.Alphabetic)
fun transformTo(destination: KeyboardForm) {
if (isBuffering.value) {
Expand All @@ -63,7 +73,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}

val keyboardCase: MutableState<KeyboardCase> = mutableStateOf(KeyboardCase.Lowercased)
private fun updateKeyboardCaseTo(case: KeyboardCase) {
private fun updateKeyboardCase(case: KeyboardCase) {
keyboardCase.value = case
}
fun performShift() {
Expand All @@ -72,7 +82,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
KeyboardCase.Uppercased -> KeyboardCase.Lowercased
KeyboardCase.CapsLocked -> KeyboardCase.Lowercased
}
updateKeyboardCaseTo(newCase)
updateKeyboardCase(newCase)
}

val candidates: MutableState<List<Candidate>> = mutableStateOf(listOf())
Expand All @@ -97,7 +107,14 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
val isBuffering: MutableState<Boolean> = mutableStateOf(false)
fun process(text: String) {
bufferText += text
when (inputMethodMode.value) {
InputMethodMode.Cantonese -> {
bufferText += text
}
InputMethodMode.ABC -> {
currentInputConnection.commitText(text, text.length)
}
}
}
fun input(text: String) {
currentInputConnection.commitText(text, text.length)
Expand All @@ -114,11 +131,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
}
fun performReturn() {
if (bufferText.isEmpty()) {
sendDefaultEditorAction(true)
} else {
if (isBuffering.value) {
currentInputConnection.commitText(bufferText, bufferText.length)
bufferText = ""
} else {
sendDefaultEditorAction(true)
}
}
fun space() {
Expand All @@ -132,14 +149,24 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
requestHideSelf(InputMethodManager.HIDE_NOT_ALWAYS)
}
fun leftKey() {
if (!isBuffering.value) {
val text = ""
if (isBuffering.value) {
// TODO: Separator
} else {
val text: String = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> ""
InputMethodMode.ABC -> ","
}
currentInputConnection.commitText(text, text.length)
}
}
fun rightKey() {
if (!isBuffering.value) {
val text = ""
if (isBuffering.value) {
// TODO: Separator
} else {
val text: String = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> ""
InputMethodMode.ABC -> "."
}
currentInputConnection.commitText(text, text.length)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
Expand All @@ -34,8 +33,12 @@ fun AlphabeticKeyboard(keyHeight: Dp) {
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
ToolBar(modifier = Modifier.alpha(if (isBuffering.value) 0f else 1f))
CandidateScrollBar(modifier = Modifier.alpha(if (isBuffering.value) 1f else 0f))
Color.keyboardLightBackground
if (isBuffering.value) {
CandidateScrollBar()
} else {
ToolBar()
}
}
Row(
modifier = Modifier
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/keyboard/BackspaceKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Backspace
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.R
import org.jyutping.jyutping.extensions.keyLight
import org.jyutping.jyutping.extensions.keyLightEmphatic

Expand All @@ -46,8 +48,9 @@ fun BackspaceKey(modifier: Modifier) {
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.Backspace,
contentDescription = null
imageVector = ImageVector.vectorResource(id = if (isPressed.value) R.drawable.key_backspacing else R.drawable.key_backspace),
contentDescription = null,
modifier = Modifier.size(22.dp)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService

@Composable
fun CandidateScrollBar(modifier: Modifier) {
fun CandidateScrollBar() {
val interactionSource = remember { MutableInteractionSource() }
val context = LocalContext.current as JyutpingInputMethodService
val candidates = remember { context.candidates }
LazyRow(
modifier = modifier
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
horizontalArrangement = Arrangement.spacedBy(0.dp),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jyutping.jyutping.keyboard

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
Expand All @@ -20,11 +22,15 @@ fun CantoneseNumericKeyboard(keyHeight: Dp) {
.background(Color.keyboardLightBackground)
.fillMaxWidth()
) {
ToolBar(
Box(
modifier = Modifier
.height(60.dp)
.fillMaxWidth()
)
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Color.keyboardLightBackground
ToolBar()
}
Row(
modifier = Modifier
.height(keyHeight)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jyutping.jyutping.keyboard

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
Expand All @@ -20,11 +22,15 @@ fun CantoneseSymbolicKeyboard(keyHeight: Dp) {
.background(Color.keyboardLightBackground)
.fillMaxWidth()
) {
ToolBar(
Box(
modifier = Modifier
.height(60.dp)
.fillMaxWidth()
)
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Color.keyboardLightBackground
ToolBar()
}
Row(
modifier = Modifier
.height(keyHeight)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jyutping.jyutping.keyboard

enum class InputMethodMode {
Cantonese,
ABC
}

fun InputMethodMode.isCantonese(): Boolean = (this == InputMethodMode.Cantonese)
fun InputMethodMode.isABC(): Boolean = (this == InputMethodMode.ABC)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jyutping.jyutping.keyboard

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.extensions.keyLight
import org.jyutping.jyutping.extensions.keyLightEmphatic

@Composable
fun InputMethodModeSwitch() {
val context = LocalContext.current as JyutpingInputMethodService
val inputMethodMode = remember { context.inputMethodMode }
val shapeWidth: Dp = 72.dp
val shapeHeight: Dp = 25.dp
val partWidth: Dp = 36.dp
val cornerRadius: Dp = 5.dp
val largerFontSize = 17.sp
val normalFontSize = 14.sp
Box(
modifier = Modifier
.clip(RoundedCornerShape(cornerRadius))
.background(Color.keyLightEmphatic)
.width(shapeWidth)
.height(shapeHeight),
contentAlignment = Alignment.Center
) {
Row(
horizontalArrangement = Arrangement.spacedBy(0.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(cornerRadius))
.background(if (inputMethodMode.value.isCantonese()) Color.keyLight else Color.Transparent)
.width(partWidth)
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Text(
text = "",
fontSize = if (inputMethodMode.value.isCantonese()) largerFontSize else normalFontSize
)
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(cornerRadius))
.background(if (inputMethodMode.value.isABC()) Color.keyLight else Color.Transparent)
.width(partWidth)
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Text(
text = "A",
fontSize = if (inputMethodMode.value.isABC()) largerFontSize else normalFontSize
)
}
}
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/org/jyutping/jyutping/keyboard/LeftKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ fun LeftKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val inputMethodMode = remember { context.inputMethodMode }
val keyText: String = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> ""
InputMethodMode.ABC -> ","
}
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.leftKey() }
Expand All @@ -45,7 +50,7 @@ fun LeftKey(modifier: Modifier) {
contentAlignment = Alignment.Center
) {
Text(
text = "",
text = keyText,
fontSize = 20.sp
)
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ReturnKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.KeyboardReturn
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.R
import org.jyutping.jyutping.extensions.keyLight
import org.jyutping.jyutping.extensions.keyLightEmphatic

Expand All @@ -46,8 +48,9 @@ fun ReturnKey(modifier: Modifier) {
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.AutoMirrored.Rounded.KeyboardReturn,
contentDescription = null
imageVector = ImageVector.vectorResource(id = R.drawable.key_return),
contentDescription = null,
modifier = Modifier.size(22.dp)
)
}
}
Expand Down
Loading

0 comments on commit e36ee33

Please sign in to comment.