Skip to content

Commit 24ab4a9

Browse files
committed
Add Example project
1 parent f900148 commit 24ab4a9

File tree

10 files changed

+1492
-0
lines changed

10 files changed

+1492
-0
lines changed

Examples/Example-macOS/Example-macOS.xcodeproj/project.pbxproj

+424
Large diffs are not rendered by default.

Examples/Example-macOS/Example-macOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Cocoa
2+
3+
@NSApplicationMain
4+
class AppDelegate: NSObject, NSApplicationDelegate {
5+
6+
@IBOutlet weak var window: NSWindow!
7+
8+
func applicationDidFinishLaunching(_ aNotification: Notification) {
9+
// Insert code here to initialize your application
10+
}
11+
12+
func applicationWillTerminate(_ aNotification: Notification) {
13+
// Insert code here to tear down your application
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "mac",
5+
"size" : "16x16",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "mac",
10+
"size" : "16x16",
11+
"scale" : "2x"
12+
},
13+
{
14+
"idiom" : "mac",
15+
"size" : "32x32",
16+
"scale" : "1x"
17+
},
18+
{
19+
"idiom" : "mac",
20+
"size" : "32x32",
21+
"scale" : "2x"
22+
},
23+
{
24+
"idiom" : "mac",
25+
"size" : "128x128",
26+
"scale" : "1x"
27+
},
28+
{
29+
"idiom" : "mac",
30+
"size" : "128x128",
31+
"scale" : "2x"
32+
},
33+
{
34+
"idiom" : "mac",
35+
"size" : "256x256",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "mac",
40+
"size" : "256x256",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "mac",
45+
"size" : "512x512",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "mac",
50+
"size" : "512x512",
51+
"scale" : "2x"
52+
}
53+
],
54+
"info" : {
55+
"version" : 1,
56+
"author" : "xcode"
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}

Examples/Example-macOS/Sources/Base.lproj/MainMenu.xib

+853
Large diffs are not rendered by default.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIconFile</key>
10+
<string></string>
11+
<key>CFBundleIdentifier</key>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>APPL</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
<key>LSMinimumSystemVersion</key>
24+
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
25+
<key>NSMainNibFile</key>
26+
<string>MainMenu</string>
27+
<key>NSPrincipalClass</key>
28+
<string>NSApplication</string>
29+
</dict>
30+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import AppKit
2+
3+
class StringCollectionViewItem: NSCollectionViewItem {
4+
let label = NSTextField()
5+
6+
override func loadView() {
7+
view = NSView(frame: NSRect(x: 0, y: 0, width: 60, height: 54))
8+
}
9+
10+
override func viewDidLoad() {
11+
super.viewDidLoad()
12+
label.font = NSFont.systemFont(ofSize: 40)
13+
view.addSubview(label)
14+
label.alignment = .center
15+
label.translatesAutoresizingMaskIntoConstraints = false
16+
17+
let constraints = [
18+
label.topAnchor.constraint(equalTo: view.topAnchor),
19+
label.bottomAnchor.constraint(equalTo: view.bottomAnchor),
20+
label.leadingAnchor.constraint(equalTo: view.leadingAnchor),
21+
label.trailingAnchor.constraint(equalTo: view.trailingAnchor)
22+
]
23+
NSLayoutConstraint.activate(constraints)
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Cocoa
2+
import DifferenceKit
3+
4+
extension String: Differentiable { }
5+
6+
class ViewController: NSViewController {
7+
@IBOutlet weak var collectionView: NSCollectionView!
8+
@IBOutlet weak var tableView: NSTableView!
9+
10+
var strings: [String] = {
11+
var strings: [String] = []
12+
for i in 0..<40 {
13+
strings.append(ViewController.randomEmoji)
14+
}
15+
return strings
16+
}()
17+
18+
override func awakeFromNib() {
19+
super.awakeFromNib()
20+
collectionView.register(StringCollectionViewItem.self, forItemWithIdentifier: .init("StringItem"))
21+
}
22+
23+
@IBAction func shufflePress(_ sender: Any) {
24+
let shuffled = strings.shuffled()
25+
let changeSet = StagedChangeset(source: strings, target: shuffled)
26+
collectionView.reload(using: changeSet, setData: { _ in })
27+
28+
tableView.reload(using: changeSet, with: [], setData: { self.strings = $0 })
29+
}
30+
31+
static var randomEmoji: String {
32+
let range = [UInt32](0x1F601...0x1F64F)
33+
let ascii = range[Int(drand48() * (Double(range.count)))]
34+
let emoji = UnicodeScalar(ascii)?.description
35+
return emoji!
36+
}
37+
}
38+
39+
extension ViewController: NSCollectionViewDataSource {
40+
func numberOfSections(in collectionView: NSCollectionView) -> Int {
41+
return 1
42+
}
43+
44+
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
45+
return strings.count
46+
}
47+
48+
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
49+
let item = collectionView.makeItem(withIdentifier: .init("StringItem"), for: indexPath) as! StringCollectionViewItem
50+
item.label.stringValue = strings[indexPath.item]
51+
return item
52+
}
53+
}
54+
55+
extension ViewController: NSTableViewDataSource, NSTableViewDelegate {
56+
func numberOfRows(in tableView: NSTableView) -> Int {
57+
return strings.count
58+
}
59+
60+
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
61+
let view = tableView.makeView(withIdentifier: .init("StringCell"), owner: tableView) as! NSTableCellView
62+
view.textField?.stringValue = strings[row]
63+
return view
64+
}
65+
}

0 commit comments

Comments
 (0)