Skip to content

Commit

Permalink
Implement KeyboardCase
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jun 30, 2024
1 parent 87470c8 commit bbce823
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 35 deletions.
8 changes: 2 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ android {
versionCode = 4
versionName = "0.4.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
vectorDrawables.useSupportLibrary = true
}
buildTypes {
debug {
Expand Down Expand Up @@ -47,9 +45,7 @@ android {
kotlinCompilerExtensionVersion = "1.5.13"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
resources.excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}

Expand Down
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.KeyboardCase
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.utilities.DatabaseHelper
import org.jyutping.jyutping.utilities.DatabasePreparer
Expand Down Expand Up @@ -58,6 +59,19 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
keyboardForm.value = destination
}

val keyboardCase: MutableState<KeyboardCase> = mutableStateOf(KeyboardCase.Lowercased)
private fun updateKeyboardCaseTo(case: KeyboardCase) {
keyboardCase.value = case
}
fun performShift() {
val newCase: KeyboardCase = when (keyboardCase.value) {
KeyboardCase.Lowercased -> KeyboardCase.Uppercased
KeyboardCase.Uppercased -> KeyboardCase.Lowercased
KeyboardCase.CapsLocked -> KeyboardCase.Lowercased
}
updateKeyboardCaseTo(newCase)
}

val candidates: MutableState<List<Candidate>> = mutableStateOf(listOf())
private val db by lazy { DatabaseHelper(this, DatabasePreparer.databaseName) }
private var bufferText: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ import androidx.compose.ui.graphics.Color

val Color.Companion.keyboardLightBackground: Color
get() = Color(0xFFD0D3D9)

val Color.Companion.keyLight: Color
get() = Color.White

val Color.Companion.keyLightEmphatic: Color
get() = Color(0xFFACB1B9)
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.extensions.keyLight
import org.jyutping.jyutping.extensions.keyLightEmphatic

@Composable
fun BackspaceKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.backspace() }
Expand All @@ -39,7 +40,7 @@ fun BackspaceKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) Color.White else lightEmphatic)
.background(if (isPressed.value) Color.keyLight else Color.keyLightEmphatic)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/jyutping/jyutping/keyboard/KeyboardCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jyutping.jyutping.keyboard

enum class KeyboardCase {
Lowercased,
Uppercased,
CapsLocked
}

fun KeyboardCase.isLowercased(): Boolean = this == KeyboardCase.Lowercased
fun KeyboardCase.isUppercased(): Boolean = this == KeyboardCase.Uppercased
fun KeyboardCase.isCapsLocked(): Boolean = this == KeyboardCase.CapsLocked
5 changes: 3 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/LeftKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import androidx.compose.ui.platform.LocalContext
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 LeftKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.leftKey() }
Expand All @@ -38,7 +39,7 @@ fun LeftKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) lightEmphatic else Color.White)
.background(if (isPressed.value) Color.keyLightEmphatic else Color.keyLight)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/keyboard/LetterKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import androidx.compose.ui.platform.LocalContext
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 LetterKey(letter: String, modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
val keyboardCase = remember { context.keyboardCase }
val keyText: String = if (keyboardCase.value.isLowercased()) letter else letter.uppercase()
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.process(letter) }
.clickable(interactionSource = interactionSource, indication = null) { context.process(keyText) }
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand All @@ -38,13 +41,13 @@ fun LetterKey(letter: String, modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) lightEmphatic else Color.White)
.background(if (isPressed.value) Color.keyLightEmphatic else Color.keyLight)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Text(
text = letter,
text = keyText,
fontSize = 22.sp
)
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ReturnKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.extensions.keyLight
import org.jyutping.jyutping.extensions.keyLightEmphatic

@Composable
fun ReturnKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.performReturn() }
Expand All @@ -39,7 +40,7 @@ fun ReturnKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) Color.White else lightEmphatic)
.background(if (isPressed.value) Color.keyLight else Color.keyLightEmphatic)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/RightKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import androidx.compose.ui.platform.LocalContext
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 RightKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.rightKey() }
Expand All @@ -38,7 +39,7 @@ fun RightKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) lightEmphatic else Color.White)
.background(if (isPressed.value) Color.keyLightEmphatic else Color.keyLight)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ShiftKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowUpward
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.alpha
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

