Skip to content

Commit

Permalink
Merge branch 'feature/ime-actions'
Browse files Browse the repository at this point in the history
  • Loading branch information
nimble0 committed Jan 27, 2019
2 parents b091e21 + af570c4 commit becdada
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/dictionaries/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"R*R": "{#Return}{^}",
"R-R": "{^}{#Return}{^}{-|}",
"SH-FT": "{#Control_L(Home)}{^}",
"SKW-BGS": "{#Return}{^}",
"SKW-BGS": "{IME:EDITOR_ACTION}{^}",
"SKWRAEURBGS": "{#Return}{#Return}{^}{-|}",
"SKWRAURBGS": "{#Return}{#Return}{^}{-|}",
"SR-RS": "{#Control_L(End)}{^}",
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/nimble/dotterel/Dotterel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ val MACHINES = mapOf(
Pair("NKRO", NkroStenoMachine.Factory())
)

interface DotterelRunnable
{
operator fun invoke(dotterel: Dotterel)

companion object
{
fun make(lambda: (Dotterel) -> Unit) =
object : DotterelRunnable
{
override fun invoke(dotterel: Dotterel) = lambda(dotterel)
}
}
}

class Dotterel : InputMethodService(), StenoMachine.Listener
{
interface EditorListener
Expand Down Expand Up @@ -228,14 +242,13 @@ class Dotterel : InputMethodService(), StenoMachine.Listener
override fun changeStroke(s: Stroke) {}
override fun applyStroke(s: Stroke)
{
val ic = this.currentInputConnection
this.translator.apply(s)
for(a in this.translator.flush())
when(a)
{
is FormattedText ->
{
ic?.run({
this.currentInputConnection?.run({
this.beginBatchEdit()
// TODO: Make sure that this is deleting the expected content.
// InputConnection.deleteSurroundingText should not delete
Expand All @@ -245,8 +258,12 @@ class Dotterel : InputMethodService(), StenoMachine.Listener
this.endBatchEdit()
})
}
is KeyCombo -> a.toAndroidKeyEvent()?.run({ ic?.sendKeyEvent(this) })
is KeyCombo ->
a.toAndroidKeyEvent()?.also({
this.currentInputConnection?.sendKeyEvent(it)
})
is Runnable -> a.run()
is DotterelRunnable -> a(this)
}
}

Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/nimble/dotterel/imeCommands.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This file is part of Dotterel which is released under GPL-2.0-or-later.
// See file <LICENSE.txt> or go to <http://www.gnu.org/licenses/> for full license details.

@file:Suppress("UNUSED_PARAMETER")

package nimble.dotterel

import android.content.Context
import android.util.Log
import android.view.inputmethod.InputMethodManager

import nimble.dotterel.translation.TranslationPart
import nimble.dotterel.translation.Translator

fun editorAction(translator: Translator, arg: String) =
TranslationPart(actions = listOf(
DotterelRunnable.make({ dotterel: Dotterel ->
dotterel.sendDefaultEditorAction(false)
})
))

fun switchPreviousIme(translator: Translator, arg: String)
: TranslationPart
{
return TranslationPart(
actions = listOf(DotterelRunnable.make({ dotterel: Dotterel ->
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P)
dotterel.switchToPreviousInputMethod()
else
{
val imm = dotterel.getSystemService(Context.INPUT_METHOD_SERVICE)
as InputMethodManager
@Suppress("DEPRECATION")
imm.switchToLastInputMethod(dotterel.window?.window?.attributes?.token)
}
})))
}

fun switchNextIme(translator: Translator, arg: String)
: TranslationPart
{
return TranslationPart(
actions = listOf(DotterelRunnable.make({ dotterel: Dotterel ->
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P)
dotterel.switchToNextInputMethod(false)
else
{
val imm = dotterel.getSystemService(Context.INPUT_METHOD_SERVICE)
as InputMethodManager
@Suppress("DEPRECATION")
imm.switchToNextInputMethod(
dotterel.window?.window?.attributes?.token,
false)
}
})))
}

fun switchIme(translator: Translator, arg: String)
: TranslationPart
{
return TranslationPart(
actions = listOf(DotterelRunnable.make({ dotterel: Dotterel ->
try
{
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P)
dotterel.switchInputMethod(arg)
else
{
val imm = dotterel.getSystemService(Context.INPUT_METHOD_SERVICE)
as InputMethodManager
@Suppress("DEPRECATION")
imm.setInputMethod(
dotterel.window?.window?.attributes?.token,
arg)
}
}
catch(e: java.lang.IllegalArgumentException)
{
Log.e("Dotterel IME:SWITCH", "Bad IME id")
}
})))
}

fun showImePicker(translator: Translator, arg: String)
: TranslationPart
{
return TranslationPart(
actions = listOf(DotterelRunnable.make({ dotterel: Dotterel ->
val imm = dotterel.getSystemService(Context.INPUT_METHOD_SERVICE)
as InputMethodManager
imm.showInputMethodPicker()
})))
}

Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ class TranslationProcessor
private fun parseCommand(translator: Translator, commandStr: String)
: TranslationPart
{
var i = commandStr.indexOf(':')
i = commandStr.indexOf(':', i + 1)

if(i == -1)
i = commandStr.length
var i = commandStr.length
while(i != -1)
{
val name = commandStr.substring(0, i).toLowerCase()
val arg = commandStr.substring(min(i + 1, commandStr.length))
val command = this.commands[name]
if(command != null)
return command(translator, arg)

val name = commandStr.substring(0, i).toLowerCase()
val arg = commandStr.substring(min(commandStr.length, i + 1))
i = commandStr.lastIndexOf(":", i - 1)
}

return this.commands[name]?.invoke(translator, arg)
?: TranslationPart()
return TranslationPart()
}

@Throws(ParseException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package nimble.dotterel.translation.systems

import nimble.dotterel.*
import nimble.dotterel.translation.*
import nimble.dotterel.translation.Orthography.Replacement

Expand Down Expand Up @@ -56,7 +57,13 @@ val IRELAND_SYSTEM = System(

Pair("MODE:SET_SPACE", ::setSpace),
Pair("MODE:RESET_CASE", ::resetTransform),
Pair("MODE:RESET_SPACE", ::resetSpace)
Pair("MODE:RESET_SPACE", ::resetSpace),

Pair("IME:EDITOR_ACTION", ::editorAction),
Pair("IME:SWITCH_PREVIOUS", ::switchPreviousIme),
Pair("IME:SWITCH_NEXT", ::switchNextIme),
Pair("IME:SWITCH", ::switchIme),
Pair("IME:SHOW_PICKER", ::showImePicker)
),

aliases = mapOf(
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/xml/method.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<!-- This file is part of Dotterel which is released under GPL-2.0-or-later. -->
<!-- See file <LICENSE.txt> or go to <http://www.gnu.org/licenses/> for full license details. -->

<input-method xmlns:android="http://schemas.android.com/apk/res/android">
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="nimble.dotterel.DotterelSettings"
android:supportsSwitchingToNextInputMethod="true">

<subtype
android:label="@string/subtype_en_US"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard" />

</input-method>

0 comments on commit becdada

Please sign in to comment.