Created
April 4, 2021 16:25
-
-
Save denisenepraunig/b222957bd589c7ecc427f8539fd9e7c5 to your computer and use it in GitHub Desktop.
Mini Prototype for an Xcode Shortcut Trainer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ShortCutTrainer.swift | |
// xcode-shortcut-trainer | |
// | |
// Created by Denise Nepraunig on 04.04.21. | |
// | |
import SwiftUI | |
struct ShortCutTrainer: View { | |
@State var showAnswer = false | |
@State var index = 0 | |
@State var finished = false | |
// data structure should be improved, first POV :-) | |
let description = ["Build", "Run", "Test"] | |
let keys: [Character] = ["b", "r", "u"] | |
let keyModifier: [EventModifiers] = [[.command], [.command], [.command]] | |
let modifierName = [ EventModifiers.command.rawValue : "CMD"] | |
var modifierKeyName: String { | |
let rawValue = keyModifier[index].rawValue | |
let keyName = modifierName[rawValue] ?? "" | |
return keyName | |
} | |
var body: some View { | |
VStack { | |
Text("Xcode Shortcut Trainer") | |
.font(.title) | |
.padding() | |
if !finished { | |
Button(description[index], action: { | |
showAnswer = true | |
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | |
if index < description.count - 1 { | |
index += 1 | |
showAnswer = false | |
} else { | |
finished = true | |
index = 0 | |
} | |
} | |
}) | |
.keyboardShortcut(KeyEquivalent(keys[index]), modifiers: keyModifier[index]) | |
// Text is alwyas visible to prevent 'jumping' in VStack layout rendering | |
Text(showAnswer ? "\(modifierKeyName) + \(keys[index].uppercased())" : " ") | |
.font(.title) | |
.padding() | |
.foregroundColor(Color(.systemPurple) ) | |
} else { | |
Button("Restart", action: { | |
showAnswer = false | |
finished = false | |
}) | |
Text("Finished ;-)") | |
.font(.title) | |
.padding() | |
.foregroundColor(Color(.systemPurple) ) | |
} | |
} | |
.frame(minWidth: 400, idealWidth: 400, maxWidth: 400, minHeight: 400, idealHeight: 400, maxHeight: 400, alignment: .center) | |
} | |
} | |
struct ShortCutTrainer_Previews: PreviewProvider { | |
static var previews: some View { | |
ShortCutTrainer() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Press the correct shortcut to activate the button. If you don't know the answer, press the button.
Currently I don't know how to distinguish if the button was pressed via the correct shortcut or via a tap.