Skip to content

Commit

Permalink
Merge pull request #47 from joshaber/parent
Browse files Browse the repository at this point in the history
Support view-less Elements
  • Loading branch information
joshaber committed Apr 17, 2015
2 parents e8b0f45 + 4da9de5 commit ce59bfd
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 155 deletions.
2 changes: 1 addition & 1 deletion External/SwiftBox
4 changes: 2 additions & 2 deletions Few-Mac/Mac.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ internal func markNeedsDisplay(view: ViewType) {
view.needsDisplay = true
}

internal func configureViewToAutoresize(view: ViewType) {
view.autoresizingMask = .ViewWidthSizable | .ViewHeightSizable
internal func configureViewToAutoresize(view: ViewType?) {
view?.autoresizingMask = .ViewWidthSizable | .ViewHeightSizable
}
2 changes: 1 addition & 1 deletion Few-Mac/QuickLook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

extension Element {
public func debugQuickLookObject() -> AnyObject? {
let realizedSelf = realize()
let realizedSelf = realize(nil)
return realizedSelf.view
}

Expand Down
16 changes: 11 additions & 5 deletions Few-Mac/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ private class FewScrollView: NSView {
}
}

private class RealizedScrollViewElement: RealizedElement {
private override func addRealizedViewForChild(child: RealizedElement) {
let scrollVew = view as! FewScrollView
scrollVew.scrollView.documentView = child.view
}
}

private class ScrollViewElement: Element {
private let didScroll: CGRect -> ()

Expand All @@ -64,13 +71,12 @@ private class ScrollViewElement: Element {
return view
}

private override func addRealizedChildView(childView: ViewType, selfView: ViewType) {
let scrollVew = selfView as! FewScrollView
scrollVew.scrollView.documentView = childView
private override func createRealizedElement(view: ViewType?, parent: RealizedElement?) -> RealizedElement {
return RealizedScrollViewElement(element: self, view: view, parent: parent)
}

private override func realize() -> RealizedElement {
let realizedElement = super.realize()
private override func realize(parent: RealizedElement?) -> RealizedElement {
let realizedElement = super.realize(parent)

let scrollView = realizedElement.view as! FewScrollView
let documentView = scrollView.scrollView.documentView as! NSView
Expand Down
17 changes: 5 additions & 12 deletions Few-Mac/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@ private class FewListCell: NSTableCellView {
if element.canDiff(realizedElement.element) {
element.applyDiff(realizedElement.element, realizedSelf: realizedElement)
} else {
realizedElement.element.derealize()
realizedElement.view.removeFromSuperview()
realizedElement.remove()

let newRealizedElement = element.realize()
newRealizedElement.view.autoresizingMask = .ViewWidthSizable | .ViewHeightSizable
newRealizedElement.view.frame = bounds
addSubview(newRealizedElement.view)
let parent = RealizedElement(element: Element(), view: self, parent: nil)
self.realizedElement = element.realize(parent)
}
} else {
let newRealizedElement = element.realize()
newRealizedElement.view.autoresizingMask = .ViewWidthSizable | .ViewHeightSizable
newRealizedElement.view.frame = bounds
addSubview(newRealizedElement.view)

realizedElement = newRealizedElement
let parent = RealizedElement(element: Element(), view: self, parent: nil)
realizedElement = element.realize(parent)
}
}

Expand Down
22 changes: 14 additions & 8 deletions Few-iOS/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ private class FewScrollView: UIScrollView, UIScrollViewDelegate {
}
}

private class RealizedScrollViewElement: RealizedElement {
private override func addRealizedViewForChild(child: RealizedElement) {
let scrollView = view as! FewScrollView
scrollView.subviews.first?.removeFromSuperview()
scrollView.addSubview <^> child.view
}
}

private class ScrollViewElement: Element {
private let didScroll: CGRect -> ()

Expand All @@ -41,15 +49,13 @@ private class ScrollViewElement: Element {
private override func createView() -> ViewType {
return FewScrollView(frame: frame, didScroll: didScroll)
}

private override func createRealizedElement(view: ViewType?, parent: RealizedElement?) -> RealizedElement {
return RealizedScrollViewElement(element: self, view: view, parent: parent)
}

private override func addRealizedChildView(childView: ViewType, selfView: ViewType) {
let scrollView = selfView as! FewScrollView
scrollView.subviews.first?.removeFromSuperview()
scrollView.addSubview(childView)
}

private override func realize() -> RealizedElement {
let realizedElement = super.realize()
private override func realize(parent: RealizedElement?) -> RealizedElement {
let realizedElement = super.realize(parent)

let scrollView = realizedElement.view as! FewScrollView
if let element = children.first {
Expand Down
17 changes: 6 additions & 11 deletions Few-iOS/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ private class FewListCell: UITableViewCell {
if element.canDiff(realizedElement.element) {
element.applyDiff(realizedElement.element, realizedSelf: realizedElement)
} else {
realizedElement.element.derealize()
realizedElement.view.removeFromSuperview()

let newRealizedElement = element.realize()
newRealizedElement.view.frame = bounds
addSubview(newRealizedElement.view)
realizedElement.remove()

let parent = RealizedElement(element: Element(), view: self, parent: nil)
self.realizedElement = element.realize(parent)
}
} else {
let newRealizedElement = element.realize()
newRealizedElement.view.frame = bounds
addSubview(newRealizedElement.view)

realizedElement = newRealizedElement
let parent = RealizedElement(element: Element(), view: self, parent: nil)
realizedElement = element.realize(parent)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Few-iOS/iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ internal func compareAndSetAlpha(view: UIView, alpha: CGFloat) {
}
}

internal func configureViewToAutoresize(view: ViewType) {
view.autoresizingMask = .FlexibleWidth | .FlexibleHeight
internal func configureViewToAutoresize(view: ViewType?) {
view?.autoresizingMask = .FlexibleWidth | .FlexibleHeight
}
38 changes: 17 additions & 21 deletions FewCore/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Component<S>: Element {
/// Is the component a root?
private var root = false

private var parent: RealizedElement?

private var frameChangedTrampoline = TargetActionTrampoline()

/// Initializes the component with its initial state. The render function
Expand Down Expand Up @@ -80,11 +82,10 @@ public class Component<S>: Element {
}

final private func realizeNewRoot(newRoot: Element) {
let realized = newRoot.realize()
let realized = newRoot.realize(parent)

configureViewToAutoresize(realized.view)

realizedRoot?.view.removeFromSuperview()
realizedRoot = realized
}

Expand All @@ -93,7 +94,7 @@ public class Component<S>: Element {
newRoot.frame = frame

let node = newRoot.assembleLayoutNode()
var layout: Layout!
let layout: Layout
if root {
layout = node.layout(maxWidth: frame.size.width)
} else {
Expand All @@ -112,13 +113,8 @@ public class Component<S>: Element {
if newRoot.canDiff(rootElement) {
newRoot.applyDiff(rootElement, realizedSelf: realizedRoot)
} else {
let superview = realizedRoot!.view.superview!
rootElement.derealize()

realizedRoot?.remove()
realizeNewRoot(newRoot)
superview.addSubview(realizedRoot!.view)

newRoot.elementDidRealize(realizedRoot!)
}

componentDidRender()
Expand Down Expand Up @@ -169,14 +165,12 @@ public class Component<S>: Element {
public func addToView(hostView: ViewType) {
root = true
frame = hostView.bounds
performInitialRenderIfNeeded()
realizeRootIfNeeded()
hostView.addSubview(realizedRoot!.view)
rootElement?.elementDidRealize(realizedRoot!)
let parent = RealizedElement(element: self, view: hostView, parent: nil)
realize(parent)

#if os(OSX)
hostView.postsFrameChangedNotifications = true
realizedRoot!.view.autoresizesSubviews = false
realizedRoot!.view?.autoresizesSubviews = false

frameChangedTrampoline.action = { [weak self] in
if let strongSelf = self {
Expand Down Expand Up @@ -289,9 +283,9 @@ public class Component<S>: Element {
public override func applyDiff(old: Element, realizedSelf: RealizedElement?) {
super.applyDiff(old, realizedSelf: realizedSelf)

// Use `unsafeBitCast` instead of `as` to avoid a runtime crash.
let oldComponent = unsafeBitCast(old, Component.self)
let oldComponent = old as! Component

parent = oldComponent.parent
root = oldComponent.root
state = oldComponent.state
rootElement = oldComponent.rootElement
Expand All @@ -300,20 +294,22 @@ public class Component<S>: Element {
renderNewRoot()
}

public override func realize() -> RealizedElement {
public override func realize(parent: RealizedElement?) -> RealizedElement {
self.parent = parent

performInitialRenderIfNeeded()
realizeRootIfNeeded()
return RealizedElement(element: self, view: realizedRoot!.view)
return super.realize(parent)
}

public override func derealize() {
componentWillDerealize()

rootElement?.derealize()
realizedRoot?.remove()
realizedRoot = nil
rootElement = nil

realizedRoot?.view.removeFromSuperview()
realizedRoot = nil
parent = nil

componentDidDerealize()
}
Expand Down
Loading

0 comments on commit ce59bfd

Please sign in to comment.