|
| 1 | +import Cocoa |
| 2 | +import CandidateUI |
| 3 | + |
| 4 | +typealias Candidate = (candidate: String, reading: String) |
| 5 | + |
| 6 | +class AppDelegate: NSObject, NSApplicationDelegate { |
| 7 | + lazy var horizontal: HorizontalCandidateController = .init() |
| 8 | + lazy var vertical: VerticalCandidateController = .init() |
| 9 | + var candidates: [Candidate] = [] |
| 10 | + let simpleCandidates: [Candidate] = [ |
| 11 | + ("天", "ㄊㄧㄢ"), |
| 12 | + ("生", "ㄕㄥ"), |
| 13 | + ("我", "ㄨㄛˇ"), |
| 14 | + ("才", "ㄘㄞˊ"), |
| 15 | + ("必", "ㄅㄧˋ"), |
| 16 | + ("有", "ㄧㄡˇ"), |
| 17 | + ("用", "ㄩㄥˋ"), |
| 18 | + ] |
| 19 | + |
| 20 | + let longCandidates: [Candidate] = [ |
| 21 | + ("天", "ㄊㄧㄢ"), |
| 22 | + ("生", "ㄕㄥ"), |
| 23 | + ("我", "ㄨㄛˇ"), |
| 24 | + ("才", "ㄘㄞˊ"), |
| 25 | + ("必", "ㄅㄧˋ"), |
| 26 | + ("有", "ㄧㄡˇ"), |
| 27 | + ("用", "ㄩㄥˋ"), |
| 28 | + ("千", "ㄑㄧㄢ"), |
| 29 | + ("金", "ㄐㄧㄣ"), |
| 30 | + ("散", "ㄙㄢˋ"), |
| 31 | + ("盡", "ㄐㄧㄣˋ"), |
| 32 | + ] |
| 33 | + |
| 34 | + var location: NSPoint { |
| 35 | + var point = NSPoint(x: 100, y: 100) |
| 36 | + if let screen = NSScreen.main { |
| 37 | + let frame = screen.visibleFrame |
| 38 | + point = NSPoint(x: frame.minX + 300, y: frame.maxY - 20) |
| 39 | + } |
| 40 | + return point |
| 41 | + } |
| 42 | + |
| 43 | + @objc |
| 44 | + func showHorizontal() { |
| 45 | + vertical.visible = false |
| 46 | + horizontal.tooltip = "" |
| 47 | + candidates = simpleCandidates |
| 48 | + horizontal.reloadData() |
| 49 | + horizontal |
| 50 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 51 | + horizontal.visible = true |
| 52 | + } |
| 53 | + |
| 54 | + @objc |
| 55 | + func showHorizontalWithPages() { |
| 56 | + vertical.visible = false |
| 57 | + horizontal.tooltip = "" |
| 58 | + candidates = longCandidates |
| 59 | + horizontal.reloadData() |
| 60 | + horizontal |
| 61 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 62 | + horizontal.visible = true |
| 63 | + } |
| 64 | + |
| 65 | + @objc |
| 66 | + func showHorizontalWithTooltip() { |
| 67 | + vertical.visible = false |
| 68 | + horizontal.tooltip = "將進酒" |
| 69 | + candidates = longCandidates |
| 70 | + horizontal.reloadData() |
| 71 | + horizontal |
| 72 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 73 | + horizontal.visible = true |
| 74 | + } |
| 75 | + |
| 76 | + @objc |
| 77 | + func showVertical() { |
| 78 | + horizontal.visible = false |
| 79 | + vertical.tooltip = "" |
| 80 | + candidates = simpleCandidates |
| 81 | + vertical.reloadData() |
| 82 | + vertical |
| 83 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 84 | + vertical.visible = true |
| 85 | + } |
| 86 | + |
| 87 | + @objc |
| 88 | + func showVerticalWithPages() { |
| 89 | + horizontal.visible = false |
| 90 | + vertical.tooltip = "" |
| 91 | + candidates = longCandidates |
| 92 | + vertical.reloadData() |
| 93 | + vertical |
| 94 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 95 | + vertical.visible = true |
| 96 | + } |
| 97 | + |
| 98 | + @objc |
| 99 | + func showVerticalWithTooltip() { |
| 100 | + horizontal.visible = false |
| 101 | + vertical.tooltip = "將進酒" |
| 102 | + candidates = longCandidates |
| 103 | + vertical.reloadData() |
| 104 | + vertical |
| 105 | + .set(windowTopLeftPoint: location, bottomOutOfScreenAdjustmentHeight: 0) |
| 106 | + vertical.visible = true |
| 107 | + } |
| 108 | + |
| 109 | + @objc |
| 110 | + func applicationDidFinishLaunching(_ notification: Notification) { |
| 111 | + let app = NSApplication.shared |
| 112 | + app.setActivationPolicy(.regular) |
| 113 | + horizontal.delegate = self |
| 114 | + vertical.delegate = self |
| 115 | + |
| 116 | + // Create a minimal menu so the app can quit. |
| 117 | + let mainMenu = NSMenu() |
| 118 | + let appMenuItem = NSMenuItem() |
| 119 | + mainMenu.addItem(appMenuItem) |
| 120 | + |
| 121 | + let appMenu = NSMenu() |
| 122 | + var item = NSMenuItem( |
| 123 | + title: "Show Horizontal Candidates (Simple)", |
| 124 | + action: #selector(showHorizontal), |
| 125 | + keyEquivalent: "" |
| 126 | + ) |
| 127 | + item.target = self |
| 128 | + appMenu.addItem(item) |
| 129 | + |
| 130 | + item = NSMenuItem( |
| 131 | + title: "Show Horizontal Candidates (Paged)", |
| 132 | + action: #selector(showHorizontalWithPages), |
| 133 | + keyEquivalent: "" |
| 134 | + ) |
| 135 | + item.target = self |
| 136 | + appMenu.addItem(item) |
| 137 | + |
| 138 | + item = NSMenuItem( |
| 139 | + title: "Show Horizontal Candidates (Tooltip)", |
| 140 | + action: #selector(showHorizontalWithTooltip), |
| 141 | + keyEquivalent: "" |
| 142 | + ) |
| 143 | + item.target = self |
| 144 | + appMenu.addItem(item) |
| 145 | + |
| 146 | + appMenu.addItem(NSMenuItem.separator()) |
| 147 | + |
| 148 | + item = NSMenuItem( |
| 149 | + title: "Show Vertical Candidates (Simple)", |
| 150 | + action: #selector(showVertical), |
| 151 | + keyEquivalent: "" |
| 152 | + ) |
| 153 | + item.target = self |
| 154 | + appMenu.addItem(item) |
| 155 | + |
| 156 | + item = NSMenuItem( |
| 157 | + title: "Show Vertical Candidates (Paged)", |
| 158 | + action: #selector(showVerticalWithPages), |
| 159 | + keyEquivalent: "" |
| 160 | + ) |
| 161 | + item.target = self |
| 162 | + appMenu.addItem(item) |
| 163 | + |
| 164 | + item = NSMenuItem( |
| 165 | + title: "Show Vertical Candidates (Tooltip)", |
| 166 | + action: #selector(showVerticalWithTooltip), |
| 167 | + keyEquivalent: "" |
| 168 | + ) |
| 169 | + item.target = self |
| 170 | + appMenu.addItem(item) |
| 171 | + |
| 172 | + appMenu.addItem(NSMenuItem.separator()) |
| 173 | + |
| 174 | + let quitItem = NSMenuItem(title: "Quit TooltipDemoApp", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") |
| 175 | + appMenu.addItem(quitItem) |
| 176 | + appMenuItem.submenu = appMenu |
| 177 | + app.mainMenu = mainMenu |
| 178 | + |
| 179 | + app.activate(ignoringOtherApps: true) |
| 180 | + showHorizontal() |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +extension AppDelegate: CandidateControllerDelegate { |
| 186 | + |
| 187 | + func candidateCountForController(_ controller: CandidateUI.CandidateController) -> UInt { |
| 188 | + UInt(candidates.count) |
| 189 | + } |
| 190 | + |
| 191 | + func candidateController(_ controller: CandidateUI.CandidateController, candidateAtIndex index: UInt) -> String { |
| 192 | + candidates[Int(index)].candidate |
| 193 | + } |
| 194 | + |
| 195 | + func candidateController(_ controller: CandidateUI.CandidateController, readingAtIndex index: UInt) -> String? { |
| 196 | + candidates[Int(index)].candidate |
| 197 | + } |
| 198 | + |
| 199 | + func candidateController(_ controller: CandidateUI.CandidateController, requestExplanationFor candidate: String, reading: String) -> String? { |
| 200 | + nil |
| 201 | + } |
| 202 | + |
| 203 | + func candidateController( |
| 204 | + _ controller: CandidateUI.CandidateController, |
| 205 | + didSelectCandidateAtIndex index: UInt |
| 206 | + ) { |
| 207 | + } |
| 208 | + |
| 209 | +} |
| 210 | + |
| 211 | +let delegate = AppDelegate() |
| 212 | + |
| 213 | +@main |
| 214 | +struct TooltipPreviewMain { |
| 215 | + static func main() { |
| 216 | + let app = NSApplication.shared |
| 217 | + app.delegate = delegate |
| 218 | + app.run() |
| 219 | + } |
| 220 | +} |
| 221 | + |
0 commit comments