Skip to content

Commit

Permalink
Add Xcode project and native targets (#142)
Browse files Browse the repository at this point in the history
* Match the Color API to the native one

* Extract the demo view to its own file

* Add Xcode project and native targets

* Fix Color

* Delete UISceneDelegate.swift

* Fix line length

* Tokamak Native → TokamakDemo Native

* Add exports for styles, fix typo
  • Loading branch information
j-f1 authored Jul 2, 2020
1 parent 38ca009 commit e652691
Show file tree
Hide file tree
Showing 21 changed files with 826 additions and 53 deletions.
26 changes: 13 additions & 13 deletions Sources/TokamakCore/Tokens/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ public struct Color: Equatable {
public let red: Double
public let green: Double
public let blue: Double
public let alpha: Double
public let opacity: Double
public let space: Space

public init(red: Double,
public init(_ colorSpace: Space = .sRGB,
red: Double,
green: Double,
blue: Double,
alpha: Double,
space: Space = .sRGB) {
opacity: Double) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
self.space = space
self.opacity = opacity
space = colorSpace
}

public static var white = Color(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
public static var black = Color(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
public static var red = Color(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
public static var green = Color(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
public static var blue = Color(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
public static var white = Color(red: 1.0, green: 1.0, blue: 1.0, opacity: 1.0)
public static var black = Color(red: 0.0, green: 0.0, blue: 0.0, opacity: 1.0)
public static var red = Color(red: 1.0, green: 0.0, blue: 0.0, opacity: 1.0)
public static var green = Color(red: 0.0, green: 1.0, blue: 0.0, opacity: 1.0)
public static var blue = Color(red: 0.0, green: 0.0, blue: 1.0, opacity: 1.0)
}

extension Color: ExpressibleByIntegerLiteral {
Expand All @@ -52,7 +52,7 @@ extension Color: ExpressibleByIntegerLiteral {
red = Double((bitMask & 0xFF0000) >> 16) / 255
green = Double((bitMask & 0x00FF00) >> 8) / 255
blue = Double(bitMask & 0x0000FF) / 255
alpha = 1
opacity = 1
space = .sRGB
}
}
Expand All @@ -73,7 +73,7 @@ extension Color {
self.red = Double(red) / 255
self.green = Double(green) / 255
self.blue = Double(blue) / 255
alpha = 1
opacity = 1
space = .sRGB
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakCore/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protocol GroupView: ParentView {}
views is made in the reconciler in `TokamakCore` based on their `body` type, host views have body
type `Never`. `ViewDeferredToRenderer` allows renderers to override that per-platform and render
host views as composite by providing their own `deferredBody` implementation.
*/
*/
public protocol ViewDeferredToRenderer {
var deferredBody: AnyView { get }
}
Expand Down
23 changes: 23 additions & 0 deletions Sources/TokamakDOM/Styles/Styles.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Jed Fox on 06/30/2020.
//

import TokamakCore

public typealias DefaultTextFieldStyle = TokamakCore.DefaultTextFieldStyle
public typealias PlainTextFieldStyle = TokamakCore.PlainTextFieldStyle
public typealias RoundedBorderTextFieldStyle = TokamakCore.RoundedBorderTextFieldStyle
public typealias SquareBorderTextFieldStyle = TokamakCore.SquareBorderTextFieldStyle
2 changes: 1 addition & 1 deletion Sources/TokamakDOM/Tokens/Tokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public typealias Color = TokamakCore.Color

extension Color: CustomStringConvertible {
public var description: String {
"rgb(\(red * 255), \(green * 255), \(blue * 255), \(alpha * 255))"
"rgb(\(red * 255), \(green * 255), \(blue * 255), \(opacity * 255))"
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakDemo/Counter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// Created by Max Desiatov on 14/02/2019.
//

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

public struct Counter: View {
@State public var count: Int
Expand Down
6 changes: 5 additions & 1 deletion Sources/TokamakDemo/EnvironmentDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
// Created by Carson Katri on 6/30/20.
//

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

struct EnvironmentDemo: View {
@Environment(\.font) var font: Font?

var body: some View {
if let font = font {
return Text("\(font)")
return Text("\(String(describing: font))")
} else {
return Text("`font` environment not set.")
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakDemo/ForEachDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

public struct ForEachDemo: View {
@State public var items: [Int] = []
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakDemo/SpacerDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// Created by Carson Katri on 6/29/20.
//

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

struct SpacerDemo: View {
var body: some View {
Expand Down
10 changes: 10 additions & 0 deletions Sources/TokamakDemo/TextDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

struct CustomModifier: ViewModifier {
func body(content: Content) -> some View {
Text("Whole new body!")
}
}

struct TextDemo: View {
var body: some View {
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakDemo/TextFieldDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakCore
import TokamakDOM
#endif

struct TextFieldDemo: View {
@State var text = ""
Expand Down
57 changes: 57 additions & 0 deletions Sources/TokamakDemo/TokamakDemo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2019-2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Jed Fox on 07/01/2020.
//

#if canImport(SwiftUI)
import SwiftUI
#else
import TokamakDOM
#endif

struct TokamakDemoView: View {
var body: some View {
ScrollView(showsIndicators: false) {
HStack {
Spacer()
}
VStack {
Counter(count: 5, limit: 15)
.padding()
.background(Color(red: 0.9, green: 0.9, blue: 0.9, opacity: 1.0))
.border(Color.red, width: 3)
ZStack {
Text("I'm on bottom")
Text("I'm forced to the top")
.zIndex(1)
Text("I'm on top")
}
.padding(20)
ForEachDemo()
TextDemo()
#if canImport(TokamakDOM)
SVGCircle()
.frame(width: 25, height: 25)
#endif
TextFieldDemo()
SpacerDemo()
Spacer()
Text("Forced to bottom.")
EnvironmentDemo()
.font(.system(size: 8))
}
}
}
}
38 changes: 1 addition & 37 deletions Sources/TokamakDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,7 @@ import TokamakDOM

let document = JSObjectRef.global.document.object!

struct CustomModifier: ViewModifier {
func body(content: Content) -> some View {
Text("Whole new body!")
}
}

let div = document.createElement!("div").object!
let renderer = DOMRenderer(
ScrollView(showsIndicators: false) {
HStack {
Spacer()
}
VStack {
Counter(count: 5, limit: 15)
.padding()
.background(Color(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0))
.border(Color.red, width: 3)
ZStack {
Text("I'm on bottom")
Text("I'm forced to the top")
.zIndex(1)
Text("I'm on top")
}
.padding(20)
ForEachDemo()
TextDemo()
SVGCircle()
.frame(width: 25, height: 25)
TextFieldDemo()
SpacerDemo()
Spacer()
Text("Forced to bottom.")
EnvironmentDemo()
.font(.system(size: 8))
}
},
div
)
let renderer = DOMRenderer(TokamakDemoView(), div)

_ = document.body.object!.appendChild!(div)
30 changes: 30 additions & 0 deletions TokamakDemo Native/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSMainStoryboardFile</key>
<string>macOS</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
25 changes: 25 additions & 0 deletions TokamakDemo Native/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
36 changes: 36 additions & 0 deletions TokamakDemo Native/NSAppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019-2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Jed Fox on 07/01/2020.
//

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!

func applicationDidFinishLaunching(_ aNotification: Notification) {
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false
)
window.isReleasedWhenClosed = false
window.center()
window.contentView = NSHostingView(rootView: TokamakDemoView())
window.makeKeyAndOrderFront(nil)
}
}
Loading

0 comments on commit e652691

Please sign in to comment.