Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.kazumaproject.custom_keyboard.view

import com.kazumaproject.custom_keyboard.data.PopupActionType
import com.kazumaproject.custom_keyboard.data.PopupMap
import com.kazumaproject.custom_keyboard.data.distanceTo
import kotlin.math.atan2
import kotlin.math.roundToInt

internal object FlickGuideLabelMapper {
private val displayDirections = listOf(
Expand Down Expand Up @@ -37,9 +42,41 @@ internal object FlickGuideLabelMapper {
return labelsFromDirections(displayDirections.associateWith { labelFor(it) })
}

fun buildFreeFlickRootGuideLabels(map: PopupMap, maxCodePoints: Int): AutoSizeButton.FlickGuideLabels {
val tap = sanitizeGuideText(map.tapText, maxCodePoints).orEmpty()
val labels = mutableMapOf(TfbiFlickDirection.TAP to tap)
// The key has one guide slot per direction. Prefer the closest region in key-relative coordinates.
for (region in map.regions.sortedBy { it.distanceTo(0f, 0f, 1f, 1f) }) {
val action = region.action
if (action.type == PopupActionType.BACK ||
(action.type == PopupActionType.TEXT && action.text.isEmpty())
) {
continue
}
val label = sanitizeGuideText(region.displayLabel(), maxCodePoints) ?: continue
val bounds = region.bounds
val direction = directionForOffset(
(bounds.left + bounds.right) / 2f,
(bounds.top + bounds.bottom) / 2f
)
labels.putIfAbsent(direction, label)
}
return labelsFromDirections(labels)
}



fun directionForOffset(x: Float, y: Float): TfbiFlickDirection {
val octant = (atan2(y, x) * 4 / Math.PI).roundToInt()
return when (octant.mod(8)) {
0 -> TfbiFlickDirection.RIGHT
1 -> TfbiFlickDirection.DOWN_RIGHT
2 -> TfbiFlickDirection.DOWN
3 -> TfbiFlickDirection.DOWN_LEFT
4 -> TfbiFlickDirection.LEFT
5 -> TfbiFlickDirection.UP_LEFT
6 -> TfbiFlickDirection.UP
else -> TfbiFlickDirection.UP_RIGHT
}
}

private fun labelsFromDirections(
labels: Map<TfbiFlickDirection, String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ class FlickKeyboardView @JvmOverloads constructor(
) {
val singleCharacterLabel = isSingleGuideCharacter(keyData.label)
val eligibleMultiCharacterLabel =
flickGuideAllowsMultiCharacterLabels && !keyData.isSpecialKey
(flickGuideAllowsMultiCharacterLabels || keyData.popupMapConfig != null) && !keyData.isSpecialKey
if (
!flickGuideEnabled ||
(!singleCharacterLabel && !eligibleMultiCharacterLabel) ||
Expand Down Expand Up @@ -1781,6 +1781,15 @@ class FlickKeyboardView @JvmOverloads constructor(
val layout = currentLayout ?: return null

keyData.popupMapConfig?.let { config ->
if (flickGuideEnabled && keyView is AutoSizeButton) {
config.map(config.startMapId)?.let { startMap ->
applyResolvedGuideLabels(
keyView,
keyData,
FlickGuideLabelMapper.buildFreeFlickRootGuideLabels(startMap, flickGuideMaxCodePoints)
)
}
}
return com.kazumaproject.custom_keyboard.controller.PopupMapInputController(
config = config,
windowAnchor = { popupWindowAnchorProvider?.invoke() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import android.graphics.RectF
import com.kazumaproject.custom_keyboard.data.PopupBounds
import com.kazumaproject.custom_keyboard.data.PopupKeyConfig
import com.kazumaproject.custom_keyboard.data.labelBounds
import kotlin.math.atan2
import kotlin.math.roundToInt

/** Free-layout labels projected into the existing compact two-step guide appearance. */
class PopupMapGuideView(context: Context) : PopupMapView(context) {
Expand All @@ -27,17 +25,7 @@ class PopupMapGuideView(context: Context) : PopupMapView(context) {
fun pointerDirection(): TfbiFlickDirection? {
if(tapActive) return null
val (x,y)=fingerPosition ?: return null
val octant=(atan2(y*sourceKeyHeight,x*sourceKeyWidth)*4/Math.PI).roundToInt()
return when((octant+8)%8) {
0 -> TfbiFlickDirection.RIGHT
1 -> TfbiFlickDirection.DOWN_RIGHT
2 -> TfbiFlickDirection.DOWN
3 -> TfbiFlickDirection.DOWN_LEFT
4 -> TfbiFlickDirection.LEFT
5 -> TfbiFlickDirection.UP_LEFT
6 -> TfbiFlickDirection.UP
else -> TfbiFlickDirection.UP_RIGHT
}
return FlickGuideLabelMapper.directionForOffset(x * sourceKeyWidth, y * sourceKeyHeight)
}
override fun onDraw(canvas: Canvas) {
val value=config ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ package com.kazumaproject.custom_keyboard.view
import com.kazumaproject.custom_keyboard.data.KeyboardLayoutOrigin
import android.view.ContextThemeWrapper
import androidx.test.core.app.ApplicationProvider
import com.kazumaproject.custom_keyboard.data.KeyAction
import com.kazumaproject.custom_keyboard.data.KeyData
import com.kazumaproject.custom_keyboard.data.KeyboardInputMode
import com.kazumaproject.custom_keyboard.data.KeyboardLayout
import com.kazumaproject.custom_keyboard.data.KeyItem
import com.kazumaproject.custom_keyboard.data.KeyType
import com.kazumaproject.custom_keyboard.data.PopupAction
import com.kazumaproject.custom_keyboard.data.PopupActionType
import com.kazumaproject.custom_keyboard.data.PopupBounds
import com.kazumaproject.custom_keyboard.data.PopupKeyConfig
import com.kazumaproject.custom_keyboard.data.PopupMap
import com.kazumaproject.custom_keyboard.data.PopupRegion
import com.kazumaproject.custom_keyboard.layout.KeyboardDefaultLayouts
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [35])
Expand Down Expand Up @@ -59,6 +70,37 @@ class FlickKeyboardViewGuidePresentationTest {
assertNull(view.findInputButtonWithText("a", layout))
}

@Test
fun customFreeFlickShowsStartMapGuidesWhenEnabled() {
val start = PopupMap("start", "Start", "タップ", listOf(
PopupRegion("far", PopupBounds(1.5f, -.5f, 2.5f, .5f), PopupAction(text = "遠い")),
PopupRegion("switch", PopupBounds(.5f, -.5f, 1.5f, .5f),
PopupAction(PopupActionType.SWITCH, targetMapId = "next"), label = "切り替え"),
PopupRegion("diagonal", PopupBounds(-1.5f, -1.5f, -.5f, -.5f), PopupAction(text = "😀入力")),
PopupRegion("empty", PopupBounds(-.5f, -1.5f, .5f, -.5f), PopupAction(), label = "未設定"),
PopupRegion("back", PopupBounds(-.5f, .5f, .5f, 1.5f), PopupAction(PopupActionType.BACK))
))
val next = PopupMap("next", "Next", "次", listOf(
PopupRegion("text", PopupBounds(-1.5f, -.5f, -.5f, .5f), PopupAction(text = "左"))
))
val key = KeyData("FREE", 0, 0, true, KeyAction.Text(start.tapText),
keyType = KeyType.FREE_FLICK, popupMapConfig = PopupKeyConfig("free", start.id, listOf(start, next)))
val layout = KeyboardLayout(listOf(key), emptyMap(), columnCount = 1, rowCount = 1)
val view = createView()
view.setKeyboard(layout, origin = KeyboardLayoutOrigin.CUSTOM)

fun guides(): AutoSizeButton.FlickGuideLabels? = ReflectionHelpers.getField(
requireNotNull(view.findInputButtonWithText("FREE", layout)), "flickGuideLabels"
)

assertNull(guides())
view.setFlickGuideMaxCodePoints(2)
view.setFlickGuideEnabled(true)
assertEquals(AutoSizeButton.FlickGuideLabels(tap = "タッ", right = "切り", upLeft = "😀入"), guides())
view.setFlickGuideEnabled(false)
assertNull(guides())
}

private fun createView(): FlickKeyboardView {
val context = ApplicationProvider.getApplicationContext<android.content.Context>()
val themedContext = ContextThemeWrapper(
Expand Down
Loading