hex/ios/HEXMobile/LanguageSettingsRows.swift
Ryan Vogel c5400a4686
iOS: Russian and Serbian (Latin) full-keyboard layouts (#2075)
A TestFlight tester (build 35) asked for Russian and Serbian (Latin) layouts in
Settings › Keyboard › Keyboard layout. KeyboardLanguage gets russian ("ru") and
serbianLatin ("sr-Latn") cases, the way #2069 added Italian, French and Portuguese.

Russian is Apple's ЙЦУКЕН: 11, 11 and 9 letters, ё on a held е and ъ on a held ь.
The long-press lists are Apple's own, read from the iOS 27.2 simulator's
TextInput_ru bundle, base letter first (Apple's combining-macron forms а̄ е̄ о̄ с̄ are
left out). Autocorrect and suggestions use UITextChecker's ru_RU dictionary.

Serbian (Latin) is Apple's QWERTZ option for it. Apple's iPhone Serbian Latin
keyboard has no keys of its own for č ć đ š ž (TextInput_sr lists QWERTY, QWERTZ and
AZERTY; only the iPad's Croatian layouts give them keys), so they are held on c, d,
s and z with Apple's lists. iOS 27.2 has no Serbian (or Croatian) UITextChecker
dictionary: suggestions and autocorrect stay off for it, never English. The
dictionary candidates are Latin-only (sr_Latn), since a Cyrillic sr dictionary
would reject every Latin word.

Automatic picks Russian for ru-* and Serbian (Latin) for sr-Latn-* dictation
languages, iPhone keyboards or preferred languages. Bare sr / sr-RS (Cyrillic)
still gets English: Hex has no Cyrillic Serbian layout. Spatial autocorrect has no
ru or sr-Latn model, so neither gets a decoder (same rule as #2069).

KeyboardLayout: Russian's nine-letter row 3 overlapped the 10-key-sized shift and
delete by about 9 pt. Where a letter row wouldn't fit, shift and delete now narrow
to fit (to about a letter's width, as on Apple's Russian keyboard); every existing
layout computes the same modW, so their frames are unchanged. Hit-testing is
untouched. DictationLanguageSwitch.nativeName names a language in its own script
("sr-Latn-RS" is "Srpski", not "Српски"); the space bar still names the dictation
language ("Русский" for ru-RU).

Snapshot scenes get Russian and Serbian drafts (--keyboard-layout ru|sr-Latn,
--accent-key е|ь|c|s).

Verified on an iOS 27.2 iPhone 17 Pro simulator: full HEXMobileTests suite, 277
tests, 0 failures, 1 skipped as before (one new Russian autocorrect case was
adjusted and re-run on its own after the full run). New cases in
KeyboardLayoutTests, AutocorrectEngineTests, FullKeyboardEngineTests and
KeyboardBridgeTests. Live in Messages with the Hex keyboard: Automatic with ru-RU
dictation shows the Russian layout and "Русский" on the space bar, "минкт"
suggests "минут", and holding е typed "ещё". Picking Srpski in Settings, holding c
and s typed "čekaj šta" with an empty strip. Compared with Apple's Russian and
Serbian Latin keyboards on the same simulator: same rows, letter-sized
shift/delete on Russian, and the same е, ь and c menus.
2026-11-20 17:44:49 +00:00

164 lines
6.3 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import UIKit
/// Settings › Dictation language: Automatic, the first iPhone language Apple Speech
/// transcribes, or any language `SpeechTranscriber` or `DictationTranscriber` has. Shown
/// on iOS 26, where Hex dictates with Apple Speech; before it, Parakeet is English only.
struct DictationLanguageRow: View {
@Bindable var dictation: DictationController
@State private var options: [SpeechLanguageOption] = []
@State private var automaticName: String?
var body: some View {
if #available(iOS 26.0, *) {
VStack(spacing: 0) {
LanguageRowDivider()
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 2) {
Text("Dictation language")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
if dictation.dictationLanguage == nil, let automaticName {
Text(automaticName)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
.lineLimit(1)
}
}
Spacer(minLength: 8)
Menu {
Button {
dictation.setDictationLanguage(nil)
} label: {
if dictation.dictationLanguage == nil {
Label("Automatic", systemImage: "checkmark")
} else {
Text("Automatic")
}
}
Divider()
ForEach(options) { option in
Button {
dictation.setDictationLanguage(option.id)
} label: {
if dictation.dictationLanguage == option.id {
Label(option.name, systemImage: "checkmark")
} else {
Text(option.name)
}
}
}
} label: {
LanguageMenuValue(value: selectedName)
}
}
.padding(.horizontal, 16)
.frame(minHeight: 48)
}
.task {
// The keyboard may have switched the language while Hex was away.
dictation.followDictationLanguageSetting()
options = await AppleSpeechLanguages.options()
automaticName = await AppleSpeechLanguages.automatic().map {
SpeechLanguagePicker.displayName(for: $0.identifier)
}
}
}
}
private var selectedName: String {
guard let id = dictation.dictationLanguage else { return "Automatic" }
return SpeechLanguagePicker.shortNames(for: options.map(\.id))[id]
?? SpeechLanguagePicker.displayName(for: id)
}
}
/// Settings › Keyboard › Keyboard layout (while the full keyboard is on): Automatic (the dictation language's
/// layout), English, Norsk, Español, Deutsch, Italiano, Français, Português, Русский, or Srpski (Latin).
struct KeyboardLayoutRow: View {
@State private var setting = LanguageSettings().keyboardLayout
private var automatic: KeyboardLanguage {
KeyboardLanguage.automatic(
dictationLanguage: LanguageSettings().effectiveDictationLanguage,
inputModes: UITextInputMode.activeInputModes.compactMap(\.primaryLanguage),
preferredLanguages: Locale.preferredLanguages
)
}
var body: some View {
VStack(spacing: 0) {
Divider()
.overlay(HexTheme.borderWeak)
.padding(.leading, 16)
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 2) {
Text("Keyboard layout")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
if setting == nil {
Text("\(automatic.displayName), like your dictation language")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
Spacer(minLength: 8)
Menu {
choice(nil, title: "Automatic")
Divider()
ForEach(KeyboardLanguage.allCases, id: \.self) { language in
choice(language, title: language.displayName)
}
} label: {
LanguageMenuValue(value: setting?.displayName ?? "Automatic")
}
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
}
.onAppear { setting = LanguageSettings().keyboardLayout }
}
private func choice(_ language: KeyboardLanguage?, title: String) -> some View {
Button {
setting = language
LanguageSettings().keyboardLayout = language
} label: {
if setting == language {
Label(title, systemImage: "checkmark")
} else {
Text(title)
}
}
}
}
/// The settings card's row divider.
private struct LanguageRowDivider: View {
var body: some View {
Rectangle()
.fill(HexTheme.borderWeak)
.frame(height: 0.5)
.padding(.leading, 16)
}
}
/// A menu's current value with the up-down chevron, as the settings card's other menus show it.
private struct LanguageMenuValue: View {
let value: String
var body: some View {
HStack(spacing: 5) {
Text(value)
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textMuted)
.lineLimit(1)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.vertical, 6)
.contentShape(Rectangle())
}
}