Skip to content

Commit

Permalink
Initial drop
Browse files Browse the repository at this point in the history
...
  • Loading branch information
helje5 committed Jun 26, 2019
1 parent 3822280 commit e8bb09e
Show file tree
Hide file tree
Showing 158 changed files with 12,028 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# hh
.DS_Store
Package.resolved

6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Legal

By submitting a pull request, you represent that you have the right to license
your contribution to Apple and the community, and agree by submitting the patch
that your contributions are licensed under the Apache 2.0 license.

5 changes: 5 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The source code is distributed under the terms of the Bad Code License.
You are forbidden from distributing software containing the code to end
users, because it is bad.

Questions? wrong@alwaysrightinstitute.com.
28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.1

import PackageDescription

let package = Package(
name: "SwiftWebUI",
products: [
.library(name: "SwiftWebUI", targets: [ "SwiftWebUI" ])
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git",
from: "2.3.0"),
.package(url: "https://github.com/SwiftWebResources/SemanticUI-Swift.git",
from: "2.3.3"),
.package(url: "https://github.com/wickwirew/Runtime.git",
from: "2.1.0"),
.package(url: "https://github.com/onmyway133/SwiftHash.git",
from: "2.0.2")

],
targets: [
.target(name: "SwiftWebUI",
dependencies: [
"NIO", "NIOHTTP1", "NIOConcurrencyHelpers",
"SwiftHash", "Runtime", "SemanticUI"
])
]
)
40 changes: 40 additions & 0 deletions Sources/SwiftWebUI/Environment/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Environment.swift
// SwiftWebUI
//
// Created by Helge Heß on 10.06.19.
// Copyright © 2019 Helge Heß. All rights reserved.
//

@propertyDelegate
public struct Environment<Value>: DynamicViewProperty {

// (\EnvironmentValues.isEnabled) or just (\.isEnabled)
let keyPath : KeyPath<EnvironmentValues, Value>

public init(_ keyPath: KeyPath<EnvironmentValues, Value>) {
self.keyPath = keyPath
}

// DynamicViewProperty
// update => this updates the value from the environment I think
// how do we receive the environment? Is that a global?

private var _value: Value?

public var value: Value {
guard let value = _value else {
fatalError("you cannot access @Environment outside of `body`")
}
return value
}

public mutating func update() {
guard let context = DynamicViewPropertyHelpers.currentContext else {
assertionFailure("you cannot access @Environment outside of `body`")
return
}

_value = context.environment[keyPath: keyPath]
}
}
177 changes: 177 additions & 0 deletions Sources/SwiftWebUI/Environment/EnvironmentKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
//
// EnvironmentKey.swift
// SwiftWebUI
//
// Created by Helge Heß on 10.06.19.
// Copyright © 2019 Helge Heß. All rights reserved.
//

public protocol EnvironmentKey {
associatedtype Value
static var defaultValue: Self.Value { get }
}

enum SizeCategoryEnvironmentKey: EnvironmentKey {
static var defaultValue: ContentSizeCategory { .medium }
}

enum IsEnabledEnvironmentKey: EnvironmentKey {
static var defaultValue: Bool { true }
}

enum FontEnvironmentKey: EnvironmentKey {
static var defaultValue: Font? { return nil }
}

enum ForegroundColorEnvironmentKey: EnvironmentKey {
static var defaultValue: Color? { return nil }
}
enum BackgroundColorEnvironmentKey: EnvironmentKey {
static var defaultValue: Color? { return nil }
}

enum ImageScaleEnvironmentKey: EnvironmentKey {
static var defaultValue: Image.Scale { return .medium }
}

enum EditModeEnvironmentKey: EnvironmentKey {
static var defaultValue: Binding<EditMode>? { return nil }
}

enum HorizontalSizeClassEnvironmentKey: EnvironmentKey {
static var defaultValue: UserInterfaceSizeClass? { return .regular }
}
enum VerticalSizeClassEnvironmentKey: EnvironmentKey {
static var defaultValue: UserInterfaceSizeClass? { return .regular }
}

import struct Foundation.Locale
import struct Foundation.TimeZone

enum LocaleEnvironmentKey: EnvironmentKey {
static var defaultValue: Locale { return .current }
}
enum TimeZoneEnvironmentKey: EnvironmentKey {
static var defaultValue: TimeZone { return .current }
}

enum EnvironmentObjectKey<O: BindableObject>: EnvironmentKey {
static var defaultValue: O? { return nil }
}

// MARK: - Value Access