@Composable
fun ShiftKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
val keyboardCase = remember { context.keyboardCase }
val drawableId: Int = when (keyboardCase.value) {
KeyboardCase.Lowercased -> R.drawable.shift
KeyboardCase.Uppercased -> R.drawable.shifted
KeyboardCase.CapsLocked -> R.drawable.capslock
}
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) {
// TODO: Shift
// TODO: Double tapping to toggle capslock
context.performShift()
}
.fillMaxWidth()
.fillMaxHeight(),
Expand All @@ -42,15 +50,14 @@ fun ShiftKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) Color.White else lightEmphatic)
.background(if (isPressed.value) Color.keyLight else Color.keyLightEmphatic)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Outlined.ArrowUpward, // TODO: Find a perfect icon
contentDescription = null,
modifier = Modifier.alpha(0f)
imageVector = ImageVector.vectorResource(id = drawableId),
contentDescription = null
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/SpaceKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import androidx.compose.ui.platform.LocalContext
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 SpaceKey(modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.space() }
Expand All @@ -38,7 +39,7 @@ fun SpaceKey(modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) lightEmphatic else Color.White)
.background(if (isPressed.value) Color.keyLightEmphatic else Color.keyLight)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/SymbolKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import androidx.compose.ui.platform.LocalContext
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 SymbolKey(symbol: String, modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = modifier
.clickable(interactionSource = interactionSource, indication = null) { context.input(symbol) }
Expand All @@ -38,7 +39,7 @@ fun SymbolKey(symbol: String, modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) lightEmphatic else Color.White)
.background(if (isPressed.value) Color.keyLightEmphatic else Color.keyLight)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/ToolBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.extensions.keyLightEmphatic

@Composable
fun ToolBar(modifier: Modifier) {
Expand Down Expand Up @@ -110,7 +111,6 @@ fun ToolBar(modifier: Modifier) {
private fun InputMethodModeSwitch() {
val interactionSource = remember { MutableInteractionSource() }
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
Box(
modifier = Modifier
.clickable(interactionSource = interactionSource, indication = null) {
Expand All @@ -124,7 +124,7 @@ private fun InputMethodModeSwitch() {
Box (
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.background(lightEmphatic)
.background(Color.keyLightEmphatic)
.width(72.dp)
.height(26.dp),
contentAlignment = Alignment.Center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import androidx.compose.ui.platform.LocalContext
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 TransformKey(destination: KeyboardForm, modifier: Modifier) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState()
val context = LocalContext.current as JyutpingInputMethodService
val lightEmphatic = Color(0xFFACB1B9)
val keyText: String = when (destination) {
KeyboardForm.Alphabetic -> "ABC"
KeyboardForm.Numeric -> "123"
Expand All @@ -44,7 +45,7 @@ fun TransformKey(destination: KeyboardForm, modifier: Modifier) {
modifier = modifier
.padding(horizontal = 3.dp, vertical = 6.dp)
.clip(RoundedCornerShape(6.dp))
.background(if (isPressed.value) Color.White else lightEmphatic)
.background(if (isPressed.value) Color.keyLight else Color.keyLightEmphatic)
.fillMaxWidth()
.fillMaxHeight(),
contentAlignment = Alignment.Center
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/capslock.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="56"
android:viewportWidth="56">
<path
android:pathData="M20.781,37.621L35.242,37.621C38.523,37.621 40.258,35.699 40.258,32.605L40.258,28.457L49.14,28.457C51.086,28.457 52.633,27.309 52.633,25.504C52.633,24.402 52.07,23.535 51.016,22.621L30.906,4.879C29.945,4.035 29.055,3.473 28,3.473C26.969,3.473 26.078,4.035 25.117,4.879L4.984,22.645C3.883,23.605 3.367,24.402 3.367,25.504C3.367,27.309 4.914,28.457 6.883,28.457L15.742,28.457L15.742,32.605C15.742,35.699 17.5,37.621 20.781,37.621ZM19.773,52.527L36.062,52.527C38.758,52.527 40.305,51.027 40.305,48.309L40.305,44.934C40.305,42.215 38.758,40.668 36.062,40.668L19.773,40.668C17.055,40.668 15.508,42.238 15.508,44.934L15.508,48.309C15.508,51.004 17.055,52.527 19.773,52.527Z"
android:fillColor="@android:color/black"/>
</vector>
Loading

0 comments on commit bbce823

Please sign in to comment.