Skip to content

Commit 8966de5

Browse files
committed
improve: better special keys layout
1 parent 840a74a commit 8966de5

File tree

2 files changed

+119
-30
lines changed

2 files changed

+119
-30
lines changed
Lines changed: 119 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,94 @@
11
package app.simple.inure.dialogs.terminal
22

3-
import android.annotation.SuppressLint
4-
import android.content.res.Resources
3+
import android.graphics.Typeface
54
import android.os.Bundle
5+
import android.text.SpannableStringBuilder
6+
import android.text.Spanned
7+
import android.text.style.ForegroundColorSpan
8+
import android.text.style.StyleSpan
69
import android.view.LayoutInflater
710
import android.view.View
811
import android.view.ViewGroup
12+
import android.widget.TextView
913
import app.simple.inure.R
1014
import app.simple.inure.decorations.typeface.TypeFaceTextView
1115
import app.simple.inure.extensions.fragments.ScopedBottomSheetFragment
1216
import app.simple.inure.preferences.TerminalPreferences
13-
import app.simple.inure.terminal.util.TermSettings
17+
import app.simple.inure.themes.manager.ThemeManager
1418

1519
class TerminalSpecialKeys : ScopedBottomSheetFragment() {
1620

1721
private lateinit var text: TypeFaceTextView
1822

1923
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2024
val view = inflater.inflate(R.layout.dialog_special_keys, container, false)
21-
2225
text = view.findViewById(R.id.text)
23-
2426
return view
2527
}
2628

27-
@SuppressLint("SetTextI18n")
2829
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2930
super.onViewCreated(view, savedInstanceState)
30-
text.text = (formatMessage(TerminalPreferences.getControlKey(), TermSettings.CONTROL_KEY_ID_NONE,
31-
resources, R.array.control_keys_short_names,
32-
R.string.control_key_dialog_control_text,
33-
R.string.control_key_dialog_control_disabled_text, "CTRLKEY")
34-
+ "\n\n" +
35-
formatMessage(TerminalPreferences.getFnKey(), TermSettings.FN_KEY_ID_NONE,
36-
resources, R.array.fn_keys_short_names,
37-
R.string.control_key_dialog_fn_text,
38-
R.string.control_key_dialog_fn_disabled_text, "FNKEY"))
31+
val controlKeyName = controlKeysShortNames[TerminalPreferences.getControlKey()]
32+
val fnKeyName = fnKeysShortNames[TerminalPreferences.getFnKey()]
33+
34+
val builder = SpannableStringBuilder()
35+
builder.append(
36+
buildFormattedKeys(
37+
controlKeyName,
38+
controlKeyMappings,
39+
ThemeManager.theme.textViewTheme.tertiaryTextColor,
40+
"Ctrl Key Mappings"
41+
)
42+
)
43+
44+
builder.append(
45+
buildFormattedKeys(
46+
fnKeyName,
47+
fnKeyMappings,
48+
ThemeManager.theme.textViewTheme.tertiaryTextColor,
49+
"Fn Key Mappings"
50+
)
51+
)
52+
53+
text.setText(builder.trim(), TextView.BufferType.SPANNABLE)
3954
}
4055