extension EnvironmentValues {

public var sizeCategory: ContentSizeCategory {
set { self[SizeCategoryEnvironmentKey.self] = newValue }
get { self[SizeCategoryEnvironmentKey.self] }
}

public var foregroundColor: Color? {
set { self[ForegroundColorEnvironmentKey.self] = newValue }
get { self[ForegroundColorEnvironmentKey.self] }
}
public var backgroundColor: Color? {
set { self[BackgroundColorEnvironmentKey.self] = newValue }
get { self[BackgroundColorEnvironmentKey.self] }
}

public var isEnabled: Bool {
set { self[IsEnabledEnvironmentKey.self] = newValue }
get { self[IsEnabledEnvironmentKey.self] }
}

public var font: Font? {
set { self[FontEnvironmentKey.self] = newValue}
get { self[FontEnvironmentKey.self] }
}

public var imageScale: Image.Scale {
set { self[ImageScaleEnvironmentKey.self] = newValue }
get { self[ImageScaleEnvironmentKey.self] }
}

public var editMode: Binding<EditMode>? {
set { self[EditModeEnvironmentKey.self] = newValue}
get { self[EditModeEnvironmentKey.self] }
}

public var horizontalSizeClass: UserInterfaceSizeClass? {
set { self[HorizontalSizeClassEnvironmentKey.self] = newValue }
get { self[HorizontalSizeClassEnvironmentKey.self] }
}
public var verticalSizeClass: UserInterfaceSizeClass? {
set { self[VerticalSizeClassEnvironmentKey.self] = newValue }
get { self[VerticalSizeClassEnvironmentKey.self] }
}

public var locale: Locale {
set { self[LocaleEnvironmentKey.self] = newValue}
get { self[LocaleEnvironmentKey.self] }
}
public var timeZone: TimeZone {
set { self[TimeZoneEnvironmentKey.self] = newValue}
get { self[TimeZoneEnvironmentKey.self] }
}
}

// MARK: - View Access

public extension View {

typealias EnvironmentView<K> =
ModifiedContent<Self, EnvironmentKeyWritingModifier<K>>

func sizeCategory(_ category: ContentSizeCategory)
-> EnvironmentView<ContentSizeCategory>
{
return environment(\.sizeCategory, category)
}

func foregroundColor(_ color: Color?) -> EnvironmentView<Color?> {
return environment(\.foregroundColor, color)
}
func backgroundColor(_ color: Color?) -> EnvironmentView<Color?> {
return environment(\.backgroundColor, color)
}

func isEnabled(_ enabled: Bool) -> EnvironmentView<Bool> {
return environment(\.isEnabled, enabled)
}
func disabled(_ disabled: Bool) -> EnvironmentView<Bool> {
return environment(\.isEnabled, !disabled)
}

func font(_ font: Font?) -> EnvironmentView<Font?> {
return environment(\.font, font)
}

func imageScale(_ scale: Image.Scale) -> EnvironmentView<Image.Scale> {
return environment(\.imageScale, scale)
}

func editMode(_ mode: Binding<EditMode>?)
-> EnvironmentView<Binding<EditMode>?>
{
return environment(\.editMode, mode)
}

func horizontalSizeClass(_ sizeClass: UserInterfaceSizeClass?)
-> EnvironmentView<UserInterfaceSizeClass?>
{
return environment(\.horizontalSizeClass, sizeClass)
}
func verticalSizeClass(_ sizeClass: UserInterfaceSizeClass?)
-> EnvironmentView<UserInterfaceSizeClass?>
{
return environment(\.verticalSizeClass, sizeClass)
}

func locale(_ locale: Locale) -> EnvironmentView<Locale> {
return environment(\.locale, locale)
}
func timeZone(_ timeZone: TimeZone) -> EnvironmentView<TimeZone> {
return environment(\.timeZone, timeZone)
}
}
59 changes: 59 additions & 0 deletions Sources/SwiftWebUI/Environment/EnvironmentValues.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// EnvironmentValues.swift
// SwiftWebUI
//
// Created by Helge Heß on 10.06.19.
// Copyright © 2019 Helge Heß. All rights reserved.
//

public struct EnvironmentValues /*: CustomStringConvertible*/ {

static let empty = EnvironmentValues()

var values = [ ObjectIdentifier : Any ]()
// TBD: can we avoid the any? Own AnyEntry protocol doesn't give much?

// a hack to support type erased values
mutating func _setAny<T>(_ key: Any.Type, _ newValue: T) {
values[ObjectIdentifier(key)] = newValue
}

public subscript<K: EnvironmentKey>(key: K.Type) -> K.Value {
set {
values[ObjectIdentifier(key)] = newValue
}
get {
// values[SizeCategoryKey.self] => ContentSizeCategory
guard let value = values[ObjectIdentifier(key)] else {
return K.defaultValue
}
guard let typedValue = value as? K.Value else {
assertionFailure("unexpected typed value: \(value)")
return K.defaultValue
}
return typedValue
}
}
}


extension TreeStateContext {

func dumpEnvironmentStack(_ title: String = "The current environments:") {
print(title)
for (i, environment) in environmentStack.enumerated().reversed() {
let indent = String(repeating: " ", count: i)

if environment.values.isEmpty {
print("[\(i)]\(indent): EMPTY")
continue
}

print("[\(i)]\(indent):")
for (oid, v ) in environment.values {
print(" \(indent) \(oid.shortRawPointerString):", v)
}
}
print("---")
}
}
Loading

0 comments on commit e8bb09e

Please sign in to comment.