Skip to content

Commit 8b814c5

Browse files
committed
💻 Add macOS target
1 parent d6e8461 commit 8b814c5

File tree

11 files changed

+523
-3
lines changed

11 files changed

+523
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// AppDelegate.swift
3+
// SwiftUI2048-mac
4+
//
5+
// Created by Hongyu on 6/9/19.
6+
// Copyright © 2019 Cyandev. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
import SwiftUI
11+
12+
@NSApplicationMain
13+
class AppDelegate: NSObject, NSApplicationDelegate {
14+
15+
var window: NSWindow!
16+
17+
func applicationDidFinishLaunching(_ aNotification: Notification) {
18+
window = NSWindow(
19+
contentRect: NSRect(x: 0, y: 0, width: 600, height: 600),
20+
styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView],
21+
backing: .buffered, defer: false)
22+
window.titlebarAppearsTransparent = true
23+
window.isMovableByWindowBackground = true
24+
window.center()
25+
window.setFrameAutosaveName("Main Window")
26+
27+
window.contentView = GameMainHostingView()
28+
29+
window.makeKeyAndOrderFront(nil)
30+
window.makeFirstResponder(window.contentView)
31+
}
32+
33+
func applicationWillTerminate(_ aNotification: Notification) {
34+
}
35+
36+
@objc func newGame(_ sender: Any?) {
37+
(window.contentView as? GameMainHostingView)?.newGame()
38+
}
39+
40+
}
41+
Lines changed: 58 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14810.11" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14810.11"/>
5+
</dependencies>
6+
<scenes>
7+
<!--Application-->
8+
<scene sceneID="JPo-4y-FX3">
9+
<objects>
10+
<application id="hnw-xV-0zn" sceneMemberID="viewController">
11+
<menu key="mainMenu" title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
12+
<items>
13+
<menuItem title="2048" id="1Xt-HY-uBw">
14+
<modifierMask key="keyEquivalentModifierMask"/>
15+
<menu key="submenu" title="2048" systemMenu="apple" id="uQy-DD-JDr">
16+
<items>
17+
<menuItem title="About 2048" id="5kV-Vb-QxS">
18+
<modifierMask key="keyEquivalentModifierMask"/>
19+
<connections>
20+
<action selector="orderFrontStandardAboutPanel:" target="Ady-hI-5gd" id="Exp-CZ-Vem"/>
21+
</connections>
22+
</menuItem>
23+
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
24+
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
25+
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
26+
<menuItem title="Services" id="NMo-om-nkz">
27+
<modifierMask key="keyEquivalentModifierMask"/>
28+
<menu key="submenu" title="Services" systemMenu="services" id="hz9-B4-Xy5"/>
29+
</menuItem>
30+
<menuItem isSeparatorItem="YES" id="4je-JR-u6R"/>
31+
<menuItem title="Hide 2048" keyEquivalent="h" id="Olw-nP-bQN">
32+
<connections>
33+
<action selector="hide:" target="Ady-hI-5gd" id="PnN-Uc-m68"/>
34+
</connections>
35+
</menuItem>
36+
<menuItem title="Hide Others" keyEquivalent="h" id="Vdr-fp-XzO">
37+
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
38+
<connections>
39+
<action selector="hideOtherApplications:" target="Ady-hI-5gd" id="VT4-aY-XCT"/>
40+
</connections>
41+
</menuItem>
42+
<menuItem title="Show All" id="Kd2-mp-pUS">
43+
<modifierMask key="keyEquivalentModifierMask"/>
44+
<connections>
45+
<action selector="unhideAllApplications:" target="Ady-hI-5gd" id="Dhg-Le-xox"/>
46+
</connections>
47+
</menuItem>
48+
<menuItem isSeparatorItem="YES" id="kCx-OE-vgT"/>
49+
<menuItem title="Quit 2048" keyEquivalent="q" id="4sb-4s-VLi">
50+
<connections>
51+
<action selector="terminate:" target="Ady-hI-5gd" id="Te7-pn-YzF"/>
52+
</connections>
53+
</menuItem>
54+
</items>
55+
</menu>
56+
</menuItem>
57+
<menuItem title="File" id="dMs-cI-mzQ">
58+
<modifierMask key="keyEquivalentModifierMask"/>
59+
<menu key="submenu" title="File" id="bib-Uj-vzu">
60+
<items>
61+
<menuItem title="New Game" keyEquivalent="n" id="hy8-5G-caG">
62+
<connections>
63+
<action selector="newGame:" target="Ady-hI-5gd" id="H3z-3m-aXf"/>
64+
</connections>
65+
</menuItem>
66+
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
67+
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
68+
<connections>
69+
<action selector="performClose:" target="Ady-hI-5gd" id="HmO-Ls-i7Q"/>
70+
</connections>
71+
</menuItem>
72+
</items>
73+
</menu>
74+
</menuItem>
75+
<menuItem title="Window" id="aUF-d1-5bR">
76+
<modifierMask key="keyEquivalentModifierMask"/>
77+
<menu key="submenu" title="Window" systemMenu="window" id="Td7-aD-5lo">
78+
<items>
79+
<menuItem title="Minimize" keyEquivalent="m" id="OY7-WF-poV">
80+
<connections>
81+
<action selector="performMiniaturize:" target="Ady-hI-5gd" id="VwT-WD-YPe"/>
82+
</connections>
83+
</menuItem>
84+
<menuItem title="Zoom" id="R4o-n2-Eq4">
85+
<modifierMask key="keyEquivalentModifierMask"/>
86+
<connections>
87+
<action selector="performZoom:" target="Ady-hI-5gd" id="DIl-cC-cCs"/>
88+
</connections>
89+
</menuItem>
90+
<menuItem isSeparatorItem="YES" id="eu3-7i-yIM"/>
91+
<menuItem title="Bring All to Front" id="LE2-aR-0XJ">
92+
<modifierMask key="keyEquivalentModifierMask"/>
93+
<connections>
94+
<action selector="arrangeInFront:" target="Ady-hI-5gd" id="DRN-fu-gQh"/>
95+
</connections>
96+
</menuItem>
97+
</items>
98+
</menu>
99+
</menuItem>
100+
<menuItem title="Help" id="wpr-3q-Mcd">
101+
<modifierMask key="keyEquivalentModifierMask"/>
102+
<menu key="submenu" title="Help" systemMenu="help" id="F2S-fz-NVQ">
103+
<items>
104+
<menuItem title="2048 Help" keyEquivalent="?" id="FKE-Sm-Kum">
105+
<connections>
106+
<action selector="showHelp:" target="Ady-hI-5gd" id="y7X-2Q-9no"/>
107+
</connections>
108+
</menuItem>
109+
</items>
110+
</menu>
111+
</menuItem>
112+
</items>
113+
</menu>
114+
<connections>
115+
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
116+
</connections>
117+
</application>
118+
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="_048_macOS_" customModuleProvider="target"/>
119+
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
120+
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
121+
</objects>
122+
<point key="canvasLocation" x="75" y="0.0"/>
123+
</scene>
124+
</scenes>
125+
</document>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// GameMainHostingView.swift
3+
// 2048
4+
//
5+
// Created by Hongyu on 6/9/19.
6+
// Copyright © 2019 Cyandev. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct GameViewWrapper : View {
12+
13+
fileprivate let gameLogic: GameLogic
14+
15+
var body: some View {
16+
GameView()
17+
.environmentObject(gameLogic)
18+
}
19+
20+
}
21+
22+
class GameMainHostingView: NSHostingView<GameViewWrapper> {
23+
24+
fileprivate var gameLogic: GameLogic!
25+
26+
init() {
27+
gameLogic = GameLogic()
28+
super.init(rootView: GameViewWrapper(gameLogic: gameLogic))
29+
}
30+
31+
@objc required init?(coder aDecoder: NSCoder) {
32+
fatalError("init(coder:) has not been implemented")
33+
}
34+
35+
required init(rootView: GameViewWrapper) {
36+
fatalError("init(rootView:) should not be called directly")
37+
}
38+
39+
override var acceptsFirstResponder: Bool {
40+
return true
41+
}
42+
43+
override var mouseDownCanMoveWindow: Bool {
44+
return true
45+
}
46+
47+
override func keyDown(with event: NSEvent) {
48+
guard !event.isARepeat else {
49+
return
50+
}
51+
52+
switch event.keyCode {
53+
case 125:
54+
self.gameLogic.move(.down)
55+
return
56+
case 123:
57+
self.gameLogic.move(.left)
58+
return
59+
case 124:
60+
self.gameLogic.move(.right)
61+
return
62+
case 126:
63+
self.gameLogic.move(.up)
64+
return
65+
default:
66+
return
67+
}
68+
}
69+
70+
func newGame() {
71+
gameLogic.newGame()
72+
}
73+
74+
}

‎SwiftUI2048-mac/Info.plist‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</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>NSHumanReadableCopyright</key>
26+
<string>Copyright © 2019 Cyandev. All rights reserved.</string>
27+
<key>NSMainStoryboardFile</key>
28+
<string>Main</string>
29+
<key>NSPrincipalClass</key>
30+
<string>NSApplication</string>
31+
<key>NSSupportsAutomaticTermination</key>
32+
<true/>
33+
<key>NSSupportsSuddenTermination</key>
34+
<true/>
35+
</dict>
36+
</plist>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
</plist>

0 commit comments

Comments
 (0)