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

Keyboard 20241126 #1517

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class AppPrefs(

var fullscreenMode by enum(FULLSCREEN_MODE, FullscreenMode.AUTO_SHOW)
val softCursorEnabled by bool(SOFT_CURSOR_ENABLED, true)
val popupKeyPressEnabled by bool(POPUP_KEY_PRESS_ENABLED, false)
val popupKeyPressEnabled = bool(POPUP_KEY_PRESS_ENABLED, false)
val switchesEnabled by bool(SWITCHES_ENABLED, true)
val switchArrowEnabled by bool(SWITCH_ARROW_ENABLED, true)

Expand Down Expand Up @@ -175,11 +175,11 @@ class AppPrefs(
val vibrationEnabled by bool(VIBRATION_ENABLED, false)
val vibrationDuration by int(VIBRATION_DURATION, 10)
val vibrationAmplitude by int(VIBRATION_AMPLITUDE, -1)
val swipeEnabled by bool(SWIPE_ENABLED, true)
val swipeTravel by int(SWIPE_TRAVEL, 80)
val swipeVelocity by int(SWIPE_VELOCITY, 800)
val longPressTimeout by int(LONG_PRESS_TIMEOUT, 400)
val repeatInterval by int(REPEAT_INTERVAL, 50)
val swipeEnabled = bool(SWIPE_ENABLED, true)
val swipeTravel = int(SWIPE_TRAVEL, 80)
val swipeVelocity = int(SWIPE_VELOCITY, 800)
val longPressTimeout = int(LONG_PRESS_TIMEOUT, 400)
val repeatInterval = int(REPEAT_INTERVAL, 50)
var isSpeakKey by bool(SPEAK_KEY_PRESS_ENABLED, false)
var isSpeakCommit by bool(SPEAK_COMMIT_ENABLED, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class GeneralStyleMapper(

val keyLongTextSize = getFloat("key_long_text_size")

val keyTextSize = getInt("key_text_size")
val keyTextSize = getFloat("key_text_size")

val keyTextOffsetX = getInt("key_text_offset_x")
val keyTextOffsetY = getInt("key_text_offset_y")
Expand Down Expand Up @@ -137,7 +137,7 @@ class GeneralStyleMapper(

val symbolFont = getStringList("symbol_font")

val symbolTextSize = getInt("symbol_text_size")
val symbolTextSize = getFloat("symbol_text_size")

val textFont = getStringList("text_font")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class GeneralStyle(
val keyBorder: Int,
val keyHeight: Int,
val keyLongTextSize: Float,
val keyTextSize: Int,
val keyTextSize: Float,
val keyTextOffsetX: Int,
val keyTextOffsetY: Int,
val keySymbolOffsetX: Int,
Expand Down Expand Up @@ -67,7 +67,7 @@ data class GeneralStyle(
val shadowRadius: Float,
val speechOpenccConfig: String,
val symbolFont: List<String>,
val symbolTextSize: Int,
val symbolTextSize: Float,
val textFont: List<String>,
val textSize: Int,
val verticalCorrection: Int,
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ class InputView(

fun finishInput() {
showingDialog?.dismiss()
keyboardWindow.mainKeyboardView.finishInput()
}

override fun onDetachedFromWindow() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* SPDX-FileCopyrightText: 2015 - 2024 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.ime.keyboard

import android.view.MotionEvent

class CustomSwipeTracker {
private val mPastX = FloatArray(NUM_PAST)
private val mPastY = FloatArray(NUM_PAST)
private val mPastTime = LongArray(NUM_PAST)
var yVelocity = 0f
var xVelocity = 0f

fun clear() {
mPastTime[0] = 0
}

fun addMovement(ev: MotionEvent) {
for (i in 0 until ev.historySize) {
addPoint(ev.getHistoricalX(i), ev.getHistoricalY(i), ev.getHistoricalEventTime(i))
}
addPoint(ev.x, ev.y, ev.eventTime)
}

private fun addPoint(
x: Float,
y: Float,
time: Long,
) {
var drop = -1
val pastTime = mPastTime
var i = 0
while (i < NUM_PAST) {
if (pastTime[i] == 0L) {
break
} else if (pastTime[i] < time - LONGEST_PAST_TIME) {
drop = i
}
i++
}
if (i == NUM_PAST && drop < 0) {
drop = 0
}
if (drop == i) drop--
val pastX = mPastX
val pastY = mPastY
if (drop >= 0) {
val start = drop + 1
val count = NUM_PAST - drop - 1
System.arraycopy(pastX, start, pastX, 0, count)
System.arraycopy(pastY, start, pastY, 0, count)
System.arraycopy(pastTime, start, pastTime, 0, count)
i -= drop + 1
}
pastX[i] = x
pastY[i] = y
pastTime[i] = time
i++
if (i < NUM_PAST) {
pastTime[i] = 0
}
}

fun computeCurrentVelocity(units: Int) {
val oldestX = mPastX[0]
val oldestY = mPastY[0]
val oldestTime = mPastTime[0]
var accumX = 0f
var accumY = 0f
val n = mPastTime.indexOfFirst { it == 0L }.coerceAtMost(NUM_PAST)
for (i in 1 until n) {
val dur = mPastTime[i] - oldestTime
if (dur == 0L) continue
val distX = mPastX[i] - oldestX
val velX = distX / dur * units // pixels/frame.
accumX += velX
val distY = mPastY[i] - oldestY
val velY = distY / dur * units // pixels/frame.
accumY += velY
}
xVelocity = accumX.coerceIn(Float.MIN_VALUE, Float.MAX_VALUE)
yVelocity = accumY.coerceIn(Float.MIN_VALUE, Float.MAX_VALUE)
}

companion object {
const val NUM_PAST = 4
const val LONGEST_PAST_TIME = 200
}
}
44 changes: 18 additions & 26 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.osfans.trime.ime.keyboard

import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.KeyEvent
import com.osfans.trime.core.Rime.Companion.hasLeft
import com.osfans.trime.core.Rime.Companion.hasMenu
Expand All @@ -17,8 +16,6 @@ import com.osfans.trime.data.theme.KeyActionManager
import com.osfans.trime.util.CollectionUtils.obtainBoolean
import com.osfans.trime.util.CollectionUtils.obtainFloat
import com.osfans.trime.util.CollectionUtils.obtainString
import com.osfans.trime.util.appContext
import com.osfans.trime.util.sp
import java.text.MessageFormat

/** [鍵盤][Keyboard]中的各個按鍵,包含單擊、長按、滑動等多種[事件][KeyAction] */
Expand All @@ -43,8 +40,8 @@ class Key(
var row = 0

var column = 0
private var label: String? = null
var hint: String? = null
private var label = ""
var hint: String = ""
private set
private lateinit var keyMap: Map<String, Any?>
private val keyBackColor get() = ColorManager.getDrawable(keyMap, "key_back_color")
Expand All @@ -55,11 +52,11 @@ class Key(
private val hilitedKeyTextColor get() = ColorManager.getColor(keyMap, "hilited_key_text_color")
private val hilitedKeySymbolColor get() = ColorManager.getColor(keyMap, "hilited_key_symbol_color")

var keyTextSize: Int? = null
var keyTextSize: Float = 0f
private set
var symbolTextSize: Int? = null
var symbolTextSize: Float = 0f
private set
var roundCorner: Float? = null
var roundCorner: Float = 0f
private set
var keyTextOffsetX = 0
get() = field + keyOffsetX
Expand All @@ -86,12 +83,7 @@ class Key(
var isOn = false
private set

@JvmField
val popupCharacters: String? = null

@JvmField
val popupResId = 0
private var labelSymbol: String? = null
private var labelSymbol = ""

/**
* Create an empty key with no attributes.
Expand All @@ -113,17 +105,17 @@ class Key(
}
}
if (hasComposingKey) mKeyboard.composingKeys.add(this)
label = obtainString(externalKeyMap, "label", "")
labelSymbol = obtainString(externalKeyMap, "label_symbol", "")
hint = obtainString(externalKeyMap, "hint", "")
label = obtainString(externalKeyMap, "label")
labelSymbol = obtainString(externalKeyMap, "label_symbol")
hint = obtainString(externalKeyMap, "hint")
if (externalKeyMap.containsKey("send_bindings")) {
sendBindings = obtainBoolean(externalKeyMap, "send_bindings", true)
} else if (!hasComposingKey) {
sendBindings = false
}
mKeyboard.setModiferKey(this.code, this)
keyTextSize = appContext.sp(obtainFloat(externalKeyMap, "key_text_size")).toInt()
symbolTextSize = appContext.sp(obtainFloat(externalKeyMap, "symbol_text_size")).toInt()
keyTextSize = obtainFloat(externalKeyMap, "key_text_size")
symbolTextSize = obtainFloat(externalKeyMap, "symbol_text_size")
roundCorner = obtainFloat(externalKeyMap, "round_corner")
}

Expand Down Expand Up @@ -386,16 +378,16 @@ class Key(

fun getCode(behavior: KeyBehavior): Int = getAction(behavior)!!.code

fun getLabel(): String? {
val event = keyAction
return if (!TextUtils.isEmpty(label) &&
event === click &&
fun getLabel(): String {
keyAction
return if (label.isNotEmpty() &&
keyAction === click &&
keyActions[KeyBehavior.ASCII.ordinal] == null &&
!showAsciiPunch()
) {
label
} else {
event!!.getLabel(mKeyboard) // 中文狀態顯示標籤
keyAction!!.getLabel(mKeyboard) // 中文狀態顯示標籤
}
}

Expand All @@ -406,9 +398,9 @@ class Key(
getAction(behavior)!!.getPreview(mKeyboard)
}

val symbolLabel: String?
val symbolLabel: String
get() {
if (labelSymbol!!.isEmpty()) {
if (labelSymbol.isEmpty()) {
val longClick = longClick
if (longClick != null) return longClick.getLabel(mKeyboard)
}
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -666,19 +666,6 @@ class Keyboard(
val isOnlyShiftOn: Boolean
get() = mShiftKey != null && mShiftKey!!.isOn && modifier and MASK_META_WITHOUT_SHIFT == 0

fun resetShifted(): Boolean = if (mShiftKey != null && !mShiftKey!!.isOn) setModifier(KeyEvent.META_SHIFT_ON, false) else false

fun resetModifer(): Boolean {
// 这里改为了一次性重置全部修饰键状态并返回TRUE刷新UI,可能有bug
modifier = 0
if (mShiftKey != null && mShiftKey!!.isOn) modifier = KeyEvent.META_SHIFT_ON
if (mAltKey != null && mAltKey!!.isOn) modifier = modifier or KeyEvent.META_ALT_ON
if (mCtrlKey != null && mCtrlKey!!.isOn) modifier = modifier or KeyEvent.META_CTRL_ON
if (mMetaKey != null && mMetaKey!!.isOn) modifier = modifier or KeyEvent.META_META_ON
if (mSymKey != null && mSymKey!!.isOn) modifier = modifier or KeyEvent.KEYCODE_SYM
return true
}

fun refreshModifier(): Boolean {
// 这里改为了一次性重置全部修饰键状态并返回TRUE刷新UI,可能有bug
var result = false
Expand Down
Loading
Loading