Skip to content

Commit

Permalink
Implement ReturnKeyForm
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Aug 3, 2024
1 parent 06d07ae commit a1f41f8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jyutping.jyutping.keyboard.KeyboardCase
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.keyboard.Pinyin
import org.jyutping.jyutping.keyboard.PinyinSegmentor
import org.jyutping.jyutping.keyboard.ReturnKeyForm
import org.jyutping.jyutping.keyboard.Segmentor
import org.jyutping.jyutping.keyboard.SpaceKeyForm
import org.jyutping.jyutping.keyboard.Structure
Expand All @@ -53,6 +54,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
decorView.setViewTreeSavedStateRegistryOwner(this)
}
updateSpaceKeyForm()
updateReturnKeyForm()
return view
}

Expand Down Expand Up @@ -99,6 +101,24 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
spaceKeyForm.value = newForm
}
}

val returnKeyForm: MutableState<ReturnKeyForm> = mutableStateOf(ReturnKeyForm.StandbyTraditional)
private fun updateReturnKeyForm() {
val newForm: ReturnKeyForm = when (inputMethodMode.value) {
InputMethodMode.ABC -> ReturnKeyForm.StandbyABC
InputMethodMode.Cantonese -> {
if (isBuffering.value) {
if (characterStandard.value.isSimplified()) ReturnKeyForm.BufferingSimplified else ReturnKeyForm.BufferingTraditional
} else {
if (characterStandard.value.isSimplified()) ReturnKeyForm.StandbySimplified else ReturnKeyForm.StandbyTraditional
}
}
}
if (returnKeyForm.value != newForm) {
returnKeyForm.value = newForm
}
}

val inputMethodMode: MutableState<InputMethodMode> = mutableStateOf(InputMethodMode.Cantonese)
fun toggleInputMethodMode() {
val newMode: InputMethodMode = when (inputMethodMode.value) {
Expand All @@ -107,6 +127,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
inputMethodMode.value = newMode
updateSpaceKeyForm()
updateReturnKeyForm()
}

val keyboardForm: MutableState<KeyboardForm> = mutableStateOf(KeyboardForm.Alphabetic)
Expand Down Expand Up @@ -154,6 +175,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
characterStandard.value = standard
// TODO: Save to Config
updateSpaceKeyForm()
updateReturnKeyForm()
}
val candidateState: MutableIntState = mutableIntStateOf(1)
val candidates: MutableState<List<Candidate>> = mutableStateOf(listOf())
Expand Down Expand Up @@ -244,6 +266,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
candidateState.intValue += 1
updateSpaceKeyForm()
updateReturnKeyForm()
}
val isBuffering: MutableState<Boolean> = mutableStateOf(false)
fun clearBuffer() {
Expand Down
21 changes: 15 additions & 6 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ReturnKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand All @@ -24,6 +25,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.R
import org.jyutping.jyutping.extensions.keyLight
Expand All @@ -46,7 +48,7 @@ fun ReturnKey(modifier: Modifier) {
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Box (
Box(
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
Expand All @@ -55,11 +57,18 @@ fun ReturnKey(modifier: Modifier) {
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.key_return),
contentDescription = null,
modifier = Modifier.size(22.dp)
)
if (context.isBuffering.value) {
Text(
text = context.returnKeyForm.value.text() ?: "return",
fontSize = 15.sp
)
} else {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.key_return),
contentDescription = null,
modifier = Modifier.size(22.dp)
)
}
}
}
}
24 changes: 24 additions & 0 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ReturnKeyForm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jyutping.jyutping.keyboard

enum class ReturnKeyForm {

BufferingSimplified,
BufferingTraditional,
StandbyABC,
StandbySimplified,
StandbyTraditional,
UnavailableABC,
UnavailableSimplified,
UnavailableTraditional;

fun text(): String? = when (this) {
BufferingSimplified -> "确认"
BufferingTraditional -> "確認"
StandbyABC -> null
StandbySimplified -> null
StandbyTraditional -> null
UnavailableABC -> null
UnavailableSimplified -> null
UnavailableTraditional -> null
}
}
Binary file modified images/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a1f41f8

Please sign in to comment.