11import Fcitx
2+ import FcitxProtocol
23import OSLog
34import UIKit
45
56let logger = Logger ( subsystem: " org.fcitx.Fcitx5 " , category: " FcitxLog " )
67
7- class KeyboardViewController : UIInputViewController {
8+ class KeyboardViewController : UIInputViewController , FcitxProtocol {
89
910 @IBOutlet var nextKeyboardButton : UIButton !
1011
11- let keys : [ [ String ] ] = [
12- [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " 0 " ] ,
13- [ " q " , " w " , " e " , " r " , " t " , " y " , " u " , " i " , " o " , " p " ] ,
14- [ " a " , " s " , " d " , " f " , " g " , " h " , " j " , " k " , " l " ] ,
15- [ " z " , " x " , " c " , " v " , " b " , " n " , " m " ] ,
16- [ " " ] ,
17- ]
18-
19- @objc private func keyPressed( _ sender: UIButton ) {
20- guard let title = sender. currentTitle else { return }
21- if !processKey( title) {
22- textDocumentProxy. insertText ( title)
23- }
24- }
25-
26- private func createButton( title: String ) -> UIButton {
27- let button = UIButton ( type: . system)
28- button. setTitle ( title, for: . normal)
29- button. titleLabel? . font = UIFont . systemFont ( ofSize: 24 )
30- button. backgroundColor = UIColor . gray. withAlphaComponent ( 0.2 )
31- button. layer. cornerRadius = 8
32- button. addTarget ( self , action: #selector( keyPressed ( _: ) ) , for: . touchUpInside)
33- return button
34- }
35-
36- private func setupKeyboardLayout( ) {
37- // Create a vertical stack view for rows
38- let stackView = UIStackView ( )
39- stackView. axis = . vertical
40- stackView. distribution = . fillEqually
41- stackView. alignment = . fill
42- stackView. spacing = 5
43-
44- // Create buttons for each row and add them to the stack view
45- for row in keys {
46- let rowStackView = UIStackView ( )
47- rowStackView. axis = . horizontal
48- rowStackView. distribution = . fillEqually
49- rowStackView. alignment = . fill
50- rowStackView. spacing = 5
51-
52- for key in row {
53- let button = createButton ( title: key)
54- rowStackView. addArrangedSubview ( button)
55- }
56- stackView. addArrangedSubview ( rowStackView)
57- }
58-
59- // Add the stack view to the view controller's view
60- stackView. translatesAutoresizingMaskIntoConstraints = false
61- view. addSubview ( stackView)
62-
63- // Set up Auto Layout constraints for the stack view
64- NSLayoutConstraint . activate ( [
65- stackView. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 10 ) ,
66- stackView. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 10 ) ,
67- stackView. topAnchor. constraint ( equalTo: view. topAnchor, constant: 10 ) ,
68- stackView. bottomAnchor. constraint ( equalTo: view. bottomAnchor, constant: - 10 ) ,
69- ] )
70- }
12+ var mainStackView : UIStackView !
7113
7214 override func updateViewConstraints( ) {
7315 super. updateViewConstraints ( )
@@ -78,9 +20,23 @@ class KeyboardViewController: UIInputViewController {
7820 override func viewDidLoad( ) {
7921 super. viewDidLoad ( )
8022
23+ mainStackView = UIStackView ( )
24+ mainStackView. axis = . vertical
25+ mainStackView. alignment = . fill
26+ mainStackView. spacing = 10
27+
28+ mainStackView. translatesAutoresizingMaskIntoConstraints = false
29+ view. addSubview ( mainStackView)
30+
31+ NSLayoutConstraint . activate ( [
32+ mainStackView. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 10 ) ,
33+ mainStackView. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 10 ) ,
34+ mainStackView. topAnchor. constraint ( equalTo: view. topAnchor, constant: 10 ) ,
35+ mainStackView. bottomAnchor. constraint ( equalTo: view. bottomAnchor, constant: - 10 ) ,
36+ ] )
37+
8138 startFcitx ( Bundle . main. bundlePath)
82- focusIn ( self . textDocumentProxy)
83- setupKeyboardLayout ( )
39+ focusIn ( self )
8440
8541 // Perform custom UI setup here
8642 self . nextKeyboardButton = UIButton ( type: . system)
@@ -121,4 +77,17 @@ class KeyboardViewController: UIInputViewController {
12177 self . nextKeyboardButton. setTitleColor ( textColor, for: [ ] )
12278 }
12379
80+ public func getView( ) -> UIStackView {
81+ return mainStackView
82+ }
83+
84+ public func keyPressed( _ key: String ) {
85+ if !processKey( key) {
86+ textDocumentProxy. insertText ( key)
87+ }
88+ }
89+
90+ public func commitString( _ commit: String ) {
91+ textDocumentProxy. insertText ( commit)
92+ }
12493}
0 commit comments