-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf13851
commit f517c26
Showing
14 changed files
with
146 additions
and
268 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import UIKit | ||
|
||
public protocol FcitxProtocol { | ||
func getView() -> UIStackView | ||
func keyPressed(_ key: String) | ||
func commitString(_ string: String) | ||
func setPreedit(_ preedit: String, _ cursor: Int) | ||
func addChild(_ childController: UIViewController) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
add_library(KeyboardUI STATIC | ||
keyboardui.swift | ||
VirtualKeyboard.swift | ||
Key.swift | ||
Keyboard.swift | ||
Candidate.swift | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,13 @@ | ||
import UIKit | ||
import SwiftUI | ||
import UIPanel | ||
|
||
class CandidateView: UICollectionViewCell { | ||
static let identifier = "CandidateView" | ||
struct CandidateView: View { | ||
let text: String | ||
let index: Int | ||
|
||
let wordLabel: UILabel = { | ||
let label = UILabel() | ||
label.textAlignment = .center | ||
label.font = UIFont.systemFont(ofSize: 18) | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
return label | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
contentView.addSubview(wordLabel) | ||
setupConstraints() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setupConstraints() { | ||
NSLayoutConstraint.activate([ | ||
wordLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), | ||
wordLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), | ||
]) | ||
} | ||
|
||
func configure(with word: String) { | ||
wordLabel.text = word | ||
var body: some View { | ||
Text(text).font(.system(size: 20)).onTapGesture { | ||
selectCandidate(Int32(index)) | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,17 @@ | ||
import UIKit | ||
import UIPanel | ||
|
||
class CandidateCollectionView: UIView { | ||
|
||
var words = [String]() | ||
|
||
private var collectionView: UICollectionView! | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setupCollectionView() | ||
setupConstraints() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setupCollectionView() { | ||
let layout = UICollectionViewFlowLayout() | ||
layout.scrollDirection = .horizontal | ||
|
||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | ||
collectionView.translatesAutoresizingMaskIntoConstraints = false | ||
collectionView.backgroundColor = UIColor.clear | ||
collectionView.delegate = self | ||
collectionView.dataSource = self | ||
collectionView.register( | ||
CandidateView.self, forCellWithReuseIdentifier: CandidateView.identifier) | ||
|
||
addSubview(collectionView) | ||
} | ||
|
||
private func setupConstraints() { | ||
NSLayoutConstraint.activate([ | ||
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor), | ||
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor), | ||
collectionView.topAnchor.constraint(equalTo: topAnchor), | ||
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor), | ||
collectionView.heightAnchor.constraint(equalToConstant: barHeight), | ||
]) | ||
} | ||
|
||
func updateCandidates(_ candidates: [String]) { | ||
words = candidates | ||
collectionView.reloadData() | ||
} | ||
} | ||
|
||
extension CandidateCollectionView: UICollectionViewDataSource { | ||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) | ||
-> Int | ||
{ | ||
return words.count | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) | ||
-> UICollectionViewCell | ||
{ | ||
guard | ||
let cell = collectionView.dequeueReusableCell( | ||
withReuseIdentifier: CandidateView.identifier, for: indexPath) as? CandidateView | ||
else { | ||
return UICollectionViewCell() | ||
} | ||
cell.configure(with: words[indexPath.item]) | ||
return cell | ||
} | ||
} | ||
|
||
extension CandidateCollectionView: UICollectionViewDelegate { | ||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
selectCandidate(Int32(indexPath.item)) | ||
} | ||
} | ||
|
||
extension CandidateCollectionView: UICollectionViewDelegateFlowLayout { | ||
func collectionView( | ||
_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, | ||
sizeForItemAt indexPath: IndexPath | ||
) -> CGSize { | ||
let word = words[indexPath.item] | ||
let width = word.size(withAttributes: [.font: UIFont.systemFont(ofSize: 18)]).width + 20 | ||
return CGSize(width: width, height: 35) | ||
} | ||
|
||
func collectionView( | ||
_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, | ||
minimumLineSpacingForSectionAt section: Int | ||
) -> CGFloat { | ||
return 10 | ||
import SwiftUI | ||
|
||
struct CandidateBarView: View { | ||
@Binding var candidates: [String] | ||
|
||
var body: some View { | ||
ScrollView(.horizontal) { | ||
HStack(spacing: 20) { | ||
ForEach(Array(candidates.enumerated()), id: \.offset) { index, candidate in | ||
CandidateView(text: candidate, index: index) | ||
} | ||
Spacer() | ||
}.frame(height: barHeight) | ||
}.scrollIndicators(.hidden) // Hide scroll bar as native keyboard. | ||
.padding([.leading], 10) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,20 @@ | ||
import FcitxProtocol | ||
import UIKit | ||
import SwiftUI | ||
|
||
class Key: UIButton { | ||
let client: FcitxProtocol | ||
struct KeyView: View { | ||
let label: String | ||
|
||
init(_ client: FcitxProtocol, _ label: String) { | ||
self.client = client | ||
super.init(frame: .zero) | ||
setTitle(label, for: .normal) | ||
titleLabel?.font = UIFont.systemFont(ofSize: 24) | ||
backgroundColor = UIColor.gray.withAlphaComponent(0.2) | ||
layer.cornerRadius = 8 | ||
addTarget(self, action: #selector(keyPressed(_:)), for: .touchUpInside) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
@objc private func keyPressed(_ sender: UIButton) { | ||
guard let currentTitle = sender.currentTitle else { | ||
return | ||
var body: some View { | ||
Button { | ||
virtualKeyboardView.keyPressed(label) | ||
} label: { | ||
Text(label) | ||
.frame(width: label == " " ? 100 : 35, height: 40) | ||
.background(Color.white) | ||
.cornerRadius(5) | ||
.overlay( | ||
RoundedRectangle(cornerRadius: 5) | ||
.stroke(Color.gray, lineWidth: 1) | ||
) | ||
} | ||
client.keyPressed(currentTitle) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.