41-
private fun formatMessage(keyId: Int, disabledKeyId: Int, r: Resources, arrayId: Int, enabledId: Int, disabledId: Int, regex: String): String {
42-
if (keyId == disabledKeyId) {
43-
return r.getString(disabledId)
56+
private fun buildFormattedKeys(keyName: String, template: List<KeyMapping>, keyColor: Int, title: String): SpannableStringBuilder {
57+
val builder = SpannableStringBuilder()
58+
val titleStart = builder.length
59+
builder.append(title + "\n")
60+
builder.setSpan(StyleSpan(Typeface.BOLD), titleStart, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
61+
builder.append("\n")
62+
63+
val lines = template.map { mapping ->
64+
val key = mapping.key.replace(Regex("(CTRLKEY|FNKEY)"), keyName)
65+
"$key : ${mapping.description}"
66+
}
67+
68+
for (line in lines) {
69+
val keyEnd = line.indexOf(':')
70+
val start = builder.length
71+
builder.append(line)
72+
if (keyEnd > 0) {
73+
builder.setSpan(
74+
ForegroundColorSpan(keyColor),
75+
start,
76+
start + keyEnd,
77+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
78+
)
79+
builder.setSpan(
80+
StyleSpan(Typeface.BOLD),
81+
start,
82+
start + keyEnd,
83+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
84+
)
85+
}
86+
builder.append("\n")
4487
}
45-
val keyNames = r.getStringArray(arrayId)
46-
val keyName = keyNames[keyId]
47-
val template = r.getString(enabledId)
48-
return template.replace(regex.toRegex(), keyName)
88+
89+
builder.append("\n")
90+
91+
return builder
4992
}
5093

5194
companion object {
@@ -55,5 +98,59 @@ class TerminalSpecialKeys : ScopedBottomSheetFragment() {
5598
fragment.arguments = args
5699
return fragment
57100
}
101+
102+
private data class KeyMapping(val key: String, val description: String)
103+
104+
private val controlKeyMappings = listOf(
105+
KeyMapping("CTRLKEY + Space", "Control-@ (NUL)"),
106+
KeyMapping("CTRLKEY + A..Z", "Control-A..Z"),
107+
KeyMapping("CTRLKEY + 5", "Control-]"),
108+
KeyMapping("CTRLKEY + 6", "Control-^"),
109+
KeyMapping("CTRLKEY + 7", "Control-_"),
110+
KeyMapping("CTRLKEY + 9", "F11"),
111+
KeyMapping("CTRLKEY + 0", "F12")
112+
)
113+
114+
private val fnKeyMappings = listOf(
115+
KeyMapping("FNKEY + 1..9", "F1-F9"),
116+
KeyMapping("FNKEY + 0", "F10"),
117+
KeyMapping("FNKEY + W", "Up"),
118+
KeyMapping("FNKEY + A", "Left"),
119+
KeyMapping("FNKEY + S", "Down"),
120+
KeyMapping("FNKEY + D", "Right"),
121+
KeyMapping("FNKEY + P", "PageUp"),
122+
KeyMapping("FNKEY + N", "PageDown"),
123+
KeyMapping("FNKEY + T", "Tab"),
124+
KeyMapping("FNKEY + L", "| (pipe)"),
125+
KeyMapping("FNKEY + U", "_ (underscore)"),
126+
KeyMapping("FNKEY + E", "Control-[ (ESC)"),
127+
KeyMapping("FNKEY + X", "Delete"),
128+
KeyMapping("FNKEY + I", "Insert"),
129+
KeyMapping("FNKEY + H", "Home"),
130+
KeyMapping("FNKEY + F", "End"),
131+
KeyMapping("FNKEY + .", "Control-\\")
132+
)
133+
134+
private val controlKeysShortNames = arrayOf(
135+
"Ball",
136+
"@",
137+
"Left Alt",
138+
"Right Alt",
139+
"Volume Up",
140+
"Volume Down",
141+
"Camera",
142+
"None"
143+
)
144+
145+
private val fnKeysShortNames = arrayOf(
146+
"Ball",
147+
"@",
148+
"Left Alt",
149+
"Right Alt",
150+
"Volume Up",
151+
"Volume Down",
152+
"Camera",
153+
"None"
154+
)
58155
}
59156
}

app/src/main/res/values/non_translatable_string.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@
3232
<string name="system_excidle" translatable="false">System-Excidle</string>
3333
<string name="help_url" translatable="false">http://jackpal.github.com/Android-Terminal-Emulator/help/index.html</string>
3434

35-
<!-- The word CTRLKEY should be left un-translated. It will be replaced at run-time with the
36-
actual control key key name. -->
37-
<string name="control_key_dialog_control_text" translatable="false">CTRLKEY Space : Control-@ (NUL)\nCTRLKEY A..Z : Control-A..Z\nCTRLKEY 5 : Control-]\nCTRLKEY 6 : Control-^\nCTRLKEY 7 : Control-_\nCTRLKEY 9 : F11\nCTRLKEY 0 : F12</string>
38-
39-
<!-- The word FNKEY should be left un-translated. It will be replaced at run-time with the
40-
actual function key key name. -->
41-
<string name="control_key_dialog_fn_text" translatable="false">FNKEY 1..9 : F1-F9\nFNKEY 0 : F10\nFNKEY W : Up\nFNKEY A : Left\nFNKEY S : Down\nFNKEY D : Right\nFNKEY P : PageUp\nFNKEY N : PageDown\nFNKEY T : Tab\nFNKEY L : | (pipe)\nFNKEY U : _ (underscore)\nFNKEY E : Control-[ (ESC)\nFNKEY X : Delete\nFNKEY I : Insert\nFNKEY H : Home\nFNKEY F : End\nFNKEY . : Control-\\</string>
42-
4335
<!-- AddShortcut.java -->
4436
<string name="addshortcut_example_hint" translatable="false">--example=\""a\"</string>
4537

0 commit comments

Comments
 (0)