Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enter selected text into Scribe #485

Merged
merged 7 commits into from
Aug 12, 2024
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
150 changes: 104 additions & 46 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2199,32 +2199,23 @@ class KeyboardViewController: UIInputViewController {

switch originalKey {
case "Scribe":
if proxy.selectedText != nil && [.idle, .selectCommand, .alreadyPlural, .invalid].contains(commandState) { // annotate word
if [.selectCommand, .alreadyPlural, .invalid].contains(commandState) {
commandState = .idle
}
emojisToShow = .zero
loadKeys()
selectedWordAnnotation(KVC: self)
} else {
if [.translate,
.conjugate,
.selectVerbConjugation,
.selectCaseDeclension,
.plural].contains(commandState) { // escape
commandState = .idle
deCaseVariantDeclensionState = .disabled
} else if [.idle, .alreadyPlural, .invalid].contains(commandState) { // ScribeKey
commandState = .selectCommand
activateBtn(btn: translateKey)
activateBtn(btn: conjugateKey)
activateBtn(btn: pluralKey)
} else { // escape
commandState = .idle
deCaseVariantDeclensionState = .disabled
}
loadKeys()
if [.translate,
.conjugate,
.selectVerbConjugation,
.selectCaseDeclension,
.plural].contains(commandState) { // escape
commandState = .idle
deCaseVariantDeclensionState = .disabled
} else if [.idle, .alreadyPlural, .invalid].contains(commandState) { // ScribeKey
commandState = .selectCommand
activateBtn(btn: translateKey)
activateBtn(btn: conjugateKey)
activateBtn(btn: pluralKey)
} else { // escape
commandState = .idle
deCaseVariantDeclensionState = .disabled
}
loadKeys()

case "return":
if ![.translate, .conjugate, .plural].contains(commandState) { // normal return button
Expand Down Expand Up @@ -2276,32 +2267,99 @@ class KeyboardViewController: UIInputViewController {
}

case "Translate":
commandState = .translate
// Always start in letters with a new keyboard.
keyboardState = .letters
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = translatePromptAndColorPlaceholder
if let selectedText = proxy.selectedText {
queryWordToTranslate(queriedWordToTranslate: selectedText)

if commandState == .invalid { // invalid state
loadKeys()
proxy.insertText(selectedText)
autoCapAtStartOfProxy()
commandBar.text = commandPromptSpacing + invalidCommandMsg
commandBar.isShowingInfoButton = true
commandBar.textColor = keyCharColor
return
} else { // functional commands above
autoActionState = .suggest
commandState = .idle
autoCapAtStartOfProxy()
loadKeys()
conditionallyDisplayAnnotation()
}
} else {
commandState = .translate
// Always start in letters with a new keyboard.
keyboardState = .letters
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = translatePromptAndColorPlaceholder
}

case "Conjugate":
commandState = .conjugate
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = conjugatePromptAndColorPlaceholder
if let selectedText = proxy.selectedText {
resetVerbConjugationState()
let verbInTable = isVerbInConjugationTable(queriedVerbToConjugate: selectedText)
if verbInTable {
commandState = .selectVerbConjugation
loadKeys() // go to conjugation view
return
} else {
commandState = .invalid
loadKeys()
proxy.insertText(selectedText)
autoCapAtStartOfProxy()
commandBar.text = commandPromptSpacing + invalidCommandMsg
commandBar.isShowingInfoButton = true
commandBar.textColor = keyCharColor
return
}
} else {
commandState = .conjugate
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = conjugatePromptAndColorPlaceholder
}

case "Plural":
commandState = .plural
if controllerLanguage == "German" { // capitalize for nouns
if shiftButtonState == .normal {
shiftButtonState = .shift
if let selectedText = proxy.selectedText {
queryPluralNoun(queriedNoun: selectedText)

if [.invalid, .alreadyPlural].contains(commandState) {
loadKeys()

if commandState == .invalid {
proxy.insertText(selectedText)
commandBar.text = commandPromptSpacing + invalidCommandMsg
commandBar.isShowingInfoButton = true
} else {
commandBar.isShowingInfoButton = false
if commandState == .alreadyPlural {
commandBar.text = commandPromptSpacing + alreadyPluralMsg
}
}
autoCapAtStartOfProxy()
commandBar.textColor = keyCharColor
return
} else { // functional commands above
autoActionState = .suggest
commandState = .idle
autoCapAtStartOfProxy()
loadKeys()
conditionallyDisplayAnnotation()
}
} else {
commandState = .plural
if controllerLanguage == "German" { // capitalize for nouns
if shiftButtonState == .normal {
shiftButtonState = .shift
}
}
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = pluralPromptAndColorPlaceholder
}
conditionallyHideEmojiDividers()
loadKeys()
commandBar.textColor = keyCharColor
commandBar.attributedText = pluralPromptAndColorPlaceholder

case "shiftFormsDisplayLeft":
shiftLeft()
Expand Down Expand Up @@ -2751,7 +2809,7 @@ class KeyboardViewController: UIInputViewController {
&& (originalKey == spaceBar || originalKey == languageTextForSpaceBar)
&& proxy.documentContextBeforeInput?.count != 1
&& doubleSpacePeriodPossible {
// The fist condition prevents a period if the prior characters are spaces as the user wants a series of spaces.
// The first condition prevents a period if the prior characters are spaces as the user wants a series of spaces.
if proxy.documentContextBeforeInput?.suffix(2) != " " && ![.translate, .conjugate, .plural].contains(commandState) {
proxy.deleteBackward()
proxy.insertText(". ")
Expand Down
18 changes: 1 addition & 17 deletions Keyboards/KeyboardsBase/ScribeFunctionality/Annotate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let prepAnnotationConversionDict = [
"Russian": ["Acc": "Вин", "Dat": "Дат", "Gen": "Род", "Loc": "Мес", "Pre": "Пре", "Ins": "Инс"]
]

/// The base function for annotation that's accessed by `selectedWordAnnotation` and `typedWordAnnotation`.
/// The base function for annotation that's accessed by `typedWordAnnotation`.
///
/// - Parameters
/// - wordToAnnotate: the word that an annotation should be created for.
Expand Down Expand Up @@ -217,22 +217,6 @@ func wordAnnotation(wordToAnnotate: String, KVC: KeyboardViewController) {
}
}

/// Annotates a word after it's selected and the Scribe key is pressed.
///
/// - Parameters
/// - KVC: the keyboard view controller.
func selectedWordAnnotation(KVC: KeyboardViewController) {
wordToCheck = proxy.selectedText ?? ""
if !wordToCheck.isEmpty {
if !languagesWithCapitalizedNouns.contains(controllerLanguage) {
wordToCheck = wordToCheck.lowercased()
}
wordAnnotation(wordToAnnotate: wordToCheck, KVC: KVC)
} else {
return
}
}

/// Annotates a typed word after a space or auto action.
///
/// - Parameters
Expand Down
19 changes: 12 additions & 7 deletions Keyboards/KeyboardsBase/ScribeFunctionality/Conjugate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func returnDeclension(keyPressed: UIButton) {
}

if !(wordPressed.contains("/") || wordPressed.contains("")) {
proxy.insertText(wordPressed + " ")
proxy.insertText(wordPressed + getOptionalSpace())
deCaseVariantDeclensionState = .disabled
autoActionState = .suggest
commandState = .idle
} else if controllerLanguage == "Russian" { // pronoun selection paths not implemented for Russian
proxy.insertText(wordPressed + " ")
proxy.insertText(wordPressed + getOptionalSpace())
deCaseVariantDeclensionState = .disabled
autoActionState = .suggest
commandState = .idle
Expand Down Expand Up @@ -204,7 +204,12 @@ func triggerVerbConjugation(commandBar: UILabel) -> Bool {
let endIndex = commandBarText.index(commandBarText.endIndex, offsetBy: -1)
verbToConjugate = String(commandBarText[startIndex ..< endIndex])
}
verbToConjugate = String(verbToConjugate.trailingSpacesTrimmed)

return isVerbInConjugationTable(queriedVerbToConjugate: verbToConjugate)
}

func isVerbInConjugationTable(queriedVerbToConjugate: String) -> Bool {
verbToConjugate = String(queriedVerbToConjugate.trailingSpacesTrimmed)

// Check to see if the input was uppercase to return an uppercase conjugation.
let firstLetter = verbToConjugate.substring(toIdx: 1)
Expand Down Expand Up @@ -244,19 +249,19 @@ func returnConjugation(keyPressed: UIButton, requestedForm: String) {
// Don't return a space as well as we have a perfect verb and the cursor will be between.
proxy.insertText(wordToReturn.capitalize())
} else {
proxy.insertText(wordToReturn.capitalized + " ")
proxy.insertText(wordToReturn.capitalized + getOptionalSpace())
}
} else {
proxy.insertText(wordToReturn + " ")
proxy.insertText(wordToReturn + getOptionalSpace())
}
} else if formsDisplayDimensions == .view2x2 {
wordToReturn = LanguageDBManager.shared.queryVerb(of: verbToConjugate, with: outputCols)[0]
potentialWordsToReturn = wordToReturn.components(separatedBy: " ")

if inputWordIsCapitalized {
proxy.insertText(wordToReturn.capitalized + " ")
proxy.insertText(wordToReturn.capitalized + getOptionalSpace())
} else {
proxy.insertText(wordToReturn + " ")
proxy.insertText(wordToReturn + getOptionalSpace())
}
}

Expand Down
13 changes: 9 additions & 4 deletions Keyboards/KeyboardsBase/ScribeFunctionality/Plural.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func queryPlural(commandBar: UILabel) {
let endIndex = commandBarText.index(before: commandBarText.endIndex)
noun = String(commandBarText[startIndex ..< endIndex])
}
noun = String(noun.trailingSpacesTrimmed)

queryPluralNoun(queriedNoun: noun)
}

func queryPluralNoun(queriedNoun: String) {
var noun = String(queriedNoun.trailingSpacesTrimmed)

// Check to see if the input was uppercase to return an uppercase plural.
inputWordIsCapitalized = false
Expand All @@ -54,12 +59,12 @@ func queryPlural(commandBar: UILabel) {

if wordToReturn != "isPlural" {
if inputWordIsCapitalized {
proxy.insertText(wordToReturn.capitalized + " ")
proxy.insertText(wordToReturn.capitalized + getOptionalSpace())
} else {
proxy.insertText(wordToReturn + " ")
proxy.insertText(wordToReturn + getOptionalSpace())
}
} else {
proxy.insertText(noun + " ")
proxy.insertText(noun + getOptionalSpace())
commandState = .alreadyPlural
}
}
11 changes: 8 additions & 3 deletions Keyboards/KeyboardsBase/ScribeFunctionality/Translate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func queryTranslation(commandBar: UILabel) {
let endIndex = commandBarText.index(commandBarText.endIndex, offsetBy: -1)
wordToTranslate = String(commandBarText[startIndex ..< endIndex])
}
wordToTranslate = String(wordToTranslate.trailingSpacesTrimmed)

queryWordToTranslate(queriedWordToTranslate: wordToTranslate)
}

func queryWordToTranslate(queriedWordToTranslate: String) {
wordToTranslate = String(queriedWordToTranslate.trailingSpacesTrimmed)

// Check to see if the input was uppercase to return an uppercase conjugation.
inputWordIsCapitalized = wordToTranslate.substring(toIdx: 1).isUppercase
Expand All @@ -48,8 +53,8 @@ func queryTranslation(commandBar: UILabel) {
}

if inputWordIsCapitalized {
proxy.insertText(wordToReturn.capitalized + " ")
proxy.insertText(wordToReturn.capitalized + getOptionalSpace())
} else {
proxy.insertText(wordToReturn + " ")
proxy.insertText(wordToReturn + getOptionalSpace())
}
}
9 changes: 9 additions & 0 deletions Keyboards/KeyboardsBase/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ func get_iso_code(keyboardLanguage: String) -> String {

return iso
}

/// Checks if an extra space is needed in the text field when generated text is inserted
func getOptionalSpace() -> String {
if proxy.documentContextAfterInput?.first == " " {
return ""
} else {
return " "
}
}
2 changes: 0 additions & 2 deletions Scribe/InstallationTab/InstallationVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,8 @@ class InstallationVC: UIViewController {
// Sets the font size for the text in the app screen and corresponding UIImage icons.
if DeviceType.isPhone {
if UIScreen.main.bounds.width > 413 || UIScreen.main.bounds.width <= 375 {
print(UIScreen.main.bounds.width)
fontSize = UIScreen.main.bounds.height / 59
} else if UIScreen.main.bounds.width <= 413 && UIScreen.main.bounds.width > 375 {
print(UIScreen.main.bounds.width)
fontSize = UIScreen.main.bounds.height / 50
}

Expand Down