Skip to content

Commit

Permalink
Implement CantoneseNumericKeyboard and CantoneseSymbolicKeyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jun 29, 2024
1 parent 98696b0 commit af0b693
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 45 deletions.
16 changes: 14 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/ComposeKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ package org.jyutping.jyutping

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.keyboard.KeyboardScreen
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.keyboard.AlphabeticKeyboard
import org.jyutping.jyutping.keyboard.CantoneseNumericKeyboard
import org.jyutping.jyutping.keyboard.CantoneseSymbolicKeyboard

class ComposeKeyboardView(context: Context) : AbstractComposeView(context) {

@Composable
override fun Content() {
KeyboardScreen(keyHeight = 58.dp)
val keyHeight = 58.dp // TODO: Responsive keyHeight
val ctx = context as JyutpingInputMethodService
val keyboardForm = remember { ctx.keyboardForm }
when (keyboardForm.value) {
KeyboardForm.Alphabetic -> AlphabeticKeyboard(keyHeight = keyHeight)
KeyboardForm.Numeric -> CantoneseNumericKeyboard(keyHeight = keyHeight)
KeyboardForm.Symbolic -> CantoneseSymbolicKeyboard(keyHeight = keyHeight)
else -> AlphabeticKeyboard(keyHeight = keyHeight)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import org.jyutping.jyutping.extensions.space
import org.jyutping.jyutping.keyboard.Candidate
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.utilities.DatabaseHelper
import org.jyutping.jyutping.utilities.DatabasePreparer

Expand Down Expand Up @@ -48,6 +49,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),

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

val keyboardForm: MutableState<KeyboardForm> = mutableStateOf(KeyboardForm.Alphabetic)
fun transformTo(destination: KeyboardForm) {
keyboardForm.value = destination
}

val candidates: MutableState<List<Candidate>> = mutableStateOf(listOf())
private val db by lazy { DatabaseHelper(this, DatabasePreparer.databaseName) }
private var bufferText: String = ""
Expand All @@ -72,6 +78,9 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
fun process(text: String) {
bufferText += text
}
fun input(text: String) {
currentInputConnection.commitText(text, text.length)
}
fun select(candidate: Candidate) {
currentInputConnection.commitText(candidate.text, candidate.text.length)
bufferText = bufferText.drop(candidate.input.length)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
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.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
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService

@Composable
fun KeyboardScreen(keyHeight: Dp) {
fun AlphabeticKeyboard(keyHeight: Dp) {
val context = LocalContext.current as JyutpingInputMethodService
val isBuffering = remember { context.isBuffering }
Column(
modifier = Modifier
.background(Color(0xFFD6D8DD))
.fillMaxWidth()
.background(Color(0xFFD6D8DD))
) {
if (isBuffering.value) {
CandidateScrollBar()
} else {
ToolBar()
Box(
modifier = Modifier
.height(60.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))
}
Row(
modifier = Modifier
Expand Down Expand Up @@ -83,7 +91,7 @@ fun KeyboardScreen(keyHeight: Dp) {
.fillMaxWidth()
.height(keyHeight)
) {
TransformKey(modifier = Modifier.weight(2f))
TransformKey(destination = KeyboardForm.Numeric, modifier = Modifier.weight(2f))
LeftKey(modifier = Modifier.weight(1f))
SpaceKey(modifier = Modifier.weight(4f))
RightKey(modifier = Modifier.weight(1f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,41 @@ package org.jyutping.jyutping.keyboard
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService

@Composable
fun CandidateScrollBar() {
fun CandidateScrollBar(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val context = LocalContext.current as JyutpingInputMethodService
val candidates = remember { context.candidates }
Box(
modifier = Modifier
.height(60.dp)
.fillMaxWidth(),
contentAlignment = Alignment.Center
LazyRow(
modifier = modifier
.fillMaxWidth()
.fillMaxHeight(),
horizontalArrangement = Arrangement.spacedBy(0.dp),
verticalAlignment = Alignment.Bottom
) {
Color.Transparent
LazyRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(candidates.value) {
CandidateView(
candidate = it,
modifier = Modifier
.padding(bottom = 8.dp)
.clickable(interactionSource = interactionSource, indication = null) {
context.select(it)
}
)
}
items(candidates.value) {
CandidateView(
candidate = it,
modifier = Modifier
.clickable(interactionSource = interactionSource, indication = null) {
context.select(it)
}
.padding(horizontal = 4.dp)
.padding(bottom = 8.dp)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.jyutping.jyutping.keyboard

import androidx.compose.foundation.background
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.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun CantoneseNumericKeyboard(keyHeight: Dp) {
Column(
modifier = Modifier
.background(Color(0xFFD6D8DD))
.fillMaxWidth()
) {
ToolBar(
modifier = Modifier
.height(60.dp)
.fillMaxWidth()
)
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
SymbolKey(symbol = "1", modifier = Modifier.weight(1f))
SymbolKey(symbol = "2", modifier = Modifier.weight(1f))
SymbolKey(symbol = "3", modifier = Modifier.weight(1f))
SymbolKey(symbol = "4", modifier = Modifier.weight(1f))
SymbolKey(symbol = "5", modifier = Modifier.weight(1f))
SymbolKey(symbol = "6", modifier = Modifier.weight(1f))
SymbolKey(symbol = "7", modifier = Modifier.weight(1f))
SymbolKey(symbol = "8", modifier = Modifier.weight(1f))
SymbolKey(symbol = "9", modifier = Modifier.weight(1f))
SymbolKey(symbol = "0", modifier = Modifier.weight(1f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
SymbolKey(symbol = "-", modifier = Modifier.weight(1f))
SymbolKey(symbol = "/", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "$", modifier = Modifier.weight(1f))
SymbolKey(symbol = "@", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
TransformKey(destination = KeyboardForm.Symbolic, modifier = Modifier.weight(1.3f))
Spacer(modifier = Modifier.weight(0.22f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = ".", modifier = Modifier.weight(1.16f))
Spacer(modifier = Modifier.weight(0.22f))
BackspaceKey(modifier = Modifier.weight(1.3f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
TransformKey(destination = KeyboardForm.Alphabetic, modifier = Modifier.weight(2f))
LeftKey(modifier = Modifier.weight(1f))
SpaceKey(modifier = Modifier.weight(4f))
RightKey(modifier = Modifier.weight(1f))
ReturnKey(modifier = Modifier.weight(2f))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.jyutping.jyutping.keyboard

import androidx.compose.foundation.background
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.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun CantoneseSymbolicKeyboard(keyHeight: Dp) {
Column(
modifier = Modifier
.background(Color(0xFFD6D8DD))
.fillMaxWidth()
) {
ToolBar(
modifier = Modifier
.height(60.dp)
.fillMaxWidth()
)
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "#", modifier = Modifier.weight(1f))
SymbolKey(symbol = "%", modifier = Modifier.weight(1f))
SymbolKey(symbol = "^", modifier = Modifier.weight(1f))
SymbolKey(symbol = "*", modifier = Modifier.weight(1f))
SymbolKey(symbol = "+", modifier = Modifier.weight(1f))
SymbolKey(symbol = "=", modifier = Modifier.weight(1f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
SymbolKey(symbol = "_", modifier = Modifier.weight(1f))
SymbolKey(symbol = "——", modifier = Modifier.weight(1f))
SymbolKey(symbol = "\\", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "", modifier = Modifier.weight(1f))
SymbolKey(symbol = "&", modifier = Modifier.weight(1f))
SymbolKey(symbol = "·", modifier = Modifier.weight(1f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
TransformKey(destination = KeyboardForm.Numeric, modifier = Modifier.weight(1.3f))
Spacer(modifier = Modifier.weight(0.22f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "©", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "", modifier = Modifier.weight(1.16f))
SymbolKey(symbol = "'", modifier = Modifier.weight(1.16f))
Spacer(modifier = Modifier.weight(0.22f))
BackspaceKey(modifier = Modifier.weight(1.3f))
}
Row(
modifier = Modifier
.height(keyHeight)
.fillMaxWidth()
) {
TransformKey(destination = KeyboardForm.Alphabetic, modifier = Modifier.weight(2f))
LeftKey(modifier = Modifier.weight(1f))
SpaceKey(modifier = Modifier.weight(4f))
RightKey(modifier = Modifier.weight(1f))
ReturnKey(modifier = Modifier.weight(2f))
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/org/jyutping/jyutping/keyboard/KeyboardForm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jyutping.jyutping.keyboard

enum class KeyboardForm {
Alphabetic,
CandidateBoard,
DecimalPad,
EditingPanel,
EmojiBoard,
NumberPad,
Numeric,
Settings,
Symbolic,
TenKeyNumeric
}
19 changes: 19 additions & 0 deletions app/src/main/java/org/jyutping/jyutping/keyboard/QwertyForm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jyutping.jyutping.keyboard

enum class QwertyForm {
/// Alphabetic, English
Abc,

/// Alphabetic, Cantonese (粵拼全鍵盤)
Jyutping,

/// Cantonese SaamPing (粵拼三拼)
TripleStroke,

Pinyin,
Cangjie,
Stroke,

/// LoengFan Reverse Lookup. 拆字、兩分反查. 例如 木 + 旦 = 查: mukdaan
Structure
}
Loading

0 comments on commit af0b693

Please sign in to comment.