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
Expand Up @@ -50,3 +50,6 @@ data class KeyDefinition(
val qwertyConfigJson: String? = null,
val longPressAction: String? = null
)

internal val KeyDefinition.specialKeyLongPressAction: String?
get() = longPressAction.takeIf { isSpecialKey }
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,18 @@ object KeyboardBackupNormalizer {
keyIndex = keyIndex,
warnings = warnings
)
val normalizedLongPressAction = remapKeyAction(
action = keyDto.longPressAction,
stableIdsByOldLayoutId = stableIdsByOldLayoutId,
layoutIndex = layoutIndex,
keyIndex = keyIndex,
warnings = warnings
)
val isSpecialKey = keyDto.isSpecialKey ?: false
val normalizedLongPressAction = if (isSpecialKey) {
remapKeyAction(
action = keyDto.longPressAction,
stableIdsByOldLayoutId = stableIdsByOldLayoutId,
layoutIndex = layoutIndex,
keyIndex = keyIndex,
warnings = warnings
)
} else {
null
}
val normalizedDoubleTapAction = when {
!isSpecialKey -> null
keyDto.doubleTapEnabled == false -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.FullKeyboardLayout
import com.google.gson.JsonParser
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.KeyWithFlicks
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.specialKeyLongPressAction

/**
* カスタムキーボードレイアウトを schemaVersion 付き object 形式の JSON に
Expand Down Expand Up @@ -125,7 +126,7 @@ internal fun KeyWithFlicks.toExportDto(): KeyWithFlicksExportDto {
},
qwerty = this.key.qwertyConfigJson?.let(JsonParser::parseString),
textInputBehavior = this.key.textInputBehavior,
longPressAction = this.key.longPressAction
longPressAction = this.key.specialKeyLongPressAction
),
flicks = this.flicks.map {
FlickMappingDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import com.kazumaproject.custom_keyboard.data.SpecialKeyColorStyle
import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy
import com.kazumaproject.custom_keyboard.data.compatibleColumnSpan
import com.kazumaproject.custom_keyboard.data.compatibleRowSpan
import com.kazumaproject.custom_keyboard.data.effectiveLongPressAction
import com.kazumaproject.custom_keyboard.data.toCircularFlickMap
import com.kazumaproject.custom_keyboard.data.toCellSpanCeilFromGridUnits
import com.kazumaproject.custom_keyboard.data.usesFlexiblePlacement
Expand Down Expand Up @@ -154,6 +155,10 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
private var selectedTargetCustomKeyboardStableId: String? = null
private var selectedDoubleTapTargetStableId: String? = null
private var longPressAction: KeyAction? = null
private val isEditingSpecialKey: Boolean
get() = _binding?.keyTypeChipGroup?.checkedChipId == R.id.chip_special
private val selectedLongPressAction: KeyAction?
get() = longPressAction.takeIf { isEditingSpecialKey }
private var selectedIconRef: KeyIconRef? = null
private var originalIconRef: KeyIconRef? = null
private val pendingUserIconPaths = mutableSetOf<String>()
Expand Down Expand Up @@ -382,7 +387,11 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
binding.buttonDone.setOnClickListener { onDone() }

binding.keyTypeChipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
if (checkedIds.isEmpty()) return@setOnCheckedStateChangeListener
updateLongPressControlsVisibility()
if (checkedIds.isEmpty()) {
updateDoneButtonState()
return@setOnCheckedStateChangeListener
}
val selectedChipId = checkedIds.first()
val isSpecialKey = selectedChipId == R.id.chip_special

Expand Down Expand Up @@ -1117,15 +1126,20 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
getString(R.string.custom_key_long_press_priority)
}
val targetId = (action as? KeyAction.MoveToCustomKeyboard)?.stableId
binding.longPressTargetLayout.isVisible = targetId != null
val targetLabel = targetId?.takeIf { it.isNotBlank() }?.let { id ->
customKeyboardTargetOptions.firstOrNull { it.stableId == id }?.label
?: getString(R.string.deleted_custom_keyboard_target)
}
binding.longPressTargetSpinner.setText(targetLabel.orEmpty(), false)
updateLongPressControlsVisibility()
updateDoneButtonState()
}

private fun updateLongPressControlsVisibility() {
binding.longPressActionLayout.isVisible = isEditingSpecialKey
binding.longPressTargetLayout.isVisible = selectedLongPressAction is KeyAction.MoveToCustomKeyboard
}

private fun selectTargetCustomKeyboard(stableId: String?) {
val id = stableId.orEmpty()
if (id.isBlank()) {
Expand Down Expand Up @@ -1238,7 +1252,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
if (outputEditMode != OutputEditMode.TOGGLE) {
binding.textCharInputLayout.error = null
}
if (!longPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) {
if (!selectedLongPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) {
binding.buttonDone.isEnabled = false
return
}
Expand Down Expand Up @@ -1369,12 +1383,13 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {

val key = currentKeyData!!
val savedLongPress = savedInstanceState?.takeIf {
selectedId == restoredEditingKeyIdentifier && it.containsKey(STATE_LONG_PRESS_ACTION)
key.isSpecialKey && selectedId == restoredEditingKeyIdentifier &&
it.containsKey(STATE_LONG_PRESS_ACTION)
}
val savedAction = savedLongPress?.getString(STATE_LONG_PRESS_ACTION)
updateLongPressAction(
when {
savedLongPress == null -> key.longPressAction
savedLongPress == null -> key.effectiveLongPressAction
savedAction == "" -> KeyAction.MoveToCustomKeyboard("")
else -> KeyActionMapper.toKeyAction(savedAction)
}
Expand Down Expand Up @@ -1951,7 +1966,8 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {

private fun onDone() {
val originalKey = currentKeyData ?: return
if (!longPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) return
val newLongPressAction = selectedLongPressAction
if (!newLongPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) return

if (
binding.keyTypeChipGroup.checkedChipId == R.id.chip_normal &&
Expand Down Expand Up @@ -2218,7 +2234,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
)
}
if (newAction == KeyAction.VoiceInput || newDoubleTapBinding?.action == KeyAction.VoiceInput ||
longPressAction == KeyAction.VoiceInput
newLongPressAction == KeyAction.VoiceInput
) {
val hasPermission = ContextCompat.checkSelfPermission(
requireContext(),
Expand Down Expand Up @@ -2257,7 +2273,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
SpecialKeyColorStyle.SPECIAL
},
doubleTapBinding = newDoubleTapBinding,
longPressAction = longPressAction,
longPressAction = newLongPressAction,
textInputBehavior = if (
!isSpecial &&
newKeyType == KeyType.PETAL_FLICK &&
Expand Down Expand Up @@ -2301,11 +2317,13 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) {
override fun onSaveInstanceState(outState: Bundle) {
editingKeyIdentifier?.let {
outState.putString(STATE_EDITING_KEY, it)
outState.putString(
STATE_LONG_PRESS_ACTION,
KeyActionMapper.fromKeyAction(longPressAction)
?: (longPressAction as? KeyAction.MoveToCustomKeyboard)?.stableId
)
if (isEditingSpecialKey) {
outState.putString(
STATE_LONG_PRESS_ACTION,
KeyActionMapper.fromKeyAction(longPressAction)
?: (longPressAction as? KeyAction.MoveToCustomKeyboard)?.stableId
)
}
}
freeFlickDraft?.let { outState.putString("freeFlickDraft", PopupKeyConfigCodec.encode(it)) }
if (_binding != null && freeFlickDraft != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy
import com.kazumaproject.custom_keyboard.data.copyWithItems
import com.kazumaproject.custom_keyboard.data.copyWithKeys
import com.kazumaproject.custom_keyboard.data.effectiveDoubleTapBinding
import com.kazumaproject.custom_keyboard.data.effectiveLongPressAction
import com.kazumaproject.custom_keyboard.data.toCircularFlickDirection
import com.kazumaproject.custom_keyboard.data.toKeyItem
import com.kazumaproject.custom_keyboard.data.usesFlexiblePlacement
Expand All @@ -40,6 +41,7 @@ import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.LongPressFl
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.SpacerDefinition
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.TwoStepFlickMapping
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.TwoStepLongPressMappingEntity
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.specialKeyLongPressAction
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.toDbStrings
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.toFlickAction
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.database.KeyboardLayoutDao
Expand Down Expand Up @@ -196,7 +198,11 @@ class KeyboardRepository @Inject constructor(
// replace existing rows.
val normalizedKeysWithFlicks = importable.keysWithFlicks.map { keyWithFlicks ->
keyWithFlicks.copy(
key = keyWithFlicks.key.copy(keyId = 0, ownerLayoutId = 0),
key = keyWithFlicks.key.copy(
keyId = 0,
ownerLayoutId = 0,
longPressAction = keyWithFlicks.key.specialKeyLongPressAction
),
flicks = keyWithFlicks.flicks.map { it.copy(ownerKeyId = 0) },
circularFlicks = keyWithFlicks.circularFlicks.map { it.copy(ownerKeyId = 0) },
twoStepFlicks = keyWithFlicks.twoStepFlicks.map { it.copy(ownerKeyId = 0) },
Expand Down Expand Up @@ -593,7 +599,7 @@ class KeyboardRepository @Inject constructor(
val keyIdentifier = key.keyIdentifier
val keyLabel = key.label.takeIf { it.isNotBlank() }

for (storedAction in listOf(key.action, key.doubleTapAction, key.longPressAction)) {
for (storedAction in listOf(key.action, key.doubleTapAction, key.specialKeyLongPressAction)) {
val action = KeyActionMapper.toKeyAction(storedAction)
if (action is KeyAction.MoveToCustomKeyboard && action.stableId == targetStableId) {
references += MoveToCustomKeyboardReference(
Expand Down Expand Up @@ -680,7 +686,11 @@ class KeyboardRepository @Inject constructor(
)

val newKeys = originalLayout.keysWithFlicks.map { keyWithFlicks ->
keyWithFlicks.key.copy(keyId = 0, ownerLayoutId = 0)
keyWithFlicks.key.copy(
keyId = 0,
ownerLayoutId = 0,
longPressAction = keyWithFlicks.key.specialKeyLongPressAction
)
}

val newFlicksMap = originalLayout.keysWithFlicks.associate { keyWithFlicks ->
Expand Down Expand Up @@ -908,7 +918,7 @@ class KeyboardRepository @Inject constructor(

val iconRef = keyIconRefFromDb(dbKey.iconType, dbKey.iconValue)
val specialKeyColorStyle = SpecialKeyColorStyle.fromDbValue(dbKey.specialKeyColorStyle)
val longPressAction = KeyActionMapper.toKeyAction(dbKey.longPressAction)
val longPressAction = KeyActionMapper.toKeyAction(dbKey.specialKeyLongPressAction)
val doubleTapBinding = KeyActionMapper.toKeyAction(dbKey.doubleTapAction)
?.takeIf { dbKey.isSpecialKey }
?.let {
Expand Down Expand Up @@ -1237,7 +1247,7 @@ class KeyboardRepository @Inject constructor(
doubleTapBinding?.action
),
doubleTapPolicy = doubleTapBinding?.policy?.serializedName,
longPressAction = KeyActionMapper.fromKeyAction(keyData.longPressAction),
longPressAction = KeyActionMapper.fromKeyAction(keyData.effectiveLongPressAction),
popupMapConfigJson = keyData.popupMapConfig?.takeIf { keyData.keyType == KeyType.FREE_FLICK }
?.let(PopupKeyConfigCodec::encode),
qwertyConfigJson = keyData.qwertyConfig?.let(QwertyKeyConfigCodec::encode),
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_key_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp">
android:layout_marginTop="24dp"
android:visibility="gone">

<AutoCompleteTextView
android:id="@+id/long_press_action_spinner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class KeyboardLayoutJsonImporterTest {
}

@Test
fun exportImport_roundTrip_preservesCutActionAndConfiguredDoubleTapShortcut() {
fun exportImport_roundTrip_preservesSpecialKeyActions() {
val fullLayout = FullKeyboardLayout(
layout = CustomKeyboardLayout(
layoutId = 1,
Expand All @@ -394,7 +394,8 @@ class KeyboardLayoutJsonImporterTest {
keyIdentifier = "cut",
action = "Cut",
doubleTapAction = "Copy",
doubleTapPolicy = "EXCLUSIVE"
doubleTapPolicy = "EXCLUSIVE",
longPressAction = "MoveToCustomKeyboard:alternate"
),
flicks = emptyList(),
circularFlicks = emptyList(),
Expand Down Expand Up @@ -430,11 +431,13 @@ class KeyboardLayoutJsonImporterTest {
assertEquals("Cut", importedKeys[0].action)
assertEquals("Copy", importedKeys[0].doubleTapAction)
assertEquals("EXCLUSIVE", importedKeys[0].doubleTapPolicy)
assertEquals("MoveToCustomKeyboard:alternate", importedKeys[0].longPressAction)
assertEquals("SelectAll", importedKeys[1].action)
}

@Test
fun exportImport_keepsLongPressAndDropsDoubleTapOnNormalCharacterKey() {
fun exportImport_ignoresActionShortcutsOnNormalCharacterKey() {
val oldLongPressAction = "MoveToCustomKeyboard:alternate"
val fullLayout = FullKeyboardLayout(
layout = CustomKeyboardLayout(
layoutId = 1,
Expand All @@ -455,7 +458,7 @@ class KeyboardLayoutJsonImporterTest {
isSpecialKey = false,
keyIdentifier = "a",
action = "Text:a",
longPressAction = "MoveToCustomKeyboard:alternate",
longPressAction = oldLongPressAction,
doubleTapAction = "Copy",
doubleTapPolicy = "PROMOTE"
),
Expand All @@ -468,21 +471,24 @@ class KeyboardLayoutJsonImporterTest {
)
)

val exportedJson = KeyboardLayoutJsonExporter.toJson(listOf(fullLayout))
val exportedKey = JsonParser.parseString(exportedJson)
.asJsonObject["layouts"].asJsonArray[0]
val exportedJson = JsonParser.parseString(KeyboardLayoutJsonExporter.toJson(listOf(fullLayout))).asJsonObject
val exportedKey = exportedJson["layouts"].asJsonArray[0]
.asJsonObject["keysWithFlicks"].asJsonArray[0]
.asJsonObject["key"].asJsonObject
val importedKey = KeyboardLayoutJsonImporter.parse(exportedJson)
.layoutsOrThrow().single().keysWithFlicks.single().key

assertFalse(exportedKey["doubleTapEnabled"].asBoolean)
assertTrue(exportedKey["doubleTapAction"].isJsonNull)
assertTrue(exportedKey["doubleTapPolicy"].isJsonNull)
assertTrue(exportedKey["longPressAction"].isJsonNull)

exportedKey.addProperty("longPressAction", oldLongPressAction)
val importedKey = KeyboardLayoutJsonImporter.parse(exportedJson.toString())
.layoutsOrThrow().single().keysWithFlicks.single().key

assertEquals("Text:a", importedKey.action)
assertNull(importedKey.doubleTapAction)
assertNull(importedKey.doubleTapPolicy)
assertEquals("MoveToCustomKeyboard:alternate", exportedKey["longPressAction"].asString)
assertEquals("MoveToCustomKeyboard:alternate", importedKey.longPressAction)
assertNull(importedKey.longPressAction)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,15 @@ class KeyboardRepositoryDeleteImpactTest {
}

@Test
fun getDeleteImpactForLayout_noReferences_returnsEmpty() = runBlocking {
fun getDeleteImpactForLayout_ignoresOldLongPressActionOnNormalKey() = runBlocking {
val target = layout(layoutId = 13, stableId = "target-no-ref", name = "Target")
val targetFull = FullKeyboardLayout(target, emptyList(), emptyList())
val textKey = keyWithTextAction(keyId = 230, keyIdentifier = "k1", label = "あ")
val unrelated = FullKeyboardLayout(
layout = layout(layoutId = 23, stableId = "stable-unrelated", name = "Unrelated"),
keysWithFlicks = listOf(
keyWithTextAction(
keyId = 230,
keyIdentifier = "k1",
label = "あ"
textKey.copy(
key = textKey.key.copy(longPressAction = "MoveToCustomKeyboard:${target.stableId}")
)
),
spacers = emptyList()
Expand Down
Loading
Loading