Skip to content

Commit

Permalink
feat: Add cursor key buttons to the iOS keyboard (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsci authored Sep 28, 2024
1 parent 8e57bda commit e5e1184
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions OpoLua/Views/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ class RootView : UIView {
return screenSize
}

private func makeKeyButton(imageName: String, key: OplKeyCode) -> UIButton {
var config: UIButton.Configuration = .plain()
config.image = UIImage(systemName: imageName)
let button = UIButton(configuration: config, primaryAction: UIAction { [weak self] action in
guard let self else {
return
}
self.delegate?.rootView(self, sendKey: key)
})
button.translatesAutoresizingMaskIntoConstraints = false
return button
}

override var inputAccessoryView: UIView? {
let view = UIView()
view.tintColor = self.tintColor
Expand All @@ -64,25 +77,25 @@ class RootView : UIView {
var doneButtonConfiguration: UIButton.Configuration = .plain()
doneButtonConfiguration.title = "Done"
let doneButton = UIButton(configuration: doneButtonConfiguration, primaryAction: UIAction(handler: { [weak self] action in
guard let self = self else {
guard let self else {
return
}
self.resignFirstResponder()
}))
doneButton.translatesAutoresizingMaskIntoConstraints = false

var escapeButtonConfiguration: UIButton.Configuration = .plain()
escapeButtonConfiguration.image = UIImage(systemName: "escape")
let escapeButton = UIButton(configuration: escapeButtonConfiguration, primaryAction: UIAction { [weak self] action in
guard let self = self else {
return
}
self.delegate?.rootView(self, sendKey: .escape)
})
escapeButton.translatesAutoresizingMaskIntoConstraints = false
let escapeButton = makeKeyButton(imageName: "escape", key: .escape)
let leftButton = makeKeyButton(imageName: "arrowtriangle.left", key: .leftArrow)
let upButton = makeKeyButton(imageName: "arrowtriangle.up", key: .upArrow)
let downButton = makeKeyButton(imageName: "arrowtriangle.down", key: .downArrow)
let rightButton = makeKeyButton(imageName: "arrowtriangle.right", key: .rightArrow)

view.addSubview(effectView)
view.addSubview(escapeButton)
view.addSubview(leftButton)
view.addSubview(upButton)
view.addSubview(downButton)
view.addSubview(rightButton)
view.addSubview(doneButton)
NSLayoutConstraint.activate([

Expand All @@ -95,6 +108,22 @@ class RootView : UIView {
escapeButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
escapeButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),

leftButton.leadingAnchor.constraint(equalTo: escapeButton.trailingAnchor),
leftButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
leftButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),

upButton.leadingAnchor.constraint(equalTo: leftButton.trailingAnchor),
upButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
upButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),

downButton.leadingAnchor.constraint(equalTo: upButton.trailingAnchor),
downButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
downButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),

rightButton.leadingAnchor.constraint(equalTo: downButton.trailingAnchor),
rightButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
rightButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),

doneButton.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
doneButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
doneButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
Expand Down

0 comments on commit e5e1184

Please sign in to comment.