Skip to content

Commit

Permalink
Merge pull request #485 from Jag-Marcel/selected-text-propagation
Browse files Browse the repository at this point in the history
Enter selected text into Scribe
  • Loading branch information
andrewtavis authored Aug 12, 2024
2 parents 37e5a1b + af349ca commit f9b1c9f
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 79 deletions.
150 changes: 104 additions & 46 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2197,32 +2197,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 @@ -2274,32 +2265,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 @@ -2749,7 +2807,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 @@ -222,10 +222,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

0 comments on commit f9b1c9f

Please sign in to comment.