Skip to content

Commit f0f36d1

Browse files
authored
use FcitxProtocol to decouple UI (#10)
1 parent ba5262c commit f0f36d1

File tree

14 files changed

+180
-69
lines changed

14 files changed

+180
-69
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Lint
1919
run: |
2020
find src keyboard iosfrontend uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run
21-
swift-format lint -rs src keyboard iosfrontend
21+
swift-format lint -rs src keyboard iosfrontend uipanel
2222
2323
build:
2424
needs: lint

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ find_host_package(Gettext)
5858

5959
add_subdirectory(fcitx5)
6060

61+
add_subdirectory(protocol)
6162
add_subdirectory(iosfrontend)
6263
add_subdirectory(uipanel)
6364

iosfrontend/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
add_library(SwiftFrontend STATIC iosfrontend.swift)
22
set_target_properties(SwiftFrontend PROPERTIES Swift_MODULE_NAME SwiftFrontend)
33
target_compile_options(SwiftFrontend PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
4-
target_include_directories(SwiftFrontend PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
4+
target_include_directories(SwiftFrontend PUBLIC
5+
"${CMAKE_CURRENT_SOURCE_DIR}"
6+
"${PROJECT_BINARY_DIR}/protocol/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
7+
)
8+
target_link_libraries(SwiftFrontend FcitxProtocol)
59

610
_swift_generate_cxx_header(
711
SwiftFrontend
812
"${CMAKE_CURRENT_BINARY_DIR}/include/iosfrontend-swift.h"
913
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/iosfrontend.swift"
10-
SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR}"
14+
SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR};${PROJECT_BINARY_DIR}/protocol/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
1115
)
1216

1317
add_library(iosfrontend STATIC iosfrontend.cpp)

iosfrontend/iosfrontend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class IosInputContext : public InputContext {
5252
void forwardKeyImpl(const ForwardKeyEvent &key) override {}
5353
void updatePreeditImpl() override;
5454
void setClient(id client) { client_ = client; }
55+
id getClient() { return client_; }
5556

5657
private:
5758
IosFrontend *frontend_;

iosfrontend/iosfrontend.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import FcitxProtocol
12
import UIKit
23

34
public func commitStringAsync(_ clientPtr: UnsafeMutableRawPointer, _ commit: String) {
45
let client: AnyObject = Unmanaged.fromOpaque(clientPtr).takeUnretainedValue()
5-
guard let client = client as? UITextDocumentProxy else {
6+
guard let client = client as? FcitxProtocol else {
67
return
78
}
89
DispatchQueue.main.async {
9-
client.insertText(commit)
10+
client.commitString(commit)
1011
}
1112
}

keyboard/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ target_link_libraries(keyboard PRIVATE
1818
spell
1919
iosfrontend
2020
SwiftFrontend
21+
KeyboardUI
2122
uipanel
2223
"${ENGINE}"
2324
)
Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,15 @@
11
import Fcitx
2+
import FcitxProtocol
23
import OSLog
34
import UIKit
45

56
let 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
}

protocol/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_library(FcitxProtocol FcitxProtocol.swift)

protocol/FcitxProtocol.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import UIKit
2+
3+
public protocol FcitxProtocol {
4+
func getView() -> UIStackView
5+
func keyPressed(_ key: String)
6+
func commitString(_ string: String)
7+
}

uipanel/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
add_library(KeyboardUI STATIC
2+
keyboardui.swift
3+
Key.swift
4+
Keyboard.swift
5+
)
6+
set_target_properties(KeyboardUI PROPERTIES Swift_MODULE_NAME KeyboardUI)
7+
target_compile_options(KeyboardUI PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
8+
target_include_directories(KeyboardUI PUBLIC
9+
"${CMAKE_CURRENT_SOURCE_DIR}"
10+
"${PROJECT_BINARY_DIR}/protocol/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
11+
)
12+
target_link_libraries(KeyboardUI FcitxProtocol)
13+
14+
_swift_generate_cxx_header(
15+
KeyboardUI
16+
"${CMAKE_CURRENT_BINARY_DIR}/include/keyboardui-swift.h"
17+
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/keyboardui.swift"
18+
SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR};${PROJECT_BINARY_DIR}/protocol/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
19+
)
20+
121
add_library(uipanel STATIC uipanel.cpp)
22+
add_dependencies(uipanel KeyboardUI)
223
target_link_libraries(uipanel Fcitx5::Core)
24+
target_include_directories(uipanel PUBLIC
25+
"${CMAKE_CURRENT_BINARY_DIR}/include"
26+
)
327

428
configure_file(uipanel.conf.in.in uipanel.conf.in @ONLY)
529
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/uipanel.conf.in uipanel.conf)

0 commit comments

Comments
 (0)