Skip to content

Commit b33f79b

Browse files
committed
Merge branch 'master' of github.com:openvanilla/McBopomofo
2 parents b66a6d1 + 774cd73 commit b33f79b

4 files changed

Lines changed: 325 additions & 6 deletions

File tree

Packages/CandidateUI/Package.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ let package = Package(
88
products: [
99
.library(
1010
name: "CandidateUI",
11-
targets: ["CandidateUI"])
11+
targets: ["CandidateUI"]),
12+
.executable(
13+
name: "CandidatePreview",
14+
targets: ["CandidatePreview"]
15+
)
1216
],
1317
dependencies: [
1418
// Dependencies declare other packages that this package depends on.
@@ -20,6 +24,10 @@ let package = Package(
2024
.target(
2125
name: "CandidateUI",
2226
dependencies: []),
27+
.executableTarget(
28+
name: "CandidatePreview",
29+
dependencies: ["CandidateUI"]
30+
),
2331
.testTarget(
2432
name: "CandidateUITests",
2533
dependencies: ["CandidateUI"]),
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+

Packages/TooltipUI/Package.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.4
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "TooltipUI",
8+
platforms: [
9+
.macOS(.v10_15)
10+
],
811
products: [
912
// Products define the executables and libraries a package produces, and make them visible to other packages.
1013
.library(
1114
name: "TooltipUI",
12-
targets: ["TooltipUI"]),
15+
targets: ["TooltipUI"]
16+
),
17+
.executable(
18+
name: "TooltipPreview",
19+
targets: ["TooltipPreview"]
20+
)
1321
],
1422
dependencies: [
1523
// Dependencies declare other packages that this package depends on.
1624
// .package(url: /* package url */, from: "1.0.0"),
1725
],
1826
targets: [
19-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20-
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2127
.target(
2228
name: "TooltipUI",
23-
dependencies: []),
29+
dependencies: []
30+
),
31+
.executableTarget(
32+
name: "TooltipPreview",
33+
dependencies: ["TooltipUI"]
34+
),
2435
]
2536
)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import Cocoa
2+
import TooltipUI
3+
4+
final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
5+
private var tooltipController: TooltipController?
6+
7+
var tooltipLocation: NSPoint {
8+
var point = NSPoint(x: 100, y: 100)
9+
if let screen = NSScreen.main {
10+
let frame = screen.visibleFrame
11+
point = NSPoint(x: frame.minX + 300, y: frame.maxY - 20)
12+
}
13+
return point
14+
}
15+
16+
var text = "Hello, World!"
17+
18+
@objc func showTooltip() {
19+
tooltipController?.show(tooltip: text, at: tooltipLocation)
20+
}
21+
22+
@objc func hideTooltip() {
23+
tooltipController?.hide()
24+
}
25+
26+
27+
func applicationDidFinishLaunching(_ notification: Notification) {
28+
let app = NSApplication.shared
29+
app.setActivationPolicy(.regular)
30+
31+
let controller = TooltipController()
32+
self.tooltipController = controller
33+
34+
// Create a minimal menu so the app can quit.
35+
let mainMenu = NSMenu()
36+
let appMenuItem = NSMenuItem()
37+
mainMenu.addItem(appMenuItem)
38+
39+
let appMenu = NSMenu()
40+
let showItem = NSMenuItem(
41+
title: "Show Tooltip",
42+
action: #selector(showTooltip),
43+
keyEquivalent: ""
44+
)
45+
showItem.target = self
46+
appMenu.addItem(showItem)
47+
let hideItem = NSMenuItem(
48+
title: "Hide Tooltip",
49+
action: #selector(hideTooltip),
50+
keyEquivalent: ""
51+
)
52+
hideItem.target = self
53+
appMenu.addItem(hideItem)
54+
appMenu.addItem(NSMenuItem.separator())
55+
56+
let quitItem = NSMenuItem(title: "Quit TooltipDemoApp", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
57+
appMenu.addItem(quitItem)
58+
appMenuItem.submenu = appMenu
59+
app.mainMenu = mainMenu
60+
61+
app.activate(ignoringOtherApps: true)
62+
showTooltip()
63+
}
64+
65+
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
66+
false
67+
}
68+
}
69+
70+
let delegate = AppDelegate()
71+
72+
@main
73+
struct TooltipPreviewMain {
74+
static func main() {
75+
let app = NSApplication.shared
76+
app.delegate = delegate
77+
app.run()
78+
}
79+
}

0 commit comments

Comments
 (0)