|
| 1 | +# CandidateUI |
| 2 | + |
| 3 | +Copyright (c) 2022 and onwards The McBopomofo Authors. |
| 4 | + |
| 5 | +CandidateUI provides the Cocoa candidate window implementation shared by the |
| 6 | +McBopomofo input method targets. It bundles a base `CandidateController` with |
| 7 | +ready-to-use horizontal and vertical variants that expose an Objective-C |
| 8 | +compatible delegate API, making it easy to embed the UI in IMK and AppKit based |
| 9 | +projects. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Horizontal and vertical `NSWindowController` subclasses ready for IMK/AppKit |
| 14 | + hosts |
| 15 | +- Delegate driven data source with optional readings and explanations |
| 16 | +- Configurable key labels, candidate fonts, tooltip text, and accessibility |
| 17 | + notifications |
| 18 | +- Built-in pagination and highlight navigation helpers for keyboard control |
| 19 | +- Objective-C bridging annotations for seamless use from mixed Swift/ObjC code |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +- macOS 10.15 or later (AppKit based host process) |
| 24 | +- Swift 5.9 or newer (Xcode 15.3 or newer) |
| 25 | +- Interactions must occur on the main queue because the window is UI backed |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +Add the package dependency to your `Package.swift`: |
| 30 | + |
| 31 | +```swift |
| 32 | +dependencies: [ |
| 33 | + .package(path: "../Packages/CandidateUI"), |
| 34 | +], |
| 35 | +targets: [ |
| 36 | + .target( |
| 37 | + name: "YourTarget", |
| 38 | + dependencies: ["CandidateUI"] |
| 39 | + ), |
| 40 | +] |
| 41 | +``` |
| 42 | + |
| 43 | +When integrating outside the McBopomofo repository, replace the local path with |
| 44 | +the corresponding Git URL and version requirement. |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +Choose either `HorizontalCandidateController` or `VerticalCandidateController` |
| 49 | +depending on the layout you want. |
| 50 | + |
| 51 | +### 1. Implement the delegate |
| 52 | + |
| 53 | +Adopt `CandidateControllerDelegate` to expose candidates, optional readings, |
| 54 | +and selection callbacks: |
| 55 | + |
| 56 | +```swift |
| 57 | +@MainActor |
| 58 | +final class DemoDelegate: NSObject, CandidateControllerDelegate { |
| 59 | + private let entries = ["ZhuYin", "Input Method", "McBopomofo"] |
| 60 | + private let readings = ["zhu4", "shu1", "mai4"] |
| 61 | + |
| 62 | + func candidateCountForController(_ controller: CandidateController) -> UInt { |
| 63 | + UInt(entries.count) |
| 64 | + } |
| 65 | + |
| 66 | + func candidateController(_ controller: CandidateController, candidateAtIndex index: UInt) -> String { |
| 67 | + entries[Int(index)] |
| 68 | + } |
| 69 | + |
| 70 | + func candidateController(_ controller: CandidateController, readingAtIndex index: UInt) -> String? { |
| 71 | + readings[Int(index)] |
| 72 | + } |
| 73 | + |
| 74 | + func candidateController( |
| 75 | + _ controller: CandidateController, |
| 76 | + requestExplanationFor candidate: String, |
| 77 | + reading: String |
| 78 | + ) -> String? { |
| 79 | + nil |
| 80 | + } |
| 81 | + |
| 82 | + func candidateController(_ controller: CandidateController, didSelectCandidateAtIndex index: UInt) { |
| 83 | + print("Selected candidate: \(entries[Int(index)])") |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### 2. Configure the controller |
| 89 | + |
| 90 | +Instantiate the controller on the main thread, assign the delegate, and adjust |
| 91 | +appearance hints. Call `reloadData()` after mutating underlying candidate data. |
| 92 | + |
| 93 | +```swift |
| 94 | +let controller = VerticalCandidateController() |
| 95 | +controller.delegate = DemoDelegate() |
| 96 | +controller.keyLabels = ["1", "2", "3"].map { CandidateKeyLabel(key: $0, displayedText: $0) } |
| 97 | +controller.keyLabelFont = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular) |
| 98 | +controller.candidateFont = NSFont.systemFont(ofSize: 18) |
| 99 | +controller.tooltip = NSLocalizedString("Press arrow keys to browse candidates", comment: "Candidate window tooltip") |
| 100 | +controller.set(windowTopLeftPoint: NSPoint(x: 320, y: 400), bottomOutOfScreenAdjustmentHeight: 24) |
| 101 | +controller.visible = true |
| 102 | +``` |
| 103 | + |
| 104 | +`set(windowTopLeftPoint:bottomOutOfScreenAdjustmentHeight:)` keeps the window |
| 105 | +visible by nudging it back on screen when it would otherwise cross a screen |
| 106 | +edge. |
| 107 | + |
| 108 | +### 3. React to keyboard events |
| 109 | + |
| 110 | +Use the navigation helpers to mirror the IMK key handling semantics: |
| 111 | + |
| 112 | +```swift |
| 113 | +@discardableResult |
| 114 | +func handleCandidateNavigation(event: NSEvent, controller: CandidateController) -> Bool { |
| 115 | + guard let specialKey = event.specialKey else { return false } |
| 116 | + switch specialKey { |
| 117 | + case .rightArrow: return controller.showNextPage() |
| 118 | + case .leftArrow: return controller.showPreviousPage() |
| 119 | + case .downArrow: return controller.highlightNextCandidate() |
| 120 | + case .upArrow: return controller.highlightPreviousCandidate() |
| 121 | + default: return false |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +For number keys, call `candidateIndexAtKeyLabelIndex(_:)` to translate the |
| 127 | +pressed label into a candidate index (`UInt.max` signals “not available”). When |
| 128 | +a new selection is confirmed, invoke the delegate’s |
| 129 | +`candidateController(_:didSelectCandidateAtIndex:)` method to commit the choice |
| 130 | +back to your input pipeline. |
| 131 | + |
| 132 | +### Objective-C integration |
| 133 | + |
| 134 | +All public controllers and protocols are annotated with `@objc`. Import the |
| 135 | +generated umbrella header (for example through `McBopomofo-Bridging-Header.h`) |
| 136 | +to drive the UI from Objective-C or Objective-C++ sources. |
| 137 | + |
| 138 | +## Testing |
| 139 | + |
| 140 | +Run the package tests with: |
| 141 | + |
| 142 | +```bash |
| 143 | +swift test --package-path Packages/CandidateUI |
| 144 | +``` |
| 145 | + |
| 146 | +The test suite covers pagination, selection handling, and layout options for |
| 147 | +both horizontal and vertical controllers. |
| 148 | + |
| 149 | +## License |
| 150 | + |
| 151 | +This package is released under the MIT license. The full text is available in |
| 152 | +the source file headers. |
0 commit comments